Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant / Configuration parser and common functions
      3  * Copyright (c) 2003-2012, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "includes.h"
     10 
     11 #include "common.h"
     12 #include "utils/uuid.h"
     13 #include "crypto/sha1.h"
     14 #include "rsn_supp/wpa.h"
     15 #include "eap_peer/eap.h"
     16 #include "p2p/p2p.h"
     17 #include "config.h"
     18 
     19 
     20 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
     21 #define NO_CONFIG_WRITE
     22 #endif
     23 
     24 /*
     25  * Structure for network configuration parsing. This data is used to implement
     26  * a generic parser for each network block variable. The table of configuration
     27  * variables is defined below in this file (ssid_fields[]).
     28  */
     29 struct parse_data {
     30 	/* Configuration variable name */
     31 	char *name;
     32 
     33 	/* Parser function for this variable */
     34 	int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
     35 		      int line, const char *value);
     36 
     37 #ifndef NO_CONFIG_WRITE
     38 	/* Writer function (i.e., to get the variable in text format from
     39 	 * internal presentation). */
     40 	char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
     41 #endif /* NO_CONFIG_WRITE */
     42 
     43 	/* Variable specific parameters for the parser. */
     44 	void *param1, *param2, *param3, *param4;
     45 
     46 	/* 0 = this variable can be included in debug output and ctrl_iface
     47 	 * 1 = this variable contains key/private data and it must not be
     48 	 *     included in debug output unless explicitly requested. In
     49 	 *     addition, this variable will not be readable through the
     50 	 *     ctrl_iface.
     51 	 */
     52 	int key_data;
     53 };
     54 
     55 
     56 static int wpa_config_parse_str(const struct parse_data *data,
     57 				struct wpa_ssid *ssid,
     58 				int line, const char *value)
     59 {
     60 	size_t res_len, *dst_len;
     61 	char **dst, *tmp;
     62 
     63 	if (os_strcmp(value, "NULL") == 0) {
     64 		wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
     65 			   data->name);
     66 		tmp = NULL;
     67 		res_len = 0;
     68 		goto set;
     69 	}
     70 
     71 	tmp = wpa_config_parse_string(value, &res_len);
     72 	if (tmp == NULL) {
     73 		wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
     74 			   line, data->name,
     75 			   data->key_data ? "[KEY DATA REMOVED]" : value);
     76 		return -1;
     77 	}
     78 
     79 	if (data->key_data) {
     80 		wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
     81 				      (u8 *) tmp, res_len);
     82 	} else {
     83 		wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
     84 				  (u8 *) tmp, res_len);
     85 	}
     86 
     87 	if (data->param3 && res_len < (size_t) data->param3) {
     88 		wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
     89 			   "min_len=%ld)", line, data->name,
     90 			   (unsigned long) res_len, (long) data->param3);
     91 		os_free(tmp);
     92 		return -1;
     93 	}
     94 
     95 	if (data->param4 && res_len > (size_t) data->param4) {
     96 		wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
     97 			   "max_len=%ld)", line, data->name,
     98 			   (unsigned long) res_len, (long) data->param4);
     99 		os_free(tmp);
    100 		return -1;
    101 	}
    102 
    103 set:
    104 	dst = (char **) (((u8 *) ssid) + (long) data->param1);
    105 	dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
    106 	os_free(*dst);
    107 	*dst = tmp;
    108 	if (data->param2)
    109 		*dst_len = res_len;
    110 
    111 	return 0;
    112 }
    113 
    114 
    115 #ifndef NO_CONFIG_WRITE
    116 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
    117 {
    118 	char *buf;
    119 
    120 	buf = os_malloc(len + 3);
    121 	if (buf == NULL)
    122 		return NULL;
    123 	buf[0] = '"';
    124 	os_memcpy(buf + 1, value, len);
    125 	buf[len + 1] = '"';
    126 	buf[len + 2] = '\0';
    127 
    128 	return buf;
    129 }
    130 
    131 
    132 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
    133 {
    134 	char *buf;
    135 
    136 	buf = os_zalloc(2 * len + 1);
    137 	if (buf == NULL)
    138 		return NULL;
    139 	wpa_snprintf_hex(buf, 2 * len + 1, value, len);
    140 
    141 	return buf;
    142 }
    143 
    144 
    145 static char * wpa_config_write_string(const u8 *value, size_t len)
    146 {
    147 	if (value == NULL)
    148 		return NULL;
    149 
    150 	if (is_hex(value, len))
    151 		return wpa_config_write_string_hex(value, len);
    152 	else
    153 		return wpa_config_write_string_ascii(value, len);
    154 }
    155 
    156 
    157 static char * wpa_config_write_str(const struct parse_data *data,
    158 				   struct wpa_ssid *ssid)
    159 {
    160 	size_t len;
    161 	char **src;
    162 
    163 	src = (char **) (((u8 *) ssid) + (long) data->param1);
    164 	if (*src == NULL)
    165 		return NULL;
    166 
    167 	if (data->param2)
    168 		len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
    169 	else
    170 		len = os_strlen(*src);
    171 
    172 	return wpa_config_write_string((const u8 *) *src, len);
    173 }
    174 #endif /* NO_CONFIG_WRITE */
    175 
    176 
    177 static int wpa_config_parse_int(const struct parse_data *data,
    178 				struct wpa_ssid *ssid,
    179 				int line, const char *value)
    180 {
    181 	int val, *dst;
    182 	char *end;
    183 
    184 	dst = (int *) (((u8 *) ssid) + (long) data->param1);
    185 	val = strtol(value, &end, 0);
    186 	if (*end) {
    187 		wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
    188 			   line, value);
    189 		return -1;
    190 	}
    191 	*dst = val;
    192 	wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
    193 
    194 	if (data->param3 && *dst < (long) data->param3) {
    195 		wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
    196 			   "min_value=%ld)", line, data->name, *dst,
    197 			   (long) data->param3);
    198 		*dst = (long) data->param3;
    199 		return -1;
    200 	}
    201 
    202 	if (data->param4 && *dst > (long) data->param4) {
    203 		wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
    204 			   "max_value=%ld)", line, data->name, *dst,
    205 			   (long) data->param4);
    206 		*dst = (long) data->param4;
    207 		return -1;
    208 	}
    209 
    210 	return 0;
    211 }
    212 
    213 
    214 #ifndef NO_CONFIG_WRITE
    215 static char * wpa_config_write_int(const struct parse_data *data,
    216 				   struct wpa_ssid *ssid)
    217 {
    218 	int *src, res;
    219 	char *value;
    220 
    221 	src = (int *) (((u8 *) ssid) + (long) data->param1);
    222 
    223 	value = os_malloc(20);
    224 	if (value == NULL)
    225 		return NULL;
    226 	res = os_snprintf(value, 20, "%d", *src);
    227 	if (res < 0 || res >= 20) {
    228 		os_free(value);
    229 		return NULL;
    230 	}
    231 	value[20 - 1] = '\0';
    232 	return value;
    233 }
    234 #endif /* NO_CONFIG_WRITE */
    235 
    236 
    237 static int wpa_config_parse_bssid(const struct parse_data *data,
    238 				  struct wpa_ssid *ssid, int line,
    239 				  const char *value)
    240 {
    241 	if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
    242 	    os_strcmp(value, "any") == 0) {
    243 		ssid->bssid_set = 0;
    244 		wpa_printf(MSG_MSGDUMP, "BSSID any");
    245 		return 0;
    246 	}
    247 	if (hwaddr_aton(value, ssid->bssid)) {
    248 		wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
    249 			   line, value);
    250 		return -1;
    251 	}
    252 	ssid->bssid_set = 1;
    253 	wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
    254 	return 0;
    255 }
    256 
    257 
    258 #ifndef NO_CONFIG_WRITE
    259 static char * wpa_config_write_bssid(const struct parse_data *data,
    260 				     struct wpa_ssid *ssid)
    261 {
    262 	char *value;
    263 	int res;
    264 
    265 	if (!ssid->bssid_set)
    266 		return NULL;
    267 
    268 	value = os_malloc(20);
    269 	if (value == NULL)
    270 		return NULL;
    271 	res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
    272 	if (res < 0 || res >= 20) {
    273 		os_free(value);
    274 		return NULL;
    275 	}
    276 	value[20 - 1] = '\0';
    277 	return value;
    278 }
    279 #endif /* NO_CONFIG_WRITE */
    280 
    281 
    282 static int wpa_config_parse_psk(const struct parse_data *data,
    283 				struct wpa_ssid *ssid, int line,
    284 				const char *value)
    285 {
    286 #ifdef CONFIG_EXT_PASSWORD
    287 	if (os_strncmp(value, "ext:", 4) == 0) {
    288 		os_free(ssid->passphrase);
    289 		ssid->passphrase = NULL;
    290 		ssid->psk_set = 0;
    291 		os_free(ssid->ext_psk);
    292 		ssid->ext_psk = os_strdup(value + 4);
    293 		if (ssid->ext_psk == NULL)
    294 			return -1;
    295 		wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
    296 			   ssid->ext_psk);
    297 		return 0;
    298 	}
    299 #endif /* CONFIG_EXT_PASSWORD */
    300 
    301 	if (*value == '"') {
    302 #ifndef CONFIG_NO_PBKDF2
    303 		const char *pos;
    304 		size_t len;
    305 
    306 		value++;
    307 		pos = os_strrchr(value, '"');
    308 		if (pos)
    309 			len = pos - value;
    310 		else
    311 			len = os_strlen(value);
    312 		if (len < 8 || len > 63) {
    313 			wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
    314 				   "length %lu (expected: 8..63) '%s'.",
    315 				   line, (unsigned long) len, value);
    316 			return -1;
    317 		}
    318 		wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
    319 				      (u8 *) value, len);
    320 		if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
    321 		    os_memcmp(ssid->passphrase, value, len) == 0)
    322 			return 0;
    323 		ssid->psk_set = 0;
    324 		os_free(ssid->passphrase);
    325 		ssid->passphrase = dup_binstr(value, len);
    326 		if (ssid->passphrase == NULL)
    327 			return -1;
    328 		return 0;
    329 #else /* CONFIG_NO_PBKDF2 */
    330 		wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
    331 			   "supported.", line);
    332 		return -1;
    333 #endif /* CONFIG_NO_PBKDF2 */
    334 	}
    335 
    336 	if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
    337 	    value[PMK_LEN * 2] != '\0') {
    338 		wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
    339 			   line, value);
    340 		return -1;
    341 	}
    342 
    343 	os_free(ssid->passphrase);
    344 	ssid->passphrase = NULL;
    345 
    346 	ssid->psk_set = 1;
    347 	wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
    348 	return 0;
    349 }
    350 
    351 
    352 #ifndef NO_CONFIG_WRITE
    353 static char * wpa_config_write_psk(const struct parse_data *data,
    354 				   struct wpa_ssid *ssid)
    355 {
    356 #ifdef CONFIG_EXT_PASSWORD
    357 	if (ssid->ext_psk) {
    358 		size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
    359 		char *buf = os_malloc(len);
    360 		if (buf == NULL)
    361 			return NULL;
    362 		os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
    363 		return buf;
    364 	}
    365 #endif /* CONFIG_EXT_PASSWORD */
    366 
    367 	if (ssid->passphrase)
    368 		return wpa_config_write_string_ascii(
    369 			(const u8 *) ssid->passphrase,
    370 			os_strlen(ssid->passphrase));
    371 
    372 	if (ssid->psk_set)
    373 		return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
    374 
    375 	return NULL;
    376 }
    377 #endif /* NO_CONFIG_WRITE */
    378 
    379 
    380 static int wpa_config_parse_proto(const struct parse_data *data,
    381 				  struct wpa_ssid *ssid, int line,
    382 				  const char *value)
    383 {
    384 	int val = 0, last, errors = 0;
    385 	char *start, *end, *buf;
    386 
    387 	buf = os_strdup(value);
    388 	if (buf == NULL)
    389 		return -1;
    390 	start = buf;
    391 
    392 	while (*start != '\0') {
    393 		while (*start == ' ' || *start == '\t')
    394 			start++;
    395 		if (*start == '\0')
    396 			break;
    397 		end = start;
    398 		while (*end != ' ' && *end != '\t' && *end != '\0')
    399 			end++;
    400 		last = *end == '\0';
    401 		*end = '\0';
    402 		if (os_strcmp(start, "WPA") == 0)
    403 			val |= WPA_PROTO_WPA;
    404 		else if (os_strcmp(start, "RSN") == 0 ||
    405 			 os_strcmp(start, "WPA2") == 0)
    406 			val |= WPA_PROTO_RSN;
    407 		else {
    408 			wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
    409 				   line, start);
    410 			errors++;
    411 		}
    412 
    413 		if (last)
    414 			break;
    415 		start = end + 1;
    416 	}
    417 	os_free(buf);
    418 
    419 	if (val == 0) {
    420 		wpa_printf(MSG_ERROR,
    421 			   "Line %d: no proto values configured.", line);
    422 		errors++;
    423 	}
    424 
    425 	wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
    426 	ssid->proto = val;
    427 	return errors ? -1 : 0;
    428 }
    429 
    430 
    431 #ifndef NO_CONFIG_WRITE
    432 static char * wpa_config_write_proto(const struct parse_data *data,
    433 				     struct wpa_ssid *ssid)
    434 {
    435 	int first = 1, ret;
    436 	char *buf, *pos, *end;
    437 
    438 	pos = buf = os_zalloc(10);
    439 	if (buf == NULL)
    440 		return NULL;
    441 	end = buf + 10;
    442 
    443 	if (ssid->proto & WPA_PROTO_WPA) {
    444 		ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
    445 		if (ret < 0 || ret >= end - pos)
    446 			return buf;
    447 		pos += ret;
    448 		first = 0;
    449 	}
    450 
    451 	if (ssid->proto & WPA_PROTO_RSN) {
    452 		ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
    453 		if (ret < 0 || ret >= end - pos)
    454 			return buf;
    455 		pos += ret;
    456 		first = 0;
    457 	}
    458 
    459 	return buf;
    460 }
    461 #endif /* NO_CONFIG_WRITE */
    462 
    463 
    464 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
    465 				     struct wpa_ssid *ssid, int line,
    466 				     const char *value)
    467 {
    468 	int val = 0, last, errors = 0;
    469 	char *start, *end, *buf;
    470 
    471 	buf = os_strdup(value);
    472 	if (buf == NULL)
    473 		return -1;
    474 	start = buf;
    475 
    476 	while (*start != '\0') {
    477 		while (*start == ' ' || *start == '\t')
    478 			start++;
    479 		if (*start == '\0')
    480 			break;
    481 		end = start;
    482 		while (*end != ' ' && *end != '\t' && *end != '\0')
    483 			end++;
    484 		last = *end == '\0';
    485 		*end = '\0';
    486 		if (os_strcmp(start, "WPA-PSK") == 0)
    487 			val |= WPA_KEY_MGMT_PSK;
    488 		else if (os_strcmp(start, "WPA-EAP") == 0)
    489 			val |= WPA_KEY_MGMT_IEEE8021X;
    490 		else if (os_strcmp(start, "IEEE8021X") == 0)
    491 			val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
    492 		else if (os_strcmp(start, "NONE") == 0)
    493 			val |= WPA_KEY_MGMT_NONE;
    494 		else if (os_strcmp(start, "WPA-NONE") == 0)
    495 			val |= WPA_KEY_MGMT_WPA_NONE;
    496 #ifdef CONFIG_IEEE80211R
    497 		else if (os_strcmp(start, "FT-PSK") == 0)
    498 			val |= WPA_KEY_MGMT_FT_PSK;
    499 		else if (os_strcmp(start, "FT-EAP") == 0)
    500 			val |= WPA_KEY_MGMT_FT_IEEE8021X;
    501 #endif /* CONFIG_IEEE80211R */
    502 #ifdef CONFIG_IEEE80211W
    503 		else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
    504 			val |= WPA_KEY_MGMT_PSK_SHA256;
    505 		else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
    506 			val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
    507 #endif /* CONFIG_IEEE80211W */
    508 #ifdef CONFIG_WPS
    509 		else if (os_strcmp(start, "WPS") == 0)
    510 			val |= WPA_KEY_MGMT_WPS;
    511 #endif /* CONFIG_WPS */
    512 #ifdef CONFIG_SAE
    513 		else if (os_strcmp(start, "SAE") == 0)
    514 			val |= WPA_KEY_MGMT_SAE;
    515 		else if (os_strcmp(start, "FT-SAE") == 0)
    516 			val |= WPA_KEY_MGMT_FT_SAE;
    517 #endif /* CONFIG_SAE */
    518 		else {
    519 			wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
    520 				   line, start);
    521 			errors++;
    522 		}
    523 
    524 		if (last)
    525 			break;
    526 		start = end + 1;
    527 	}
    528 	os_free(buf);
    529 
    530 	if (val == 0) {
    531 		wpa_printf(MSG_ERROR,
    532 			   "Line %d: no key_mgmt values configured.", line);
    533 		errors++;
    534 	}
    535 
    536 	wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
    537 	ssid->key_mgmt = val;
    538 	return errors ? -1 : 0;
    539 }
    540 
    541 
    542 #ifndef NO_CONFIG_WRITE
    543 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
    544 					struct wpa_ssid *ssid)
    545 {
    546 	char *buf, *pos, *end;
    547 	int ret;
    548 
    549 	pos = buf = os_zalloc(50);
    550 	if (buf == NULL)
    551 		return NULL;
    552 	end = buf + 50;
    553 
    554 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
    555 		ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
    556 				  pos == buf ? "" : " ");
    557 		if (ret < 0 || ret >= end - pos) {
    558 			end[-1] = '\0';
    559 			return buf;
    560 		}
    561 		pos += ret;
    562 	}
    563 
    564 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
    565 		ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
    566 				  pos == buf ? "" : " ");
    567 		if (ret < 0 || ret >= end - pos) {
    568 			end[-1] = '\0';
    569 			return buf;
    570 		}
    571 		pos += ret;
    572 	}
    573 
    574 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
    575 		ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
    576 				  pos == buf ? "" : " ");
    577 		if (ret < 0 || ret >= end - pos) {
    578 			end[-1] = '\0';
    579 			return buf;
    580 		}
    581 		pos += ret;
    582 	}
    583 
    584 	if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
    585 		ret = os_snprintf(pos, end - pos, "%sNONE",
    586 				  pos == buf ? "" : " ");
    587 		if (ret < 0 || ret >= end - pos) {
    588 			end[-1] = '\0';
    589 			return buf;
    590 		}
    591 		pos += ret;
    592 	}
    593 
    594 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
    595 		ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
    596 				  pos == buf ? "" : " ");
    597 		if (ret < 0 || ret >= end - pos) {
    598 			end[-1] = '\0';
    599 			return buf;
    600 		}
    601 		pos += ret;
    602 	}
    603 
    604 #ifdef CONFIG_IEEE80211R
    605 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
    606 		pos += os_snprintf(pos, end - pos, "%sFT-PSK",
    607 				   pos == buf ? "" : " ");
    608 
    609 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
    610 		pos += os_snprintf(pos, end - pos, "%sFT-EAP",
    611 				   pos == buf ? "" : " ");
    612 #endif /* CONFIG_IEEE80211R */
    613 
    614 #ifdef CONFIG_IEEE80211W
    615 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
    616 		pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
    617 				   pos == buf ? "" : " ");
    618 
    619 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
    620 		pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
    621 				   pos == buf ? "" : " ");
    622 #endif /* CONFIG_IEEE80211W */
    623 
    624 #ifdef CONFIG_WPS
    625 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
    626 		pos += os_snprintf(pos, end - pos, "%sWPS",
    627 				   pos == buf ? "" : " ");
    628 #endif /* CONFIG_WPS */
    629 
    630 	return buf;
    631 }
    632 #endif /* NO_CONFIG_WRITE */
    633 
    634 
    635 static int wpa_config_parse_cipher(int line, const char *value)
    636 {
    637 	int val = wpa_parse_cipher(value);
    638 	if (val < 0) {
    639 		wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
    640 			   line, value);
    641 		return -1;
    642 	}
    643 	if (val == 0) {
    644 		wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
    645 			   line);
    646 		return -1;
    647 	}
    648 	return val;
    649 }
    650 
    651 
    652 #ifndef NO_CONFIG_WRITE
    653 static char * wpa_config_write_cipher(int cipher)
    654 {
    655 	char *buf = os_zalloc(50);
    656 	if (buf == NULL)
    657 		return NULL;
    658 
    659 	if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
    660 		os_free(buf);
    661 		return NULL;
    662 	}
    663 
    664 	return buf;
    665 }
    666 #endif /* NO_CONFIG_WRITE */
    667 
    668 
    669 static int wpa_config_parse_pairwise(const struct parse_data *data,
    670 				     struct wpa_ssid *ssid, int line,
    671 				     const char *value)
    672 {
    673 	int val;
    674 	val = wpa_config_parse_cipher(line, value);
    675 	if (val == -1)
    676 		return -1;
    677 	if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
    678 		wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
    679 			   "(0x%x).", line, val);
    680 		return -1;
    681 	}
    682 
    683 	wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
    684 	ssid->pairwise_cipher = val;
    685 	return 0;
    686 }
    687 
    688 
    689 #ifndef NO_CONFIG_WRITE
    690 static char * wpa_config_write_pairwise(const struct parse_data *data,
    691 					struct wpa_ssid *ssid)
    692 {
    693 	return wpa_config_write_cipher(ssid->pairwise_cipher);
    694 }
    695 #endif /* NO_CONFIG_WRITE */
    696 
    697 
    698 static int wpa_config_parse_group(const struct parse_data *data,
    699 				  struct wpa_ssid *ssid, int line,
    700 				  const char *value)
    701 {
    702 	int val;
    703 	val = wpa_config_parse_cipher(line, value);
    704 	if (val == -1)
    705 		return -1;
    706 	if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
    707 		wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
    708 			   "(0x%x).", line, val);
    709 		return -1;
    710 	}
    711 
    712 	wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
    713 	ssid->group_cipher = val;
    714 	return 0;
    715 }
    716 
    717 
    718 #ifndef NO_CONFIG_WRITE
    719 static char * wpa_config_write_group(const struct parse_data *data,
    720 				     struct wpa_ssid *ssid)
    721 {
    722 	return wpa_config_write_cipher(ssid->group_cipher);
    723 }
    724 #endif /* NO_CONFIG_WRITE */
    725 
    726 
    727 static int wpa_config_parse_auth_alg(const struct parse_data *data,
    728 				     struct wpa_ssid *ssid, int line,
    729 				     const char *value)
    730 {
    731 	int val = 0, last, errors = 0;
    732 	char *start, *end, *buf;
    733 
    734 	buf = os_strdup(value);
    735 	if (buf == NULL)
    736 		return -1;
    737 	start = buf;
    738 
    739 	while (*start != '\0') {
    740 		while (*start == ' ' || *start == '\t')
    741 			start++;
    742 		if (*start == '\0')
    743 			break;
    744 		end = start;
    745 		while (*end != ' ' && *end != '\t' && *end != '\0')
    746 			end++;
    747 		last = *end == '\0';
    748 		*end = '\0';
    749 		if (os_strcmp(start, "OPEN") == 0)
    750 			val |= WPA_AUTH_ALG_OPEN;
    751 		else if (os_strcmp(start, "SHARED") == 0)
    752 			val |= WPA_AUTH_ALG_SHARED;
    753 		else if (os_strcmp(start, "LEAP") == 0)
    754 			val |= WPA_AUTH_ALG_LEAP;
    755 		else {
    756 			wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
    757 				   line, start);
    758 			errors++;
    759 		}
    760 
    761 		if (last)
    762 			break;
    763 		start = end + 1;
    764 	}
    765 	os_free(buf);
    766 
    767 	if (val == 0) {
    768 		wpa_printf(MSG_ERROR,
    769 			   "Line %d: no auth_alg values configured.", line);
    770 		errors++;
    771 	}
    772 
    773 	wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
    774 	ssid->auth_alg = val;
    775 	return errors ? -1 : 0;
    776 }
    777 
    778 
    779 #ifndef NO_CONFIG_WRITE
    780 static char * wpa_config_write_auth_alg(const struct parse_data *data,
    781 					struct wpa_ssid *ssid)
    782 {
    783 	char *buf, *pos, *end;
    784 	int ret;
    785 
    786 	pos = buf = os_zalloc(30);
    787 	if (buf == NULL)
    788 		return NULL;
    789 	end = buf + 30;
    790 
    791 	if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
    792 		ret = os_snprintf(pos, end - pos, "%sOPEN",
    793 				  pos == buf ? "" : " ");
    794 		if (ret < 0 || ret >= end - pos) {
    795 			end[-1] = '\0';
    796 			return buf;
    797 		}
    798 		pos += ret;
    799 	}
    800 
    801 	if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
    802 		ret = os_snprintf(pos, end - pos, "%sSHARED",
    803 				  pos == buf ? "" : " ");
    804 		if (ret < 0 || ret >= end - pos) {
    805 			end[-1] = '\0';
    806 			return buf;
    807 		}
    808 		pos += ret;
    809 	}
    810 
    811 	if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
    812 		ret = os_snprintf(pos, end - pos, "%sLEAP",
    813 				  pos == buf ? "" : " ");
    814 		if (ret < 0 || ret >= end - pos) {
    815 			end[-1] = '\0';
    816 			return buf;
    817 		}
    818 		pos += ret;
    819 	}
    820 
    821 	return buf;
    822 }
    823 #endif /* NO_CONFIG_WRITE */
    824 
    825 
    826 static int * wpa_config_parse_int_array(const char *value)
    827 {
    828 	int *freqs;
    829 	size_t used, len;
    830 	const char *pos;
    831 
    832 	used = 0;
    833 	len = 10;
    834 	freqs = os_calloc(len + 1, sizeof(int));
    835 	if (freqs == NULL)
    836 		return NULL;
    837 
    838 	pos = value;
    839 	while (pos) {
    840 		while (*pos == ' ')
    841 			pos++;
    842 		if (used == len) {
    843 			int *n;
    844 			size_t i;
    845 			n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
    846 			if (n == NULL) {
    847 				os_free(freqs);
    848 				return NULL;
    849 			}
    850 			for (i = len; i <= len * 2; i++)
    851 				n[i] = 0;
    852 			freqs = n;
    853 			len *= 2;
    854 		}
    855 
    856 		freqs[used] = atoi(pos);
    857 		if (freqs[used] == 0)
    858 			break;
    859 		used++;
    860 		pos = os_strchr(pos + 1, ' ');
    861 	}
    862 
    863 	return freqs;
    864 }
    865 
    866 
    867 static int wpa_config_parse_scan_freq(const struct parse_data *data,
    868 				      struct wpa_ssid *ssid, int line,
    869 				      const char *value)
    870 {
    871 	int *freqs;
    872 
    873 	freqs = wpa_config_parse_int_array(value);
    874 	if (freqs == NULL)
    875 		return -1;
    876 	os_free(ssid->scan_freq);
    877 	ssid->scan_freq = freqs;
    878 
    879 	return 0;
    880 }
    881 
    882 
    883 static int wpa_config_parse_freq_list(const struct parse_data *data,
    884 				      struct wpa_ssid *ssid, int line,
    885 				      const char *value)
    886 {
    887 	int *freqs;
    888 
    889 	freqs = wpa_config_parse_int_array(value);
    890 	if (freqs == NULL)
    891 		return -1;
    892 	os_free(ssid->freq_list);
    893 	ssid->freq_list = freqs;
    894 
    895 	return 0;
    896 }
    897 
    898 
    899 #ifndef NO_CONFIG_WRITE
    900 static char * wpa_config_write_freqs(const struct parse_data *data,
    901 				     const int *freqs)
    902 {
    903 	char *buf, *pos, *end;
    904 	int i, ret;
    905 	size_t count;
    906 
    907 	if (freqs == NULL)
    908 		return NULL;
    909 
    910 	count = 0;
    911 	for (i = 0; freqs[i]; i++)
    912 		count++;
    913 
    914 	pos = buf = os_zalloc(10 * count + 1);
    915 	if (buf == NULL)
    916 		return NULL;
    917 	end = buf + 10 * count + 1;
    918 
    919 	for (i = 0; freqs[i]; i++) {
    920 		ret = os_snprintf(pos, end - pos, "%s%u",
    921 				  i == 0 ? "" : " ", freqs[i]);
    922 		if (ret < 0 || ret >= end - pos) {
    923 			end[-1] = '\0';
    924 			return buf;
    925 		}
    926 		pos += ret;
    927 	}
    928 
    929 	return buf;
    930 }
    931 
    932 
    933 static char * wpa_config_write_scan_freq(const struct parse_data *data,
    934 					 struct wpa_ssid *ssid)
    935 {
    936 	return wpa_config_write_freqs(data, ssid->scan_freq);
    937 }
    938 
    939 
    940 static char * wpa_config_write_freq_list(const struct parse_data *data,
    941 					 struct wpa_ssid *ssid)
    942 {
    943 	return wpa_config_write_freqs(data, ssid->freq_list);
    944 }
    945 #endif /* NO_CONFIG_WRITE */
    946 
    947 
    948 #ifdef IEEE8021X_EAPOL
    949 static int wpa_config_parse_eap(const struct parse_data *data,
    950 				struct wpa_ssid *ssid, int line,
    951 				const char *value)
    952 {
    953 	int last, errors = 0;
    954 	char *start, *end, *buf;
    955 	struct eap_method_type *methods = NULL, *tmp;
    956 	size_t num_methods = 0;
    957 
    958 	buf = os_strdup(value);
    959 	if (buf == NULL)
    960 		return -1;
    961 	start = buf;
    962 
    963 	while (*start != '\0') {
    964 		while (*start == ' ' || *start == '\t')
    965 			start++;
    966 		if (*start == '\0')
    967 			break;
    968 		end = start;
    969 		while (*end != ' ' && *end != '\t' && *end != '\0')
    970 			end++;
    971 		last = *end == '\0';
    972 		*end = '\0';
    973 		tmp = methods;
    974 		methods = os_realloc_array(methods, num_methods + 1,
    975 					   sizeof(*methods));
    976 		if (methods == NULL) {
    977 			os_free(tmp);
    978 			os_free(buf);
    979 			return -1;
    980 		}
    981 		methods[num_methods].method = eap_peer_get_type(
    982 			start, &methods[num_methods].vendor);
    983 		if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
    984 		    methods[num_methods].method == EAP_TYPE_NONE) {
    985 			wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
    986 				   "'%s'", line, start);
    987 			wpa_printf(MSG_ERROR, "You may need to add support for"
    988 				   " this EAP method during wpa_supplicant\n"
    989 				   "build time configuration.\n"
    990 				   "See README for more information.");
    991 			errors++;
    992 		} else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
    993 			   methods[num_methods].method == EAP_TYPE_LEAP)
    994 			ssid->leap++;
    995 		else
    996 			ssid->non_leap++;
    997 		num_methods++;
    998 		if (last)
    999 			break;
   1000 		start = end + 1;
   1001 	}
   1002 	os_free(buf);
   1003 
   1004 	tmp = methods;
   1005 	methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
   1006 	if (methods == NULL) {
   1007 		os_free(tmp);
   1008 		return -1;
   1009 	}
   1010 	methods[num_methods].vendor = EAP_VENDOR_IETF;
   1011 	methods[num_methods].method = EAP_TYPE_NONE;
   1012 	num_methods++;
   1013 
   1014 	wpa_hexdump(MSG_MSGDUMP, "eap methods",
   1015 		    (u8 *) methods, num_methods * sizeof(*methods));
   1016 	os_free(ssid->eap.eap_methods);
   1017 	ssid->eap.eap_methods = methods;
   1018 	return errors ? -1 : 0;
   1019 }
   1020 
   1021 
   1022 static char * wpa_config_write_eap(const struct parse_data *data,
   1023 				   struct wpa_ssid *ssid)
   1024 {
   1025 	int i, ret;
   1026 	char *buf, *pos, *end;
   1027 	const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
   1028 	const char *name;
   1029 
   1030 	if (eap_methods == NULL)
   1031 		return NULL;
   1032 
   1033 	pos = buf = os_zalloc(100);
   1034 	if (buf == NULL)
   1035 		return NULL;
   1036 	end = buf + 100;
   1037 
   1038 	for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
   1039 		     eap_methods[i].method != EAP_TYPE_NONE; i++) {
   1040 		name = eap_get_name(eap_methods[i].vendor,
   1041 				    eap_methods[i].method);
   1042 		if (name) {
   1043 			ret = os_snprintf(pos, end - pos, "%s%s",
   1044 					  pos == buf ? "" : " ", name);
   1045 			if (ret < 0 || ret >= end - pos)
   1046 				break;
   1047 			pos += ret;
   1048 		}
   1049 	}
   1050 
   1051 	end[-1] = '\0';
   1052 
   1053 	return buf;
   1054 }
   1055 
   1056 
   1057 static int wpa_config_parse_password(const struct parse_data *data,
   1058 				     struct wpa_ssid *ssid, int line,
   1059 				     const char *value)
   1060 {
   1061 	u8 *hash;
   1062 
   1063 	if (os_strcmp(value, "NULL") == 0) {
   1064 		wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
   1065 		os_free(ssid->eap.password);
   1066 		ssid->eap.password = NULL;
   1067 		ssid->eap.password_len = 0;
   1068 		return 0;
   1069 	}
   1070 
   1071 #ifdef CONFIG_EXT_PASSWORD
   1072 	if (os_strncmp(value, "ext:", 4) == 0) {
   1073 		char *name = os_strdup(value + 4);
   1074 		if (name == NULL)
   1075 			return -1;
   1076 		os_free(ssid->eap.password);
   1077 		ssid->eap.password = (u8 *) name;
   1078 		ssid->eap.password_len = os_strlen(name);
   1079 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
   1080 		ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
   1081 		return 0;
   1082 	}
   1083 #endif /* CONFIG_EXT_PASSWORD */
   1084 
   1085 	if (os_strncmp(value, "hash:", 5) != 0) {
   1086 		char *tmp;
   1087 		size_t res_len;
   1088 
   1089 		tmp = wpa_config_parse_string(value, &res_len);
   1090 		if (tmp == NULL) {
   1091 			wpa_printf(MSG_ERROR, "Line %d: failed to parse "
   1092 				   "password.", line);
   1093 			return -1;
   1094 		}
   1095 		wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
   1096 				      (u8 *) tmp, res_len);
   1097 
   1098 		os_free(ssid->eap.password);
   1099 		ssid->eap.password = (u8 *) tmp;
   1100 		ssid->eap.password_len = res_len;
   1101 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
   1102 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
   1103 
   1104 		return 0;
   1105 	}
   1106 
   1107 
   1108 	/* NtPasswordHash: hash:<32 hex digits> */
   1109 	if (os_strlen(value + 5) != 2 * 16) {
   1110 		wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
   1111 			   "(expected 32 hex digits)", line);
   1112 		return -1;
   1113 	}
   1114 
   1115 	hash = os_malloc(16);
   1116 	if (hash == NULL)
   1117 		return -1;
   1118 
   1119 	if (hexstr2bin(value + 5, hash, 16)) {
   1120 		os_free(hash);
   1121 		wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
   1122 		return -1;
   1123 	}
   1124 
   1125 	wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
   1126 
   1127 	os_free(ssid->eap.password);
   1128 	ssid->eap.password = hash;
   1129 	ssid->eap.password_len = 16;
   1130 	ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
   1131 	ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
   1132 
   1133 	return 0;
   1134 }
   1135 
   1136 
   1137 static char * wpa_config_write_password(const struct parse_data *data,
   1138 					struct wpa_ssid *ssid)
   1139 {
   1140 	char *buf;
   1141 
   1142 	if (ssid->eap.password == NULL)
   1143 		return NULL;
   1144 
   1145 #ifdef CONFIG_EXT_PASSWORD
   1146 	if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
   1147 		buf = os_zalloc(4 + ssid->eap.password_len + 1);
   1148 		if (buf == NULL)
   1149 			return NULL;
   1150 		os_memcpy(buf, "ext:", 4);
   1151 		os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
   1152 		return buf;
   1153 	}
   1154 #endif /* CONFIG_EXT_PASSWORD */
   1155 
   1156 	if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
   1157 		return wpa_config_write_string(
   1158 			ssid->eap.password, ssid->eap.password_len);
   1159 	}
   1160 
   1161 	buf = os_malloc(5 + 32 + 1);
   1162 	if (buf == NULL)
   1163 		return NULL;
   1164 
   1165 	os_memcpy(buf, "hash:", 5);
   1166 	wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
   1167 
   1168 	return buf;
   1169 }
   1170 #endif /* IEEE8021X_EAPOL */
   1171 
   1172 
   1173 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
   1174 				    const char *value, int idx)
   1175 {
   1176 	char *buf, title[20];
   1177 	int res;
   1178 
   1179 	buf = wpa_config_parse_string(value, len);
   1180 	if (buf == NULL) {
   1181 		wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
   1182 			   line, idx, value);
   1183 		return -1;
   1184 	}
   1185 	if (*len > MAX_WEP_KEY_LEN) {
   1186 		wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
   1187 			   line, idx, value);
   1188 		os_free(buf);
   1189 		return -1;
   1190 	}
   1191 	if (*len && *len != 5 && *len != 13 && *len != 16) {
   1192 		wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
   1193 			   "this network block will be ignored",
   1194 			   line, (unsigned int) *len);
   1195 	}
   1196 	os_memcpy(key, buf, *len);
   1197 	os_free(buf);
   1198 	res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
   1199 	if (res >= 0 && (size_t) res < sizeof(title))
   1200 		wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
   1201 	return 0;
   1202 }
   1203 
   1204 
   1205 static int wpa_config_parse_wep_key0(const struct parse_data *data,
   1206 				     struct wpa_ssid *ssid, int line,
   1207 				     const char *value)
   1208 {
   1209 	return wpa_config_parse_wep_key(ssid->wep_key[0],
   1210 					&ssid->wep_key_len[0], line,
   1211 					value, 0);
   1212 }
   1213 
   1214 
   1215 static int wpa_config_parse_wep_key1(const struct parse_data *data,
   1216 				     struct wpa_ssid *ssid, int line,
   1217 				     const char *value)
   1218 {
   1219 	return wpa_config_parse_wep_key(ssid->wep_key[1],
   1220 					&ssid->wep_key_len[1], line,
   1221 					value, 1);
   1222 }
   1223 
   1224 
   1225 static int wpa_config_parse_wep_key2(const struct parse_data *data,
   1226 				     struct wpa_ssid *ssid, int line,
   1227 				     const char *value)
   1228 {
   1229 	return wpa_config_parse_wep_key(ssid->wep_key[2],
   1230 					&ssid->wep_key_len[2], line,
   1231 					value, 2);
   1232 }
   1233 
   1234 
   1235 static int wpa_config_parse_wep_key3(const struct parse_data *data,
   1236 				     struct wpa_ssid *ssid, int line,
   1237 				     const char *value)
   1238 {
   1239 	return wpa_config_parse_wep_key(ssid->wep_key[3],
   1240 					&ssid->wep_key_len[3], line,
   1241 					value, 3);
   1242 }
   1243 
   1244 
   1245 #ifndef NO_CONFIG_WRITE
   1246 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
   1247 {
   1248 	if (ssid->wep_key_len[idx] == 0)
   1249 		return NULL;
   1250 	return wpa_config_write_string(ssid->wep_key[idx],
   1251 				       ssid->wep_key_len[idx]);
   1252 }
   1253 
   1254 
   1255 static char * wpa_config_write_wep_key0(const struct parse_data *data,
   1256 					struct wpa_ssid *ssid)
   1257 {
   1258 	return wpa_config_write_wep_key(ssid, 0);
   1259 }
   1260 
   1261 
   1262 static char * wpa_config_write_wep_key1(const struct parse_data *data,
   1263 					struct wpa_ssid *ssid)
   1264 {
   1265 	return wpa_config_write_wep_key(ssid, 1);
   1266 }
   1267 
   1268 
   1269 static char * wpa_config_write_wep_key2(const struct parse_data *data,
   1270 					struct wpa_ssid *ssid)
   1271 {
   1272 	return wpa_config_write_wep_key(ssid, 2);
   1273 }
   1274 
   1275 
   1276 static char * wpa_config_write_wep_key3(const struct parse_data *data,
   1277 					struct wpa_ssid *ssid)
   1278 {
   1279 	return wpa_config_write_wep_key(ssid, 3);
   1280 }
   1281 #endif /* NO_CONFIG_WRITE */
   1282 
   1283 
   1284 #ifdef CONFIG_P2P
   1285 
   1286 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
   1287 					    struct wpa_ssid *ssid, int line,
   1288 					    const char *value)
   1289 {
   1290 	const char *pos;
   1291 	u8 *buf, *n, addr[ETH_ALEN];
   1292 	size_t count;
   1293 
   1294 	buf = NULL;
   1295 	count = 0;
   1296 
   1297 	pos = value;
   1298 	while (pos && *pos) {
   1299 		while (*pos == ' ')
   1300 			pos++;
   1301 
   1302 		if (hwaddr_aton(pos, addr)) {
   1303 			if (count == 0) {
   1304 				wpa_printf(MSG_ERROR, "Line %d: Invalid "
   1305 					   "p2p_client_list address '%s'.",
   1306 					   line, value);
   1307 				os_free(buf);
   1308 				return -1;
   1309 			}
   1310 			/* continue anyway since this could have been from a
   1311 			 * truncated configuration file line */
   1312 			wpa_printf(MSG_INFO, "Line %d: Ignore likely "
   1313 				   "truncated p2p_client_list address '%s'",
   1314 				   line, pos);
   1315 		} else {
   1316 			n = os_realloc_array(buf, count + 1, ETH_ALEN);
   1317 			if (n == NULL) {
   1318 				os_free(buf);
   1319 				return -1;
   1320 			}
   1321 			buf = n;
   1322 			os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
   1323 			os_memcpy(buf, addr, ETH_ALEN);
   1324 			count++;
   1325 			wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
   1326 				    addr, ETH_ALEN);
   1327 		}
   1328 
   1329 		pos = os_strchr(pos, ' ');
   1330 	}
   1331 
   1332 	os_free(ssid->p2p_client_list);
   1333 	ssid->p2p_client_list = buf;
   1334 	ssid->num_p2p_clients = count;
   1335 
   1336 	return 0;
   1337 }
   1338 
   1339 
   1340 #ifndef NO_CONFIG_WRITE
   1341 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
   1342 					       struct wpa_ssid *ssid)
   1343 {
   1344 	char *value, *end, *pos;
   1345 	int res;
   1346 	size_t i;
   1347 
   1348 	if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
   1349 		return NULL;
   1350 
   1351 	value = os_malloc(20 * ssid->num_p2p_clients);
   1352 	if (value == NULL)
   1353 		return NULL;
   1354 	pos = value;
   1355 	end = value + 20 * ssid->num_p2p_clients;
   1356 
   1357 	for (i = ssid->num_p2p_clients; i > 0; i--) {
   1358 		res = os_snprintf(pos, end - pos, MACSTR " ",
   1359 				  MAC2STR(ssid->p2p_client_list +
   1360 					  (i - 1) * ETH_ALEN));
   1361 		if (res < 0 || res >= end - pos) {
   1362 			os_free(value);
   1363 			return NULL;
   1364 		}
   1365 		pos += res;
   1366 	}
   1367 
   1368 	if (pos > value)
   1369 		pos[-1] = '\0';
   1370 
   1371 	return value;
   1372 }
   1373 #endif /* NO_CONFIG_WRITE */
   1374 
   1375 
   1376 static int wpa_config_parse_psk_list(const struct parse_data *data,
   1377 				     struct wpa_ssid *ssid, int line,
   1378 				     const char *value)
   1379 {
   1380 	struct psk_list_entry *p;
   1381 	const char *pos;
   1382 
   1383 	p = os_zalloc(sizeof(*p));
   1384 	if (p == NULL)
   1385 		return -1;
   1386 
   1387 	pos = value;
   1388 	if (os_strncmp(pos, "P2P-", 4) == 0) {
   1389 		p->p2p = 1;
   1390 		pos += 4;
   1391 	}
   1392 
   1393 	if (hwaddr_aton(pos, p->addr)) {
   1394 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
   1395 			   line, pos);
   1396 		os_free(p);
   1397 		return -1;
   1398 	}
   1399 	pos += 17;
   1400 	if (*pos != '-') {
   1401 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
   1402 			   line, pos);
   1403 		os_free(p);
   1404 		return -1;
   1405 	}
   1406 	pos++;
   1407 
   1408 	if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
   1409 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
   1410 			   line, pos);
   1411 		os_free(p);
   1412 		return -1;
   1413 	}
   1414 
   1415 	dl_list_add(&ssid->psk_list, &p->list);
   1416 
   1417 	return 0;
   1418 }
   1419 
   1420 
   1421 #ifndef NO_CONFIG_WRITE
   1422 static char * wpa_config_write_psk_list(const struct parse_data *data,
   1423 					struct wpa_ssid *ssid)
   1424 {
   1425 	return NULL;
   1426 }
   1427 #endif /* NO_CONFIG_WRITE */
   1428 
   1429 #endif /* CONFIG_P2P */
   1430 
   1431 /* Helper macros for network block parser */
   1432 
   1433 #ifdef OFFSET
   1434 #undef OFFSET
   1435 #endif /* OFFSET */
   1436 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
   1437 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
   1438 
   1439 /* STR: Define a string variable for an ASCII string; f = field name */
   1440 #ifdef NO_CONFIG_WRITE
   1441 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
   1442 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
   1443 #else /* NO_CONFIG_WRITE */
   1444 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
   1445 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
   1446 #endif /* NO_CONFIG_WRITE */
   1447 #define STR(f) _STR(f), NULL, NULL, NULL, 0
   1448 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
   1449 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
   1450 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
   1451 
   1452 /* STR_LEN: Define a string variable with a separate variable for storing the
   1453  * data length. Unlike STR(), this can be used to store arbitrary binary data
   1454  * (i.e., even nul termination character). */
   1455 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
   1456 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
   1457 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
   1458 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
   1459 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
   1460 
   1461 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
   1462  * explicitly specified. */
   1463 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
   1464 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
   1465 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
   1466 
   1467 #ifdef NO_CONFIG_WRITE
   1468 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
   1469 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
   1470 #else /* NO_CONFIG_WRITE */
   1471 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
   1472 	OFFSET(f), (void *) 0
   1473 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
   1474 	OFFSET(eap.f), (void *) 0
   1475 #endif /* NO_CONFIG_WRITE */
   1476 
   1477 /* INT: Define an integer variable */
   1478 #define INT(f) _INT(f), NULL, NULL, 0
   1479 #define INTe(f) _INTe(f), NULL, NULL, 0
   1480 
   1481 /* INT_RANGE: Define an integer variable with allowed value range */
   1482 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
   1483 
   1484 /* FUNC: Define a configuration variable that uses a custom function for
   1485  * parsing and writing the value. */
   1486 #ifdef NO_CONFIG_WRITE
   1487 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
   1488 #else /* NO_CONFIG_WRITE */
   1489 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
   1490 	NULL, NULL, NULL, NULL
   1491 #endif /* NO_CONFIG_WRITE */
   1492 #define FUNC(f) _FUNC(f), 0
   1493 #define FUNC_KEY(f) _FUNC(f), 1
   1494 
   1495 /*
   1496  * Table of network configuration variables. This table is used to parse each
   1497  * network configuration variable, e.g., each line in wpa_supplicant.conf file
   1498  * that is inside a network block.
   1499  *
   1500  * This table is generated using the helper macros defined above and with
   1501  * generous help from the C pre-processor. The field name is stored as a string
   1502  * into .name and for STR and INT types, the offset of the target buffer within
   1503  * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
   1504  * offset to the field containing the length of the configuration variable.
   1505  * .param3 and .param4 can be used to mark the allowed range (length for STR
   1506  * and value for INT).
   1507  *
   1508  * For each configuration line in wpa_supplicant.conf, the parser goes through
   1509  * this table and select the entry that matches with the field name. The parser
   1510  * function (.parser) is then called to parse the actual value of the field.
   1511  *
   1512  * This kind of mechanism makes it easy to add new configuration parameters,
   1513  * since only one line needs to be added into this table and into the
   1514  * struct wpa_ssid definition if the new variable is either a string or
   1515  * integer. More complex types will need to use their own parser and writer
   1516  * functions.
   1517  */
   1518 static const struct parse_data ssid_fields[] = {
   1519 	{ STR_RANGE(ssid, 0, MAX_SSID_LEN) },
   1520 	{ INT_RANGE(scan_ssid, 0, 1) },
   1521 	{ FUNC(bssid) },
   1522 	{ FUNC_KEY(psk) },
   1523 	{ FUNC(proto) },
   1524 	{ FUNC(key_mgmt) },
   1525 	{ INT(bg_scan_period) },
   1526 	{ FUNC(pairwise) },
   1527 	{ FUNC(group) },
   1528 	{ FUNC(auth_alg) },
   1529 	{ FUNC(scan_freq) },
   1530 	{ FUNC(freq_list) },
   1531 #ifdef IEEE8021X_EAPOL
   1532 	{ FUNC(eap) },
   1533 	{ STR_LENe(identity) },
   1534 	{ STR_LENe(anonymous_identity) },
   1535 	{ FUNC_KEY(password) },
   1536 	{ STRe(ca_cert) },
   1537 	{ STRe(ca_path) },
   1538 	{ STRe(client_cert) },
   1539 	{ STRe(private_key) },
   1540 	{ STR_KEYe(private_key_passwd) },
   1541 	{ STRe(dh_file) },
   1542 	{ STRe(subject_match) },
   1543 	{ STRe(altsubject_match) },
   1544 	{ STRe(ca_cert2) },
   1545 	{ STRe(ca_path2) },
   1546 	{ STRe(client_cert2) },
   1547 	{ STRe(private_key2) },
   1548 	{ STR_KEYe(private_key2_passwd) },
   1549 	{ STRe(dh_file2) },
   1550 	{ STRe(subject_match2) },
   1551 	{ STRe(altsubject_match2) },
   1552 	{ STRe(phase1) },
   1553 	{ STRe(phase2) },
   1554 	{ STRe(pcsc) },
   1555 	{ STR_KEYe(pin) },
   1556 	{ STRe(engine_id) },
   1557 	{ STRe(key_id) },
   1558 	{ STRe(cert_id) },
   1559 	{ STRe(ca_cert_id) },
   1560 	{ STR_KEYe(pin2) },
   1561 	{ STRe(engine2_id) },
   1562 	{ STRe(key2_id) },
   1563 	{ STRe(cert2_id) },
   1564 	{ STRe(ca_cert2_id) },
   1565 	{ INTe(engine) },
   1566 	{ INTe(engine2) },
   1567 	{ INT(eapol_flags) },
   1568 #endif /* IEEE8021X_EAPOL */
   1569 	{ FUNC_KEY(wep_key0) },
   1570 	{ FUNC_KEY(wep_key1) },
   1571 	{ FUNC_KEY(wep_key2) },
   1572 	{ FUNC_KEY(wep_key3) },
   1573 	{ INT(wep_tx_keyidx) },
   1574 	{ INT(priority) },
   1575 #ifdef IEEE8021X_EAPOL
   1576 	{ INT(eap_workaround) },
   1577 	{ STRe(pac_file) },
   1578 	{ INTe(fragment_size) },
   1579 	{ INTe(ocsp) },
   1580 #endif /* IEEE8021X_EAPOL */
   1581 	{ INT_RANGE(mode, 0, 4) },
   1582 	{ INT_RANGE(proactive_key_caching, 0, 1) },
   1583 	{ INT_RANGE(disabled, 0, 2) },
   1584 	{ STR(id_str) },
   1585 #ifdef CONFIG_IEEE80211W
   1586 	{ INT_RANGE(ieee80211w, 0, 2) },
   1587 #endif /* CONFIG_IEEE80211W */
   1588 	{ INT_RANGE(peerkey, 0, 1) },
   1589 	{ INT_RANGE(mixed_cell, 0, 1) },
   1590 	{ INT_RANGE(frequency, 0, 65000) },
   1591 	{ INT(wpa_ptk_rekey) },
   1592 	{ STR(bgscan) },
   1593 	{ INT_RANGE(ignore_broadcast_ssid, 0, 2) },
   1594 #ifdef CONFIG_P2P
   1595 	{ FUNC(p2p_client_list) },
   1596 	{ FUNC(psk_list) },
   1597 #endif /* CONFIG_P2P */
   1598 #ifdef CONFIG_HT_OVERRIDES
   1599 	{ INT_RANGE(disable_ht, 0, 1) },
   1600 	{ INT_RANGE(disable_ht40, -1, 1) },
   1601 	{ INT_RANGE(disable_sgi, 0, 1) },
   1602 	{ INT_RANGE(disable_max_amsdu, -1, 1) },
   1603 	{ INT_RANGE(ampdu_factor, -1, 3) },
   1604 	{ INT_RANGE(ampdu_density, -1, 7) },
   1605 	{ STR(ht_mcs) },
   1606 #endif /* CONFIG_HT_OVERRIDES */
   1607 #ifdef CONFIG_VHT_OVERRIDES
   1608 	{ INT_RANGE(disable_vht, 0, 1) },
   1609 	{ INT(vht_capa) },
   1610 	{ INT(vht_capa_mask) },
   1611 	{ INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
   1612 	{ INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
   1613 	{ INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
   1614 	{ INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
   1615 	{ INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
   1616 	{ INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
   1617 	{ INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
   1618 	{ INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
   1619 	{ INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
   1620 	{ INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
   1621 	{ INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
   1622 	{ INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
   1623 	{ INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
   1624 	{ INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
   1625 	{ INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
   1626 	{ INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
   1627 #endif /* CONFIG_VHT_OVERRIDES */
   1628 	{ INT(ap_max_inactivity) },
   1629 	{ INT(dtim_period) },
   1630 	{ INT(beacon_int) },
   1631 };
   1632 
   1633 #undef OFFSET
   1634 #undef _STR
   1635 #undef STR
   1636 #undef STR_KEY
   1637 #undef _STR_LEN
   1638 #undef STR_LEN
   1639 #undef STR_LEN_KEY
   1640 #undef _STR_RANGE
   1641 #undef STR_RANGE
   1642 #undef STR_RANGE_KEY
   1643 #undef _INT
   1644 #undef INT
   1645 #undef INT_RANGE
   1646 #undef _FUNC
   1647 #undef FUNC
   1648 #undef FUNC_KEY
   1649 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
   1650 
   1651 
   1652 /**
   1653  * wpa_config_add_prio_network - Add a network to priority lists
   1654  * @config: Configuration data from wpa_config_read()
   1655  * @ssid: Pointer to the network configuration to be added to the list
   1656  * Returns: 0 on success, -1 on failure
   1657  *
   1658  * This function is used to add a network block to the priority list of
   1659  * networks. This must be called for each network when reading in the full
   1660  * configuration. In addition, this can be used indirectly when updating
   1661  * priorities by calling wpa_config_update_prio_list().
   1662  */
   1663 int wpa_config_add_prio_network(struct wpa_config *config,
   1664 				struct wpa_ssid *ssid)
   1665 {
   1666 	int prio;
   1667 	struct wpa_ssid *prev, **nlist;
   1668 
   1669 	/*
   1670 	 * Add to an existing priority list if one is available for the
   1671 	 * configured priority level for this network.
   1672 	 */
   1673 	for (prio = 0; prio < config->num_prio; prio++) {
   1674 		prev = config->pssid[prio];
   1675 		if (prev->priority == ssid->priority) {
   1676 			while (prev->pnext)
   1677 				prev = prev->pnext;
   1678 			prev->pnext = ssid;
   1679 			return 0;
   1680 		}
   1681 	}
   1682 
   1683 	/* First network for this priority - add a new priority list */
   1684 	nlist = os_realloc_array(config->pssid, config->num_prio + 1,
   1685 				 sizeof(struct wpa_ssid *));
   1686 	if (nlist == NULL)
   1687 		return -1;
   1688 
   1689 	for (prio = 0; prio < config->num_prio; prio++) {
   1690 		if (nlist[prio]->priority < ssid->priority) {
   1691 			os_memmove(&nlist[prio + 1], &nlist[prio],
   1692 				   (config->num_prio - prio) *
   1693 				   sizeof(struct wpa_ssid *));
   1694 			break;
   1695 		}
   1696 	}
   1697 
   1698 	nlist[prio] = ssid;
   1699 	config->num_prio++;
   1700 	config->pssid = nlist;
   1701 
   1702 	return 0;
   1703 }
   1704 
   1705 
   1706 /**
   1707  * wpa_config_update_prio_list - Update network priority list
   1708  * @config: Configuration data from wpa_config_read()
   1709  * Returns: 0 on success, -1 on failure
   1710  *
   1711  * This function is called to update the priority list of networks in the
   1712  * configuration when a network is being added or removed. This is also called
   1713  * if a priority for a network is changed.
   1714  */
   1715 int wpa_config_update_prio_list(struct wpa_config *config)
   1716 {
   1717 	struct wpa_ssid *ssid;
   1718 	int ret = 0;
   1719 
   1720 	os_free(config->pssid);
   1721 	config->pssid = NULL;
   1722 	config->num_prio = 0;
   1723 
   1724 	ssid = config->ssid;
   1725 	while (ssid) {
   1726 		ssid->pnext = NULL;
   1727 		if (wpa_config_add_prio_network(config, ssid) < 0)
   1728 			ret = -1;
   1729 		ssid = ssid->next;
   1730 	}
   1731 
   1732 	return ret;
   1733 }
   1734 
   1735 
   1736 #ifdef IEEE8021X_EAPOL
   1737 static void eap_peer_config_free(struct eap_peer_config *eap)
   1738 {
   1739 	os_free(eap->eap_methods);
   1740 	os_free(eap->identity);
   1741 	os_free(eap->anonymous_identity);
   1742 	os_free(eap->password);
   1743 	os_free(eap->ca_cert);
   1744 	os_free(eap->ca_path);
   1745 	os_free(eap->client_cert);
   1746 	os_free(eap->private_key);
   1747 	os_free(eap->private_key_passwd);
   1748 	os_free(eap->dh_file);
   1749 	os_free(eap->subject_match);
   1750 	os_free(eap->altsubject_match);
   1751 	os_free(eap->ca_cert2);
   1752 	os_free(eap->ca_path2);
   1753 	os_free(eap->client_cert2);
   1754 	os_free(eap->private_key2);
   1755 	os_free(eap->private_key2_passwd);
   1756 	os_free(eap->dh_file2);
   1757 	os_free(eap->subject_match2);
   1758 	os_free(eap->altsubject_match2);
   1759 	os_free(eap->phase1);
   1760 	os_free(eap->phase2);
   1761 	os_free(eap->pcsc);
   1762 	os_free(eap->pin);
   1763 	os_free(eap->engine_id);
   1764 	os_free(eap->key_id);
   1765 	os_free(eap->cert_id);
   1766 	os_free(eap->ca_cert_id);
   1767 	os_free(eap->key2_id);
   1768 	os_free(eap->cert2_id);
   1769 	os_free(eap->ca_cert2_id);
   1770 	os_free(eap->pin2);
   1771 	os_free(eap->engine2_id);
   1772 	os_free(eap->otp);
   1773 	os_free(eap->pending_req_otp);
   1774 	os_free(eap->pac_file);
   1775 	os_free(eap->new_password);
   1776 }
   1777 #endif /* IEEE8021X_EAPOL */
   1778 
   1779 
   1780 /**
   1781  * wpa_config_free_ssid - Free network/ssid configuration data
   1782  * @ssid: Configuration data for the network
   1783  *
   1784  * This function frees all resources allocated for the network configuration
   1785  * data.
   1786  */
   1787 void wpa_config_free_ssid(struct wpa_ssid *ssid)
   1788 {
   1789 	struct psk_list_entry *psk;
   1790 
   1791 	os_free(ssid->ssid);
   1792 	os_free(ssid->passphrase);
   1793 	os_free(ssid->ext_psk);
   1794 #ifdef IEEE8021X_EAPOL
   1795 	eap_peer_config_free(&ssid->eap);
   1796 #endif /* IEEE8021X_EAPOL */
   1797 	os_free(ssid->id_str);
   1798 	os_free(ssid->scan_freq);
   1799 	os_free(ssid->freq_list);
   1800 	os_free(ssid->bgscan);
   1801 	os_free(ssid->p2p_client_list);
   1802 #ifdef CONFIG_HT_OVERRIDES
   1803 	os_free(ssid->ht_mcs);
   1804 #endif /* CONFIG_HT_OVERRIDES */
   1805 	while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
   1806 				    list))) {
   1807 		dl_list_del(&psk->list);
   1808 		os_free(psk);
   1809 	}
   1810 	os_free(ssid);
   1811 }
   1812 
   1813 
   1814 void wpa_config_free_cred(struct wpa_cred *cred)
   1815 {
   1816 	os_free(cred->realm);
   1817 	os_free(cred->username);
   1818 	os_free(cred->password);
   1819 	os_free(cred->ca_cert);
   1820 	os_free(cred->client_cert);
   1821 	os_free(cred->private_key);
   1822 	os_free(cred->private_key_passwd);
   1823 	os_free(cred->imsi);
   1824 	os_free(cred->milenage);
   1825 	os_free(cred->domain);
   1826 	os_free(cred->eap_method);
   1827 	os_free(cred->phase1);
   1828 	os_free(cred->phase2);
   1829 	os_free(cred->excluded_ssid);
   1830 	os_free(cred);
   1831 }
   1832 
   1833 
   1834 /**
   1835  * wpa_config_free - Free configuration data
   1836  * @config: Configuration data from wpa_config_read()
   1837  *
   1838  * This function frees all resources allocated for the configuration data by
   1839  * wpa_config_read().
   1840  */
   1841 void wpa_config_free(struct wpa_config *config)
   1842 {
   1843 #ifndef CONFIG_NO_CONFIG_BLOBS
   1844 	struct wpa_config_blob *blob, *prevblob;
   1845 #endif /* CONFIG_NO_CONFIG_BLOBS */
   1846 	struct wpa_ssid *ssid, *prev = NULL;
   1847 	struct wpa_cred *cred, *cprev;
   1848 
   1849 	ssid = config->ssid;
   1850 	while (ssid) {
   1851 		prev = ssid;
   1852 		ssid = ssid->next;
   1853 		wpa_config_free_ssid(prev);
   1854 	}
   1855 
   1856 	cred = config->cred;
   1857 	while (cred) {
   1858 		cprev = cred;
   1859 		cred = cred->next;
   1860 		wpa_config_free_cred(cprev);
   1861 	}
   1862 
   1863 #ifndef CONFIG_NO_CONFIG_BLOBS
   1864 	blob = config->blobs;
   1865 	prevblob = NULL;
   1866 	while (blob) {
   1867 		prevblob = blob;
   1868 		blob = blob->next;
   1869 		wpa_config_free_blob(prevblob);
   1870 	}
   1871 #endif /* CONFIG_NO_CONFIG_BLOBS */
   1872 
   1873 	wpabuf_free(config->wps_vendor_ext_m1);
   1874 	os_free(config->ctrl_interface);
   1875 	os_free(config->ctrl_interface_group);
   1876 	os_free(config->opensc_engine_path);
   1877 	os_free(config->pkcs11_engine_path);
   1878 	os_free(config->pkcs11_module_path);
   1879 	os_free(config->pcsc_reader);
   1880 	os_free(config->pcsc_pin);
   1881 	os_free(config->driver_param);
   1882 	os_free(config->device_name);
   1883 	os_free(config->manufacturer);
   1884 	os_free(config->model_name);
   1885 	os_free(config->model_number);
   1886 	os_free(config->serial_number);
   1887 	os_free(config->config_methods);
   1888 	os_free(config->p2p_ssid_postfix);
   1889 	os_free(config->pssid);
   1890 	os_free(config->p2p_pref_chan);
   1891 	os_free(config->autoscan);
   1892 	os_free(config->freq_list);
   1893 	wpabuf_free(config->wps_nfc_dh_pubkey);
   1894 	wpabuf_free(config->wps_nfc_dh_privkey);
   1895 	wpabuf_free(config->wps_nfc_dev_pw);
   1896 	os_free(config->ext_password_backend);
   1897 	os_free(config->sae_groups);
   1898 	wpabuf_free(config->ap_vendor_elements);
   1899 	os_free(config);
   1900 }
   1901 
   1902 
   1903 /**
   1904  * wpa_config_foreach_network - Iterate over each configured network
   1905  * @config: Configuration data from wpa_config_read()
   1906  * @func: Callback function to process each network
   1907  * @arg: Opaque argument to pass to callback function
   1908  *
   1909  * Iterate over the set of configured networks calling the specified
   1910  * function for each item. We guard against callbacks removing the
   1911  * supplied network.
   1912  */
   1913 void wpa_config_foreach_network(struct wpa_config *config,
   1914 				void (*func)(void *, struct wpa_ssid *),
   1915 				void *arg)
   1916 {
   1917 	struct wpa_ssid *ssid, *next;
   1918 
   1919 	ssid = config->ssid;
   1920 	while (ssid) {
   1921 		next = ssid->next;
   1922 		func(arg, ssid);
   1923 		ssid = next;
   1924 	}
   1925 }
   1926 
   1927 
   1928 /**
   1929  * wpa_config_get_network - Get configured network based on id
   1930  * @config: Configuration data from wpa_config_read()
   1931  * @id: Unique network id to search for
   1932  * Returns: Network configuration or %NULL if not found
   1933  */
   1934 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
   1935 {
   1936 	struct wpa_ssid *ssid;
   1937 
   1938 	ssid = config->ssid;
   1939 	while (ssid) {
   1940 		if (id == ssid->id)
   1941 			break;
   1942 		ssid = ssid->next;
   1943 	}
   1944 
   1945 	return ssid;
   1946 }
   1947 
   1948 
   1949 /**
   1950  * wpa_config_add_network - Add a new network with empty configuration
   1951  * @config: Configuration data from wpa_config_read()
   1952  * Returns: The new network configuration or %NULL if operation failed
   1953  */
   1954 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
   1955 {
   1956 	int id;
   1957 	struct wpa_ssid *ssid, *last = NULL;
   1958 
   1959 	id = -1;
   1960 	ssid = config->ssid;
   1961 	while (ssid) {
   1962 		if (ssid->id > id)
   1963 			id = ssid->id;
   1964 		last = ssid;
   1965 		ssid = ssid->next;
   1966 	}
   1967 	id++;
   1968 
   1969 	ssid = os_zalloc(sizeof(*ssid));
   1970 	if (ssid == NULL)
   1971 		return NULL;
   1972 	ssid->id = id;
   1973 	dl_list_init(&ssid->psk_list);
   1974 	if (last)
   1975 		last->next = ssid;
   1976 	else
   1977 		config->ssid = ssid;
   1978 
   1979 	wpa_config_update_prio_list(config);
   1980 
   1981 	return ssid;
   1982 }
   1983 
   1984 
   1985 /**
   1986  * wpa_config_remove_network - Remove a configured network based on id
   1987  * @config: Configuration data from wpa_config_read()
   1988  * @id: Unique network id to search for
   1989  * Returns: 0 on success, or -1 if the network was not found
   1990  */
   1991 int wpa_config_remove_network(struct wpa_config *config, int id)
   1992 {
   1993 	struct wpa_ssid *ssid, *prev = NULL;
   1994 
   1995 	ssid = config->ssid;
   1996 	while (ssid) {
   1997 		if (id == ssid->id)
   1998 			break;
   1999 		prev = ssid;
   2000 		ssid = ssid->next;
   2001 	}
   2002 
   2003 	if (ssid == NULL)
   2004 		return -1;
   2005 
   2006 	if (prev)
   2007 		prev->next = ssid->next;
   2008 	else
   2009 		config->ssid = ssid->next;
   2010 
   2011 	wpa_config_update_prio_list(config);
   2012 	wpa_config_free_ssid(ssid);
   2013 	return 0;
   2014 }
   2015 
   2016 
   2017 /**
   2018  * wpa_config_set_network_defaults - Set network default values
   2019  * @ssid: Pointer to network configuration data
   2020  */
   2021 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
   2022 {
   2023 	ssid->proto = DEFAULT_PROTO;
   2024 	ssid->pairwise_cipher = DEFAULT_PAIRWISE;
   2025 	ssid->group_cipher = DEFAULT_GROUP;
   2026 	ssid->key_mgmt = DEFAULT_KEY_MGMT;
   2027 	ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
   2028 #ifdef IEEE8021X_EAPOL
   2029 	ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
   2030 	ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
   2031 	ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
   2032 #endif /* IEEE8021X_EAPOL */
   2033 #ifdef CONFIG_HT_OVERRIDES
   2034 	ssid->disable_ht = DEFAULT_DISABLE_HT;
   2035 	ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
   2036 	ssid->disable_sgi = DEFAULT_DISABLE_SGI;
   2037 	ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
   2038 	ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
   2039 	ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
   2040 #endif /* CONFIG_HT_OVERRIDES */
   2041 #ifdef CONFIG_VHT_OVERRIDES
   2042 	ssid->vht_rx_mcs_nss_1 = -1;
   2043 	ssid->vht_rx_mcs_nss_2 = -1;
   2044 	ssid->vht_rx_mcs_nss_3 = -1;
   2045 	ssid->vht_rx_mcs_nss_4 = -1;
   2046 	ssid->vht_rx_mcs_nss_5 = -1;
   2047 	ssid->vht_rx_mcs_nss_6 = -1;
   2048 	ssid->vht_rx_mcs_nss_7 = -1;
   2049 	ssid->vht_rx_mcs_nss_8 = -1;
   2050 	ssid->vht_tx_mcs_nss_1 = -1;
   2051 	ssid->vht_tx_mcs_nss_2 = -1;
   2052 	ssid->vht_tx_mcs_nss_3 = -1;
   2053 	ssid->vht_tx_mcs_nss_4 = -1;
   2054 	ssid->vht_tx_mcs_nss_5 = -1;
   2055 	ssid->vht_tx_mcs_nss_6 = -1;
   2056 	ssid->vht_tx_mcs_nss_7 = -1;
   2057 	ssid->vht_tx_mcs_nss_8 = -1;
   2058 #endif /* CONFIG_VHT_OVERRIDES */
   2059 	ssid->proactive_key_caching = -1;
   2060 #ifdef CONFIG_IEEE80211W
   2061 	ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
   2062 #endif /* CONFIG_IEEE80211W */
   2063 }
   2064 
   2065 
   2066 /**
   2067  * wpa_config_set - Set a variable in network configuration
   2068  * @ssid: Pointer to network configuration data
   2069  * @var: Variable name, e.g., "ssid"
   2070  * @value: Variable value
   2071  * @line: Line number in configuration file or 0 if not used
   2072  * Returns: 0 on success, -1 on failure
   2073  *
   2074  * This function can be used to set network configuration variables based on
   2075  * both the configuration file and management interface input. The value
   2076  * parameter must be in the same format as the text-based configuration file is
   2077  * using. For example, strings are using double quotation marks.
   2078  */
   2079 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
   2080 		   int line)
   2081 {
   2082 	size_t i;
   2083 	int ret = 0;
   2084 
   2085 	if (ssid == NULL || var == NULL || value == NULL)
   2086 		return -1;
   2087 
   2088 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
   2089 		const struct parse_data *field = &ssid_fields[i];
   2090 		if (os_strcmp(var, field->name) != 0)
   2091 			continue;
   2092 
   2093 		if (field->parser(field, ssid, line, value)) {
   2094 			if (line) {
   2095 				wpa_printf(MSG_ERROR, "Line %d: failed to "
   2096 					   "parse %s '%s'.", line, var, value);
   2097 			}
   2098 			ret = -1;
   2099 		}
   2100 		break;
   2101 	}
   2102 	if (i == NUM_SSID_FIELDS) {
   2103 		if (line) {
   2104 			wpa_printf(MSG_ERROR, "Line %d: unknown network field "
   2105 				   "'%s'.", line, var);
   2106 		}
   2107 		ret = -1;
   2108 	}
   2109 
   2110 	return ret;
   2111 }
   2112 
   2113 
   2114 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
   2115 			  const char *value)
   2116 {
   2117 	size_t len;
   2118 	char *buf;
   2119 	int ret;
   2120 
   2121 	len = os_strlen(value);
   2122 	buf = os_malloc(len + 3);
   2123 	if (buf == NULL)
   2124 		return -1;
   2125 	buf[0] = '"';
   2126 	os_memcpy(buf + 1, value, len);
   2127 	buf[len + 1] = '"';
   2128 	buf[len + 2] = '\0';
   2129 	ret = wpa_config_set(ssid, var, buf, 0);
   2130 	os_free(buf);
   2131 	return ret;
   2132 }
   2133 
   2134 
   2135 /**
   2136  * wpa_config_get_all - Get all options from network configuration
   2137  * @ssid: Pointer to network configuration data
   2138  * @get_keys: Determines if keys/passwords will be included in returned list
   2139  *	(if they may be exported)
   2140  * Returns: %NULL terminated list of all set keys and their values in the form
   2141  * of [key1, val1, key2, val2, ... , NULL]
   2142  *
   2143  * This function can be used to get list of all configured network properties.
   2144  * The caller is responsible for freeing the returned list and all its
   2145  * elements.
   2146  */
   2147 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
   2148 {
   2149 	const struct parse_data *field;
   2150 	char *key, *value;
   2151 	size_t i;
   2152 	char **props;
   2153 	int fields_num;
   2154 
   2155 	get_keys = get_keys && ssid->export_keys;
   2156 
   2157 	props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
   2158 	if (!props)
   2159 		return NULL;
   2160 
   2161 	fields_num = 0;
   2162 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
   2163 		field = &ssid_fields[i];
   2164 		if (field->key_data && !get_keys)
   2165 			continue;
   2166 		value = field->writer(field, ssid);
   2167 		if (value == NULL)
   2168 			continue;
   2169 		if (os_strlen(value) == 0) {
   2170 			os_free(value);
   2171 			continue;
   2172 		}
   2173 
   2174 		key = os_strdup(field->name);
   2175 		if (key == NULL) {
   2176 			os_free(value);
   2177 			goto err;
   2178 		}
   2179 
   2180 		props[fields_num * 2] = key;
   2181 		props[fields_num * 2 + 1] = value;
   2182 
   2183 		fields_num++;
   2184 	}
   2185 
   2186 	return props;
   2187 
   2188 err:
   2189 	value = *props;
   2190 	while (value)
   2191 		os_free(value++);
   2192 	os_free(props);
   2193 	return NULL;
   2194 }
   2195 
   2196 
   2197 #ifndef NO_CONFIG_WRITE
   2198 /**
   2199  * wpa_config_get - Get a variable in network configuration
   2200  * @ssid: Pointer to network configuration data
   2201  * @var: Variable name, e.g., "ssid"
   2202  * Returns: Value of the variable or %NULL on failure
   2203  *
   2204  * This function can be used to get network configuration variables. The
   2205  * returned value is a copy of the configuration variable in text format, i.e,.
   2206  * the same format that the text-based configuration file and wpa_config_set()
   2207  * are using for the value. The caller is responsible for freeing the returned
   2208  * value.
   2209  */
   2210 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
   2211 {
   2212 	size_t i;
   2213 
   2214 	if (ssid == NULL || var == NULL)
   2215 		return NULL;
   2216 
   2217 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
   2218 		const struct parse_data *field = &ssid_fields[i];
   2219 		if (os_strcmp(var, field->name) == 0)
   2220 			return field->writer(field, ssid);
   2221 	}
   2222 
   2223 	return NULL;
   2224 }
   2225 
   2226 
   2227 /**
   2228  * wpa_config_get_no_key - Get a variable in network configuration (no keys)
   2229  * @ssid: Pointer to network configuration data
   2230  * @var: Variable name, e.g., "ssid"
   2231  * Returns: Value of the variable or %NULL on failure
   2232  *
   2233  * This function can be used to get network configuration variable like
   2234  * wpa_config_get(). The only difference is that this functions does not expose
   2235  * key/password material from the configuration. In case a key/password field
   2236  * is requested, the returned value is an empty string or %NULL if the variable
   2237  * is not set or "*" if the variable is set (regardless of its value). The
   2238  * returned value is a copy of the configuration variable in text format, i.e,.
   2239  * the same format that the text-based configuration file and wpa_config_set()
   2240  * are using for the value. The caller is responsible for freeing the returned
   2241  * value.
   2242  */
   2243 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
   2244 {
   2245 	size_t i;
   2246 
   2247 	if (ssid == NULL || var == NULL)
   2248 		return NULL;
   2249 
   2250 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
   2251 		const struct parse_data *field = &ssid_fields[i];
   2252 		if (os_strcmp(var, field->name) == 0) {
   2253 			char *res = field->writer(field, ssid);
   2254 			if (field->key_data) {
   2255 				if (res && res[0]) {
   2256 					wpa_printf(MSG_DEBUG, "Do not allow "
   2257 						   "key_data field to be "
   2258 						   "exposed");
   2259 					os_free(res);
   2260 					return os_strdup("*");
   2261 				}
   2262 
   2263 				os_free(res);
   2264 				return NULL;
   2265 			}
   2266 			return res;
   2267 		}
   2268 	}
   2269 
   2270 	return NULL;
   2271 }
   2272 #endif /* NO_CONFIG_WRITE */
   2273 
   2274 
   2275 /**
   2276  * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
   2277  * @ssid: Pointer to network configuration data
   2278  *
   2279  * This function must be called to update WPA PSK when either SSID or the
   2280  * passphrase has changed for the network configuration.
   2281  */
   2282 void wpa_config_update_psk(struct wpa_ssid *ssid)
   2283 {
   2284 #ifndef CONFIG_NO_PBKDF2
   2285 	pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
   2286 		    ssid->psk, PMK_LEN);
   2287 	wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
   2288 			ssid->psk, PMK_LEN);
   2289 	ssid->psk_set = 1;
   2290 #endif /* CONFIG_NO_PBKDF2 */
   2291 }
   2292 
   2293 
   2294 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
   2295 			const char *value, int line)
   2296 {
   2297 	char *val;
   2298 	size_t len;
   2299 
   2300 	if (os_strcmp(var, "priority") == 0) {
   2301 		cred->priority = atoi(value);
   2302 		return 0;
   2303 	}
   2304 
   2305 	if (os_strcmp(var, "pcsc") == 0) {
   2306 		cred->pcsc = atoi(value);
   2307 		return 0;
   2308 	}
   2309 
   2310 	if (os_strcmp(var, "eap") == 0) {
   2311 		struct eap_method_type method;
   2312 		method.method = eap_peer_get_type(value, &method.vendor);
   2313 		if (method.vendor == EAP_VENDOR_IETF &&
   2314 		    method.method == EAP_TYPE_NONE) {
   2315 			wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
   2316 				   "for a credential", line, value);
   2317 			return -1;
   2318 		}
   2319 		os_free(cred->eap_method);
   2320 		cred->eap_method = os_malloc(sizeof(*cred->eap_method));
   2321 		if (cred->eap_method == NULL)
   2322 			return -1;
   2323 		os_memcpy(cred->eap_method, &method, sizeof(method));
   2324 		return 0;
   2325 	}
   2326 
   2327 	if (os_strcmp(var, "password") == 0 &&
   2328 	    os_strncmp(value, "ext:", 4) == 0) {
   2329 		os_free(cred->password);
   2330 		cred->password = os_strdup(value);
   2331 		cred->ext_password = 1;
   2332 		return 0;
   2333 	}
   2334 
   2335 	val = wpa_config_parse_string(value, &len);
   2336 	if (val == NULL) {
   2337 		wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
   2338 			   "value '%s'.", line, var, value);
   2339 		return -1;
   2340 	}
   2341 
   2342 	if (os_strcmp(var, "realm") == 0) {
   2343 		os_free(cred->realm);
   2344 		cred->realm = val;
   2345 		return 0;
   2346 	}
   2347 
   2348 	if (os_strcmp(var, "username") == 0) {
   2349 		os_free(cred->username);
   2350 		cred->username = val;
   2351 		return 0;
   2352 	}
   2353 
   2354 	if (os_strcmp(var, "password") == 0) {
   2355 		os_free(cred->password);
   2356 		cred->password = val;
   2357 		cred->ext_password = 0;
   2358 		return 0;
   2359 	}
   2360 
   2361 	if (os_strcmp(var, "ca_cert") == 0) {
   2362 		os_free(cred->ca_cert);
   2363 		cred->ca_cert = val;
   2364 		return 0;
   2365 	}
   2366 
   2367 	if (os_strcmp(var, "client_cert") == 0) {
   2368 		os_free(cred->client_cert);
   2369 		cred->client_cert = val;
   2370 		return 0;
   2371 	}
   2372 
   2373 	if (os_strcmp(var, "private_key") == 0) {
   2374 		os_free(cred->private_key);
   2375 		cred->private_key = val;
   2376 		return 0;
   2377 	}
   2378 
   2379 	if (os_strcmp(var, "private_key_passwd") == 0) {
   2380 		os_free(cred->private_key_passwd);
   2381 		cred->private_key_passwd = val;
   2382 		return 0;
   2383 	}
   2384 
   2385 	if (os_strcmp(var, "imsi") == 0) {
   2386 		os_free(cred->imsi);
   2387 		cred->imsi = val;
   2388 		return 0;
   2389 	}
   2390 
   2391 	if (os_strcmp(var, "milenage") == 0) {
   2392 		os_free(cred->milenage);
   2393 		cred->milenage = val;
   2394 		return 0;
   2395 	}
   2396 
   2397 	if (os_strcmp(var, "domain") == 0) {
   2398 		os_free(cred->domain);
   2399 		cred->domain = val;
   2400 		return 0;
   2401 	}
   2402 
   2403 	if (os_strcmp(var, "phase1") == 0) {
   2404 		os_free(cred->phase1);
   2405 		cred->phase1 = val;
   2406 		return 0;
   2407 	}
   2408 
   2409 	if (os_strcmp(var, "phase2") == 0) {
   2410 		os_free(cred->phase2);
   2411 		cred->phase2 = val;
   2412 		return 0;
   2413 	}
   2414 
   2415 	if (os_strcmp(var, "roaming_consortium") == 0) {
   2416 		if (len < 3 || len > sizeof(cred->roaming_consortium)) {
   2417 			wpa_printf(MSG_ERROR, "Line %d: invalid "
   2418 				   "roaming_consortium length %d (3..15 "
   2419 				   "expected)", line, (int) len);
   2420 			os_free(val);
   2421 			return -1;
   2422 		}
   2423 		os_memcpy(cred->roaming_consortium, val, len);
   2424 		cred->roaming_consortium_len = len;
   2425 		os_free(val);
   2426 		return 0;
   2427 	}
   2428 
   2429 	if (os_strcmp(var, "excluded_ssid") == 0) {
   2430 		struct excluded_ssid *e;
   2431 
   2432 		if (len > MAX_SSID_LEN) {
   2433 			wpa_printf(MSG_ERROR, "Line %d: invalid "
   2434 				   "excluded_ssid length %d", line, (int) len);
   2435 			os_free(val);
   2436 			return -1;
   2437 		}
   2438 
   2439 		e = os_realloc_array(cred->excluded_ssid,
   2440 				     cred->num_excluded_ssid + 1,
   2441 				     sizeof(struct excluded_ssid));
   2442 		if (e == NULL) {
   2443 			os_free(val);
   2444 			return -1;
   2445 		}
   2446 		cred->excluded_ssid = e;
   2447 
   2448 		e = &cred->excluded_ssid[cred->num_excluded_ssid++];
   2449 		os_memcpy(e->ssid, val, len);
   2450 		e->ssid_len = len;
   2451 
   2452 		os_free(val);
   2453 
   2454 		return 0;
   2455 	}
   2456 
   2457 	if (line) {
   2458 		wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
   2459 			   line, var);
   2460 	}
   2461 
   2462 	os_free(val);
   2463 
   2464 	return -1;
   2465 }
   2466 
   2467 
   2468 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
   2469 {
   2470 	struct wpa_cred *cred;
   2471 
   2472 	cred = config->cred;
   2473 	while (cred) {
   2474 		if (id == cred->id)
   2475 			break;
   2476 		cred = cred->next;
   2477 	}
   2478 
   2479 	return cred;
   2480 }
   2481 
   2482 
   2483 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
   2484 {
   2485 	int id;
   2486 	struct wpa_cred *cred, *last = NULL;
   2487 
   2488 	id = -1;
   2489 	cred = config->cred;
   2490 	while (cred) {
   2491 		if (cred->id > id)
   2492 			id = cred->id;
   2493 		last = cred;
   2494 		cred = cred->next;
   2495 	}
   2496 	id++;
   2497 
   2498 	cred = os_zalloc(sizeof(*cred));
   2499 	if (cred == NULL)
   2500 		return NULL;
   2501 	cred->id = id;
   2502 	if (last)
   2503 		last->next = cred;
   2504 	else
   2505 		config->cred = cred;
   2506 
   2507 	return cred;
   2508 }
   2509 
   2510 
   2511 int wpa_config_remove_cred(struct wpa_config *config, int id)
   2512 {
   2513 	struct wpa_cred *cred, *prev = NULL;
   2514 
   2515 	cred = config->cred;
   2516 	while (cred) {
   2517 		if (id == cred->id)
   2518 			break;
   2519 		prev = cred;
   2520 		cred = cred->next;
   2521 	}
   2522 
   2523 	if (cred == NULL)
   2524 		return -1;
   2525 
   2526 	if (prev)
   2527 		prev->next = cred->next;
   2528 	else
   2529 		config->cred = cred->next;
   2530 
   2531 	wpa_config_free_cred(cred);
   2532 	return 0;
   2533 }
   2534 
   2535 
   2536 #ifndef CONFIG_NO_CONFIG_BLOBS
   2537 /**
   2538  * wpa_config_get_blob - Get a named configuration blob
   2539  * @config: Configuration data from wpa_config_read()
   2540  * @name: Name of the blob
   2541  * Returns: Pointer to blob data or %NULL if not found
   2542  */
   2543 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
   2544 						   const char *name)
   2545 {
   2546 	struct wpa_config_blob *blob = config->blobs;
   2547 
   2548 	while (blob) {
   2549 		if (os_strcmp(blob->name, name) == 0)
   2550 			return blob;
   2551 		blob = blob->next;
   2552 	}
   2553 	return NULL;
   2554 }
   2555 
   2556 
   2557 /**
   2558  * wpa_config_set_blob - Set or add a named configuration blob
   2559  * @config: Configuration data from wpa_config_read()
   2560  * @blob: New value for the blob
   2561  *
   2562  * Adds a new configuration blob or replaces the current value of an existing
   2563  * blob.
   2564  */
   2565 void wpa_config_set_blob(struct wpa_config *config,
   2566 			 struct wpa_config_blob *blob)
   2567 {
   2568 	wpa_config_remove_blob(config, blob->name);
   2569 	blob->next = config->blobs;
   2570 	config->blobs = blob;
   2571 }
   2572 
   2573 
   2574 /**
   2575  * wpa_config_free_blob - Free blob data
   2576  * @blob: Pointer to blob to be freed
   2577  */
   2578 void wpa_config_free_blob(struct wpa_config_blob *blob)
   2579 {
   2580 	if (blob) {
   2581 		os_free(blob->name);
   2582 		os_free(blob->data);
   2583 		os_free(blob);
   2584 	}
   2585 }
   2586 
   2587 
   2588 /**
   2589  * wpa_config_remove_blob - Remove a named configuration blob
   2590  * @config: Configuration data from wpa_config_read()
   2591  * @name: Name of the blob to remove
   2592  * Returns: 0 if blob was removed or -1 if blob was not found
   2593  */
   2594 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
   2595 {
   2596 	struct wpa_config_blob *pos = config->blobs, *prev = NULL;
   2597 
   2598 	while (pos) {
   2599 		if (os_strcmp(pos->name, name) == 0) {
   2600 			if (prev)
   2601 				prev->next = pos->next;
   2602 			else
   2603 				config->blobs = pos->next;
   2604 			wpa_config_free_blob(pos);
   2605 			return 0;
   2606 		}
   2607 		prev = pos;
   2608 		pos = pos->next;
   2609 	}
   2610 
   2611 	return -1;
   2612 }
   2613 #endif /* CONFIG_NO_CONFIG_BLOBS */
   2614 
   2615 
   2616 /**
   2617  * wpa_config_alloc_empty - Allocate an empty configuration
   2618  * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
   2619  * socket
   2620  * @driver_param: Driver parameters
   2621  * Returns: Pointer to allocated configuration data or %NULL on failure
   2622  */
   2623 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
   2624 					   const char *driver_param)
   2625 {
   2626 	struct wpa_config *config;
   2627 	const int aCWmin = 4, aCWmax = 10;
   2628 	const struct hostapd_wmm_ac_params ac_bk =
   2629 		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
   2630 	const struct hostapd_wmm_ac_params ac_be =
   2631 		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
   2632 	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
   2633 		{ aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
   2634 	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
   2635 		{ aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
   2636 
   2637 	config = os_zalloc(sizeof(*config));
   2638 	if (config == NULL)
   2639 		return NULL;
   2640 	config->eapol_version = DEFAULT_EAPOL_VERSION;
   2641 	config->ap_scan = DEFAULT_AP_SCAN;
   2642 	config->fast_reauth = DEFAULT_FAST_REAUTH;
   2643 	config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
   2644 	config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
   2645 	config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
   2646 	config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
   2647 	config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
   2648 	config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
   2649 	config->max_num_sta = DEFAULT_MAX_NUM_STA;
   2650 	config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
   2651 	config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
   2652 	config->wmm_ac_params[0] = ac_be;
   2653 	config->wmm_ac_params[1] = ac_bk;
   2654 	config->wmm_ac_params[2] = ac_vi;
   2655 	config->wmm_ac_params[3] = ac_vo;
   2656 
   2657 	if (ctrl_interface)
   2658 		config->ctrl_interface = os_strdup(ctrl_interface);
   2659 	if (driver_param)
   2660 		config->driver_param = os_strdup(driver_param);
   2661 
   2662 	return config;
   2663 }
   2664 
   2665 
   2666 #ifndef CONFIG_NO_STDOUT_DEBUG
   2667 /**
   2668  * wpa_config_debug_dump_networks - Debug dump of configured networks
   2669  * @config: Configuration data from wpa_config_read()
   2670  */
   2671 void wpa_config_debug_dump_networks(struct wpa_config *config)
   2672 {
   2673 	int prio;
   2674 	struct wpa_ssid *ssid;
   2675 
   2676 	for (prio = 0; prio < config->num_prio; prio++) {
   2677 		ssid = config->pssid[prio];
   2678 		wpa_printf(MSG_DEBUG, "Priority group %d",
   2679 			   ssid->priority);
   2680 		while (ssid) {
   2681 			wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
   2682 				   ssid->id,
   2683 				   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
   2684 			ssid = ssid->pnext;
   2685 		}
   2686 	}
   2687 }
   2688 #endif /* CONFIG_NO_STDOUT_DEBUG */
   2689 
   2690 
   2691 struct global_parse_data {
   2692 	char *name;
   2693 	int (*parser)(const struct global_parse_data *data,
   2694 		      struct wpa_config *config, int line, const char *value);
   2695 	void *param1, *param2, *param3;
   2696 	unsigned int changed_flag;
   2697 };
   2698 
   2699 
   2700 static int wpa_global_config_parse_int(const struct global_parse_data *data,
   2701 				       struct wpa_config *config, int line,
   2702 				       const char *pos)
   2703 {
   2704 	int val, *dst;
   2705 	char *end;
   2706 
   2707 	dst = (int *) (((u8 *) config) + (long) data->param1);
   2708 	val = strtol(pos, &end, 0);
   2709 	if (*end) {
   2710 		wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
   2711 			   line, pos);
   2712 		return -1;
   2713 	}
   2714 	*dst = val;
   2715 
   2716 	wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
   2717 
   2718 	if (data->param2 && *dst < (long) data->param2) {
   2719 		wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
   2720 			   "min_value=%ld)", line, data->name, *dst,
   2721 			   (long) data->param2);
   2722 		*dst = (long) data->param2;
   2723 		return -1;
   2724 	}
   2725 
   2726 	if (data->param3 && *dst > (long) data->param3) {
   2727 		wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
   2728 			   "max_value=%ld)", line, data->name, *dst,
   2729 			   (long) data->param3);
   2730 		*dst = (long) data->param3;
   2731 		return -1;
   2732 	}
   2733 
   2734 	return 0;
   2735 }
   2736 
   2737 
   2738 static int wpa_global_config_parse_str(const struct global_parse_data *data,
   2739 				       struct wpa_config *config, int line,
   2740 				       const char *pos)
   2741 {
   2742 	size_t len;
   2743 	char **dst, *tmp;
   2744 
   2745 	len = os_strlen(pos);
   2746 	if (data->param2 && len < (size_t) data->param2) {
   2747 		wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
   2748 			   "min_len=%ld)", line, data->name,
   2749 			   (unsigned long) len, (long) data->param2);
   2750 		return -1;
   2751 	}
   2752 
   2753 	if (data->param3 && len > (size_t) data->param3) {
   2754 		wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
   2755 			   "max_len=%ld)", line, data->name,
   2756 			   (unsigned long) len, (long) data->param3);
   2757 		return -1;
   2758 	}
   2759 
   2760 	tmp = os_strdup(pos);
   2761 	if (tmp == NULL)
   2762 		return -1;
   2763 
   2764 	dst = (char **) (((u8 *) config) + (long) data->param1);
   2765 	os_free(*dst);
   2766 	*dst = tmp;
   2767 	wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
   2768 
   2769 	return 0;
   2770 }
   2771 
   2772 
   2773 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
   2774 				       struct wpa_config *config, int line,
   2775 				       const char *pos)
   2776 {
   2777 	size_t len;
   2778 	struct wpabuf **dst, *tmp;
   2779 
   2780 	len = os_strlen(pos);
   2781 	if (len & 0x01)
   2782 		return -1;
   2783 
   2784 	tmp = wpabuf_alloc(len / 2);
   2785 	if (tmp == NULL)
   2786 		return -1;
   2787 
   2788 	if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
   2789 		wpabuf_free(tmp);
   2790 		return -1;
   2791 	}
   2792 
   2793 	dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
   2794 	wpabuf_free(*dst);
   2795 	*dst = tmp;
   2796 	wpa_printf(MSG_DEBUG, "%s", data->name);
   2797 
   2798 	return 0;
   2799 }
   2800 
   2801 
   2802 static int wpa_config_process_freq_list(const struct global_parse_data *data,
   2803 					struct wpa_config *config, int line,
   2804 					const char *value)
   2805 {
   2806 	int *freqs;
   2807 
   2808 	freqs = wpa_config_parse_int_array(value);
   2809 	if (freqs == NULL)
   2810 		return -1;
   2811 	os_free(config->freq_list);
   2812 	config->freq_list = freqs;
   2813 	return 0;
   2814 }
   2815 
   2816 
   2817 static int wpa_config_process_country(const struct global_parse_data *data,
   2818 				      struct wpa_config *config, int line,
   2819 				      const char *pos)
   2820 {
   2821 	if (!pos[0] || !pos[1]) {
   2822 		wpa_printf(MSG_DEBUG, "Invalid country set");
   2823 		return -1;
   2824 	}
   2825 	config->country[0] = pos[0];
   2826 	config->country[1] = pos[1];
   2827 	wpa_printf(MSG_DEBUG, "country='%c%c'",
   2828 		   config->country[0], config->country[1]);
   2829 	return 0;
   2830 }
   2831 
   2832 
   2833 static int wpa_config_process_load_dynamic_eap(
   2834 	const struct global_parse_data *data, struct wpa_config *config,
   2835 	int line, const char *so)
   2836 {
   2837 	int ret;
   2838 	wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
   2839 	ret = eap_peer_method_load(so);
   2840 	if (ret == -2) {
   2841 		wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
   2842 			   "reloading.");
   2843 	} else if (ret) {
   2844 		wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
   2845 			   "method '%s'.", line, so);
   2846 		return -1;
   2847 	}
   2848 
   2849 	return 0;
   2850 }
   2851 
   2852 
   2853 #ifdef CONFIG_WPS
   2854 
   2855 static int wpa_config_process_uuid(const struct global_parse_data *data,
   2856 				   struct wpa_config *config, int line,
   2857 				   const char *pos)
   2858 {
   2859 	char buf[40];
   2860 	if (uuid_str2bin(pos, config->uuid)) {
   2861 		wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
   2862 		return -1;
   2863 	}
   2864 	uuid_bin2str(config->uuid, buf, sizeof(buf));
   2865 	wpa_printf(MSG_DEBUG, "uuid=%s", buf);
   2866 	return 0;
   2867 }
   2868 
   2869 
   2870 static int wpa_config_process_device_type(
   2871 	const struct global_parse_data *data,
   2872 	struct wpa_config *config, int line, const char *pos)
   2873 {
   2874 	return wps_dev_type_str2bin(pos, config->device_type);
   2875 }
   2876 
   2877 
   2878 static int wpa_config_process_os_version(const struct global_parse_data *data,
   2879 					 struct wpa_config *config, int line,
   2880 					 const char *pos)
   2881 {
   2882 	if (hexstr2bin(pos, config->os_version, 4)) {
   2883 		wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
   2884 		return -1;
   2885 	}
   2886 	wpa_printf(MSG_DEBUG, "os_version=%08x",
   2887 		   WPA_GET_BE32(config->os_version));
   2888 	return 0;
   2889 }
   2890 
   2891 
   2892 static int wpa_config_process_wps_vendor_ext_m1(
   2893 	const struct global_parse_data *data,
   2894 	struct wpa_config *config, int line, const char *pos)
   2895 {
   2896 	struct wpabuf *tmp;
   2897 	int len = os_strlen(pos) / 2;
   2898 	u8 *p;
   2899 
   2900 	if (!len) {
   2901 		wpa_printf(MSG_ERROR, "Line %d: "
   2902 			   "invalid wps_vendor_ext_m1", line);
   2903 		return -1;
   2904 	}
   2905 
   2906 	tmp = wpabuf_alloc(len);
   2907 	if (tmp) {
   2908 		p = wpabuf_put(tmp, len);
   2909 
   2910 		if (hexstr2bin(pos, p, len)) {
   2911 			wpa_printf(MSG_ERROR, "Line %d: "
   2912 				   "invalid wps_vendor_ext_m1", line);
   2913 			wpabuf_free(tmp);
   2914 			return -1;
   2915 		}
   2916 
   2917 		wpabuf_free(config->wps_vendor_ext_m1);
   2918 		config->wps_vendor_ext_m1 = tmp;
   2919 	} else {
   2920 		wpa_printf(MSG_ERROR, "Can not allocate "
   2921 			   "memory for wps_vendor_ext_m1");
   2922 		return -1;
   2923 	}
   2924 
   2925 	return 0;
   2926 }
   2927 
   2928 #endif /* CONFIG_WPS */
   2929 
   2930 #ifdef CONFIG_P2P
   2931 static int wpa_config_process_sec_device_type(
   2932 	const struct global_parse_data *data,
   2933 	struct wpa_config *config, int line, const char *pos)
   2934 {
   2935 	int idx;
   2936 
   2937 	if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
   2938 		wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
   2939 			   "items", line);
   2940 		return -1;
   2941 	}
   2942 
   2943 	idx = config->num_sec_device_types;
   2944 
   2945 	if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
   2946 		return -1;
   2947 
   2948 	config->num_sec_device_types++;
   2949 	return 0;
   2950 }
   2951 
   2952 
   2953 static int wpa_config_process_p2p_pref_chan(
   2954 	const struct global_parse_data *data,
   2955 	struct wpa_config *config, int line, const char *pos)
   2956 {
   2957 	struct p2p_channel *pref = NULL, *n;
   2958 	unsigned int num = 0;
   2959 	const char *pos2;
   2960 	u8 op_class, chan;
   2961 
   2962 	/* format: class:chan,class:chan,... */
   2963 
   2964 	while (*pos) {
   2965 		op_class = atoi(pos);
   2966 		pos2 = os_strchr(pos, ':');
   2967 		if (pos2 == NULL)
   2968 			goto fail;
   2969 		pos2++;
   2970 		chan = atoi(pos2);
   2971 
   2972 		n = os_realloc_array(pref, num + 1,
   2973 				     sizeof(struct p2p_channel));
   2974 		if (n == NULL)
   2975 			goto fail;
   2976 		pref = n;
   2977 		pref[num].op_class = op_class;
   2978 		pref[num].chan = chan;
   2979 		num++;
   2980 
   2981 		pos = os_strchr(pos2, ',');
   2982 		if (pos == NULL)
   2983 			break;
   2984 		pos++;
   2985 	}
   2986 
   2987 	os_free(config->p2p_pref_chan);
   2988 	config->p2p_pref_chan = pref;
   2989 	config->num_p2p_pref_chan = num;
   2990 	wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
   2991 		    (u8 *) config->p2p_pref_chan,
   2992 		    config->num_p2p_pref_chan * sizeof(struct p2p_channel));
   2993 
   2994 	return 0;
   2995 
   2996 fail:
   2997 	os_free(pref);
   2998 	wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
   2999 	return -1;
   3000 }
   3001 #endif /* CONFIG_P2P */
   3002 
   3003 
   3004 static int wpa_config_process_hessid(
   3005 	const struct global_parse_data *data,
   3006 	struct wpa_config *config, int line, const char *pos)
   3007 {
   3008 	if (hwaddr_aton2(pos, config->hessid) < 0) {
   3009 		wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
   3010 			   line, pos);
   3011 		return -1;
   3012 	}
   3013 
   3014 	return 0;
   3015 }
   3016 
   3017 
   3018 static int wpa_config_process_sae_groups(
   3019 	const struct global_parse_data *data,
   3020 	struct wpa_config *config, int line, const char *pos)
   3021 {
   3022 	int *groups = wpa_config_parse_int_array(pos);
   3023 	if (groups == NULL) {
   3024 		wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
   3025 			   line, pos);
   3026 		return -1;
   3027 	}
   3028 
   3029 	os_free(config->sae_groups);
   3030 	config->sae_groups = groups;
   3031 
   3032 	return 0;
   3033 }
   3034 
   3035 
   3036 static int wpa_config_process_ap_vendor_elements(
   3037 	const struct global_parse_data *data,
   3038 	struct wpa_config *config, int line, const char *pos)
   3039 {
   3040 	struct wpabuf *tmp;
   3041 	int len = os_strlen(pos) / 2;
   3042 	u8 *p;
   3043 
   3044 	if (!len) {
   3045 		wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
   3046 			   line);
   3047 		return -1;
   3048 	}
   3049 
   3050 	tmp = wpabuf_alloc(len);
   3051 	if (tmp) {
   3052 		p = wpabuf_put(tmp, len);
   3053 
   3054 		if (hexstr2bin(pos, p, len)) {
   3055 			wpa_printf(MSG_ERROR, "Line %d: invalid "
   3056 				   "ap_vendor_elements", line);
   3057 			wpabuf_free(tmp);
   3058 			return -1;
   3059 		}
   3060 
   3061 		wpabuf_free(config->ap_vendor_elements);
   3062 		config->ap_vendor_elements = tmp;
   3063 	} else {
   3064 		wpa_printf(MSG_ERROR, "Cannot allocate memory for "
   3065 			   "ap_vendor_elements");
   3066 		return -1;
   3067 	}
   3068 
   3069 	return 0;
   3070 }
   3071 
   3072 
   3073 #ifdef OFFSET
   3074 #undef OFFSET
   3075 #endif /* OFFSET */
   3076 /* OFFSET: Get offset of a variable within the wpa_config structure */
   3077 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
   3078 
   3079 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
   3080 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
   3081 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
   3082 #define INT(f) _INT(f), NULL, NULL
   3083 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
   3084 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
   3085 #define STR(f) _STR(f), NULL, NULL
   3086 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
   3087 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
   3088 
   3089 static const struct global_parse_data global_fields[] = {
   3090 #ifdef CONFIG_CTRL_IFACE
   3091 	{ STR(ctrl_interface), 0 },
   3092 	{ STR(ctrl_interface_group), 0 } /* deprecated */,
   3093 #endif /* CONFIG_CTRL_IFACE */
   3094 	{ INT_RANGE(eapol_version, 1, 2), 0 },
   3095 	{ INT(ap_scan), 0 },
   3096 	{ INT(disable_scan_offload), 0 },
   3097 	{ INT(fast_reauth), 0 },
   3098 	{ STR(opensc_engine_path), 0 },
   3099 	{ STR(pkcs11_engine_path), 0 },
   3100 	{ STR(pkcs11_module_path), 0 },
   3101 	{ STR(pcsc_reader), 0 },
   3102 	{ STR(pcsc_pin), 0 },
   3103 	{ STR(driver_param), 0 },
   3104 	{ INT(dot11RSNAConfigPMKLifetime), 0 },
   3105 	{ INT(dot11RSNAConfigPMKReauthThreshold), 0 },
   3106 	{ INT(dot11RSNAConfigSATimeout), 0 },
   3107 #ifndef CONFIG_NO_CONFIG_WRITE
   3108 	{ INT(update_config), 0 },
   3109 #endif /* CONFIG_NO_CONFIG_WRITE */
   3110 	{ FUNC_NO_VAR(load_dynamic_eap), 0 },
   3111 #ifdef CONFIG_WPS
   3112 	{ FUNC(uuid), CFG_CHANGED_UUID },
   3113 	{ STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
   3114 	{ STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
   3115 	{ STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
   3116 	{ STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
   3117 	{ STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
   3118 	{ FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
   3119 	{ FUNC(os_version), CFG_CHANGED_OS_VERSION },
   3120 	{ STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
   3121 	{ INT_RANGE(wps_cred_processing, 0, 2), 0 },
   3122 	{ FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
   3123 #endif /* CONFIG_WPS */
   3124 #ifdef CONFIG_P2P
   3125 	{ FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
   3126 	{ INT(p2p_listen_reg_class), 0 },
   3127 	{ INT(p2p_listen_channel), 0 },
   3128 	{ INT(p2p_oper_reg_class), 0 },
   3129 	{ INT(p2p_oper_channel), 0 },
   3130 	{ INT_RANGE(p2p_go_intent, 0, 15), 0 },
   3131 	{ STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
   3132 	{ INT_RANGE(persistent_reconnect, 0, 1), 0 },
   3133 	{ INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
   3134 	{ INT(p2p_group_idle), 0 },
   3135 	{ FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
   3136 	{ INT(p2p_go_ht40), 0 },
   3137 	{ INT(p2p_disabled), 0 },
   3138 	{ INT(p2p_no_group_iface), 0 },
   3139 	{ INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
   3140 #endif /* CONFIG_P2P */
   3141 	{ FUNC(country), CFG_CHANGED_COUNTRY },
   3142 	{ INT(bss_max_count), 0 },
   3143 	{ INT(bss_expiration_age), 0 },
   3144 	{ INT(bss_expiration_scan_count), 0 },
   3145 	{ INT_RANGE(filter_ssids, 0, 1), 0 },
   3146 	{ INT_RANGE(filter_rssi, -100, 0), 0 },
   3147 	{ INT(max_num_sta), 0 },
   3148 	{ INT_RANGE(disassoc_low_ack, 0, 1), 0 },
   3149 #ifdef CONFIG_HS20
   3150 	{ INT_RANGE(hs20, 0, 1), 0 },
   3151 #endif /* CONFIG_HS20 */
   3152 	{ INT_RANGE(interworking, 0, 1), 0 },
   3153 	{ FUNC(hessid), 0 },
   3154 	{ INT_RANGE(access_network_type, 0, 15), 0 },
   3155 	{ INT_RANGE(pbc_in_m1, 0, 1), 0 },
   3156 	{ STR(autoscan), 0 },
   3157 	{ INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
   3158 	  CFG_CHANGED_NFC_PASSWORD_TOKEN },
   3159 	{ BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
   3160 	{ BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
   3161 	{ BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
   3162 	{ STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
   3163 	{ INT(p2p_go_max_inactivity), 0 },
   3164 	{ INT_RANGE(auto_interworking, 0, 1), 0 },
   3165 	{ INT(okc), 0 },
   3166 	{ INT(pmf), 0 },
   3167 	{ FUNC(sae_groups), 0 },
   3168 	{ INT(dtim_period), 0 },
   3169 	{ INT(beacon_int), 0 },
   3170 	{ FUNC(ap_vendor_elements), 0 },
   3171 	{ INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
   3172 	{ FUNC(freq_list), 0 },
   3173 	{ INT(scan_cur_freq), 0 },
   3174 	{ INT(sched_scan_interval), 0 },
   3175 };
   3176 
   3177 #undef FUNC
   3178 #undef _INT
   3179 #undef INT
   3180 #undef INT_RANGE
   3181 #undef _STR
   3182 #undef STR
   3183 #undef STR_RANGE
   3184 #undef BIN
   3185 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
   3186 
   3187 
   3188 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
   3189 {
   3190 	size_t i;
   3191 	int ret = 0;
   3192 
   3193 	for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
   3194 		const struct global_parse_data *field = &global_fields[i];
   3195 		size_t flen = os_strlen(field->name);
   3196 		if (os_strncmp(pos, field->name, flen) != 0 ||
   3197 		    pos[flen] != '=')
   3198 			continue;
   3199 
   3200 		if (field->parser(field, config, line, pos + flen + 1)) {
   3201 			wpa_printf(MSG_ERROR, "Line %d: failed to "
   3202 				   "parse '%s'.", line, pos);
   3203 			ret = -1;
   3204 		}
   3205 		if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
   3206 			config->wps_nfc_pw_from_config = 1;
   3207 		config->changed_parameters |= field->changed_flag;
   3208 		break;
   3209 	}
   3210 	if (i == NUM_GLOBAL_FIELDS) {
   3211 #ifdef CONFIG_AP
   3212 		if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
   3213 			char *tmp = os_strchr(pos, '=');
   3214 			if (tmp == NULL) {
   3215 				if (line < 0)
   3216 					return -1;
   3217 				wpa_printf(MSG_ERROR, "Line %d: invalid line "
   3218 					   "'%s'", line, pos);
   3219 				return -1;
   3220 			}
   3221 			*tmp++ = '\0';
   3222 			if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
   3223 						  tmp)) {
   3224 				wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
   3225 					   "AC item", line);
   3226 				return -1;
   3227 			}
   3228 		}
   3229 #endif /* CONFIG_AP */
   3230 		if (line < 0)
   3231 			return -1;
   3232 		wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
   3233 			   line, pos);
   3234 		ret = -1;
   3235 	}
   3236 
   3237 	return ret;
   3238 }
   3239