2 * WPA Supplicant / Configuration parser and common functions
3 * Copyright (c) 2003-2015, 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"
22 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
23 #define NO_CONFIG_WRITE
27 * Structure for network configuration parsing. This data is used to implement
28 * a generic parser for each network block variable. The table of configuration
29 * variables is defined below in this file (ssid_fields[]).
32 /* Configuration variable name */
35 /* Parser function for this variable */
36 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
37 int line, const char *value);
39 #ifndef NO_CONFIG_WRITE
40 /* Writer function (i.e., to get the variable in text format from
41 * internal presentation). */
42 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
43 #endif /* NO_CONFIG_WRITE */
45 /* Variable specific parameters for the parser. */
46 void *param1, *param2, *param3, *param4;
48 /* 0 = this variable can be included in debug output and ctrl_iface
49 * 1 = this variable contains key/private data and it must not be
50 * included in debug output unless explicitly requested. In
51 * addition, this variable will not be readable through the
58 static int wpa_config_parse_str(const struct parse_data *data,
59 struct wpa_ssid *ssid,
60 int line, const char *value)
62 size_t res_len, *dst_len;
65 if (os_strcmp(value, "NULL") == 0) {
66 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
73 tmp = wpa_config_parse_string(value, &res_len);
75 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
77 data->key_data ? "[KEY DATA REMOVED]" : value);
82 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
85 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
89 if (data->param3 && res_len < (size_t) data->param3) {
90 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
91 "min_len=%ld)", line, data->name,
92 (unsigned long) res_len, (long) data->param3);
97 if (data->param4 && res_len > (size_t) data->param4) {
98 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
99 "max_len=%ld)", line, data->name,
100 (unsigned long) res_len, (long) data->param4);
106 dst = (char **) (((u8 *) ssid) + (long) data->param1);
107 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
117 #ifndef NO_CONFIG_WRITE
118 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
122 buf = os_malloc(len + 3);
126 os_memcpy(buf + 1, value, len);
134 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
138 buf = os_zalloc(2 * len + 1);
141 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
147 static char * wpa_config_write_string(const u8 *value, size_t len)
152 if (is_hex(value, len))
153 return wpa_config_write_string_hex(value, len);
155 return wpa_config_write_string_ascii(value, len);
159 static char * wpa_config_write_str(const struct parse_data *data,
160 struct wpa_ssid *ssid)
165 src = (char **) (((u8 *) ssid) + (long) data->param1);
170 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
172 len = os_strlen(*src);
174 return wpa_config_write_string((const u8 *) *src, len);
176 #endif /* NO_CONFIG_WRITE */
179 static int wpa_config_parse_int(const struct parse_data *data,
180 struct wpa_ssid *ssid,
181 int line, const char *value)
186 dst = (int *) (((u8 *) ssid) + (long) data->param1);
187 val = strtol(value, &end, 0);
189 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
194 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
196 if (data->param3 && *dst < (long) data->param3) {
197 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
198 "min_value=%ld)", line, data->name, *dst,
199 (long) data->param3);
200 *dst = (long) data->param3;
204 if (data->param4 && *dst > (long) data->param4) {
205 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
206 "max_value=%ld)", line, data->name, *dst,
207 (long) data->param4);
208 *dst = (long) data->param4;
216 #ifndef NO_CONFIG_WRITE
217 static char * wpa_config_write_int(const struct parse_data *data,
218 struct wpa_ssid *ssid)
223 src = (int *) (((u8 *) ssid) + (long) data->param1);
225 value = os_malloc(20);
228 res = os_snprintf(value, 20, "%d", *src);
229 if (os_snprintf_error(20, res)) {
233 value[20 - 1] = '\0';
236 #endif /* NO_CONFIG_WRITE */
239 static int wpa_config_parse_addr_list(const struct parse_data *data,
240 int line, const char *value,
241 u8 **list, size_t *num, char *name,
242 u8 abort_on_error, u8 masked)
245 u8 *buf, *n, addr[2 * ETH_ALEN];
252 while (pos && *pos) {
256 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
257 if (abort_on_error || count == 0) {
258 wpa_printf(MSG_ERROR,
259 "Line %d: Invalid %s address '%s'",
264 /* continue anyway since this could have been from a
265 * truncated configuration file line */
267 "Line %d: Ignore likely truncated %s address '%s'",
270 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
276 os_memmove(buf + 2 * ETH_ALEN, buf,
277 count * 2 * ETH_ALEN);
278 os_memcpy(buf, addr, 2 * ETH_ALEN);
280 wpa_printf(MSG_MSGDUMP,
281 "%s: addr=" MACSTR " mask=" MACSTR,
283 MAC2STR(&addr[ETH_ALEN]));
286 pos = os_strchr(pos, ' ');
297 #ifndef NO_CONFIG_WRITE
298 static char * wpa_config_write_addr_list(const struct parse_data *data,
299 const u8 *list, size_t num, char *name)
301 char *value, *end, *pos;
305 if (list == NULL || num == 0)
308 value = os_malloc(2 * 20 * num);
312 end = value + 2 * 20 * num;
314 for (i = num; i > 0; i--) {
315 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
316 const u8 *m = a + ETH_ALEN;
320 res = hwaddr_mask_txt(pos, end - pos, a, m);
330 #endif /* NO_CONFIG_WRITE */
332 static int wpa_config_parse_bssid(const struct parse_data *data,
333 struct wpa_ssid *ssid, int line,
336 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
337 os_strcmp(value, "any") == 0) {
339 wpa_printf(MSG_MSGDUMP, "BSSID any");
342 if (hwaddr_aton(value, ssid->bssid)) {
343 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
348 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
353 #ifndef NO_CONFIG_WRITE
354 static char * wpa_config_write_bssid(const struct parse_data *data,
355 struct wpa_ssid *ssid)
360 if (!ssid->bssid_set)
363 value = os_malloc(20);
366 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
367 if (os_snprintf_error(20, res)) {
371 value[20 - 1] = '\0';
374 #endif /* NO_CONFIG_WRITE */
377 static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
378 struct wpa_ssid *ssid, int line,
381 return wpa_config_parse_addr_list(data, line, value,
382 &ssid->bssid_blacklist,
383 &ssid->num_bssid_blacklist,
384 "bssid_blacklist", 1, 1);
388 #ifndef NO_CONFIG_WRITE
389 static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
390 struct wpa_ssid *ssid)
392 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
393 ssid->num_bssid_blacklist,
396 #endif /* NO_CONFIG_WRITE */
399 static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
400 struct wpa_ssid *ssid, int line,
403 return wpa_config_parse_addr_list(data, line, value,
404 &ssid->bssid_whitelist,
405 &ssid->num_bssid_whitelist,
406 "bssid_whitelist", 1, 1);
410 #ifndef NO_CONFIG_WRITE
411 static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
412 struct wpa_ssid *ssid)
414 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
415 ssid->num_bssid_whitelist,
418 #endif /* NO_CONFIG_WRITE */
421 static int wpa_config_parse_psk(const struct parse_data *data,
422 struct wpa_ssid *ssid, int line,
425 #ifdef CONFIG_EXT_PASSWORD
426 if (os_strncmp(value, "ext:", 4) == 0) {
427 str_clear_free(ssid->passphrase);
428 ssid->passphrase = NULL;
430 os_free(ssid->ext_psk);
431 ssid->ext_psk = os_strdup(value + 4);
432 if (ssid->ext_psk == NULL)
434 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
438 #endif /* CONFIG_EXT_PASSWORD */
441 #ifndef CONFIG_NO_PBKDF2
446 pos = os_strrchr(value, '"');
450 len = os_strlen(value);
451 if (len < 8 || len > 63) {
452 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
453 "length %lu (expected: 8..63) '%s'.",
454 line, (unsigned long) len, value);
457 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
459 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
460 os_memcmp(ssid->passphrase, value, len) == 0)
463 str_clear_free(ssid->passphrase);
464 ssid->passphrase = dup_binstr(value, len);
465 if (ssid->passphrase == NULL)
468 #else /* CONFIG_NO_PBKDF2 */
469 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
472 #endif /* CONFIG_NO_PBKDF2 */
475 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
476 value[PMK_LEN * 2] != '\0') {
477 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
482 str_clear_free(ssid->passphrase);
483 ssid->passphrase = NULL;
486 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
491 #ifndef NO_CONFIG_WRITE
492 static char * wpa_config_write_psk(const struct parse_data *data,
493 struct wpa_ssid *ssid)
495 #ifdef CONFIG_EXT_PASSWORD
497 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
498 char *buf = os_malloc(len);
503 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
504 if (os_snprintf_error(len, res)) {
510 #endif /* CONFIG_EXT_PASSWORD */
512 if (ssid->passphrase)
513 return wpa_config_write_string_ascii(
514 (const u8 *) ssid->passphrase,
515 os_strlen(ssid->passphrase));
518 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
522 #endif /* NO_CONFIG_WRITE */
525 static int wpa_config_parse_proto(const struct parse_data *data,
526 struct wpa_ssid *ssid, int line,
529 int val = 0, last, errors = 0;
530 char *start, *end, *buf;
532 buf = os_strdup(value);
537 while (*start != '\0') {
538 while (*start == ' ' || *start == '\t')
543 while (*end != ' ' && *end != '\t' && *end != '\0')
547 if (os_strcmp(start, "WPA") == 0)
548 val |= WPA_PROTO_WPA;
549 else if (os_strcmp(start, "RSN") == 0 ||
550 os_strcmp(start, "WPA2") == 0)
551 val |= WPA_PROTO_RSN;
552 else if (os_strcmp(start, "OSEN") == 0)
553 val |= WPA_PROTO_OSEN;
555 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
567 wpa_printf(MSG_ERROR,
568 "Line %d: no proto values configured.", line);
572 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
574 return errors ? -1 : 0;
578 #ifndef NO_CONFIG_WRITE
579 static char * wpa_config_write_proto(const struct parse_data *data,
580 struct wpa_ssid *ssid)
583 char *buf, *pos, *end;
585 pos = buf = os_zalloc(20);
590 if (ssid->proto & WPA_PROTO_WPA) {
591 ret = os_snprintf(pos, end - pos, "%sWPA",
592 pos == buf ? "" : " ");
593 if (os_snprintf_error(end - pos, ret))
598 if (ssid->proto & WPA_PROTO_RSN) {
599 ret = os_snprintf(pos, end - pos, "%sRSN",
600 pos == buf ? "" : " ");
601 if (os_snprintf_error(end - pos, ret))
606 if (ssid->proto & WPA_PROTO_OSEN) {
607 ret = os_snprintf(pos, end - pos, "%sOSEN",
608 pos == buf ? "" : " ");
609 if (os_snprintf_error(end - pos, ret))
621 #endif /* NO_CONFIG_WRITE */
624 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
625 struct wpa_ssid *ssid, int line,
628 int val = 0, last, errors = 0;
629 char *start, *end, *buf;
631 buf = os_strdup(value);
636 while (*start != '\0') {
637 while (*start == ' ' || *start == '\t')
642 while (*end != ' ' && *end != '\t' && *end != '\0')
646 if (os_strcmp(start, "WPA-PSK") == 0)
647 val |= WPA_KEY_MGMT_PSK;
648 else if (os_strcmp(start, "WPA-EAP") == 0)
649 val |= WPA_KEY_MGMT_IEEE8021X;
650 else if (os_strcmp(start, "IEEE8021X") == 0)
651 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
652 else if (os_strcmp(start, "NONE") == 0)
653 val |= WPA_KEY_MGMT_NONE;
654 else if (os_strcmp(start, "WPA-NONE") == 0)
655 val |= WPA_KEY_MGMT_WPA_NONE;
656 #ifdef CONFIG_IEEE80211R
657 else if (os_strcmp(start, "FT-PSK") == 0)
658 val |= WPA_KEY_MGMT_FT_PSK;
659 else if (os_strcmp(start, "FT-EAP") == 0)
660 val |= WPA_KEY_MGMT_FT_IEEE8021X;
661 #endif /* CONFIG_IEEE80211R */
662 #ifdef CONFIG_IEEE80211W
663 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
664 val |= WPA_KEY_MGMT_PSK_SHA256;
665 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
666 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
667 #endif /* CONFIG_IEEE80211W */
669 else if (os_strcmp(start, "WPS") == 0)
670 val |= WPA_KEY_MGMT_WPS;
671 #endif /* CONFIG_WPS */
673 else if (os_strcmp(start, "SAE") == 0)
674 val |= WPA_KEY_MGMT_SAE;
675 else if (os_strcmp(start, "FT-SAE") == 0)
676 val |= WPA_KEY_MGMT_FT_SAE;
677 #endif /* CONFIG_SAE */
679 else if (os_strcmp(start, "OSEN") == 0)
680 val |= WPA_KEY_MGMT_OSEN;
681 #endif /* CONFIG_HS20 */
683 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
684 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
685 #endif /* CONFIG_SUITEB */
686 #ifdef CONFIG_SUITEB192
687 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
688 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
689 #endif /* CONFIG_SUITEB192 */
691 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
703 wpa_printf(MSG_ERROR,
704 "Line %d: no key_mgmt values configured.", line);
708 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
709 ssid->key_mgmt = val;
710 return errors ? -1 : 0;
714 #ifndef NO_CONFIG_WRITE
715 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
716 struct wpa_ssid *ssid)
718 char *buf, *pos, *end;
721 pos = buf = os_zalloc(100);
726 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
727 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
728 pos == buf ? "" : " ");
729 if (os_snprintf_error(end - pos, ret)) {
736 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
737 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
738 pos == buf ? "" : " ");
739 if (os_snprintf_error(end - pos, ret)) {
746 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
747 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
748 pos == buf ? "" : " ");
749 if (os_snprintf_error(end - pos, ret)) {
756 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
757 ret = os_snprintf(pos, end - pos, "%sNONE",
758 pos == buf ? "" : " ");
759 if (os_snprintf_error(end - pos, ret)) {
766 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
767 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
768 pos == buf ? "" : " ");
769 if (os_snprintf_error(end - pos, ret)) {
776 #ifdef CONFIG_IEEE80211R
777 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
778 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
779 pos == buf ? "" : " ");
780 if (os_snprintf_error(end - pos, ret)) {
787 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
788 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
789 pos == buf ? "" : " ");
790 if (os_snprintf_error(end - pos, ret)) {
796 #endif /* CONFIG_IEEE80211R */
798 #ifdef CONFIG_IEEE80211W
799 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
800 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
801 pos == buf ? "" : " ");
802 if (os_snprintf_error(end - pos, ret)) {
809 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
810 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
811 pos == buf ? "" : " ");
812 if (os_snprintf_error(end - pos, ret)) {
818 #endif /* CONFIG_IEEE80211W */
821 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
822 ret = os_snprintf(pos, end - pos, "%sWPS",
823 pos == buf ? "" : " ");
824 if (os_snprintf_error(end - pos, ret)) {
830 #endif /* CONFIG_WPS */
833 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
834 ret = os_snprintf(pos, end - pos, "%sSAE",
835 pos == buf ? "" : " ");
836 if (os_snprintf_error(end - pos, ret)) {
843 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
844 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
845 pos == buf ? "" : " ");
846 if (os_snprintf_error(end - pos, ret)) {
852 #endif /* CONFIG_SAE */
855 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
856 ret = os_snprintf(pos, end - pos, "%sOSEN",
857 pos == buf ? "" : " ");
858 if (os_snprintf_error(end - pos, ret)) {
864 #endif /* CONFIG_HS20 */
867 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
868 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
869 pos == buf ? "" : " ");
870 if (os_snprintf_error(end - pos, ret)) {
876 #endif /* CONFIG_SUITEB */
878 #ifdef CONFIG_SUITEB192
879 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
880 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
881 pos == buf ? "" : " ");
882 if (os_snprintf_error(end - pos, ret)) {
888 #endif /* CONFIG_SUITEB192 */
897 #endif /* NO_CONFIG_WRITE */
900 static int wpa_config_parse_cipher(int line, const char *value)
904 #else /* CONFIG_NO_WPA */
905 int val = wpa_parse_cipher(value);
907 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
912 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
917 #endif /* CONFIG_NO_WPA */
921 #ifndef NO_CONFIG_WRITE
922 static char * wpa_config_write_cipher(int cipher)
926 #else /* CONFIG_NO_WPA */
927 char *buf = os_zalloc(50);
931 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
937 #endif /* CONFIG_NO_WPA */
939 #endif /* NO_CONFIG_WRITE */
942 static int wpa_config_parse_pairwise(const struct parse_data *data,
943 struct wpa_ssid *ssid, int line,
947 val = wpa_config_parse_cipher(line, value);
950 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
951 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
952 "(0x%x).", line, val);
956 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
957 ssid->pairwise_cipher = val;
962 #ifndef NO_CONFIG_WRITE
963 static char * wpa_config_write_pairwise(const struct parse_data *data,
964 struct wpa_ssid *ssid)
966 return wpa_config_write_cipher(ssid->pairwise_cipher);
968 #endif /* NO_CONFIG_WRITE */
971 static int wpa_config_parse_group(const struct parse_data *data,
972 struct wpa_ssid *ssid, int line,
976 val = wpa_config_parse_cipher(line, value);
981 * Backwards compatibility - filter out WEP ciphers that were previously
984 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
986 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
987 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
988 "(0x%x).", line, val);
992 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
993 ssid->group_cipher = val;
998 #ifndef NO_CONFIG_WRITE
999 static char * wpa_config_write_group(const struct parse_data *data,
1000 struct wpa_ssid *ssid)
1002 return wpa_config_write_cipher(ssid->group_cipher);
1004 #endif /* NO_CONFIG_WRITE */
1007 static int wpa_config_parse_auth_alg(const struct parse_data *data,
1008 struct wpa_ssid *ssid, int line,
1011 int val = 0, last, errors = 0;
1012 char *start, *end, *buf;
1014 buf = os_strdup(value);
1019 while (*start != '\0') {
1020 while (*start == ' ' || *start == '\t')
1025 while (*end != ' ' && *end != '\t' && *end != '\0')
1027 last = *end == '\0';
1029 if (os_strcmp(start, "OPEN") == 0)
1030 val |= WPA_AUTH_ALG_OPEN;
1031 else if (os_strcmp(start, "SHARED") == 0)
1032 val |= WPA_AUTH_ALG_SHARED;
1033 else if (os_strcmp(start, "LEAP") == 0)
1034 val |= WPA_AUTH_ALG_LEAP;
1036 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1048 wpa_printf(MSG_ERROR,
1049 "Line %d: no auth_alg values configured.", line);
1053 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1054 ssid->auth_alg = val;
1055 return errors ? -1 : 0;
1059 #ifndef NO_CONFIG_WRITE
1060 static char * wpa_config_write_auth_alg(const struct parse_data *data,
1061 struct wpa_ssid *ssid)
1063 char *buf, *pos, *end;
1066 pos = buf = os_zalloc(30);
1071 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1072 ret = os_snprintf(pos, end - pos, "%sOPEN",
1073 pos == buf ? "" : " ");
1074 if (os_snprintf_error(end - pos, ret)) {
1081 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1082 ret = os_snprintf(pos, end - pos, "%sSHARED",
1083 pos == buf ? "" : " ");
1084 if (os_snprintf_error(end - pos, ret)) {
1091 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1092 ret = os_snprintf(pos, end - pos, "%sLEAP",
1093 pos == buf ? "" : " ");
1094 if (os_snprintf_error(end - pos, ret)) {
1108 #endif /* NO_CONFIG_WRITE */
1111 static int * wpa_config_parse_int_array(const char *value)
1119 freqs = os_calloc(len + 1, sizeof(int));
1130 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
1135 for (i = len; i <= len * 2; i++)
1141 freqs[used] = atoi(pos);
1142 if (freqs[used] == 0)
1145 pos = os_strchr(pos + 1, ' ');
1152 static int wpa_config_parse_scan_freq(const struct parse_data *data,
1153 struct wpa_ssid *ssid, int line,
1158 freqs = wpa_config_parse_int_array(value);
1161 if (freqs[0] == 0) {
1165 os_free(ssid->scan_freq);
1166 ssid->scan_freq = freqs;
1172 static int wpa_config_parse_freq_list(const struct parse_data *data,
1173 struct wpa_ssid *ssid, int line,
1178 freqs = wpa_config_parse_int_array(value);
1181 if (freqs[0] == 0) {
1185 os_free(ssid->freq_list);
1186 ssid->freq_list = freqs;
1192 #ifndef NO_CONFIG_WRITE
1193 static char * wpa_config_write_freqs(const struct parse_data *data,
1196 char *buf, *pos, *end;
1204 for (i = 0; freqs[i]; i++)
1207 pos = buf = os_zalloc(10 * count + 1);
1210 end = buf + 10 * count + 1;
1212 for (i = 0; freqs[i]; i++) {
1213 ret = os_snprintf(pos, end - pos, "%s%u",
1214 i == 0 ? "" : " ", freqs[i]);
1215 if (os_snprintf_error(end - pos, ret)) {
1226 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1227 struct wpa_ssid *ssid)
1229 return wpa_config_write_freqs(data, ssid->scan_freq);
1233 static char * wpa_config_write_freq_list(const struct parse_data *data,
1234 struct wpa_ssid *ssid)
1236 return wpa_config_write_freqs(data, ssid->freq_list);
1238 #endif /* NO_CONFIG_WRITE */
1241 #ifdef IEEE8021X_EAPOL
1242 static int wpa_config_parse_eap(const struct parse_data *data,
1243 struct wpa_ssid *ssid, int line,
1246 int last, errors = 0;
1247 char *start, *end, *buf;
1248 struct eap_method_type *methods = NULL, *tmp;
1249 size_t num_methods = 0;
1251 buf = os_strdup(value);
1256 while (*start != '\0') {
1257 while (*start == ' ' || *start == '\t')
1262 while (*end != ' ' && *end != '\t' && *end != '\0')
1264 last = *end == '\0';
1267 methods = os_realloc_array(methods, num_methods + 1,
1269 if (methods == NULL) {
1274 methods[num_methods].method = eap_peer_get_type(
1275 start, &methods[num_methods].vendor);
1276 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1277 methods[num_methods].method == EAP_TYPE_NONE) {
1278 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1279 "'%s'", line, start);
1280 wpa_printf(MSG_ERROR, "You may need to add support for"
1281 " this EAP method during wpa_supplicant\n"
1282 "build time configuration.\n"
1283 "See README for more information.");
1285 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1286 methods[num_methods].method == EAP_TYPE_LEAP)
1298 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1299 if (methods == NULL) {
1303 methods[num_methods].vendor = EAP_VENDOR_IETF;
1304 methods[num_methods].method = EAP_TYPE_NONE;
1307 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1308 (u8 *) methods, num_methods * sizeof(*methods));
1309 os_free(ssid->eap.eap_methods);
1310 ssid->eap.eap_methods = methods;
1311 return errors ? -1 : 0;
1315 #ifndef NO_CONFIG_WRITE
1316 static char * wpa_config_write_eap(const struct parse_data *data,
1317 struct wpa_ssid *ssid)
1320 char *buf, *pos, *end;
1321 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1324 if (eap_methods == NULL)
1327 pos = buf = os_zalloc(100);
1332 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1333 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1334 name = eap_get_name(eap_methods[i].vendor,
1335 eap_methods[i].method);
1337 ret = os_snprintf(pos, end - pos, "%s%s",
1338 pos == buf ? "" : " ", name);
1339 if (os_snprintf_error(end - pos, ret))
1349 #endif /* NO_CONFIG_WRITE */
1352 static int wpa_config_parse_password(const struct parse_data *data,
1353 struct wpa_ssid *ssid, int line,
1358 if (os_strcmp(value, "NULL") == 0) {
1359 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1360 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1361 ssid->eap.password = NULL;
1362 ssid->eap.password_len = 0;
1366 #ifdef CONFIG_EXT_PASSWORD
1367 if (os_strncmp(value, "ext:", 4) == 0) {
1368 char *name = os_strdup(value + 4);
1371 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1372 ssid->eap.password = (u8 *) name;
1373 ssid->eap.password_len = os_strlen(name);
1374 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1375 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1378 #endif /* CONFIG_EXT_PASSWORD */
1380 if (os_strncmp(value, "hash:", 5) != 0) {
1384 tmp = wpa_config_parse_string(value, &res_len);
1386 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1390 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1391 (u8 *) tmp, res_len);
1393 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1394 ssid->eap.password = (u8 *) tmp;
1395 ssid->eap.password_len = res_len;
1396 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1397 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1403 /* NtPasswordHash: hash:<32 hex digits> */
1404 if (os_strlen(value + 5) != 2 * 16) {
1405 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1406 "(expected 32 hex digits)", line);
1410 hash = os_malloc(16);
1414 if (hexstr2bin(value + 5, hash, 16)) {
1416 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1420 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1422 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1423 ssid->eap.password = hash;
1424 ssid->eap.password_len = 16;
1425 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1426 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1432 #ifndef NO_CONFIG_WRITE
1433 static char * wpa_config_write_password(const struct parse_data *data,
1434 struct wpa_ssid *ssid)
1438 if (ssid->eap.password == NULL)
1441 #ifdef CONFIG_EXT_PASSWORD
1442 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1443 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1446 os_memcpy(buf, "ext:", 4);
1447 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1450 #endif /* CONFIG_EXT_PASSWORD */
1452 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1453 return wpa_config_write_string(
1454 ssid->eap.password, ssid->eap.password_len);
1457 buf = os_malloc(5 + 32 + 1);
1461 os_memcpy(buf, "hash:", 5);
1462 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1466 #endif /* NO_CONFIG_WRITE */
1467 #endif /* IEEE8021X_EAPOL */
1470 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1471 const char *value, int idx)
1473 char *buf, title[20];
1476 buf = wpa_config_parse_string(value, len);
1478 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1482 if (*len > MAX_WEP_KEY_LEN) {
1483 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1488 if (*len && *len != 5 && *len != 13 && *len != 16) {
1489 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1490 "this network block will be ignored",
1491 line, (unsigned int) *len);
1493 os_memcpy(key, buf, *len);
1494 str_clear_free(buf);
1495 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1496 if (!os_snprintf_error(sizeof(title), res))
1497 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1502 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1503 struct wpa_ssid *ssid, int line,
1506 return wpa_config_parse_wep_key(ssid->wep_key[0],
1507 &ssid->wep_key_len[0], line,
1512 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1513 struct wpa_ssid *ssid, int line,
1516 return wpa_config_parse_wep_key(ssid->wep_key[1],
1517 &ssid->wep_key_len[1], line,
1522 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1523 struct wpa_ssid *ssid, int line,
1526 return wpa_config_parse_wep_key(ssid->wep_key[2],
1527 &ssid->wep_key_len[2], line,
1532 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1533 struct wpa_ssid *ssid, int line,
1536 return wpa_config_parse_wep_key(ssid->wep_key[3],
1537 &ssid->wep_key_len[3], line,
1542 #ifndef NO_CONFIG_WRITE
1543 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1545 if (ssid->wep_key_len[idx] == 0)
1547 return wpa_config_write_string(ssid->wep_key[idx],
1548 ssid->wep_key_len[idx]);
1552 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1553 struct wpa_ssid *ssid)
1555 return wpa_config_write_wep_key(ssid, 0);
1559 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1560 struct wpa_ssid *ssid)
1562 return wpa_config_write_wep_key(ssid, 1);
1566 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1567 struct wpa_ssid *ssid)
1569 return wpa_config_write_wep_key(ssid, 2);
1573 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1574 struct wpa_ssid *ssid)
1576 return wpa_config_write_wep_key(ssid, 3);
1578 #endif /* NO_CONFIG_WRITE */
1583 static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1584 struct wpa_ssid *ssid, int line,
1587 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1588 os_strcmp(value, "any") == 0) {
1589 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1590 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1593 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1594 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1598 ssid->bssid_set = 1;
1599 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1600 MAC2STR(ssid->go_p2p_dev_addr));
1605 #ifndef NO_CONFIG_WRITE
1606 static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1607 struct wpa_ssid *ssid)
1612 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1615 value = os_malloc(20);
1618 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1619 if (os_snprintf_error(20, res)) {
1623 value[20 - 1] = '\0';
1626 #endif /* NO_CONFIG_WRITE */
1629 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1630 struct wpa_ssid *ssid, int line,
1633 return wpa_config_parse_addr_list(data, line, value,
1634 &ssid->p2p_client_list,
1635 &ssid->num_p2p_clients,
1636 "p2p_client_list", 0, 0);
1640 #ifndef NO_CONFIG_WRITE
1641 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1642 struct wpa_ssid *ssid)
1644 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1645 ssid->num_p2p_clients,
1648 #endif /* NO_CONFIG_WRITE */
1651 static int wpa_config_parse_psk_list(const struct parse_data *data,
1652 struct wpa_ssid *ssid, int line,
1655 struct psk_list_entry *p;
1658 p = os_zalloc(sizeof(*p));
1663 if (os_strncmp(pos, "P2P-", 4) == 0) {
1668 if (hwaddr_aton(pos, p->addr)) {
1669 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1676 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1683 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1684 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1690 dl_list_add(&ssid->psk_list, &p->list);
1696 #ifndef NO_CONFIG_WRITE
1697 static char * wpa_config_write_psk_list(const struct parse_data *data,
1698 struct wpa_ssid *ssid)
1702 #endif /* NO_CONFIG_WRITE */
1704 #endif /* CONFIG_P2P */
1709 static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1710 struct wpa_ssid *ssid, int line,
1713 int *rates = wpa_config_parse_int_array(value);
1715 if (rates == NULL) {
1716 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1720 if (rates[0] == 0) {
1725 os_free(ssid->mesh_basic_rates);
1726 ssid->mesh_basic_rates = rates;
1732 #ifndef NO_CONFIG_WRITE
1734 static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1735 struct wpa_ssid *ssid)
1737 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1740 #endif /* NO_CONFIG_WRITE */
1742 #endif /* CONFIG_MESH */
1745 /* Helper macros for network block parser */
1750 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1751 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1753 /* STR: Define a string variable for an ASCII string; f = field name */
1754 #ifdef NO_CONFIG_WRITE
1755 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1756 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1757 #else /* NO_CONFIG_WRITE */
1758 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1759 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1760 #endif /* NO_CONFIG_WRITE */
1761 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1762 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1763 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1764 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1766 /* STR_LEN: Define a string variable with a separate variable for storing the
1767 * data length. Unlike STR(), this can be used to store arbitrary binary data
1768 * (i.e., even nul termination character). */
1769 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1770 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1771 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1772 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1773 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1775 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1776 * explicitly specified. */
1777 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1778 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1779 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1781 #ifdef NO_CONFIG_WRITE
1782 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1783 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1784 #else /* NO_CONFIG_WRITE */
1785 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1786 OFFSET(f), (void *) 0
1787 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1788 OFFSET(eap.f), (void *) 0
1789 #endif /* NO_CONFIG_WRITE */
1791 /* INT: Define an integer variable */
1792 #define INT(f) _INT(f), NULL, NULL, 0
1793 #define INTe(f) _INTe(f), NULL, NULL, 0
1795 /* INT_RANGE: Define an integer variable with allowed value range */
1796 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1798 /* FUNC: Define a configuration variable that uses a custom function for
1799 * parsing and writing the value. */
1800 #ifdef NO_CONFIG_WRITE
1801 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1802 #else /* NO_CONFIG_WRITE */
1803 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1804 NULL, NULL, NULL, NULL
1805 #endif /* NO_CONFIG_WRITE */
1806 #define FUNC(f) _FUNC(f), 0
1807 #define FUNC_KEY(f) _FUNC(f), 1
1810 * Table of network configuration variables. This table is used to parse each
1811 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1812 * that is inside a network block.
1814 * This table is generated using the helper macros defined above and with
1815 * generous help from the C pre-processor. The field name is stored as a string
1816 * into .name and for STR and INT types, the offset of the target buffer within
1817 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1818 * offset to the field containing the length of the configuration variable.
1819 * .param3 and .param4 can be used to mark the allowed range (length for STR
1820 * and value for INT).
1822 * For each configuration line in wpa_supplicant.conf, the parser goes through
1823 * this table and select the entry that matches with the field name. The parser
1824 * function (.parser) is then called to parse the actual value of the field.
1826 * This kind of mechanism makes it easy to add new configuration parameters,
1827 * since only one line needs to be added into this table and into the
1828 * struct wpa_ssid definition if the new variable is either a string or
1829 * integer. More complex types will need to use their own parser and writer
1832 static const struct parse_data ssid_fields[] = {
1833 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
1834 { INT_RANGE(scan_ssid, 0, 1) },
1836 { FUNC(bssid_blacklist) },
1837 { FUNC(bssid_whitelist) },
1839 { INT(mem_only_psk) },
1842 { INT(bg_scan_period) },
1846 { FUNC(scan_freq) },
1847 { FUNC(freq_list) },
1848 #ifdef IEEE8021X_EAPOL
1850 { STR_LENe(identity) },
1851 { STR_LENe(anonymous_identity) },
1852 { FUNC_KEY(password) },
1855 { STRe(client_cert) },
1856 { STRe(private_key) },
1857 { STR_KEYe(private_key_passwd) },
1859 { STRe(subject_match) },
1860 { STRe(altsubject_match) },
1861 { STRe(domain_suffix_match) },
1862 { STRe(domain_match) },
1865 { STRe(client_cert2) },
1866 { STRe(private_key2) },
1867 { STR_KEYe(private_key2_passwd) },
1869 { STRe(subject_match2) },
1870 { STRe(altsubject_match2) },
1871 { STRe(domain_suffix_match2) },
1872 { STRe(domain_match2) },
1877 { STRe(engine_id) },
1880 { STRe(ca_cert_id) },
1882 { STRe(engine2_id) },
1885 { STRe(ca_cert2_id) },
1888 { INT(eapol_flags) },
1890 { STRe(openssl_ciphers) },
1892 #endif /* IEEE8021X_EAPOL */
1893 { FUNC_KEY(wep_key0) },
1894 { FUNC_KEY(wep_key1) },
1895 { FUNC_KEY(wep_key2) },
1896 { FUNC_KEY(wep_key3) },
1897 { INT(wep_tx_keyidx) },
1899 #ifdef IEEE8021X_EAPOL
1900 { INT(eap_workaround) },
1902 { INTe(fragment_size) },
1904 #endif /* IEEE8021X_EAPOL */
1906 { INT_RANGE(mode, 0, 5) },
1907 { INT_RANGE(no_auto_peer, 0, 1) },
1908 #else /* CONFIG_MESH */
1909 { INT_RANGE(mode, 0, 4) },
1910 #endif /* CONFIG_MESH */
1911 { INT_RANGE(proactive_key_caching, 0, 1) },
1912 { INT_RANGE(disabled, 0, 2) },
1914 #ifdef CONFIG_IEEE80211W
1915 { INT_RANGE(ieee80211w, 0, 2) },
1916 #endif /* CONFIG_IEEE80211W */
1917 { INT_RANGE(peerkey, 0, 1) },
1918 { INT_RANGE(mixed_cell, 0, 1) },
1919 { INT_RANGE(frequency, 0, 65000) },
1920 { INT_RANGE(fixed_freq, 0, 1) },
1922 { FUNC(mesh_basic_rates) },
1923 { INT(dot11MeshMaxRetries) },
1924 { INT(dot11MeshRetryTimeout) },
1925 { INT(dot11MeshConfirmTimeout) },
1926 { INT(dot11MeshHoldingTimeout) },
1927 #endif /* CONFIG_MESH */
1928 { INT(wpa_ptk_rekey) },
1930 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1932 { FUNC(go_p2p_dev_addr) },
1933 { FUNC(p2p_client_list) },
1935 #endif /* CONFIG_P2P */
1936 #ifdef CONFIG_HT_OVERRIDES
1937 { INT_RANGE(disable_ht, 0, 1) },
1938 { INT_RANGE(disable_ht40, -1, 1) },
1939 { INT_RANGE(disable_sgi, 0, 1) },
1940 { INT_RANGE(disable_ldpc, 0, 1) },
1941 { INT_RANGE(ht40_intolerant, 0, 1) },
1942 { INT_RANGE(disable_max_amsdu, -1, 1) },
1943 { INT_RANGE(ampdu_factor, -1, 3) },
1944 { INT_RANGE(ampdu_density, -1, 7) },
1946 #endif /* CONFIG_HT_OVERRIDES */
1947 #ifdef CONFIG_VHT_OVERRIDES
1948 { INT_RANGE(disable_vht, 0, 1) },
1950 { INT(vht_capa_mask) },
1951 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1952 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1953 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1954 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1955 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1956 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1957 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1958 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1959 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1960 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1961 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1962 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1963 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1964 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1965 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1966 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1967 #endif /* CONFIG_VHT_OVERRIDES */
1968 { INT(ap_max_inactivity) },
1969 { INT(dtim_period) },
1970 { INT(beacon_int) },
1971 #ifdef CONFIG_MACSEC
1972 { INT_RANGE(macsec_policy, 0, 1) },
1973 #endif /* CONFIG_MACSEC */
1975 { INT(update_identifier) },
1976 #endif /* CONFIG_HS20 */
1977 { INT_RANGE(mac_addr, 0, 2) },
1989 #undef STR_RANGE_KEY
1996 #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
2000 * wpa_config_add_prio_network - Add a network to priority lists
2001 * @config: Configuration data from wpa_config_read()
2002 * @ssid: Pointer to the network configuration to be added to the list
2003 * Returns: 0 on success, -1 on failure
2005 * This function is used to add a network block to the priority list of
2006 * networks. This must be called for each network when reading in the full
2007 * configuration. In addition, this can be used indirectly when updating
2008 * priorities by calling wpa_config_update_prio_list().
2010 int wpa_config_add_prio_network(struct wpa_config *config,
2011 struct wpa_ssid *ssid)
2014 struct wpa_ssid *prev, **nlist;
2017 * Add to an existing priority list if one is available for the
2018 * configured priority level for this network.
2020 for (prio = 0; prio < config->num_prio; prio++) {
2021 prev = config->pssid[prio];
2022 if (prev->priority == ssid->priority) {
2030 /* First network for this priority - add a new priority list */
2031 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2032 sizeof(struct wpa_ssid *));
2036 for (prio = 0; prio < config->num_prio; prio++) {
2037 if (nlist[prio]->priority < ssid->priority) {
2038 os_memmove(&nlist[prio + 1], &nlist[prio],
2039 (config->num_prio - prio) *
2040 sizeof(struct wpa_ssid *));
2047 config->pssid = nlist;
2054 * wpa_config_update_prio_list - Update network priority list
2055 * @config: Configuration data from wpa_config_read()
2056 * Returns: 0 on success, -1 on failure
2058 * This function is called to update the priority list of networks in the
2059 * configuration when a network is being added or removed. This is also called
2060 * if a priority for a network is changed.
2062 int wpa_config_update_prio_list(struct wpa_config *config)
2064 struct wpa_ssid *ssid;
2067 os_free(config->pssid);
2068 config->pssid = NULL;
2069 config->num_prio = 0;
2071 ssid = config->ssid;
2074 if (wpa_config_add_prio_network(config, ssid) < 0)
2083 #ifdef IEEE8021X_EAPOL
2084 static void eap_peer_config_free(struct eap_peer_config *eap)
2086 os_free(eap->eap_methods);
2087 bin_clear_free(eap->identity, eap->identity_len);
2088 os_free(eap->anonymous_identity);
2089 bin_clear_free(eap->password, eap->password_len);
2090 os_free(eap->ca_cert);
2091 os_free(eap->ca_path);
2092 os_free(eap->client_cert);
2093 os_free(eap->private_key);
2094 str_clear_free(eap->private_key_passwd);
2095 os_free(eap->dh_file);
2096 os_free(eap->subject_match);
2097 os_free(eap->altsubject_match);
2098 os_free(eap->domain_suffix_match);
2099 os_free(eap->domain_match);
2100 os_free(eap->ca_cert2);
2101 os_free(eap->ca_path2);
2102 os_free(eap->client_cert2);
2103 os_free(eap->private_key2);
2104 str_clear_free(eap->private_key2_passwd);
2105 os_free(eap->dh_file2);
2106 os_free(eap->subject_match2);
2107 os_free(eap->altsubject_match2);
2108 os_free(eap->domain_suffix_match2);
2109 os_free(eap->domain_match2);
2110 os_free(eap->phase1);
2111 os_free(eap->phase2);
2113 str_clear_free(eap->pin);
2114 os_free(eap->engine_id);
2115 os_free(eap->key_id);
2116 os_free(eap->cert_id);
2117 os_free(eap->ca_cert_id);
2118 os_free(eap->key2_id);
2119 os_free(eap->cert2_id);
2120 os_free(eap->ca_cert2_id);
2121 str_clear_free(eap->pin2);
2122 os_free(eap->engine2_id);
2124 os_free(eap->pending_req_otp);
2125 os_free(eap->pac_file);
2126 bin_clear_free(eap->new_password, eap->new_password_len);
2127 str_clear_free(eap->external_sim_resp);
2128 os_free(eap->openssl_ciphers);
2130 #endif /* IEEE8021X_EAPOL */
2134 * wpa_config_free_ssid - Free network/ssid configuration data
2135 * @ssid: Configuration data for the network
2137 * This function frees all resources allocated for the network configuration
2140 void wpa_config_free_ssid(struct wpa_ssid *ssid)
2142 struct psk_list_entry *psk;
2144 os_free(ssid->ssid);
2145 str_clear_free(ssid->passphrase);
2146 os_free(ssid->ext_psk);
2147 #ifdef IEEE8021X_EAPOL
2148 eap_peer_config_free(&ssid->eap);
2149 #endif /* IEEE8021X_EAPOL */
2150 os_free(ssid->id_str);
2151 os_free(ssid->scan_freq);
2152 os_free(ssid->freq_list);
2153 os_free(ssid->bgscan);
2154 os_free(ssid->p2p_client_list);
2155 os_free(ssid->bssid_blacklist);
2156 os_free(ssid->bssid_whitelist);
2157 #ifdef CONFIG_HT_OVERRIDES
2158 os_free(ssid->ht_mcs);
2159 #endif /* CONFIG_HT_OVERRIDES */
2161 os_free(ssid->mesh_basic_rates);
2162 #endif /* CONFIG_MESH */
2163 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2165 dl_list_del(&psk->list);
2166 bin_clear_free(psk, sizeof(*psk));
2168 bin_clear_free(ssid, sizeof(*ssid));
2172 void wpa_config_free_cred(struct wpa_cred *cred)
2176 os_free(cred->realm);
2177 str_clear_free(cred->username);
2178 str_clear_free(cred->password);
2179 os_free(cred->ca_cert);
2180 os_free(cred->client_cert);
2181 os_free(cred->private_key);
2182 str_clear_free(cred->private_key_passwd);
2183 os_free(cred->imsi);
2184 str_clear_free(cred->milenage);
2185 for (i = 0; i < cred->num_domain; i++)
2186 os_free(cred->domain[i]);
2187 os_free(cred->domain);
2188 os_free(cred->domain_suffix_match);
2189 os_free(cred->eap_method);
2190 os_free(cred->phase1);
2191 os_free(cred->phase2);
2192 os_free(cred->excluded_ssid);
2193 os_free(cred->roaming_partner);
2194 os_free(cred->provisioning_sp);
2195 for (i = 0; i < cred->num_req_conn_capab; i++)
2196 os_free(cred->req_conn_capab_port[i]);
2197 os_free(cred->req_conn_capab_port);
2198 os_free(cred->req_conn_capab_proto);
2203 void wpa_config_flush_blobs(struct wpa_config *config)
2205 #ifndef CONFIG_NO_CONFIG_BLOBS
2206 struct wpa_config_blob *blob, *prev;
2208 blob = config->blobs;
2209 config->blobs = NULL;
2213 wpa_config_free_blob(prev);
2215 #endif /* CONFIG_NO_CONFIG_BLOBS */
2220 * wpa_config_free - Free configuration data
2221 * @config: Configuration data from wpa_config_read()
2223 * This function frees all resources allocated for the configuration data by
2224 * wpa_config_read().
2226 void wpa_config_free(struct wpa_config *config)
2228 struct wpa_ssid *ssid, *prev = NULL;
2229 struct wpa_cred *cred, *cprev;
2232 ssid = config->ssid;
2236 wpa_config_free_ssid(prev);
2239 cred = config->cred;
2243 wpa_config_free_cred(cprev);
2246 wpa_config_flush_blobs(config);
2248 wpabuf_free(config->wps_vendor_ext_m1);
2249 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2250 wpabuf_free(config->wps_vendor_ext[i]);
2251 os_free(config->ctrl_interface);
2252 os_free(config->ctrl_interface_group);
2253 os_free(config->opensc_engine_path);
2254 os_free(config->pkcs11_engine_path);
2255 os_free(config->pkcs11_module_path);
2256 os_free(config->openssl_ciphers);
2257 os_free(config->pcsc_reader);
2258 str_clear_free(config->pcsc_pin);
2259 os_free(config->driver_param);
2260 os_free(config->device_name);
2261 os_free(config->manufacturer);
2262 os_free(config->model_name);
2263 os_free(config->model_number);
2264 os_free(config->serial_number);
2265 os_free(config->config_methods);
2266 os_free(config->p2p_ssid_postfix);
2267 os_free(config->pssid);
2268 os_free(config->p2p_pref_chan);
2269 os_free(config->p2p_no_go_freq.range);
2270 os_free(config->autoscan);
2271 os_free(config->freq_list);
2272 wpabuf_free(config->wps_nfc_dh_pubkey);
2273 wpabuf_free(config->wps_nfc_dh_privkey);
2274 wpabuf_free(config->wps_nfc_dev_pw);
2275 os_free(config->ext_password_backend);
2276 os_free(config->sae_groups);
2277 wpabuf_free(config->ap_vendor_elements);
2278 os_free(config->osu_dir);
2279 os_free(config->bgscan);
2280 os_free(config->wowlan_triggers);
2281 os_free(config->fst_group_id);
2287 * wpa_config_foreach_network - Iterate over each configured network
2288 * @config: Configuration data from wpa_config_read()
2289 * @func: Callback function to process each network
2290 * @arg: Opaque argument to pass to callback function
2292 * Iterate over the set of configured networks calling the specified
2293 * function for each item. We guard against callbacks removing the
2296 void wpa_config_foreach_network(struct wpa_config *config,
2297 void (*func)(void *, struct wpa_ssid *),
2300 struct wpa_ssid *ssid, *next;
2302 ssid = config->ssid;
2312 * wpa_config_get_network - Get configured network based on id
2313 * @config: Configuration data from wpa_config_read()
2314 * @id: Unique network id to search for
2315 * Returns: Network configuration or %NULL if not found
2317 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2319 struct wpa_ssid *ssid;
2321 ssid = config->ssid;
2333 * wpa_config_add_network - Add a new network with empty configuration
2334 * @config: Configuration data from wpa_config_read()
2335 * Returns: The new network configuration or %NULL if operation failed
2337 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2340 struct wpa_ssid *ssid, *last = NULL;
2343 ssid = config->ssid;
2352 ssid = os_zalloc(sizeof(*ssid));
2356 dl_list_init(&ssid->psk_list);
2360 config->ssid = ssid;
2362 wpa_config_update_prio_list(config);
2369 * wpa_config_remove_network - Remove a configured network based on id
2370 * @config: Configuration data from wpa_config_read()
2371 * @id: Unique network id to search for
2372 * Returns: 0 on success, or -1 if the network was not found
2374 int wpa_config_remove_network(struct wpa_config *config, int id)
2376 struct wpa_ssid *ssid, *prev = NULL;
2378 ssid = config->ssid;
2390 prev->next = ssid->next;
2392 config->ssid = ssid->next;
2394 wpa_config_update_prio_list(config);
2395 wpa_config_free_ssid(ssid);
2401 * wpa_config_set_network_defaults - Set network default values
2402 * @ssid: Pointer to network configuration data
2404 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2406 ssid->proto = DEFAULT_PROTO;
2407 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2408 ssid->group_cipher = DEFAULT_GROUP;
2409 ssid->key_mgmt = DEFAULT_KEY_MGMT;
2410 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
2411 #ifdef IEEE8021X_EAPOL
2412 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2413 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2414 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2415 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
2416 #endif /* IEEE8021X_EAPOL */
2418 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2419 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2420 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2421 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2422 #endif /* CONFIG_MESH */
2423 #ifdef CONFIG_HT_OVERRIDES
2424 ssid->disable_ht = DEFAULT_DISABLE_HT;
2425 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2426 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
2427 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
2428 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2429 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2430 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2431 #endif /* CONFIG_HT_OVERRIDES */
2432 #ifdef CONFIG_VHT_OVERRIDES
2433 ssid->vht_rx_mcs_nss_1 = -1;
2434 ssid->vht_rx_mcs_nss_2 = -1;
2435 ssid->vht_rx_mcs_nss_3 = -1;
2436 ssid->vht_rx_mcs_nss_4 = -1;
2437 ssid->vht_rx_mcs_nss_5 = -1;
2438 ssid->vht_rx_mcs_nss_6 = -1;
2439 ssid->vht_rx_mcs_nss_7 = -1;
2440 ssid->vht_rx_mcs_nss_8 = -1;
2441 ssid->vht_tx_mcs_nss_1 = -1;
2442 ssid->vht_tx_mcs_nss_2 = -1;
2443 ssid->vht_tx_mcs_nss_3 = -1;
2444 ssid->vht_tx_mcs_nss_4 = -1;
2445 ssid->vht_tx_mcs_nss_5 = -1;
2446 ssid->vht_tx_mcs_nss_6 = -1;
2447 ssid->vht_tx_mcs_nss_7 = -1;
2448 ssid->vht_tx_mcs_nss_8 = -1;
2449 #endif /* CONFIG_VHT_OVERRIDES */
2450 ssid->proactive_key_caching = -1;
2451 #ifdef CONFIG_IEEE80211W
2452 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2453 #endif /* CONFIG_IEEE80211W */
2454 ssid->mac_addr = -1;
2459 * wpa_config_set - Set a variable in network configuration
2460 * @ssid: Pointer to network configuration data
2461 * @var: Variable name, e.g., "ssid"
2462 * @value: Variable value
2463 * @line: Line number in configuration file or 0 if not used
2464 * Returns: 0 on success, -1 on failure
2466 * This function can be used to set network configuration variables based on
2467 * both the configuration file and management interface input. The value
2468 * parameter must be in the same format as the text-based configuration file is
2469 * using. For example, strings are using double quotation marks.
2471 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2477 if (ssid == NULL || var == NULL || value == NULL)
2480 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2481 const struct parse_data *field = &ssid_fields[i];
2482 if (os_strcmp(var, field->name) != 0)
2485 if (field->parser(field, ssid, line, value)) {
2487 wpa_printf(MSG_ERROR, "Line %d: failed to "
2488 "parse %s '%s'.", line, var, value);
2494 if (i == NUM_SSID_FIELDS) {
2496 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2497 "'%s'.", line, var);
2506 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2513 len = os_strlen(value);
2514 buf = os_malloc(len + 3);
2518 os_memcpy(buf + 1, value, len);
2520 buf[len + 2] = '\0';
2521 ret = wpa_config_set(ssid, var, buf, 0);
2528 * wpa_config_get_all - Get all options from network configuration
2529 * @ssid: Pointer to network configuration data
2530 * @get_keys: Determines if keys/passwords will be included in returned list
2531 * (if they may be exported)
2532 * Returns: %NULL terminated list of all set keys and their values in the form
2533 * of [key1, val1, key2, val2, ... , NULL]
2535 * This function can be used to get list of all configured network properties.
2536 * The caller is responsible for freeing the returned list and all its
2539 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2541 #ifdef NO_CONFIG_WRITE
2543 #else /* NO_CONFIG_WRITE */
2544 const struct parse_data *field;
2550 get_keys = get_keys && ssid->export_keys;
2552 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2557 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2558 field = &ssid_fields[i];
2559 if (field->key_data && !get_keys)
2561 value = field->writer(field, ssid);
2564 if (os_strlen(value) == 0) {
2569 key = os_strdup(field->name);
2575 props[fields_num * 2] = key;
2576 props[fields_num * 2 + 1] = value;
2589 #endif /* NO_CONFIG_WRITE */
2593 #ifndef NO_CONFIG_WRITE
2595 * wpa_config_get - Get a variable in network configuration
2596 * @ssid: Pointer to network configuration data
2597 * @var: Variable name, e.g., "ssid"
2598 * Returns: Value of the variable or %NULL on failure
2600 * This function can be used to get network configuration variables. The
2601 * returned value is a copy of the configuration variable in text format, i.e,.
2602 * the same format that the text-based configuration file and wpa_config_set()
2603 * are using for the value. The caller is responsible for freeing the returned
2606 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2610 if (ssid == NULL || var == NULL)
2613 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2614 const struct parse_data *field = &ssid_fields[i];
2615 if (os_strcmp(var, field->name) == 0)
2616 return field->writer(field, ssid);
2624 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2625 * @ssid: Pointer to network configuration data
2626 * @var: Variable name, e.g., "ssid"
2627 * Returns: Value of the variable or %NULL on failure
2629 * This function can be used to get network configuration variable like
2630 * wpa_config_get(). The only difference is that this functions does not expose
2631 * key/password material from the configuration. In case a key/password field
2632 * is requested, the returned value is an empty string or %NULL if the variable
2633 * is not set or "*" if the variable is set (regardless of its value). The
2634 * returned value is a copy of the configuration variable in text format, i.e,.
2635 * the same format that the text-based configuration file and wpa_config_set()
2636 * are using for the value. The caller is responsible for freeing the returned
2639 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2643 if (ssid == NULL || var == NULL)
2646 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2647 const struct parse_data *field = &ssid_fields[i];
2648 if (os_strcmp(var, field->name) == 0) {
2649 char *res = field->writer(field, ssid);
2650 if (field->key_data) {
2651 if (res && res[0]) {
2652 wpa_printf(MSG_DEBUG, "Do not allow "
2653 "key_data field to be "
2655 str_clear_free(res);
2656 return os_strdup("*");
2668 #endif /* NO_CONFIG_WRITE */
2672 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2673 * @ssid: Pointer to network configuration data
2675 * This function must be called to update WPA PSK when either SSID or the
2676 * passphrase has changed for the network configuration.
2678 void wpa_config_update_psk(struct wpa_ssid *ssid)
2680 #ifndef CONFIG_NO_PBKDF2
2681 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2682 ssid->psk, PMK_LEN);
2683 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2684 ssid->psk, PMK_LEN);
2686 #endif /* CONFIG_NO_PBKDF2 */
2690 static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2695 int *ports, *nports;
2697 unsigned int num_ports;
2699 proto = os_realloc_array(cred->req_conn_capab_proto,
2700 cred->num_req_conn_capab + 1, sizeof(u8));
2703 cred->req_conn_capab_proto = proto;
2705 port = os_realloc_array(cred->req_conn_capab_port,
2706 cred->num_req_conn_capab + 1, sizeof(int *));
2709 cred->req_conn_capab_port = port;
2711 proto[cred->num_req_conn_capab] = atoi(value);
2713 pos = os_strchr(value, ':');
2715 port[cred->num_req_conn_capab] = NULL;
2716 cred->num_req_conn_capab++;
2725 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2726 if (nports == NULL) {
2731 ports[num_ports++] = atoi(pos);
2733 pos = os_strchr(pos, ',');
2739 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2740 if (nports == NULL) {
2745 ports[num_ports] = -1;
2747 port[cred->num_req_conn_capab] = ports;
2748 cred->num_req_conn_capab++;
2753 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2754 const char *value, int line)
2759 if (os_strcmp(var, "temporary") == 0) {
2760 cred->temporary = atoi(value);
2764 if (os_strcmp(var, "priority") == 0) {
2765 cred->priority = atoi(value);
2769 if (os_strcmp(var, "sp_priority") == 0) {
2770 int prio = atoi(value);
2771 if (prio < 0 || prio > 255)
2773 cred->sp_priority = prio;
2777 if (os_strcmp(var, "pcsc") == 0) {
2778 cred->pcsc = atoi(value);
2782 if (os_strcmp(var, "eap") == 0) {
2783 struct eap_method_type method;
2784 method.method = eap_peer_get_type(value, &method.vendor);
2785 if (method.vendor == EAP_VENDOR_IETF &&
2786 method.method == EAP_TYPE_NONE) {
2787 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2788 "for a credential", line, value);
2791 os_free(cred->eap_method);
2792 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2793 if (cred->eap_method == NULL)
2795 os_memcpy(cred->eap_method, &method, sizeof(method));
2799 if (os_strcmp(var, "password") == 0 &&
2800 os_strncmp(value, "ext:", 4) == 0) {
2801 str_clear_free(cred->password);
2802 cred->password = os_strdup(value);
2803 cred->ext_password = 1;
2807 if (os_strcmp(var, "update_identifier") == 0) {
2808 cred->update_identifier = atoi(value);
2812 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2813 cred->min_dl_bandwidth_home = atoi(value);
2817 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2818 cred->min_ul_bandwidth_home = atoi(value);
2822 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2823 cred->min_dl_bandwidth_roaming = atoi(value);
2827 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2828 cred->min_ul_bandwidth_roaming = atoi(value);
2832 if (os_strcmp(var, "max_bss_load") == 0) {
2833 cred->max_bss_load = atoi(value);
2837 if (os_strcmp(var, "req_conn_capab") == 0)
2838 return wpa_config_set_cred_req_conn_capab(cred, value);
2840 if (os_strcmp(var, "ocsp") == 0) {
2841 cred->ocsp = atoi(value);
2845 if (os_strcmp(var, "sim_num") == 0) {
2846 cred->sim_num = atoi(value);
2850 val = wpa_config_parse_string(value, &len);
2852 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2853 "value '%s'.", line, var, value);
2857 if (os_strcmp(var, "realm") == 0) {
2858 os_free(cred->realm);
2863 if (os_strcmp(var, "username") == 0) {
2864 str_clear_free(cred->username);
2865 cred->username = val;
2869 if (os_strcmp(var, "password") == 0) {
2870 str_clear_free(cred->password);
2871 cred->password = val;
2872 cred->ext_password = 0;
2876 if (os_strcmp(var, "ca_cert") == 0) {
2877 os_free(cred->ca_cert);
2878 cred->ca_cert = val;
2882 if (os_strcmp(var, "client_cert") == 0) {
2883 os_free(cred->client_cert);
2884 cred->client_cert = val;
2888 if (os_strcmp(var, "private_key") == 0) {
2889 os_free(cred->private_key);
2890 cred->private_key = val;
2894 if (os_strcmp(var, "private_key_passwd") == 0) {
2895 str_clear_free(cred->private_key_passwd);
2896 cred->private_key_passwd = val;
2900 if (os_strcmp(var, "imsi") == 0) {
2901 os_free(cred->imsi);
2906 if (os_strcmp(var, "milenage") == 0) {
2907 str_clear_free(cred->milenage);
2908 cred->milenage = val;
2912 if (os_strcmp(var, "domain_suffix_match") == 0) {
2913 os_free(cred->domain_suffix_match);
2914 cred->domain_suffix_match = val;
2918 if (os_strcmp(var, "domain") == 0) {
2920 new_domain = os_realloc_array(cred->domain,
2921 cred->num_domain + 1,
2923 if (new_domain == NULL) {
2927 new_domain[cred->num_domain++] = val;
2928 cred->domain = new_domain;
2932 if (os_strcmp(var, "phase1") == 0) {
2933 os_free(cred->phase1);
2938 if (os_strcmp(var, "phase2") == 0) {
2939 os_free(cred->phase2);
2944 if (os_strcmp(var, "roaming_consortium") == 0) {
2945 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2946 wpa_printf(MSG_ERROR, "Line %d: invalid "
2947 "roaming_consortium length %d (3..15 "
2948 "expected)", line, (int) len);
2952 os_memcpy(cred->roaming_consortium, val, len);
2953 cred->roaming_consortium_len = len;
2958 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2959 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2961 wpa_printf(MSG_ERROR, "Line %d: invalid "
2962 "required_roaming_consortium length %d "
2963 "(3..15 expected)", line, (int) len);
2967 os_memcpy(cred->required_roaming_consortium, val, len);
2968 cred->required_roaming_consortium_len = len;
2973 if (os_strcmp(var, "excluded_ssid") == 0) {
2974 struct excluded_ssid *e;
2976 if (len > SSID_MAX_LEN) {
2977 wpa_printf(MSG_ERROR, "Line %d: invalid "
2978 "excluded_ssid length %d", line, (int) len);
2983 e = os_realloc_array(cred->excluded_ssid,
2984 cred->num_excluded_ssid + 1,
2985 sizeof(struct excluded_ssid));
2990 cred->excluded_ssid = e;
2992 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2993 os_memcpy(e->ssid, val, len);
3001 if (os_strcmp(var, "roaming_partner") == 0) {
3002 struct roaming_partner *p;
3005 p = os_realloc_array(cred->roaming_partner,
3006 cred->num_roaming_partner + 1,
3007 sizeof(struct roaming_partner));
3012 cred->roaming_partner = p;
3014 p = &cred->roaming_partner[cred->num_roaming_partner];
3016 pos = os_strchr(val, ',');
3022 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3026 os_memcpy(p->fqdn, val, pos - val);
3028 p->exact_match = atoi(pos);
3030 pos = os_strchr(pos, ',');
3037 p->priority = atoi(pos);
3039 pos = os_strchr(pos, ',');
3046 if (os_strlen(pos) >= sizeof(p->country)) {
3050 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3052 cred->num_roaming_partner++;
3058 if (os_strcmp(var, "provisioning_sp") == 0) {
3059 os_free(cred->provisioning_sp);
3060 cred->provisioning_sp = val;
3065 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3075 static char * alloc_int_str(int val)
3077 const unsigned int bufsize = 20;
3081 buf = os_malloc(bufsize);
3084 res = os_snprintf(buf, bufsize, "%d", val);
3085 if (os_snprintf_error(bufsize, res)) {
3093 static char * alloc_strdup(const char *str)
3097 return os_strdup(str);
3101 char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3103 if (os_strcmp(var, "temporary") == 0)
3104 return alloc_int_str(cred->temporary);
3106 if (os_strcmp(var, "priority") == 0)
3107 return alloc_int_str(cred->priority);
3109 if (os_strcmp(var, "sp_priority") == 0)
3110 return alloc_int_str(cred->sp_priority);
3112 if (os_strcmp(var, "pcsc") == 0)
3113 return alloc_int_str(cred->pcsc);
3115 if (os_strcmp(var, "eap") == 0) {
3116 if (!cred->eap_method)
3118 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3119 cred->eap_method[0].method));
3122 if (os_strcmp(var, "update_identifier") == 0)
3123 return alloc_int_str(cred->update_identifier);
3125 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3126 return alloc_int_str(cred->min_dl_bandwidth_home);
3128 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3129 return alloc_int_str(cred->min_ul_bandwidth_home);
3131 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3132 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3134 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3135 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3137 if (os_strcmp(var, "max_bss_load") == 0)
3138 return alloc_int_str(cred->max_bss_load);
3140 if (os_strcmp(var, "req_conn_capab") == 0) {
3142 char *buf, *end, *pos;
3145 if (!cred->num_req_conn_capab)
3148 buf = os_malloc(4000);
3153 for (i = 0; i < cred->num_req_conn_capab; i++) {
3156 ret = os_snprintf(pos, end - pos, "%s%u",
3158 cred->req_conn_capab_proto[i]);
3159 if (os_snprintf_error(end - pos, ret))
3163 ports = cred->req_conn_capab_port[i];
3166 for (j = 0; ports[j] != -1; j++) {
3167 ret = os_snprintf(pos, end - pos,
3171 if (os_snprintf_error(end - pos, ret))
3181 if (os_strcmp(var, "ocsp") == 0)
3182 return alloc_int_str(cred->ocsp);
3184 if (os_strcmp(var, "realm") == 0)
3185 return alloc_strdup(cred->realm);
3187 if (os_strcmp(var, "username") == 0)
3188 return alloc_strdup(cred->username);
3190 if (os_strcmp(var, "password") == 0) {
3191 if (!cred->password)
3193 return alloc_strdup("*");
3196 if (os_strcmp(var, "ca_cert") == 0)
3197 return alloc_strdup(cred->ca_cert);
3199 if (os_strcmp(var, "client_cert") == 0)
3200 return alloc_strdup(cred->client_cert);
3202 if (os_strcmp(var, "private_key") == 0)
3203 return alloc_strdup(cred->private_key);
3205 if (os_strcmp(var, "private_key_passwd") == 0) {
3206 if (!cred->private_key_passwd)
3208 return alloc_strdup("*");
3211 if (os_strcmp(var, "imsi") == 0)
3212 return alloc_strdup(cred->imsi);
3214 if (os_strcmp(var, "milenage") == 0) {
3215 if (!(cred->milenage))
3217 return alloc_strdup("*");
3220 if (os_strcmp(var, "domain_suffix_match") == 0)
3221 return alloc_strdup(cred->domain_suffix_match);
3223 if (os_strcmp(var, "domain") == 0) {
3225 char *buf, *end, *pos;
3228 if (!cred->num_domain)
3231 buf = os_malloc(4000);
3237 for (i = 0; i < cred->num_domain; i++) {
3238 ret = os_snprintf(pos, end - pos, "%s%s",
3239 i > 0 ? "\n" : "", cred->domain[i]);
3240 if (os_snprintf_error(end - pos, ret))
3248 if (os_strcmp(var, "phase1") == 0)
3249 return alloc_strdup(cred->phase1);
3251 if (os_strcmp(var, "phase2") == 0)
3252 return alloc_strdup(cred->phase2);
3254 if (os_strcmp(var, "roaming_consortium") == 0) {
3258 if (!cred->roaming_consortium_len)
3260 buflen = cred->roaming_consortium_len * 2 + 1;
3261 buf = os_malloc(buflen);
3264 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3265 cred->roaming_consortium_len);
3269 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3273 if (!cred->required_roaming_consortium_len)
3275 buflen = cred->required_roaming_consortium_len * 2 + 1;
3276 buf = os_malloc(buflen);
3279 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3280 cred->required_roaming_consortium_len);
3284 if (os_strcmp(var, "excluded_ssid") == 0) {
3286 char *buf, *end, *pos;
3288 if (!cred->num_excluded_ssid)
3291 buf = os_malloc(4000);
3297 for (i = 0; i < cred->num_excluded_ssid; i++) {
3298 struct excluded_ssid *e;
3301 e = &cred->excluded_ssid[i];
3302 ret = os_snprintf(pos, end - pos, "%s%s",
3304 wpa_ssid_txt(e->ssid, e->ssid_len));
3305 if (os_snprintf_error(end - pos, ret))
3313 if (os_strcmp(var, "roaming_partner") == 0) {
3315 char *buf, *end, *pos;
3317 if (!cred->num_roaming_partner)
3320 buf = os_malloc(4000);
3326 for (i = 0; i < cred->num_roaming_partner; i++) {
3327 struct roaming_partner *p;
3330 p = &cred->roaming_partner[i];
3331 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3333 p->fqdn, p->exact_match, p->priority,
3335 if (os_snprintf_error(end - pos, ret))
3343 if (os_strcmp(var, "provisioning_sp") == 0)
3344 return alloc_strdup(cred->provisioning_sp);
3350 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3352 struct wpa_cred *cred;
3354 cred = config->cred;
3365 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3368 struct wpa_cred *cred, *last = NULL;
3371 cred = config->cred;
3380 cred = os_zalloc(sizeof(*cred));
3384 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
3388 config->cred = cred;
3394 int wpa_config_remove_cred(struct wpa_config *config, int id)
3396 struct wpa_cred *cred, *prev = NULL;
3398 cred = config->cred;
3410 prev->next = cred->next;
3412 config->cred = cred->next;
3414 wpa_config_free_cred(cred);
3419 #ifndef CONFIG_NO_CONFIG_BLOBS
3421 * wpa_config_get_blob - Get a named configuration blob
3422 * @config: Configuration data from wpa_config_read()
3423 * @name: Name of the blob
3424 * Returns: Pointer to blob data or %NULL if not found
3426 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3429 struct wpa_config_blob *blob = config->blobs;
3432 if (os_strcmp(blob->name, name) == 0)
3441 * wpa_config_set_blob - Set or add a named configuration blob
3442 * @config: Configuration data from wpa_config_read()
3443 * @blob: New value for the blob
3445 * Adds a new configuration blob or replaces the current value of an existing
3448 void wpa_config_set_blob(struct wpa_config *config,
3449 struct wpa_config_blob *blob)
3451 wpa_config_remove_blob(config, blob->name);
3452 blob->next = config->blobs;
3453 config->blobs = blob;
3458 * wpa_config_free_blob - Free blob data
3459 * @blob: Pointer to blob to be freed
3461 void wpa_config_free_blob(struct wpa_config_blob *blob)
3464 os_free(blob->name);
3465 bin_clear_free(blob->data, blob->len);
3472 * wpa_config_remove_blob - Remove a named configuration blob
3473 * @config: Configuration data from wpa_config_read()
3474 * @name: Name of the blob to remove
3475 * Returns: 0 if blob was removed or -1 if blob was not found
3477 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3479 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3482 if (os_strcmp(pos->name, name) == 0) {
3484 prev->next = pos->next;
3486 config->blobs = pos->next;
3487 wpa_config_free_blob(pos);
3496 #endif /* CONFIG_NO_CONFIG_BLOBS */
3500 * wpa_config_alloc_empty - Allocate an empty configuration
3501 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3503 * @driver_param: Driver parameters
3504 * Returns: Pointer to allocated configuration data or %NULL on failure
3506 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3507 const char *driver_param)
3509 struct wpa_config *config;
3510 const int aCWmin = 4, aCWmax = 10;
3511 const struct hostapd_wmm_ac_params ac_bk =
3512 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3513 const struct hostapd_wmm_ac_params ac_be =
3514 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3515 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3516 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3517 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3518 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
3520 config = os_zalloc(sizeof(*config));
3523 config->eapol_version = DEFAULT_EAPOL_VERSION;
3524 config->ap_scan = DEFAULT_AP_SCAN;
3525 config->user_mpm = DEFAULT_USER_MPM;
3526 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
3527 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
3528 config->dot11RSNASAERetransPeriod =
3529 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
3530 config->fast_reauth = DEFAULT_FAST_REAUTH;
3531 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3532 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
3533 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
3534 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
3535 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
3536 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
3537 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3538 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3539 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3540 config->max_num_sta = DEFAULT_MAX_NUM_STA;
3541 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
3542 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
3543 config->wmm_ac_params[0] = ac_be;
3544 config->wmm_ac_params[1] = ac_bk;
3545 config->wmm_ac_params[2] = ac_vi;
3546 config->wmm_ac_params[3] = ac_vo;
3547 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
3548 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
3549 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
3550 config->cert_in_cb = DEFAULT_CERT_IN_CB;
3551 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
3554 config->ctrl_interface = os_strdup(ctrl_interface);
3556 config->driver_param = os_strdup(driver_param);
3562 #ifndef CONFIG_NO_STDOUT_DEBUG
3564 * wpa_config_debug_dump_networks - Debug dump of configured networks
3565 * @config: Configuration data from wpa_config_read()
3567 void wpa_config_debug_dump_networks(struct wpa_config *config)
3570 struct wpa_ssid *ssid;
3572 for (prio = 0; prio < config->num_prio; prio++) {
3573 ssid = config->pssid[prio];
3574 wpa_printf(MSG_DEBUG, "Priority group %d",
3577 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3579 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3584 #endif /* CONFIG_NO_STDOUT_DEBUG */
3587 struct global_parse_data {
3589 int (*parser)(const struct global_parse_data *data,
3590 struct wpa_config *config, int line, const char *value);
3591 int (*get)(const char *name, struct wpa_config *config, long offset,
3592 char *buf, size_t buflen, int pretty_print);
3593 void *param1, *param2, *param3;
3594 unsigned int changed_flag;
3598 static int wpa_global_config_parse_int(const struct global_parse_data *data,
3599 struct wpa_config *config, int line,
3605 dst = (int *) (((u8 *) config) + (long) data->param1);
3606 val = strtol(pos, &end, 0);
3608 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3614 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3616 if (data->param2 && *dst < (long) data->param2) {
3617 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3618 "min_value=%ld)", line, data->name, *dst,
3619 (long) data->param2);
3620 *dst = (long) data->param2;
3624 if (data->param3 && *dst > (long) data->param3) {
3625 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3626 "max_value=%ld)", line, data->name, *dst,
3627 (long) data->param3);
3628 *dst = (long) data->param3;
3636 static int wpa_global_config_parse_str(const struct global_parse_data *data,
3637 struct wpa_config *config, int line,
3643 len = os_strlen(pos);
3644 if (data->param2 && len < (size_t) data->param2) {
3645 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3646 "min_len=%ld)", line, data->name,
3647 (unsigned long) len, (long) data->param2);
3651 if (data->param3 && len > (size_t) data->param3) {
3652 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3653 "max_len=%ld)", line, data->name,
3654 (unsigned long) len, (long) data->param3);
3658 tmp = os_strdup(pos);
3662 dst = (char **) (((u8 *) config) + (long) data->param1);
3665 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3671 static int wpa_config_process_bgscan(const struct global_parse_data *data,
3672 struct wpa_config *config, int line,
3679 tmp = wpa_config_parse_string(pos, &len);
3681 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3686 res = wpa_global_config_parse_str(data, config, line, tmp);
3692 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3693 struct wpa_config *config, int line,
3697 struct wpabuf **dst, *tmp;
3699 len = os_strlen(pos);
3703 tmp = wpabuf_alloc(len / 2);
3707 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3712 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3715 wpa_printf(MSG_DEBUG, "%s", data->name);
3721 static int wpa_config_process_freq_list(const struct global_parse_data *data,
3722 struct wpa_config *config, int line,
3727 freqs = wpa_config_parse_int_array(value);
3730 if (freqs[0] == 0) {
3734 os_free(config->freq_list);
3735 config->freq_list = freqs;
3741 static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3742 struct wpa_config *config, int line,
3746 struct hostapd_ip_addr addr;
3748 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3750 if (addr.af != AF_INET)
3753 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3754 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3755 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3756 WPA_GET_BE32((u8 *) dst));
3760 #endif /* CONFIG_P2P */
3763 static int wpa_config_process_country(const struct global_parse_data *data,
3764 struct wpa_config *config, int line,
3767 if (!pos[0] || !pos[1]) {
3768 wpa_printf(MSG_DEBUG, "Invalid country set");
3771 config->country[0] = pos[0];
3772 config->country[1] = pos[1];
3773 wpa_printf(MSG_DEBUG, "country='%c%c'",
3774 config->country[0], config->country[1]);
3779 static int wpa_config_process_load_dynamic_eap(
3780 const struct global_parse_data *data, struct wpa_config *config,
3781 int line, const char *so)
3784 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3785 ret = eap_peer_method_load(so);
3787 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3790 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3791 "method '%s'.", line, so);
3801 static int wpa_config_process_uuid(const struct global_parse_data *data,
3802 struct wpa_config *config, int line,
3806 if (uuid_str2bin(pos, config->uuid)) {
3807 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3810 uuid_bin2str(config->uuid, buf, sizeof(buf));
3811 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3816 static int wpa_config_process_device_type(
3817 const struct global_parse_data *data,
3818 struct wpa_config *config, int line, const char *pos)
3820 return wps_dev_type_str2bin(pos, config->device_type);
3824 static int wpa_config_process_os_version(const struct global_parse_data *data,
3825 struct wpa_config *config, int line,
3828 if (hexstr2bin(pos, config->os_version, 4)) {
3829 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3832 wpa_printf(MSG_DEBUG, "os_version=%08x",
3833 WPA_GET_BE32(config->os_version));
3838 static int wpa_config_process_wps_vendor_ext_m1(
3839 const struct global_parse_data *data,
3840 struct wpa_config *config, int line, const char *pos)
3843 int len = os_strlen(pos) / 2;
3847 wpa_printf(MSG_ERROR, "Line %d: "
3848 "invalid wps_vendor_ext_m1", line);
3852 tmp = wpabuf_alloc(len);
3854 p = wpabuf_put(tmp, len);
3856 if (hexstr2bin(pos, p, len)) {
3857 wpa_printf(MSG_ERROR, "Line %d: "
3858 "invalid wps_vendor_ext_m1", line);
3863 wpabuf_free(config->wps_vendor_ext_m1);
3864 config->wps_vendor_ext_m1 = tmp;
3866 wpa_printf(MSG_ERROR, "Can not allocate "
3867 "memory for wps_vendor_ext_m1");
3874 #endif /* CONFIG_WPS */
3877 static int wpa_config_process_sec_device_type(
3878 const struct global_parse_data *data,
3879 struct wpa_config *config, int line, const char *pos)
3883 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3884 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3889 idx = config->num_sec_device_types;
3891 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3894 config->num_sec_device_types++;
3899 static int wpa_config_process_p2p_pref_chan(
3900 const struct global_parse_data *data,
3901 struct wpa_config *config, int line, const char *pos)
3903 struct p2p_channel *pref = NULL, *n;
3904 unsigned int num = 0;
3908 /* format: class:chan,class:chan,... */
3911 op_class = atoi(pos);
3912 pos2 = os_strchr(pos, ':');
3918 n = os_realloc_array(pref, num + 1,
3919 sizeof(struct p2p_channel));
3923 pref[num].op_class = op_class;
3924 pref[num].chan = chan;
3927 pos = os_strchr(pos2, ',');
3933 os_free(config->p2p_pref_chan);
3934 config->p2p_pref_chan = pref;
3935 config->num_p2p_pref_chan = num;
3936 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3937 (u8 *) config->p2p_pref_chan,
3938 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3944 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3949 static int wpa_config_process_p2p_no_go_freq(
3950 const struct global_parse_data *data,
3951 struct wpa_config *config, int line, const char *pos)
3955 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3957 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3961 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3962 config->p2p_no_go_freq.num);
3967 #endif /* CONFIG_P2P */
3970 static int wpa_config_process_hessid(
3971 const struct global_parse_data *data,
3972 struct wpa_config *config, int line, const char *pos)
3974 if (hwaddr_aton2(pos, config->hessid) < 0) {
3975 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3984 static int wpa_config_process_sae_groups(
3985 const struct global_parse_data *data,
3986 struct wpa_config *config, int line, const char *pos)
3988 int *groups = wpa_config_parse_int_array(pos);
3989 if (groups == NULL) {
3990 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3995 os_free(config->sae_groups);
3996 config->sae_groups = groups;
4002 static int wpa_config_process_ap_vendor_elements(
4003 const struct global_parse_data *data,
4004 struct wpa_config *config, int line, const char *pos)
4007 int len = os_strlen(pos) / 2;
4011 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4016 tmp = wpabuf_alloc(len);
4018 p = wpabuf_put(tmp, len);
4020 if (hexstr2bin(pos, p, len)) {
4021 wpa_printf(MSG_ERROR, "Line %d: invalid "
4022 "ap_vendor_elements", line);
4027 wpabuf_free(config->ap_vendor_elements);
4028 config->ap_vendor_elements = tmp;
4030 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4031 "ap_vendor_elements");
4039 #ifdef CONFIG_CTRL_IFACE
4040 static int wpa_config_process_no_ctrl_interface(
4041 const struct global_parse_data *data,
4042 struct wpa_config *config, int line, const char *pos)
4044 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4045 os_free(config->ctrl_interface);
4046 config->ctrl_interface = NULL;
4049 #endif /* CONFIG_CTRL_IFACE */
4052 static int wpa_config_get_int(const char *name, struct wpa_config *config,
4053 long offset, char *buf, size_t buflen,
4056 int *val = (int *) (((u8 *) config) + (long) offset);
4059 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4060 return os_snprintf(buf, buflen, "%d", *val);
4064 static int wpa_config_get_str(const char *name, struct wpa_config *config,
4065 long offset, char *buf, size_t buflen,
4068 char **val = (char **) (((u8 *) config) + (long) offset);
4072 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4073 *val ? *val : "null");
4077 res = os_snprintf(buf, buflen, "%s", *val);
4078 if (os_snprintf_error(buflen, res))
4086 static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4087 long offset, char *buf, size_t buflen,
4090 void *val = ((u8 *) config) + (long) offset;
4092 char addr[INET_ADDRSTRLEN];
4094 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4098 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4100 res = os_snprintf(buf, buflen, "%s", addr);
4102 if (os_snprintf_error(buflen, res))
4107 #endif /* CONFIG_P2P */
4113 /* OFFSET: Get offset of a variable within the wpa_config structure */
4114 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4116 #define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4117 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4118 #define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
4119 #define INT(f) _INT(f), NULL, NULL
4120 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
4121 #define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
4122 #define STR(f) _STR(f), NULL, NULL
4123 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
4124 #define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
4125 #define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4126 OFFSET(f), NULL, NULL
4128 static const struct global_parse_data global_fields[] = {
4129 #ifdef CONFIG_CTRL_IFACE
4130 { STR(ctrl_interface), 0 },
4131 { FUNC_NO_VAR(no_ctrl_interface), 0 },
4132 { STR(ctrl_interface_group), 0 } /* deprecated */,
4133 #endif /* CONFIG_CTRL_IFACE */
4134 #ifdef CONFIG_MACSEC
4135 { INT_RANGE(eapol_version, 1, 3), 0 },
4136 #else /* CONFIG_MACSEC */
4137 { INT_RANGE(eapol_version, 1, 2), 0 },
4138 #endif /* CONFIG_MACSEC */
4139 { INT(ap_scan), 0 },
4140 { FUNC(bgscan), 0 },
4142 { INT(user_mpm), 0 },
4143 { INT_RANGE(max_peer_links, 0, 255), 0 },
4144 { INT(mesh_max_inactivity), 0 },
4145 { INT(dot11RSNASAERetransPeriod), 0 },
4146 #endif /* CONFIG_MESH */
4147 { INT(disable_scan_offload), 0 },
4148 { INT(fast_reauth), 0 },
4149 { STR(opensc_engine_path), 0 },
4150 { STR(pkcs11_engine_path), 0 },
4151 { STR(pkcs11_module_path), 0 },
4152 { STR(openssl_ciphers), 0 },
4153 { STR(pcsc_reader), 0 },
4154 { STR(pcsc_pin), 0 },
4155 { INT(external_sim), 0 },
4156 { STR(driver_param), 0 },
4157 { INT(dot11RSNAConfigPMKLifetime), 0 },
4158 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4159 { INT(dot11RSNAConfigSATimeout), 0 },
4160 #ifndef CONFIG_NO_CONFIG_WRITE
4161 { INT(update_config), 0 },
4162 #endif /* CONFIG_NO_CONFIG_WRITE */
4163 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4165 { FUNC(uuid), CFG_CHANGED_UUID },
4166 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4167 CFG_CHANGED_DEVICE_NAME },
4168 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4169 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4170 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4171 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4172 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4173 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4174 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4175 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
4176 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
4177 #endif /* CONFIG_WPS */
4179 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
4180 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4181 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4182 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4183 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
4184 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4185 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4186 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4187 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4188 { INT(p2p_group_idle), 0 },
4189 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
4190 { INT_RANGE(p2p_passphrase_len, 8, 63),
4191 CFG_CHANGED_P2P_PASSPHRASE_LEN },
4192 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
4193 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4194 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
4195 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
4196 { INT(p2p_go_ht40), 0 },
4197 { INT(p2p_go_vht), 0 },
4198 { INT(p2p_disabled), 0 },
4199 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
4200 { INT(p2p_no_group_iface), 0 },
4201 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
4202 { IPV4(ip_addr_go), 0 },
4203 { IPV4(ip_addr_mask), 0 },
4204 { IPV4(ip_addr_start), 0 },
4205 { IPV4(ip_addr_end), 0 },
4206 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
4207 #endif /* CONFIG_P2P */
4208 { FUNC(country), CFG_CHANGED_COUNTRY },
4209 { INT(bss_max_count), 0 },
4210 { INT(bss_expiration_age), 0 },
4211 { INT(bss_expiration_scan_count), 0 },
4212 { INT_RANGE(filter_ssids, 0, 1), 0 },
4213 { INT_RANGE(filter_rssi, -100, 0), 0 },
4214 { INT(max_num_sta), 0 },
4215 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
4217 { INT_RANGE(hs20, 0, 1), 0 },
4218 #endif /* CONFIG_HS20 */
4219 { INT_RANGE(interworking, 0, 1), 0 },
4220 { FUNC(hessid), 0 },
4221 { INT_RANGE(access_network_type, 0, 15), 0 },
4222 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4223 { STR(autoscan), 0 },
4224 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4225 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4226 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4227 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4228 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4229 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4230 { INT(p2p_go_max_inactivity), 0 },
4231 { INT_RANGE(auto_interworking, 0, 1), 0 },
4234 { FUNC(sae_groups), 0 },
4235 { INT(dtim_period), 0 },
4236 { INT(beacon_int), 0 },
4237 { FUNC(ap_vendor_elements), 0 },
4238 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
4239 { FUNC(freq_list), 0 },
4240 { INT(scan_cur_freq), 0 },
4241 { INT(sched_scan_interval), 0 },
4242 { INT(tdls_external_control), 0},
4243 { STR(osu_dir), 0 },
4244 { STR(wowlan_triggers), 0 },
4245 { INT(p2p_search_delay), 0},
4246 { INT(mac_addr), 0 },
4247 { INT(rand_addr_lifetime), 0 },
4248 { INT(preassoc_mac_addr), 0 },
4249 { INT(key_mgmt_offload), 0},
4250 { INT(passive_scan), 0 },
4251 { INT(reassoc_same_bss_optim), 0 },
4252 { INT(wps_priority), 0},
4254 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4255 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4256 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4257 #endif /* CONFIG_FST */
4258 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
4270 #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
4273 int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4278 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4279 const struct global_parse_data *field = &global_fields[i];
4285 tmp = field->get(field->name, config, (long) field->param1,
4297 int wpa_config_get_value(const char *name, struct wpa_config *config,
4298 char *buf, size_t buflen)
4302 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4303 const struct global_parse_data *field = &global_fields[i];
4305 if (os_strcmp(name, field->name) != 0)
4309 return field->get(name, config, (long) field->param1,
4317 int wpa_config_get_num_global_field_names(void)
4319 return NUM_GLOBAL_FIELDS;
4323 const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4325 if (i >= NUM_GLOBAL_FIELDS)
4329 *no_var = !global_fields[i].param1;
4330 return global_fields[i].name;
4334 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4339 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4340 const struct global_parse_data *field = &global_fields[i];
4341 size_t flen = os_strlen(field->name);
4342 if (os_strncmp(pos, field->name, flen) != 0 ||
4346 if (field->parser(field, config, line, pos + flen + 1)) {
4347 wpa_printf(MSG_ERROR, "Line %d: failed to "
4348 "parse '%s'.", line, pos);
4351 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4352 config->wps_nfc_pw_from_config = 1;
4353 config->changed_parameters |= field->changed_flag;
4356 if (i == NUM_GLOBAL_FIELDS) {
4358 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4359 char *tmp = os_strchr(pos, '=');
4363 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4368 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4370 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4375 #endif /* CONFIG_AP */
4378 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",