• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / Configuration parser and common functions
3  * Copyright (c) 2003-2019, Jouni Malinen <j@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 "utils/ip_addr.h"
14 #include "common/ieee802_1x_defs.h"
15 #include "common/sae.h"
16 #include "crypto/sha1.h"
17 #include "rsn_supp/wpa.h"
18 #include "eap_peer/eap.h"
19 #include "p2p/p2p.h"
20 #include "fst/fst.h"
21 #include "config.h"
22 
23 
24 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25 #define NO_CONFIG_WRITE
26 #endif
27 
28 /*
29  * Structure for network configuration parsing. This data is used to implement
30  * a generic parser for each network block variable. The table of configuration
31  * variables is defined below in this file (ssid_fields[]).
32  */
33 struct parse_data {
34 	/* Configuration variable name */
35 	char *name;
36 
37 	/* Parser function for this variable. The parser functions return 0 or 1
38 	 * to indicate success. Value 0 indicates that the parameter value may
39 	 * have changed while value 1 means that the value did not change.
40 	 * Error cases (failure to parse the string) are indicated by returning
41 	 * -1. */
42 	int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
43 		      int line, const char *value);
44 
45 #ifndef NO_CONFIG_WRITE
46 	/* Writer function (i.e., to get the variable in text format from
47 	 * internal presentation). */
48 	char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
49 #endif /* NO_CONFIG_WRITE */
50 
51 	/* Variable specific parameters for the parser. */
52 	void *param1, *param2, *param3, *param4;
53 
54 	/* 0 = this variable can be included in debug output and ctrl_iface
55 	 * 1 = this variable contains key/private data and it must not be
56 	 *     included in debug output unless explicitly requested. In
57 	 *     addition, this variable will not be readable through the
58 	 *     ctrl_iface.
59 	 */
60 	int key_data;
61 };
62 
63 
wpa_config_parse_str(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)64 static int wpa_config_parse_str(const struct parse_data *data,
65 				struct wpa_ssid *ssid,
66 				int line, const char *value)
67 {
68 	size_t res_len, *dst_len, prev_len;
69 	char **dst, *tmp;
70 
71 	if (os_strcmp(value, "NULL") == 0) {
72 		wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
73 			   data->name);
74 		tmp = NULL;
75 		res_len = 0;
76 		goto set;
77 	}
78 
79 	tmp = wpa_config_parse_string(value, &res_len);
80 	if (tmp == NULL) {
81 		wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
82 			   line, data->name,
83 			   data->key_data ? "[KEY DATA REMOVED]" : value);
84 		return -1;
85 	}
86 
87 	if (data->key_data) {
88 		wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
89 				      (u8 *) tmp, res_len);
90 	} else {
91 		wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
92 				  (u8 *) tmp, res_len);
93 	}
94 
95 	if (data->param3 && res_len < (size_t) data->param3) {
96 		wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
97 			   "min_len=%ld)", line, data->name,
98 			   (unsigned long) res_len, (long) data->param3);
99 		os_free(tmp);
100 		return -1;
101 	}
102 
103 	if (data->param4 && res_len > (size_t) data->param4) {
104 		wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
105 			   "max_len=%ld)", line, data->name,
106 			   (unsigned long) res_len, (long) data->param4);
107 		os_free(tmp);
108 		return -1;
109 	}
110 
111 set:
112 	dst = (char **) (((u8 *) ssid) + (long) data->param1);
113 	dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
114 
115 	if (data->param2)
116 		prev_len = *dst_len;
117 	else if (*dst)
118 		prev_len = os_strlen(*dst);
119 	else
120 		prev_len = 0;
121 	if ((*dst == NULL && tmp == NULL) ||
122 	    (*dst && tmp && prev_len == res_len &&
123 	     os_memcmp(*dst, tmp, res_len) == 0)) {
124 		/* No change to the previously configured value */
125 		os_free(tmp);
126 		return 1;
127 	}
128 
129 	os_free(*dst);
130 	*dst = tmp;
131 	if (data->param2)
132 		*dst_len = res_len;
133 
134 	return 0;
135 }
136 
137 
138 #ifndef NO_CONFIG_WRITE
wpa_config_write_string_ascii(const u8 * value,size_t len)139 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
140 {
141 	char *buf;
142 
143 	buf = os_malloc(len + 3);
144 	if (buf == NULL)
145 		return NULL;
146 	buf[0] = '"';
147 	os_memcpy(buf + 1, value, len);
148 	buf[len + 1] = '"';
149 	buf[len + 2] = '\0';
150 
151 	return buf;
152 }
153 
154 
wpa_config_write_string_hex(const u8 * value,size_t len)155 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
156 {
157 	char *buf;
158 
159 	buf = os_zalloc(2 * len + 1);
160 	if (buf == NULL)
161 		return NULL;
162 	wpa_snprintf_hex(buf, 2 * len + 1, value, len);
163 
164 	return buf;
165 }
166 
167 
wpa_config_write_string(const u8 * value,size_t len)168 static char * wpa_config_write_string(const u8 *value, size_t len)
169 {
170 	if (value == NULL)
171 		return NULL;
172 
173 	if (is_hex(value, len))
174 		return wpa_config_write_string_hex(value, len);
175 	else
176 		return wpa_config_write_string_ascii(value, len);
177 }
178 
179 
wpa_config_write_str(const struct parse_data * data,struct wpa_ssid * ssid)180 static char * wpa_config_write_str(const struct parse_data *data,
181 				   struct wpa_ssid *ssid)
182 {
183 	size_t len;
184 	char **src;
185 
186 	src = (char **) (((u8 *) ssid) + (long) data->param1);
187 	if (*src == NULL)
188 		return NULL;
189 
190 	if (data->param2)
191 		len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
192 	else
193 		len = os_strlen(*src);
194 
195 	return wpa_config_write_string((const u8 *) *src, len);
196 }
197 #endif /* NO_CONFIG_WRITE */
198 
199 
wpa_config_parse_int(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)200 static int wpa_config_parse_int(const struct parse_data *data,
201 				struct wpa_ssid *ssid,
202 				int line, const char *value)
203 {
204 	int val, *dst;
205 	char *end;
206 
207 	dst = (int *) (((u8 *) ssid) + (long) data->param1);
208 	val = strtol(value, &end, 0);
209 	if (*end) {
210 		wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
211 			   line, value);
212 		return -1;
213 	}
214 
215 	if (*dst == val)
216 		return 1;
217 	*dst = val;
218 	wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
219 
220 	if (data->param3 && *dst < (long) data->param3) {
221 		wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
222 			   "min_value=%ld)", line, data->name, *dst,
223 			   (long) data->param3);
224 		*dst = (long) data->param3;
225 		return -1;
226 	}
227 
228 	if (data->param4 && *dst > (long) data->param4) {
229 		wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
230 			   "max_value=%ld)", line, data->name, *dst,
231 			   (long) data->param4);
232 		*dst = (long) data->param4;
233 		return -1;
234 	}
235 
236 	return 0;
237 }
238 
239 
240 #ifndef NO_CONFIG_WRITE
wpa_config_write_int(const struct parse_data * data,struct wpa_ssid * ssid)241 static char * wpa_config_write_int(const struct parse_data *data,
242 				   struct wpa_ssid *ssid)
243 {
244 	int *src, res;
245 	char *value;
246 
247 	src = (int *) (((u8 *) ssid) + (long) data->param1);
248 
249 	value = os_malloc(20);
250 	if (value == NULL)
251 		return NULL;
252 	res = os_snprintf(value, 20, "%d", *src);
253 	if (os_snprintf_error(20, res)) {
254 		os_free(value);
255 		return NULL;
256 	}
257 	value[20 - 1] = '\0';
258 	return value;
259 }
260 #endif /* NO_CONFIG_WRITE */
261 
262 #ifdef CONFIG_P2P
wpa_config_parse_addr_list(const struct parse_data * data,int line,const char * value,u8 ** list,size_t * num,char * name,u8 abort_on_error,u8 masked)263 static int wpa_config_parse_addr_list(const struct parse_data *data,
264 				      int line, const char *value,
265 				      u8 **list, size_t *num, char *name,
266 				      u8 abort_on_error, u8 masked)
267 {
268 	const char *pos;
269 	u8 *buf, *n, addr[2 * ETH_ALEN];
270 	size_t count;
271 
272 	buf = NULL;
273 	count = 0;
274 
275 	pos = value;
276 	while (pos && *pos) {
277 		while (*pos == ' ')
278 			pos++;
279 
280 		if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
281 			if (abort_on_error || count == 0) {
282 				wpa_printf(MSG_ERROR,
283 					   "Line %d: Invalid %s address '%s'",
284 					   line, name, value);
285 				os_free(buf);
286 				return -1;
287 			}
288 			/* continue anyway since this could have been from a
289 			 * truncated configuration file line */
290 			wpa_printf(MSG_INFO,
291 				   "Line %d: Ignore likely truncated %s address '%s'",
292 				   line, name, pos);
293 		} else {
294 			n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
295 			if (n == NULL) {
296 				os_free(buf);
297 				return -1;
298 			}
299 			buf = n;
300 			os_memmove(buf + 2 * ETH_ALEN, buf,
301 				   count * 2 * ETH_ALEN);
302 			os_memcpy(buf, addr, 2 * ETH_ALEN);
303 			count++;
304 			wpa_printf(MSG_MSGDUMP,
305 				   "%s: addr=" MACSTR " mask=" MACSTR,
306 				   name, MAC2STR(addr),
307 				   MAC2STR(&addr[ETH_ALEN]));
308 		}
309 
310 		pos = os_strchr(pos, ' ');
311 	}
312 
313 	os_free(*list);
314 	*list = buf;
315 	*num = count;
316 
317 	return 0;
318 }
319 
320 
321 #ifndef NO_CONFIG_WRITE
wpa_config_write_addr_list(const struct parse_data * data,const u8 * list,size_t num,char * name)322 static char * wpa_config_write_addr_list(const struct parse_data *data,
323 					 const u8 *list, size_t num, char *name)
324 {
325 	char *value, *end, *pos;
326 	int res;
327 	size_t i;
328 
329 	if (list == NULL || num == 0)
330 		return NULL;
331 
332 	value = os_malloc(2 * 20 * num);
333 	if (value == NULL)
334 		return NULL;
335 	pos = value;
336 	end = value + 2 * 20 * num;
337 
338 	for (i = num; i > 0; i--) {
339 		const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
340 		const u8 *m = a + ETH_ALEN;
341 
342 		if (i < num)
343 			*pos++ = ' ';
344 		res = hwaddr_mask_txt(pos, end - pos, a, m);
345 		if (res < 0) {
346 			os_free(value);
347 			return NULL;
348 		}
349 		pos += res;
350 	}
351 
352 	return value;
353 }
354 #endif /* NO_CONFIG_WRITE */
355 #endif /* CONFIG_P2P */
356 
wpa_config_parse_bssid(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)357 static int wpa_config_parse_bssid(const struct parse_data *data,
358 				  struct wpa_ssid *ssid, int line,
359 				  const char *value)
360 {
361 	if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
362 	    os_strcmp(value, "any") == 0) {
363 		ssid->bssid_set = 0;
364 		wpa_printf(MSG_MSGDUMP, "BSSID any");
365 		return 0;
366 	}
367 	if (hwaddr_aton(value, ssid->bssid)) {
368 		wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
369 			   line, value);
370 		return -1;
371 	}
372 	ssid->bssid_set = 1;
373 	wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
374 	return 0;
375 }
376 
377 
378 #ifndef NO_CONFIG_WRITE
wpa_config_write_bssid(const struct parse_data * data,struct wpa_ssid * ssid)379 static char * wpa_config_write_bssid(const struct parse_data *data,
380 				     struct wpa_ssid *ssid)
381 {
382 	char *value;
383 	int res;
384 
385 	if (!ssid->bssid_set)
386 		return NULL;
387 
388 	value = os_malloc(20);
389 	if (value == NULL)
390 		return NULL;
391 	res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
392 	if (os_snprintf_error(20, res)) {
393 		os_free(value);
394 		return NULL;
395 	}
396 	value[20 - 1] = '\0';
397 	return value;
398 }
399 #endif /* NO_CONFIG_WRITE */
400 
401 #ifndef EXT_CODE_CROP
wpa_config_parse_bssid_hint(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)402 static int wpa_config_parse_bssid_hint(const struct parse_data *data,
403 				       struct wpa_ssid *ssid, int line,
404 				       const char *value)
405 {
406 	if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
407 	    os_strcmp(value, "any") == 0) {
408 		ssid->bssid_hint_set = 0;
409 		wpa_printf(MSG_MSGDUMP, "BSSID hint any");
410 		return 0;
411 	}
412 	if (hwaddr_aton(value, ssid->bssid_hint)) {
413 		wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
414 			   line, value);
415 		return -1;
416 	}
417 	ssid->bssid_hint_set = 1;
418 	wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
419 	return 0;
420 }
421 
422 
423 #ifndef NO_CONFIG_WRITE
wpa_config_write_bssid_hint(const struct parse_data * data,struct wpa_ssid * ssid)424 static char * wpa_config_write_bssid_hint(const struct parse_data *data,
425 					  struct wpa_ssid *ssid)
426 {
427 	char *value;
428 	int res;
429 
430 	if (!ssid->bssid_hint_set)
431 		return NULL;
432 
433 	value = os_malloc(20);
434 	if (!value)
435 		return NULL;
436 	res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
437 	if (os_snprintf_error(20, res)) {
438 		os_free(value);
439 		return NULL;
440 	}
441 	return value;
442 }
443 #endif /* NO_CONFIG_WRITE */
444 
445 
wpa_config_parse_bssid_ignore(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)446 static int wpa_config_parse_bssid_ignore(const struct parse_data *data,
447 					 struct wpa_ssid *ssid, int line,
448 					 const char *value)
449 {
450 	return wpa_config_parse_addr_list(data, line, value,
451 					  &ssid->bssid_ignore,
452 					  &ssid->num_bssid_ignore,
453 					  "bssid_ignore", 1, 1);
454 }
455 
456 
457 /* deprecated alias for bssid_ignore for backwards compatibility */
wpa_config_parse_bssid_blacklist(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)458 static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
459 					    struct wpa_ssid *ssid, int line,
460 					    const char *value)
461 {
462 	return wpa_config_parse_addr_list(data, line, value,
463 					  &ssid->bssid_ignore,
464 					  &ssid->num_bssid_ignore,
465 					  "bssid_ignore", 1, 1);
466 }
467 
468 
469 #ifndef NO_CONFIG_WRITE
470 
wpa_config_write_bssid_ignore(const struct parse_data * data,struct wpa_ssid * ssid)471 static char * wpa_config_write_bssid_ignore(const struct parse_data *data,
472 					    struct wpa_ssid *ssid)
473 {
474 	return wpa_config_write_addr_list(data, ssid->bssid_ignore,
475 					  ssid->num_bssid_ignore,
476 					  "bssid_ignore");
477 }
478 
479 
480 /* deprecated alias for bssid_ignore for backwards compatibility */
wpa_config_write_bssid_blacklist(const struct parse_data * data,struct wpa_ssid * ssid)481 static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
482 					       struct wpa_ssid *ssid)
483 {
484 	return wpa_config_write_addr_list(data, ssid->bssid_ignore,
485 					  ssid->num_bssid_ignore,
486 					  "bssid_ignore");
487 }
488 
489 #endif /* NO_CONFIG_WRITE */
490 
491 
wpa_config_parse_bssid_accept(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)492 static int wpa_config_parse_bssid_accept(const struct parse_data *data,
493 					 struct wpa_ssid *ssid, int line,
494 					 const char *value)
495 {
496 	return wpa_config_parse_addr_list(data, line, value,
497 					  &ssid->bssid_accept,
498 					  &ssid->num_bssid_accept,
499 					  "bssid_accept", 1, 1);
500 }
501 
502 
503 /* deprecated alias for bssid_accept for backwards compatibility */
wpa_config_parse_bssid_whitelist(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)504 static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
505 					    struct wpa_ssid *ssid, int line,
506 					    const char *value)
507 {
508 	return wpa_config_parse_addr_list(data, line, value,
509 					  &ssid->bssid_accept,
510 					  &ssid->num_bssid_accept,
511 					  "bssid_accept", 1, 1);
512 }
513 
514 
515 #ifndef NO_CONFIG_WRITE
516 
wpa_config_write_bssid_accept(const struct parse_data * data,struct wpa_ssid * ssid)517 static char * wpa_config_write_bssid_accept(const struct parse_data *data,
518 					    struct wpa_ssid *ssid)
519 {
520 	return wpa_config_write_addr_list(data, ssid->bssid_accept,
521 					  ssid->num_bssid_accept,
522 					  "bssid_accept");
523 }
524 
525 
526 /* deprecated alias for bssid_accept for backwards compatibility */
wpa_config_write_bssid_whitelist(const struct parse_data * data,struct wpa_ssid * ssid)527 static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
528 					       struct wpa_ssid *ssid)
529 {
530 	return wpa_config_write_addr_list(data, ssid->bssid_accept,
531 					  ssid->num_bssid_accept,
532 					  "bssid_accept");
533 }
534 
535 #endif /* NO_CONFIG_WRITE */
536 #endif /* EXT_CODE_CROP */
537 
538 #ifndef NO_CONFIG_WRITE
539 #endif /* NO_CONFIG_WRITE */
540 
541 
wpa_config_parse_psk(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)542 static int wpa_config_parse_psk(const struct parse_data *data,
543 				struct wpa_ssid *ssid, int line,
544 				const char *value)
545 {
546 #ifdef CONFIG_EXT_PASSWORD
547 	if (os_strncmp(value, "ext:", 4) == 0) {
548 		str_clear_free(ssid->passphrase);
549 		ssid->passphrase = NULL;
550 		ssid->psk_set = 0;
551 		os_free(ssid->ext_psk);
552 		ssid->ext_psk = os_strdup(value + 4);
553 		if (ssid->ext_psk == NULL)
554 			return -1;
555 		wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
556 			   ssid->ext_psk);
557 		return 0;
558 	}
559 #endif /* CONFIG_EXT_PASSWORD */
560 
561 	if (*value == '"') {
562 #ifndef CONFIG_NO_PBKDF2
563 		const char *pos;
564 		size_t len;
565 
566 		value++;
567 		pos = os_strrchr(value, '"');
568 		if (pos)
569 			len = pos - value;
570 		else
571 			len = os_strlen(value);
572 		if (len < 8 || len > 63) {
573 			wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
574 				   "length %lu (expected: 8..63) '%s'.",
575 				   line, (unsigned long) len, value);
576 			return -1;
577 		}
578 		wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
579 				      (u8 *) value, len);
580 		if (has_ctrl_char((u8 *) value, len)) {
581 			wpa_printf(MSG_ERROR,
582 				   "Line %d: Invalid passphrase character",
583 				   line);
584 			return -1;
585 		}
586 		if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
587 		    os_memcmp(ssid->passphrase, value, len) == 0) {
588 			/* No change to the previously configured value */
589 			return 1;
590 		}
591 		ssid->psk_set = 0;
592 		str_clear_free(ssid->passphrase);
593 		ssid->passphrase = dup_binstr(value, len);
594 		if (ssid->passphrase == NULL)
595 			return -1;
596 		return 0;
597 #else /* CONFIG_NO_PBKDF2 */
598 		wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
599 			   "supported.", line);
600 		return -1;
601 #endif /* CONFIG_NO_PBKDF2 */
602 	}
603 #ifdef CONFIG_WAPI
604 	if (ssid->key_mgmt == WPA_KEY_MGMT_WAPI_PSK) {
605 		ssid->hex_key_len = strlen(value) / 2; /* 2: half size of hex string to hexadecimal */
606 		if (hexstr2bin(value, ssid->psk, ssid->hex_key_len)) {
607 			ssid->hex_key_len = 0;
608 			return -1;
609 		}
610 	} else {
611 #endif /* CONFIG_WAPI */
612 	if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
613 	    value[PMK_LEN * 2] != '\0') {
614 		wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
615 			   line, value);
616 		return -1;
617 		}
618 #ifdef CONFIG_WAPI
619 	}
620 #endif /* CONFIG_WAPI */
621 	str_clear_free(ssid->passphrase);
622 	ssid->passphrase = NULL;
623 
624 	ssid->psk_set = 1;
625 	wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
626 	return 0;
627 }
628 
629 
630 #ifndef NO_CONFIG_WRITE
wpa_config_write_psk(const struct parse_data * data,struct wpa_ssid * ssid)631 static char * wpa_config_write_psk(const struct parse_data *data,
632 				   struct wpa_ssid *ssid)
633 {
634 #ifdef CONFIG_EXT_PASSWORD
635 	if (ssid->ext_psk) {
636 		size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
637 		char *buf = os_malloc(len);
638 		int res;
639 
640 		if (buf == NULL)
641 			return NULL;
642 		res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
643 		if (os_snprintf_error(len, res)) {
644 			os_free(buf);
645 			buf = NULL;
646 		}
647 		return buf;
648 	}
649 #endif /* CONFIG_EXT_PASSWORD */
650 
651 	if (ssid->passphrase)
652 		return wpa_config_write_string_ascii(
653 			(const u8 *) ssid->passphrase,
654 			os_strlen(ssid->passphrase));
655 
656 	if (ssid->psk_set)
657 		return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
658 
659 	return NULL;
660 }
661 #endif /* NO_CONFIG_WRITE */
662 
663 
664 #ifndef EXT_CODE_CROP
wpa_config_parse_proto(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)665 static int wpa_config_parse_proto(const struct parse_data *data,
666 				  struct wpa_ssid *ssid, int line,
667 				  const char *value)
668 {
669 	int val = 0, last, errors = 0;
670 	char *start, *end, *buf;
671 
672 	buf = os_strdup(value);
673 	if (buf == NULL)
674 		return -1;
675 	start = buf;
676 
677 	while (*start != '\0') {
678 		while (*start == ' ' || *start == '\t')
679 			start++;
680 		if (*start == '\0')
681 			break;
682 		end = start;
683 		while (*end != ' ' && *end != '\t' && *end != '\0')
684 			end++;
685 		last = *end == '\0';
686 		*end = '\0';
687 		if (os_strcmp(start, "WPA") == 0)
688 			val |= WPA_PROTO_WPA;
689 		else if (os_strcmp(start, "RSN") == 0 ||
690 			 os_strcmp(start, "WPA2") == 0)
691 			val |= WPA_PROTO_RSN;
692 		else if (os_strcmp(start, "OSEN") == 0)
693 			val |= WPA_PROTO_OSEN;
694 		else {
695 			wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
696 				   line, start);
697 			errors++;
698 		}
699 
700 		if (last)
701 			break;
702 		start = end + 1;
703 	}
704 	os_free(buf);
705 
706 	if (val == 0) {
707 		wpa_printf(MSG_ERROR,
708 			   "Line %d: no proto values configured.", line);
709 		errors++;
710 	}
711 
712 	if (!errors && ssid->proto == val)
713 		return 1;
714 	wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
715 	ssid->proto = val;
716 	return errors ? -1 : 0;
717 }
718 
719 
720 #ifndef NO_CONFIG_WRITE
wpa_config_write_proto(const struct parse_data * data,struct wpa_ssid * ssid)721 static char * wpa_config_write_proto(const struct parse_data *data,
722 				     struct wpa_ssid *ssid)
723 {
724 	int ret;
725 	char *buf, *pos, *end;
726 
727 	pos = buf = os_zalloc(20);
728 	if (buf == NULL)
729 		return NULL;
730 	end = buf + 20;
731 
732 	if (ssid->proto & WPA_PROTO_WPA) {
733 		ret = os_snprintf(pos, end - pos, "%sWPA",
734 				  pos == buf ? "" : " ");
735 		if (os_snprintf_error(end - pos, ret))
736 			return buf;
737 		pos += ret;
738 	}
739 
740 	if (ssid->proto & WPA_PROTO_RSN) {
741 		ret = os_snprintf(pos, end - pos, "%sRSN",
742 				  pos == buf ? "" : " ");
743 		if (os_snprintf_error(end - pos, ret))
744 			return buf;
745 		pos += ret;
746 	}
747 
748 	if (ssid->proto & WPA_PROTO_OSEN) {
749 		ret = os_snprintf(pos, end - pos, "%sOSEN",
750 				  pos == buf ? "" : " ");
751 		if (os_snprintf_error(end - pos, ret))
752 			return buf;
753 		pos += ret;
754 	}
755 
756 	if (pos == buf) {
757 		os_free(buf);
758 		buf = NULL;
759 	}
760 
761 	return buf;
762 }
763 #endif /* NO_CONFIG_WRITE */
764 #endif /* EXT_CODE_CROP */
765 
766 
wpa_config_parse_key_mgmt(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)767 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
768 				     struct wpa_ssid *ssid, int line,
769 				     const char *value)
770 {
771 	int val = 0, last, errors = 0;
772 	char *start, *end, *buf;
773 
774 	buf = os_strdup(value);
775 	if (buf == NULL)
776 		return -1;
777 	start = buf;
778 
779 	while (*start != '\0') {
780 		while (*start == ' ' || *start == '\t')
781 			start++;
782 		if (*start == '\0')
783 			break;
784 		end = start;
785 		while (*end != ' ' && *end != '\t' && *end != '\0')
786 			end++;
787 		last = *end == '\0';
788 		*end = '\0';
789 		if (os_strcmp(start, "WPA-PSK") == 0)
790 			val |= WPA_KEY_MGMT_PSK;
791 		else if (os_strcmp(start, "WPA-EAP") == 0)
792 			val |= WPA_KEY_MGMT_IEEE8021X;
793 		else if (os_strcmp(start, "IEEE8021X") == 0)
794 			val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
795 		else if (os_strcmp(start, "NONE") == 0)
796 			val |= WPA_KEY_MGMT_NONE;
797 		else if (os_strcmp(start, "WPA-NONE") == 0)
798 			val |= WPA_KEY_MGMT_WPA_NONE;
799 #ifdef CONFIG_WAPI
800 		else if (os_strcmp(start, "WAPI-PSK") == 0)
801 			val |= WPA_KEY_MGMT_WAPI_PSK;
802 #endif /* CONFIG_WAPI */
803 #ifdef CONFIG_IEEE80211R
804 		else if (os_strcmp(start, "FT-PSK") == 0)
805 			val |= WPA_KEY_MGMT_FT_PSK;
806 		else if (os_strcmp(start, "FT-EAP") == 0)
807 			val |= WPA_KEY_MGMT_FT_IEEE8021X;
808 #ifdef CONFIG_SHA384
809 		else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
810 			val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
811 #endif /* CONFIG_SHA384 */
812 #endif /* CONFIG_IEEE80211R */
813 		else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
814 			val |= WPA_KEY_MGMT_PSK_SHA256;
815 		else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
816 			val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
817 #ifdef CONFIG_WPS
818 		else if (os_strcmp(start, "WPS") == 0)
819 			val |= WPA_KEY_MGMT_WPS;
820 #endif /* CONFIG_WPS */
821 #ifdef CONFIG_SAE
822 		else if (os_strcmp(start, "SAE") == 0)
823 			val |= WPA_KEY_MGMT_SAE;
824 		else if (os_strcmp(start, "FT-SAE") == 0)
825 			val |= WPA_KEY_MGMT_FT_SAE;
826 #endif /* CONFIG_SAE */
827 #ifdef CONFIG_HS20
828 		else if (os_strcmp(start, "OSEN") == 0)
829 			val |= WPA_KEY_MGMT_OSEN;
830 #endif /* CONFIG_HS20 */
831 #ifdef CONFIG_SUITEB
832 		else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
833 			val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
834 #endif /* CONFIG_SUITEB */
835 #ifdef CONFIG_SUITEB192
836 		else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
837 			val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
838 #endif /* CONFIG_SUITEB192 */
839 #ifdef CONFIG_FILS
840 		else if (os_strcmp(start, "FILS-SHA256") == 0)
841 			val |= WPA_KEY_MGMT_FILS_SHA256;
842 		else if (os_strcmp(start, "FILS-SHA384") == 0)
843 			val |= WPA_KEY_MGMT_FILS_SHA384;
844 #ifdef CONFIG_IEEE80211R
845 		else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
846 			val |= WPA_KEY_MGMT_FT_FILS_SHA256;
847 		else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
848 			val |= WPA_KEY_MGMT_FT_FILS_SHA384;
849 #endif /* CONFIG_IEEE80211R */
850 #endif /* CONFIG_FILS */
851 #ifdef CONFIG_OWE
852 		else if (os_strcmp(start, "OWE") == 0)
853 			val |= WPA_KEY_MGMT_OWE;
854 #endif /* CONFIG_OWE */
855 #ifdef CONFIG_DPP
856 		else if (os_strcmp(start, "DPP") == 0)
857 			val |= WPA_KEY_MGMT_DPP;
858 #endif /* CONFIG_DPP */
859 		else {
860 			wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
861 				   line, start);
862 			errors++;
863 		}
864 
865 		if (last)
866 			break;
867 		start = end + 1;
868 	}
869 	os_free(buf);
870 
871 	if (val == 0) {
872 		wpa_printf(MSG_ERROR,
873 			   "Line %d: no key_mgmt values configured.", line);
874 		errors++;
875 	}
876 
877 	if (!errors && ssid->key_mgmt == val)
878 		return 1;
879 	wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
880 	ssid->key_mgmt = val;
881 	return errors ? -1 : 0;
882 }
883 
884 
885 #ifndef NO_CONFIG_WRITE
wpa_config_write_key_mgmt(const struct parse_data * data,struct wpa_ssid * ssid)886 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
887 					struct wpa_ssid *ssid)
888 {
889 	char *buf, *pos, *end;
890 	int ret;
891 
892 	pos = buf = os_zalloc(100);
893 	if (buf == NULL)
894 		return NULL;
895 	end = buf + 100;
896 
897 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
898 		ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
899 				  pos == buf ? "" : " ");
900 		if (os_snprintf_error(end - pos, ret)) {
901 			end[-1] = '\0';
902 			return buf;
903 		}
904 		pos += ret;
905 	}
906 #ifndef EXT_WPA_KEY_MGMT_CROP
907 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
908 		ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
909 				  pos == buf ? "" : " ");
910 		if (os_snprintf_error(end - pos, ret)) {
911 			end[-1] = '\0';
912 			return buf;
913 		}
914 		pos += ret;
915 	}
916 
917 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
918 		ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
919 				  pos == buf ? "" : " ");
920 		if (os_snprintf_error(end - pos, ret)) {
921 			end[-1] = '\0';
922 			return buf;
923 		}
924 		pos += ret;
925 	}
926 #endif /* EXT_WPA_KEY_MGMT_CROP */
927 	if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
928 		ret = os_snprintf(pos, end - pos, "%sNONE",
929 				  pos == buf ? "" : " ");
930 		if (os_snprintf_error(end - pos, ret)) {
931 			end[-1] = '\0';
932 			return buf;
933 		}
934 		pos += ret;
935 	}
936 
937 #ifndef EXT_WPA_KEY_MGMT_CROP
938 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
939 		ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
940 				  pos == buf ? "" : " ");
941 		if (os_snprintf_error(end - pos, ret)) {
942 			end[-1] = '\0';
943 			return buf;
944 		}
945 		pos += ret;
946 	}
947 #endif /* EXT_WPA_KEY_MGMT_CROP */
948 
949 #ifdef CONFIG_IEEE80211R
950 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
951 		ret = os_snprintf(pos, end - pos, "%sFT-PSK",
952 				  pos == buf ? "" : " ");
953 		if (os_snprintf_error(end - pos, ret)) {
954 			end[-1] = '\0';
955 			return buf;
956 		}
957 		pos += ret;
958 	}
959 
960 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
961 		ret = os_snprintf(pos, end - pos, "%sFT-EAP",
962 				  pos == buf ? "" : " ");
963 		if (os_snprintf_error(end - pos, ret)) {
964 			end[-1] = '\0';
965 			return buf;
966 		}
967 		pos += ret;
968 	}
969 #ifndef EXT_CODE_CROP
970 #ifdef CONFIG_SHA384
971 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
972 		ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
973 				  pos == buf ? "" : " ");
974 		if (os_snprintf_error(end - pos, ret)) {
975 			end[-1] = '\0';
976 			return buf;
977 		}
978 		pos += ret;
979 	}
980 #endif /* CONFIG_SHA384 */
981 #endif /* EXT_CODE_CROP */
982 #endif /* CONFIG_IEEE80211R */
983 
984 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
985 		ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
986 				  pos == buf ? "" : " ");
987 		if (os_snprintf_error(end - pos, ret)) {
988 			end[-1] = '\0';
989 			return buf;
990 		}
991 		pos += ret;
992 	}
993 #ifndef EXT_WPA_KEY_MGMT_CROP
994 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
995 		ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
996 				  pos == buf ? "" : " ");
997 		if (os_snprintf_error(end - pos, ret)) {
998 			end[-1] = '\0';
999 			return buf;
1000 		}
1001 		pos += ret;
1002 	}
1003 #endif /* EXT_WPA_KEY_MGMT_CROP */
1004 
1005 #ifdef CONFIG_WPS
1006 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1007 		ret = os_snprintf(pos, end - pos, "%sWPS",
1008 				  pos == buf ? "" : " ");
1009 		if (os_snprintf_error(end - pos, ret)) {
1010 			end[-1] = '\0';
1011 			return buf;
1012 		}
1013 		pos += ret;
1014 	}
1015 #endif /* CONFIG_WPS */
1016 
1017 #ifdef CONFIG_SAE
1018 	if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1019 		ret = os_snprintf(pos, end - pos, "%sSAE",
1020 				  pos == buf ? "" : " ");
1021 		if (os_snprintf_error(end - pos, ret)) {
1022 			end[-1] = '\0';
1023 			return buf;
1024 		}
1025 		pos += ret;
1026 	}
1027 #ifndef EXT_WPA_KEY_MGMT_CROP
1028 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1029 		ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1030 				  pos == buf ? "" : " ");
1031 		if (os_snprintf_error(end - pos, ret)) {
1032 			end[-1] = '\0';
1033 			return buf;
1034 		}
1035 		pos += ret;
1036 	}
1037 #endif /* EXT_WPA_KEY_MGMT_CROP */
1038 #endif /* CONFIG_SAE */
1039 #ifdef CONFIG_HS20
1040 	if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
1041 		ret = os_snprintf(pos, end - pos, "%sOSEN",
1042 				  pos == buf ? "" : " ");
1043 		if (os_snprintf_error(end - pos, ret)) {
1044 			end[-1] = '\0';
1045 			return buf;
1046 		}
1047 		pos += ret;
1048 	}
1049 #endif /* CONFIG_HS20 */
1050 
1051 #ifdef CONFIG_SUITEB
1052 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1053 		ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1054 				  pos == buf ? "" : " ");
1055 		if (os_snprintf_error(end - pos, ret)) {
1056 			end[-1] = '\0';
1057 			return buf;
1058 		}
1059 		pos += ret;
1060 	}
1061 #endif /* CONFIG_SUITEB */
1062 
1063 #ifdef CONFIG_SUITEB192
1064 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1065 		ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1066 				  pos == buf ? "" : " ");
1067 		if (os_snprintf_error(end - pos, ret)) {
1068 			end[-1] = '\0';
1069 			return buf;
1070 		}
1071 		pos += ret;
1072 	}
1073 #endif /* CONFIG_SUITEB192 */
1074 
1075 #ifdef CONFIG_FILS
1076 	if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1077 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1078 				  pos == buf ? "" : " ");
1079 		if (os_snprintf_error(end - pos, ret)) {
1080 			end[-1] = '\0';
1081 			return buf;
1082 		}
1083 		pos += ret;
1084 	}
1085 	if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1086 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1087 				  pos == buf ? "" : " ");
1088 		if (os_snprintf_error(end - pos, ret)) {
1089 			end[-1] = '\0';
1090 			return buf;
1091 		}
1092 		pos += ret;
1093 	}
1094 #ifdef CONFIG_IEEE80211R
1095 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1096 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1097 				  pos == buf ? "" : " ");
1098 		if (os_snprintf_error(end - pos, ret)) {
1099 			end[-1] = '\0';
1100 			return buf;
1101 		}
1102 		pos += ret;
1103 	}
1104 	if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1105 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1106 				  pos == buf ? "" : " ");
1107 		if (os_snprintf_error(end - pos, ret)) {
1108 			end[-1] = '\0';
1109 			return buf;
1110 		}
1111 		pos += ret;
1112 	}
1113 #endif /* CONFIG_IEEE80211R */
1114 #endif /* CONFIG_FILS */
1115 
1116 #ifdef CONFIG_DPP
1117 	if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1118 		ret = os_snprintf(pos, end - pos, "%sDPP",
1119 				  pos == buf ? "" : " ");
1120 		if (os_snprintf_error(end - pos, ret)) {
1121 			end[-1] = '\0';
1122 			return buf;
1123 		}
1124 		pos += ret;
1125 	}
1126 #endif /* CONFIG_DPP */
1127 
1128 #ifdef CONFIG_OWE
1129 	if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1130 		ret = os_snprintf(pos, end - pos, "%sOWE",
1131 				  pos == buf ? "" : " ");
1132 		if (os_snprintf_error(end - pos, ret)) {
1133 			end[-1] = '\0';
1134 			return buf;
1135 		}
1136 		pos += ret;
1137 	}
1138 #endif /* CONFIG_OWE */
1139 
1140 	if (pos == buf) {
1141 		os_free(buf);
1142 		buf = NULL;
1143 	}
1144 
1145 	return buf;
1146 }
1147 #endif /* NO_CONFIG_WRITE */
1148 
1149 
wpa_config_parse_cipher(int line,const char * value)1150 static int wpa_config_parse_cipher(int line, const char *value)
1151 {
1152 #ifdef CONFIG_NO_WPA
1153 	return -1;
1154 #else /* CONFIG_NO_WPA */
1155 	int val = wpa_parse_cipher(value);
1156 	if (val < 0) {
1157 		wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1158 			   line, value);
1159 		return -1;
1160 	}
1161 	if (val == 0) {
1162 		wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1163 			   line);
1164 		return -1;
1165 	}
1166 	return val;
1167 #endif /* CONFIG_NO_WPA */
1168 }
1169 
1170 
1171 #ifndef NO_CONFIG_WRITE
wpa_config_write_cipher(int cipher)1172 static char * wpa_config_write_cipher(int cipher)
1173 {
1174 #ifdef CONFIG_NO_WPA
1175 	return NULL;
1176 #else /* CONFIG_NO_WPA */
1177 	char *buf = os_zalloc(50);
1178 	if (buf == NULL)
1179 		return NULL;
1180 
1181 	if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1182 		os_free(buf);
1183 		return NULL;
1184 	}
1185 
1186 	return buf;
1187 #endif /* CONFIG_NO_WPA */
1188 }
1189 #endif /* NO_CONFIG_WRITE */
1190 
1191 
wpa_config_parse_pairwise(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1192 static int wpa_config_parse_pairwise(const struct parse_data *data,
1193 				     struct wpa_ssid *ssid, int line,
1194 				     const char *value)
1195 {
1196 	int val;
1197 	val = wpa_config_parse_cipher(line, value);
1198 	if (val == -1)
1199 		return -1;
1200 	if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
1201 		wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1202 			   "(0x%x).", line, val);
1203 		return -1;
1204 	}
1205 
1206 	if (ssid->pairwise_cipher == val)
1207 		return 1;
1208 	wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1209 	ssid->pairwise_cipher = val;
1210 	return 0;
1211 }
1212 
1213 
1214 #ifndef NO_CONFIG_WRITE
wpa_config_write_pairwise(const struct parse_data * data,struct wpa_ssid * ssid)1215 static char * wpa_config_write_pairwise(const struct parse_data *data,
1216 					struct wpa_ssid *ssid)
1217 {
1218 	return wpa_config_write_cipher(ssid->pairwise_cipher);
1219 }
1220 #endif /* NO_CONFIG_WRITE */
1221 
1222 
wpa_config_parse_group(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1223 static int wpa_config_parse_group(const struct parse_data *data,
1224 				  struct wpa_ssid *ssid, int line,
1225 				  const char *value)
1226 {
1227 	int val;
1228 	val = wpa_config_parse_cipher(line, value);
1229 	if (val == -1)
1230 		return -1;
1231 
1232 	/*
1233 	 * Backwards compatibility - filter out WEP ciphers that were previously
1234 	 * allowed.
1235 	 */
1236 	val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1237 
1238 	if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
1239 		wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1240 			   "(0x%x).", line, val);
1241 		return -1;
1242 	}
1243 
1244 	if (ssid->group_cipher == val)
1245 		return 1;
1246 	wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1247 	ssid->group_cipher = val;
1248 	return 0;
1249 }
1250 
1251 
1252 #ifndef NO_CONFIG_WRITE
wpa_config_write_group(const struct parse_data * data,struct wpa_ssid * ssid)1253 static char * wpa_config_write_group(const struct parse_data *data,
1254 				     struct wpa_ssid *ssid)
1255 {
1256 	return wpa_config_write_cipher(ssid->group_cipher);
1257 }
1258 #endif /* NO_CONFIG_WRITE */
1259 
1260 
wpa_config_parse_group_mgmt(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1261 static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1262 				       struct wpa_ssid *ssid, int line,
1263 				       const char *value)
1264 {
1265 	int val;
1266 
1267 	val = wpa_config_parse_cipher(line, value);
1268 	if (val == -1)
1269 		return -1;
1270 
1271 	if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1272 		wpa_printf(MSG_ERROR,
1273 			   "Line %d: not allowed group management cipher (0x%x).",
1274 			   line, val);
1275 		return -1;
1276 	}
1277 
1278 	if (ssid->group_mgmt_cipher == val)
1279 		return 1;
1280 	wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1281 	ssid->group_mgmt_cipher = val;
1282 	return 0;
1283 }
1284 
1285 
1286 #ifndef NO_CONFIG_WRITE
wpa_config_write_group_mgmt(const struct parse_data * data,struct wpa_ssid * ssid)1287 static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1288 					  struct wpa_ssid *ssid)
1289 {
1290 	return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1291 }
1292 #endif /* NO_CONFIG_WRITE */
1293 
1294 
wpa_config_parse_auth_alg(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1295 static int wpa_config_parse_auth_alg(const struct parse_data *data,
1296 				     struct wpa_ssid *ssid, int line,
1297 				     const char *value)
1298 {
1299 	int val = 0, last, errors = 0;
1300 	char *start, *end, *buf;
1301 
1302 	buf = os_strdup(value);
1303 	if (buf == NULL)
1304 		return -1;
1305 	start = buf;
1306 
1307 	while (*start != '\0') {
1308 		while (*start == ' ' || *start == '\t')
1309 			start++;
1310 		if (*start == '\0')
1311 			break;
1312 		end = start;
1313 		while (*end != ' ' && *end != '\t' && *end != '\0')
1314 			end++;
1315 		last = *end == '\0';
1316 		*end = '\0';
1317 		if (os_strcmp(start, "OPEN") == 0)
1318 			val |= WPA_AUTH_ALG_OPEN;
1319 		else if (os_strcmp(start, "SHARED") == 0)
1320 			val |= WPA_AUTH_ALG_SHARED;
1321 		else if (os_strcmp(start, "LEAP") == 0)
1322 			val |= WPA_AUTH_ALG_LEAP;
1323 		else {
1324 			wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1325 				   line, start);
1326 			errors++;
1327 		}
1328 
1329 		if (last)
1330 			break;
1331 		start = end + 1;
1332 	}
1333 	os_free(buf);
1334 
1335 	if (val == 0) {
1336 		wpa_printf(MSG_ERROR,
1337 			   "Line %d: no auth_alg values configured.", line);
1338 		errors++;
1339 	}
1340 
1341 	if (!errors && ssid->auth_alg == val)
1342 		return 1;
1343 	wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1344 	ssid->auth_alg = val;
1345 	return errors ? -1 : 0;
1346 }
1347 
1348 
1349 #ifndef NO_CONFIG_WRITE
wpa_config_write_auth_alg(const struct parse_data * data,struct wpa_ssid * ssid)1350 static char * wpa_config_write_auth_alg(const struct parse_data *data,
1351 					struct wpa_ssid *ssid)
1352 {
1353 	char *buf, *pos, *end;
1354 	int ret;
1355 
1356 	pos = buf = os_zalloc(30);
1357 	if (buf == NULL)
1358 		return NULL;
1359 	end = buf + 30;
1360 
1361 	if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1362 		ret = os_snprintf(pos, end - pos, "%sOPEN",
1363 				  pos == buf ? "" : " ");
1364 		if (os_snprintf_error(end - pos, ret)) {
1365 			end[-1] = '\0';
1366 			return buf;
1367 		}
1368 		pos += ret;
1369 	}
1370 
1371 	if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1372 		ret = os_snprintf(pos, end - pos, "%sSHARED",
1373 				  pos == buf ? "" : " ");
1374 		if (os_snprintf_error(end - pos, ret)) {
1375 			end[-1] = '\0';
1376 			return buf;
1377 		}
1378 		pos += ret;
1379 	}
1380 
1381 	if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1382 		ret = os_snprintf(pos, end - pos, "%sLEAP",
1383 				  pos == buf ? "" : " ");
1384 		if (os_snprintf_error(end - pos, ret)) {
1385 			end[-1] = '\0';
1386 			return buf;
1387 		}
1388 		pos += ret;
1389 	}
1390 
1391 	if (pos == buf) {
1392 		os_free(buf);
1393 		buf = NULL;
1394 	}
1395 
1396 	return buf;
1397 }
1398 #endif /* NO_CONFIG_WRITE */
1399 
1400 
1401 #ifndef EXT_CODE_CROP
wpa_config_parse_int_array(const char * value)1402 static int * wpa_config_parse_int_array(const char *value)
1403 {
1404 	int *freqs;
1405 	size_t used, len;
1406 	const char *pos;
1407 
1408 	used = 0;
1409 	len = 10;
1410 	freqs = os_calloc(len + 1, sizeof(int));
1411 	if (freqs == NULL)
1412 		return NULL;
1413 
1414 	pos = value;
1415 	while (pos) {
1416 		while (*pos == ' ')
1417 			pos++;
1418 		if (used == len) {
1419 			int *n;
1420 			size_t i;
1421 			n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
1422 			if (n == NULL) {
1423 				os_free(freqs);
1424 				return NULL;
1425 			}
1426 			for (i = len; i <= len * 2; i++)
1427 				n[i] = 0;
1428 			freqs = n;
1429 			len *= 2;
1430 		}
1431 
1432 		freqs[used] = atoi(pos);
1433 		if (freqs[used] == 0)
1434 			break;
1435 		used++;
1436 		pos = os_strchr(pos + 1, ' ');
1437 	}
1438 
1439 	return freqs;
1440 }
1441 
1442 
wpa_config_parse_scan_freq(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1443 static int wpa_config_parse_scan_freq(const struct parse_data *data,
1444 				      struct wpa_ssid *ssid, int line,
1445 				      const char *value)
1446 {
1447 	int *freqs;
1448 
1449 	freqs = wpa_config_parse_int_array(value);
1450 	if (freqs == NULL)
1451 		return -1;
1452 	if (freqs[0] == 0) {
1453 		os_free(freqs);
1454 		freqs = NULL;
1455 	}
1456 	os_free(ssid->scan_freq);
1457 	ssid->scan_freq = freqs;
1458 
1459 	return 0;
1460 }
1461 
1462 
wpa_config_parse_freq_list(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1463 static int wpa_config_parse_freq_list(const struct parse_data *data,
1464 				      struct wpa_ssid *ssid, int line,
1465 				      const char *value)
1466 {
1467 	int *freqs;
1468 
1469 	freqs = wpa_config_parse_int_array(value);
1470 	if (freqs == NULL)
1471 		return -1;
1472 	if (freqs[0] == 0) {
1473 		os_free(freqs);
1474 		freqs = NULL;
1475 	}
1476 	os_free(ssid->freq_list);
1477 	ssid->freq_list = freqs;
1478 
1479 	return 0;
1480 }
1481 
1482 
1483 #ifndef NO_CONFIG_WRITE
wpa_config_write_freqs(const struct parse_data * data,const int * freqs)1484 static char * wpa_config_write_freqs(const struct parse_data *data,
1485 				     const int *freqs)
1486 {
1487 	char *buf, *pos, *end;
1488 	int i, ret;
1489 	size_t count;
1490 
1491 	if (freqs == NULL)
1492 		return NULL;
1493 
1494 	count = 0;
1495 	for (i = 0; freqs[i]; i++)
1496 		count++;
1497 
1498 	pos = buf = os_zalloc(10 * count + 1);
1499 	if (buf == NULL)
1500 		return NULL;
1501 	end = buf + 10 * count + 1;
1502 
1503 	for (i = 0; freqs[i]; i++) {
1504 		ret = os_snprintf(pos, end - pos, "%s%u",
1505 				  i == 0 ? "" : " ", freqs[i]);
1506 		if (os_snprintf_error(end - pos, ret)) {
1507 			end[-1] = '\0';
1508 			return buf;
1509 		}
1510 		pos += ret;
1511 	}
1512 
1513 	return buf;
1514 }
1515 
1516 
wpa_config_write_scan_freq(const struct parse_data * data,struct wpa_ssid * ssid)1517 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1518 					 struct wpa_ssid *ssid)
1519 {
1520 	return wpa_config_write_freqs(data, ssid->scan_freq);
1521 }
1522 
1523 
wpa_config_write_freq_list(const struct parse_data * data,struct wpa_ssid * ssid)1524 static char * wpa_config_write_freq_list(const struct parse_data *data,
1525 					 struct wpa_ssid *ssid)
1526 {
1527 	return wpa_config_write_freqs(data, ssid->freq_list);
1528 }
1529 #endif /* NO_CONFIG_WRITE */
1530 #endif /* EXT_CODE_CROP */
1531 
1532 
1533 #ifdef IEEE8021X_EAPOL
1534 #if defined(CONFIG_WPS) || defined(LOS_CONFIG_WPA_ENTERPRISE)
wpa_config_parse_eap(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1535 static int wpa_config_parse_eap(const struct parse_data *data,
1536 				struct wpa_ssid *ssid, int line,
1537 				const char *value)
1538 {
1539 	int last, errors = 0;
1540 	char *start, *end, *buf;
1541 	struct eap_method_type *methods = NULL, *tmp;
1542 	size_t num_methods = 0;
1543 
1544 	buf = os_strdup(value);
1545 	if (buf == NULL)
1546 		return -1;
1547 	start = buf;
1548 
1549 	while (*start != '\0') {
1550 		while (*start == ' ' || *start == '\t')
1551 			start++;
1552 		if (*start == '\0')
1553 			break;
1554 		end = start;
1555 		while (*end != ' ' && *end != '\t' && *end != '\0')
1556 			end++;
1557 		last = *end == '\0';
1558 		*end = '\0';
1559 		tmp = methods;
1560 		methods = os_realloc_array(methods, num_methods + 1,
1561 					   sizeof(*methods));
1562 		if (methods == NULL) {
1563 			os_free(tmp);
1564 			os_free(buf);
1565 			return -1;
1566 		}
1567 		methods[num_methods].method = eap_peer_get_type(
1568 			start, &methods[num_methods].vendor);
1569 		if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1570 		    methods[num_methods].method == EAP_TYPE_NONE) {
1571 			wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1572 				   "'%s'", line, start);
1573 			wpa_printf(MSG_ERROR, "You may need to add support for"
1574 				   " this EAP method during wpa_supplicant\n"
1575 				   "build time configuration.\n"
1576 				   "See README for more information.");
1577 			errors++;
1578 		}
1579 #ifndef EXT_EAP_TRIM
1580 		else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1581 			   methods[num_methods].method == EAP_TYPE_LEAP)
1582 			ssid->leap++;
1583 		else
1584 			ssid->non_leap++;
1585 #endif /* EXT_EAP_TRIM */
1586 		num_methods++;
1587 		if (last)
1588 			break;
1589 		start = end + 1;
1590 	}
1591 	os_free(buf);
1592 
1593 	tmp = methods;
1594 	methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1595 	if (methods == NULL) {
1596 		os_free(tmp);
1597 		return -1;
1598 	}
1599 	methods[num_methods].vendor = EAP_VENDOR_IETF;
1600 	methods[num_methods].method = EAP_TYPE_NONE;
1601 	num_methods++;
1602 
1603 	if (!errors && ssid->eap.eap_methods) {
1604 		struct eap_method_type *prev_m;
1605 		size_t i, j, prev_methods, match = 0;
1606 
1607 		prev_m = ssid->eap.eap_methods;
1608 		for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1609 			     prev_m[i].method != EAP_TYPE_NONE; i++) {
1610 			/* Count the methods */
1611 		}
1612 		prev_methods = i + 1;
1613 
1614 		for (i = 0; prev_methods == num_methods && i < prev_methods;
1615 		     i++) {
1616 			for (j = 0; j < num_methods; j++) {
1617 				if (prev_m[i].vendor == methods[j].vendor &&
1618 				    prev_m[i].method == methods[j].method) {
1619 					match++;
1620 					break;
1621 				}
1622 			}
1623 		}
1624 		if (match == num_methods) {
1625 			os_free(methods);
1626 			return 1;
1627 		}
1628 	}
1629 	wpa_hexdump(MSG_MSGDUMP, "eap methods",
1630 		    (u8 *) methods, num_methods * sizeof(*methods));
1631 	os_free(ssid->eap.eap_methods);
1632 	ssid->eap.eap_methods = methods;
1633 	return errors ? -1 : 0;
1634 }
1635 #endif /* CONFIG_WPS */
1636 
1637 #ifndef NO_CONFIG_WRITE
wpa_config_write_eap(const struct parse_data * data,struct wpa_ssid * ssid)1638 static char * wpa_config_write_eap(const struct parse_data *data,
1639 				   struct wpa_ssid *ssid)
1640 {
1641 	int i, ret;
1642 	char *buf, *pos, *end;
1643 	const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1644 	const char *name;
1645 
1646 	if (eap_methods == NULL)
1647 		return NULL;
1648 
1649 	pos = buf = os_zalloc(100);
1650 	if (buf == NULL)
1651 		return NULL;
1652 	end = buf + 100;
1653 
1654 	for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1655 		     eap_methods[i].method != EAP_TYPE_NONE; i++) {
1656 		name = eap_get_name(eap_methods[i].vendor,
1657 				    eap_methods[i].method);
1658 		if (name) {
1659 			ret = os_snprintf(pos, end - pos, "%s%s",
1660 					  pos == buf ? "" : " ", name);
1661 			if (os_snprintf_error(end - pos, ret))
1662 				break;
1663 			pos += ret;
1664 		}
1665 	}
1666 
1667 	end[-1] = '\0';
1668 
1669 	return buf;
1670 }
1671 #endif /* NO_CONFIG_WRITE */
1672 
1673 
1674 #ifndef EXT_CODE_CROP
wpa_config_parse_password(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1675 static int wpa_config_parse_password(const struct parse_data *data,
1676 				     struct wpa_ssid *ssid, int line,
1677 				     const char *value)
1678 {
1679 	u8 *hash;
1680 
1681 	if (os_strcmp(value, "NULL") == 0) {
1682 		if (!ssid->eap.password)
1683 			return 1; /* Already unset */
1684 		wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1685 		bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1686 		ssid->eap.password = NULL;
1687 		ssid->eap.password_len = 0;
1688 		return 0;
1689 	}
1690 
1691 #ifdef CONFIG_EXT_PASSWORD
1692 	if (os_strncmp(value, "ext:", 4) == 0) {
1693 		char *name = os_strdup(value + 4);
1694 		if (!name)
1695 			return -1;
1696 		bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1697 		ssid->eap.password = (u8 *) name;
1698 		ssid->eap.password_len = os_strlen(name);
1699 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1700 		ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1701 		return 0;
1702 	}
1703 #endif /* CONFIG_EXT_PASSWORD */
1704 
1705 	if (os_strncmp(value, "hash:", 5) != 0) {
1706 		char *tmp;
1707 		size_t res_len;
1708 
1709 		tmp = wpa_config_parse_string(value, &res_len);
1710 		if (!tmp) {
1711 			wpa_printf(MSG_ERROR,
1712 				   "Line %d: failed to parse password.", line);
1713 			return -1;
1714 		}
1715 		wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1716 				      (u8 *) tmp, res_len);
1717 
1718 		bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1719 		ssid->eap.password = (u8 *) tmp;
1720 		ssid->eap.password_len = res_len;
1721 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1722 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1723 
1724 		return 0;
1725 	}
1726 
1727 
1728 	/* NtPasswordHash: hash:<32 hex digits> */
1729 	if (os_strlen(value + 5) != 2 * 16) {
1730 		wpa_printf(MSG_ERROR,
1731 			   "Line %d: Invalid password hash length (expected 32 hex digits)",
1732 			   line);
1733 		return -1;
1734 	}
1735 
1736 	hash = os_malloc(16);
1737 	if (!hash)
1738 		return -1;
1739 
1740 	if (hexstr2bin(value + 5, hash, 16)) {
1741 		os_free(hash);
1742 		wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1743 		return -1;
1744 	}
1745 
1746 	wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1747 
1748 	if (ssid->eap.password && ssid->eap.password_len == 16 &&
1749 	    os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1750 	    (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1751 		bin_clear_free(hash, 16);
1752 		return 1;
1753 	}
1754 	bin_clear_free(ssid->eap.password, ssid->eap.password_len);
1755 	ssid->eap.password = hash;
1756 	ssid->eap.password_len = 16;
1757 	ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1758 	ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1759 
1760 	return 0;
1761 }
1762 #endif /* EXT_CODE_CROP */
1763 
wpa_config_parse_machine_password(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1764 static int wpa_config_parse_machine_password(const struct parse_data *data,
1765 					     struct wpa_ssid *ssid, int line,
1766 					     const char *value)
1767 {
1768 	u8 *hash;
1769 
1770 	if (os_strcmp(value, "NULL") == 0) {
1771 		if (!ssid->eap.machine_password)
1772 			return 1; /* Already unset */
1773 		wpa_printf(MSG_DEBUG,
1774 			   "Unset configuration string 'machine_password'");
1775 		bin_clear_free(ssid->eap.machine_password,
1776 			       ssid->eap.machine_password_len);
1777 		ssid->eap.machine_password = NULL;
1778 		ssid->eap.machine_password_len = 0;
1779 		return 0;
1780 	}
1781 
1782 #ifdef CONFIG_EXT_PASSWORD
1783 	if (os_strncmp(value, "ext:", 4) == 0) {
1784 		char *name = os_strdup(value + 4);
1785 
1786 		if (!name)
1787 			return -1;
1788 		bin_clear_free(ssid->eap.machine_password,
1789 			       ssid->eap.machine_password_len);
1790 		ssid->eap.machine_password = (u8 *) name;
1791 		ssid->eap.machine_password_len = os_strlen(name);
1792 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1793 		ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1794 		return 0;
1795 	}
1796 #endif /* CONFIG_EXT_PASSWORD */
1797 
1798 	if (os_strncmp(value, "hash:", 5) != 0) {
1799 		char *tmp;
1800 		size_t res_len;
1801 
1802 		tmp = wpa_config_parse_string(value, &res_len);
1803 		if (!tmp) {
1804 			wpa_printf(MSG_ERROR,
1805 				   "Line %d: failed to parse machine_password.",
1806 				   line);
1807 			return -1;
1808 		}
1809 		wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1810 				      (u8 *) tmp, res_len);
1811 
1812 		bin_clear_free(ssid->eap.machine_password,
1813 			       ssid->eap.machine_password_len);
1814 		ssid->eap.machine_password = (u8 *) tmp;
1815 		ssid->eap.machine_password_len = res_len;
1816 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1817 		ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1818 
1819 		return 0;
1820 	}
1821 
1822 
1823 	/* NtPasswordHash: hash:<32 hex digits> */
1824 	if (os_strlen(value + 5) != 2 * 16) {
1825 		wpa_printf(MSG_ERROR,
1826 			   "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1827 			   line);
1828 		return -1;
1829 	}
1830 
1831 	hash = os_malloc(16);
1832 	if (!hash)
1833 		return -1;
1834 
1835 	if (hexstr2bin(value + 5, hash, 16)) {
1836 		os_free(hash);
1837 		wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1838 			   line);
1839 		return -1;
1840 	}
1841 
1842 	wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1843 
1844 	if (ssid->eap.machine_password &&
1845 	    ssid->eap.machine_password_len == 16 &&
1846 	    os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1847 	    (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1848 		bin_clear_free(hash, 16);
1849 		return 1;
1850 	}
1851 	bin_clear_free(ssid->eap.machine_password,
1852 		       ssid->eap.machine_password_len);
1853 	ssid->eap.machine_password = hash;
1854 	ssid->eap.machine_password_len = 16;
1855 	ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1856 	ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1857 
1858 	return 0;
1859 }
1860 
1861 
1862 #ifndef NO_CONFIG_WRITE
1863 
wpa_config_write_password(const struct parse_data * data,struct wpa_ssid * ssid)1864 static char * wpa_config_write_password(const struct parse_data *data,
1865 					struct wpa_ssid *ssid)
1866 {
1867 	char *buf;
1868 
1869 	if (!ssid->eap.password)
1870 		return NULL;
1871 
1872 #ifdef CONFIG_EXT_PASSWORD
1873 	if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1874 		buf = os_zalloc(4 + ssid->eap.password_len + 1);
1875 		if (!buf)
1876 			return NULL;
1877 		os_memcpy(buf, "ext:", 4);
1878 		os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1879 		return buf;
1880 	}
1881 #endif /* CONFIG_EXT_PASSWORD */
1882 
1883 	if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1884 		return wpa_config_write_string(
1885 			ssid->eap.password, ssid->eap.password_len);
1886 	}
1887 
1888 	buf = os_malloc(5 + 32 + 1);
1889 	if (!buf)
1890 		return NULL;
1891 
1892 	os_memcpy(buf, "hash:", 5);
1893 	wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1894 
1895 	return buf;
1896 }
1897 
1898 
wpa_config_write_machine_password(const struct parse_data * data,struct wpa_ssid * ssid)1899 static char * wpa_config_write_machine_password(const struct parse_data *data,
1900 						struct wpa_ssid *ssid)
1901 {
1902 	char *buf;
1903 
1904 	if (!ssid->eap.machine_password)
1905 		return NULL;
1906 
1907 #ifdef CONFIG_EXT_PASSWORD
1908 	if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1909 		buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1910 		if (!buf)
1911 			return NULL;
1912 		os_memcpy(buf, "ext:", 4);
1913 		os_memcpy(buf + 4, ssid->eap.machine_password,
1914 			  ssid->eap.machine_password_len);
1915 		return buf;
1916 	}
1917 #endif /* CONFIG_EXT_PASSWORD */
1918 
1919 	if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1920 		return wpa_config_write_string(
1921 			ssid->eap.machine_password,
1922 			ssid->eap.machine_password_len);
1923 	}
1924 
1925 	buf = os_malloc(5 + 32 + 1);
1926 	if (!buf)
1927 		return NULL;
1928 
1929 	os_memcpy(buf, "hash:", 5);
1930 	wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1931 
1932 	return buf;
1933 }
1934 
1935 #endif /* NO_CONFIG_WRITE */
1936 #endif /* IEEE8021X_EAPOL */
1937 
1938 
1939 #ifdef CONFIG_WEP
1940 
wpa_config_parse_wep_key(u8 * key,size_t * len,int line,const char * value,int idx)1941 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1942 				    const char *value, int idx)
1943 {
1944 	char *buf, title[20];
1945 	int res;
1946 
1947 	buf = wpa_config_parse_string(value, len);
1948 	if (buf == NULL) {
1949 		wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1950 			   line, idx, value);
1951 		return -1;
1952 	}
1953 	if (*len > MAX_WEP_KEY_LEN) {
1954 		wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1955 			   line, idx, value);
1956 		os_free(buf);
1957 		return -1;
1958 	}
1959 	if (*len && *len != 5 && *len != 13 && *len != 16) {
1960 		wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1961 			   "this network block will be ignored",
1962 			   line, (unsigned int) *len);
1963 	}
1964 	os_memcpy(key, buf, *len);
1965 	str_clear_free(buf);
1966 	res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1967 	if (!os_snprintf_error(sizeof(title), res))
1968 		wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1969 	return 0;
1970 }
1971 
1972 
wpa_config_parse_wep_key0(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1973 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1974 				     struct wpa_ssid *ssid, int line,
1975 				     const char *value)
1976 {
1977 	return wpa_config_parse_wep_key(ssid->wep_key[0],
1978 					&ssid->wep_key_len[0], line,
1979 					value, 0);
1980 }
1981 
1982 
wpa_config_parse_wep_key1(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1983 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1984 				     struct wpa_ssid *ssid, int line,
1985 				     const char *value)
1986 {
1987 	return wpa_config_parse_wep_key(ssid->wep_key[1],
1988 					&ssid->wep_key_len[1], line,
1989 					value, 1);
1990 }
1991 
1992 
wpa_config_parse_wep_key2(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)1993 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1994 				     struct wpa_ssid *ssid, int line,
1995 				     const char *value)
1996 {
1997 	return wpa_config_parse_wep_key(ssid->wep_key[2],
1998 					&ssid->wep_key_len[2], line,
1999 					value, 2);
2000 }
2001 
2002 
wpa_config_parse_wep_key3(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2003 static int wpa_config_parse_wep_key3(const struct parse_data *data,
2004 				     struct wpa_ssid *ssid, int line,
2005 				     const char *value)
2006 {
2007 	return wpa_config_parse_wep_key(ssid->wep_key[3],
2008 					&ssid->wep_key_len[3], line,
2009 					value, 3);
2010 }
2011 
2012 
2013 #ifndef NO_CONFIG_WRITE
wpa_config_write_wep_key(struct wpa_ssid * ssid,int idx)2014 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2015 {
2016 	if (ssid->wep_key_len[idx] == 0)
2017 		return NULL;
2018 	return wpa_config_write_string(ssid->wep_key[idx],
2019 				       ssid->wep_key_len[idx]);
2020 }
2021 
2022 
wpa_config_write_wep_key0(const struct parse_data * data,struct wpa_ssid * ssid)2023 static char * wpa_config_write_wep_key0(const struct parse_data *data,
2024 					struct wpa_ssid *ssid)
2025 {
2026 	return wpa_config_write_wep_key(ssid, 0);
2027 }
2028 
2029 
wpa_config_write_wep_key1(const struct parse_data * data,struct wpa_ssid * ssid)2030 static char * wpa_config_write_wep_key1(const struct parse_data *data,
2031 					struct wpa_ssid *ssid)
2032 {
2033 	return wpa_config_write_wep_key(ssid, 1);
2034 }
2035 
2036 
wpa_config_write_wep_key2(const struct parse_data * data,struct wpa_ssid * ssid)2037 static char * wpa_config_write_wep_key2(const struct parse_data *data,
2038 					struct wpa_ssid *ssid)
2039 {
2040 	return wpa_config_write_wep_key(ssid, 2);
2041 }
2042 
2043 
wpa_config_write_wep_key3(const struct parse_data * data,struct wpa_ssid * ssid)2044 static char * wpa_config_write_wep_key3(const struct parse_data *data,
2045 					struct wpa_ssid *ssid)
2046 {
2047 	return wpa_config_write_wep_key(ssid, 3);
2048 }
2049 #endif /* NO_CONFIG_WRITE */
2050 
2051 #endif /* CONFIG_WEP */
2052 
2053 
2054 #ifdef CONFIG_P2P
2055 
wpa_config_parse_go_p2p_dev_addr(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2056 static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2057 					    struct wpa_ssid *ssid, int line,
2058 					    const char *value)
2059 {
2060 	if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2061 	    os_strcmp(value, "any") == 0) {
2062 		os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2063 		wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2064 		return 0;
2065 	}
2066 	if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2067 		wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2068 			   line, value);
2069 		return -1;
2070 	}
2071 	ssid->bssid_set = 1;
2072 	wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2073 		   MAC2STR(ssid->go_p2p_dev_addr));
2074 	return 0;
2075 }
2076 
2077 
2078 #ifndef NO_CONFIG_WRITE
wpa_config_write_go_p2p_dev_addr(const struct parse_data * data,struct wpa_ssid * ssid)2079 static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2080 					       struct wpa_ssid *ssid)
2081 {
2082 	char *value;
2083 	int res;
2084 
2085 	if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2086 		return NULL;
2087 
2088 	value = os_malloc(20);
2089 	if (value == NULL)
2090 		return NULL;
2091 	res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
2092 	if (os_snprintf_error(20, res)) {
2093 		os_free(value);
2094 		return NULL;
2095 	}
2096 	value[20 - 1] = '\0';
2097 	return value;
2098 }
2099 #endif /* NO_CONFIG_WRITE */
2100 
2101 
wpa_config_parse_p2p_client_list(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2102 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2103 					    struct wpa_ssid *ssid, int line,
2104 					    const char *value)
2105 {
2106 	return wpa_config_parse_addr_list(data, line, value,
2107 					  &ssid->p2p_client_list,
2108 					  &ssid->num_p2p_clients,
2109 					  "p2p_client_list", 0, 0);
2110 }
2111 
2112 
2113 #ifndef NO_CONFIG_WRITE
wpa_config_write_p2p_client_list(const struct parse_data * data,struct wpa_ssid * ssid)2114 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2115 					       struct wpa_ssid *ssid)
2116 {
2117 	return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2118 					  ssid->num_p2p_clients,
2119 					  "p2p_client_list");
2120 }
2121 #endif /* NO_CONFIG_WRITE */
2122 
2123 
wpa_config_parse_psk_list(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2124 static int wpa_config_parse_psk_list(const struct parse_data *data,
2125 				     struct wpa_ssid *ssid, int line,
2126 				     const char *value)
2127 {
2128 	struct psk_list_entry *p;
2129 	const char *pos;
2130 
2131 	p = os_zalloc(sizeof(*p));
2132 	if (p == NULL)
2133 		return -1;
2134 
2135 	pos = value;
2136 	if (os_strncmp(pos, "P2P-", 4) == 0) {
2137 		p->p2p = 1;
2138 		pos += 4;
2139 	}
2140 
2141 	if (hwaddr_aton(pos, p->addr)) {
2142 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2143 			   line, pos);
2144 		os_free(p);
2145 		return -1;
2146 	}
2147 	pos += 17;
2148 	if (*pos != '-') {
2149 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2150 			   line, pos);
2151 		os_free(p);
2152 		return -1;
2153 	}
2154 	pos++;
2155 
2156 	if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2157 		wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2158 			   line, pos);
2159 		os_free(p);
2160 		return -1;
2161 	}
2162 
2163 	dl_list_add(&ssid->psk_list, &p->list);
2164 
2165 	return 0;
2166 }
2167 
2168 
2169 #ifndef NO_CONFIG_WRITE
wpa_config_write_psk_list(const struct parse_data * data,struct wpa_ssid * ssid)2170 static char * wpa_config_write_psk_list(const struct parse_data *data,
2171 					struct wpa_ssid *ssid)
2172 {
2173 	return NULL;
2174 }
2175 #endif /* NO_CONFIG_WRITE */
2176 
2177 #endif /* CONFIG_P2P */
2178 
2179 
2180 #ifndef EXT_CODE_CROP
2181 #ifdef CONFIG_MESH
2182 
wpa_config_parse_mesh_basic_rates(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2183 static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2184 					     struct wpa_ssid *ssid, int line,
2185 					     const char *value)
2186 {
2187 	int *rates = wpa_config_parse_int_array(value);
2188 
2189 	if (rates == NULL) {
2190 		wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2191 			   line, value);
2192 		return -1;
2193 	}
2194 	if (rates[0] == 0) {
2195 		os_free(rates);
2196 		rates = NULL;
2197 	}
2198 
2199 	os_free(ssid->mesh_basic_rates);
2200 	ssid->mesh_basic_rates = rates;
2201 
2202 	return 0;
2203 }
2204 
2205 
2206 #ifndef NO_CONFIG_WRITE
2207 
wpa_config_write_mesh_basic_rates(const struct parse_data * data,struct wpa_ssid * ssid)2208 static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2209 						struct wpa_ssid *ssid)
2210 {
2211 	return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2212 }
2213 
2214 #endif /* NO_CONFIG_WRITE */
2215 
2216 #endif /* CONFIG_MESH */
2217 
2218 
2219 #ifdef CONFIG_MACSEC
2220 
wpa_config_parse_mka_cak(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2221 static int wpa_config_parse_mka_cak(const struct parse_data *data,
2222 				    struct wpa_ssid *ssid, int line,
2223 				    const char *value)
2224 {
2225 	size_t len;
2226 
2227 	len = os_strlen(value);
2228 	if (len > 2 * MACSEC_CAK_MAX_LEN ||
2229 	    (len != 2 * 16 && len != 2 * 32) ||
2230 	    hexstr2bin(value, ssid->mka_cak, len / 2)) {
2231 		wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2232 			   line, value);
2233 		return -1;
2234 	}
2235 	ssid->mka_cak_len = len / 2;
2236 	ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2237 
2238 	wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2239 			ssid->mka_cak_len);
2240 	return 0;
2241 }
2242 
2243 
wpa_config_parse_mka_ckn(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2244 static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2245 				    struct wpa_ssid *ssid, int line,
2246 				    const char *value)
2247 {
2248 	size_t len;
2249 
2250 	len = os_strlen(value);
2251 	if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2252 	    len < 2 || /* too short */
2253 	    len % 2 != 0 /* not an integral number of bytes */) {
2254 		wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2255 			   line, value);
2256 		return -1;
2257 	}
2258 	ssid->mka_ckn_len = len / 2;
2259 	if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
2260 		wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2261 			   line, value);
2262 		return -1;
2263 	}
2264 
2265 	ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2266 
2267 	wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2268 			ssid->mka_ckn_len);
2269 	return 0;
2270 }
2271 
2272 
2273 #ifndef NO_CONFIG_WRITE
2274 
wpa_config_write_mka_cak(const struct parse_data * data,struct wpa_ssid * ssid)2275 static char * wpa_config_write_mka_cak(const struct parse_data *data,
2276 				       struct wpa_ssid *ssid)
2277 {
2278 	if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2279 		return NULL;
2280 
2281 	return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
2282 }
2283 
2284 
wpa_config_write_mka_ckn(const struct parse_data * data,struct wpa_ssid * ssid)2285 static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2286 				       struct wpa_ssid *ssid)
2287 {
2288 	if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2289 		return NULL;
2290 	return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
2291 }
2292 
2293 #endif /* NO_CONFIG_WRITE */
2294 
2295 #endif /* CONFIG_MACSEC */
2296 
2297 
2298 #ifdef CONFIG_OCV
2299 
wpa_config_parse_ocv(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2300 static int wpa_config_parse_ocv(const struct parse_data *data,
2301 				struct wpa_ssid *ssid, int line,
2302 				const char *value)
2303 {
2304 	char *end;
2305 
2306 	ssid->ocv = strtol(value, &end, 0);
2307 	if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2308 		wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2309 			   line, value);
2310 		return -1;
2311 	}
2312 	if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2313 		ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2314 	return 0;
2315 }
2316 
2317 
2318 #ifndef NO_CONFIG_WRITE
wpa_config_write_ocv(const struct parse_data * data,struct wpa_ssid * ssid)2319 static char * wpa_config_write_ocv(const struct parse_data *data,
2320 				   struct wpa_ssid *ssid)
2321 {
2322 	char *value = os_malloc(20);
2323 
2324 	if (!value)
2325 		return NULL;
2326 	os_snprintf(value, 20, "%d", ssid->ocv);
2327 	value[20 - 1] = '\0';
2328 	return value;
2329 }
2330 #endif /* NO_CONFIG_WRITE */
2331 
2332 #endif /* CONFIG_OCV */
2333 
2334 
wpa_config_parse_peerkey(const struct parse_data * data,struct wpa_ssid * ssid,int line,const char * value)2335 static int wpa_config_parse_peerkey(const struct parse_data *data,
2336 				    struct wpa_ssid *ssid, int line,
2337 				    const char *value)
2338 {
2339 	wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2340 	return 0;
2341 }
2342 
2343 
2344 #ifndef NO_CONFIG_WRITE
wpa_config_write_peerkey(const struct parse_data * data,struct wpa_ssid * ssid)2345 static char * wpa_config_write_peerkey(const struct parse_data *data,
2346 				       struct wpa_ssid *ssid)
2347 {
2348 	return NULL;
2349 }
2350 #endif /* NO_CONFIG_WRITE */
2351 #endif /* EXT_CODE_CROP */
2352 
2353 
2354 /* Helper macros for network block parser */
2355 
2356 #ifdef OFFSET
2357 #undef OFFSET
2358 #endif /* OFFSET */
2359 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
2360 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2361 
2362 /* STR: Define a string variable for an ASCII string; f = field name */
2363 #ifdef NO_CONFIG_WRITE
2364 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
2365 #define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
2366 #else /* NO_CONFIG_WRITE */
2367 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
2368 #define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2369 		OFFSET(eap.m)
2370 #endif /* NO_CONFIG_WRITE */
2371 #define STR(f) _STR(f), NULL, NULL, NULL, 0
2372 #define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
2373 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
2374 #define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
2375 
2376 /* STR_LEN: Define a string variable with a separate variable for storing the
2377  * data length. Unlike STR(), this can be used to store arbitrary binary data
2378  * (i.e., even nul termination character). */
2379 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
2380 #define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
2381 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
2382 #define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
2383 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2384 
2385 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2386  * explicitly specified. */
2387 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2388 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2389 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2390 
2391 #ifdef NO_CONFIG_WRITE
2392 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
2393 #define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
2394 #else /* NO_CONFIG_WRITE */
2395 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2396 	OFFSET(f), (void *) 0
2397 #define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int,	\
2398 	OFFSET(eap.m), (void *) 0
2399 #endif /* NO_CONFIG_WRITE */
2400 
2401 /* INT: Define an integer variable */
2402 #define INT(f) _INT(f), NULL, NULL, 0
2403 #define INTe(f, m) _INTe(f, m), NULL, NULL, 0
2404 
2405 /* INT_RANGE: Define an integer variable with allowed value range */
2406 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2407 
2408 /* FUNC: Define a configuration variable that uses a custom function for
2409  * parsing and writing the value. */
2410 #ifdef NO_CONFIG_WRITE
2411 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2412 #else /* NO_CONFIG_WRITE */
2413 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2414 	NULL, NULL, NULL, NULL
2415 #endif /* NO_CONFIG_WRITE */
2416 #define FUNC(f) _FUNC(f), 0
2417 #define FUNC_KEY(f) _FUNC(f), 1
2418 
2419 /*
2420  * Table of network configuration variables. This table is used to parse each
2421  * network configuration variable, e.g., each line in wpa_supplicant.conf file
2422  * that is inside a network block.
2423  *
2424  * This table is generated using the helper macros defined above and with
2425  * generous help from the C pre-processor. The field name is stored as a string
2426  * into .name and for STR and INT types, the offset of the target buffer within
2427  * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2428  * offset to the field containing the length of the configuration variable.
2429  * .param3 and .param4 can be used to mark the allowed range (length for STR
2430  * and value for INT).
2431  *
2432  * For each configuration line in wpa_supplicant.conf, the parser goes through
2433  * this table and select the entry that matches with the field name. The parser
2434  * function (.parser) is then called to parse the actual value of the field.
2435  *
2436  * This kind of mechanism makes it easy to add new configuration parameters,
2437  * since only one line needs to be added into this table and into the
2438  * struct wpa_ssid definition if the new variable is either a string or
2439  * integer. More complex types will need to use their own parser and writer
2440  * functions.
2441  */
2442 static const struct parse_data ssid_fields[] = {
2443 	{ STR_RANGE(ssid, 0, SSID_MAX_LEN) },
2444 #ifdef CONFIG_OWE
2445 	{ INT_RANGE(scan_ssid, 0, 1) },
2446 #endif /* CONFIG_OWE */
2447 	{ FUNC(bssid) },
2448 #ifndef EXT_CODE_CROP
2449 	{ FUNC(bssid_hint) },
2450 	{ FUNC(bssid_ignore) },
2451 	{ FUNC(bssid_accept) },
2452 	{ FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2453 	{ FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
2454 #endif /* EXT_CODE_CROP */
2455 	{ FUNC_KEY(psk) },
2456 #ifndef EXT_CODE_CROP
2457 	{ INT(mem_only_psk) },
2458 #endif /* EXT_CODE_CROP */
2459 #ifdef CONFIG_WPA3
2460 	{ STR_KEY(sae_password) },
2461 #endif /* CONFIG_WPA3 */
2462 #ifndef EXT_CODE_CROP
2463 	{ FUNC(proto) },
2464 #endif /* EXT_CODE_CROP */
2465 	{ FUNC(key_mgmt) },
2466 #ifndef EXT_CODE_CROP
2467 	{ INT(bg_scan_period) },
2468 #endif /* EXT_CODE_CROP */
2469 	{ FUNC(pairwise) },
2470 	{ FUNC(group) },
2471 	{ FUNC(group_mgmt) },
2472 	{ FUNC(auth_alg) },
2473 #ifndef EXT_CODE_CROP
2474 	{ FUNC(scan_freq) },
2475 	{ FUNC(freq_list) },
2476 	{ INT_RANGE(ht, 0, 1) },
2477 	{ INT_RANGE(vht, 0, 1) },
2478 	{ INT_RANGE(ht40, -1, 1) },
2479 	{ INT_RANGE(max_oper_chwidth, CHANWIDTH_USE_HT,
2480 		    CHANWIDTH_80P80MHZ) },
2481 	{ INT(vht_center_freq1) },
2482 	{ INT(vht_center_freq2) },
2483 #endif /* EXT_CODE_CROP */
2484 #ifdef IEEE8021X_EAPOL
2485 #if defined(CONFIG_WPS) || defined(LOS_CONFIG_WPA_ENTERPRISE)
2486 	{ FUNC(eap) },
2487 	{ STR_LENe(identity, identity) },
2488 #endif /* CONFIG_WPS */
2489 #ifndef EXT_CODE_CROP
2490 	{ STR_LENe(anonymous_identity, anonymous_identity) },
2491 	{ STR_LENe(imsi_identity, imsi_identity) },
2492 	{ STR_LENe(machine_identity, machine_identity) },
2493 	{ FUNC_KEY(password) },
2494 	{ FUNC_KEY(machine_password) },
2495 	{ STRe(ca_cert, cert.ca_cert) },
2496 	{ STRe(ca_path, cert.ca_path) },
2497 	{ STRe(client_cert, cert.client_cert) },
2498 #endif /* EXT_CODE_CROP */
2499 #ifdef LOS_CONFIG_EAP_TLS
2500 	{ STRe(private_key, cert.private_key) },
2501 #endif /* LOS_CONFIG_EAP_TLS */
2502 #ifndef EXT_CODE_CROP
2503 	{ STR_KEYe(private_key_passwd, cert.private_key_passwd) },
2504 	{ STRe(dh_file, cert.dh_file) },
2505 	{ STRe(subject_match, cert.subject_match) },
2506 	{ STRe(check_cert_subject, cert.check_cert_subject) },
2507 	{ STRe(altsubject_match, cert.altsubject_match) },
2508 	{ STRe(domain_suffix_match, cert.domain_suffix_match) },
2509 	{ STRe(domain_match, cert.domain_match) },
2510 	{ STRe(ca_cert2, phase2_cert.ca_cert) },
2511 	{ STRe(ca_path2, phase2_cert.ca_path) },
2512 	{ STRe(client_cert2, phase2_cert.client_cert) },
2513 	{ STRe(private_key2, phase2_cert.private_key) },
2514 	{ STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
2515 	{ STRe(dh_file2, phase2_cert.dh_file) },
2516 	{ STRe(subject_match2, phase2_cert.subject_match) },
2517 	{ STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2518 	{ STRe(altsubject_match2, phase2_cert.altsubject_match) },
2519 	{ STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2520 	{ STRe(domain_match2, phase2_cert.domain_match) },
2521 #endif /* EXT_CODE_CROP */
2522 #ifdef CONFIG_WPS
2523 	{ STRe(phase1, phase1) },
2524 #endif /* CONFIG_WPS */
2525 #ifndef EXT_CODE_CROP
2526 	{ STRe(phase2, phase2) },
2527 	{ STRe(machine_phase2, machine_phase2) },
2528 	{ STRe(pcsc, pcsc) },
2529 	{ STR_KEYe(pin, cert.pin) },
2530 	{ STRe(engine_id, cert.engine_id) },
2531 	{ STRe(key_id, cert.key_id) },
2532 	{ STRe(cert_id, cert.cert_id) },
2533 	{ STRe(ca_cert_id, cert.ca_cert_id) },
2534 	{ STR_KEYe(pin2, phase2_cert.pin) },
2535 	{ STRe(engine_id2, phase2_cert.engine_id) },
2536 	{ STRe(key_id2, phase2_cert.key_id) },
2537 	{ STRe(cert_id2, phase2_cert.cert_id) },
2538 	{ STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2539 	{ INTe(engine, cert.engine) },
2540 	{ INTe(engine2, phase2_cert.engine) },
2541 #endif /* EXT_CODE_CROP */
2542 	{ STRe(machine_ca_cert, machine_cert.ca_cert) },
2543 	{ STRe(machine_ca_path, machine_cert.ca_path) },
2544 	{ STRe(machine_client_cert, machine_cert.client_cert) },
2545 	{ STRe(machine_private_key, machine_cert.private_key) },
2546 	{ STR_KEYe(machine_private_key_passwd,
2547 		   machine_cert.private_key_passwd) },
2548 	{ STRe(machine_dh_file, machine_cert.dh_file) },
2549 	{ STRe(machine_subject_match, machine_cert.subject_match) },
2550 	{ STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2551 	{ STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2552 	{ STRe(machine_domain_suffix_match,
2553 	       machine_cert.domain_suffix_match) },
2554 	{ STRe(machine_domain_match, machine_cert.domain_match) },
2555 	{ STR_KEYe(machine_pin, machine_cert.pin) },
2556 	{ STRe(machine_engine_id, machine_cert.engine_id) },
2557 	{ STRe(machine_key_id, machine_cert.key_id) },
2558 	{ STRe(machine_cert_id, machine_cert.cert_id) },
2559 	{ STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2560 	{ INTe(machine_engine, machine_cert.engine) },
2561 	{ INTe(machine_ocsp, machine_cert.ocsp) },
2562 	{ INT(eapol_flags) },
2563 	{ INTe(sim_num, sim_num) },
2564 	{ STRe(openssl_ciphers, openssl_ciphers) },
2565 	{ INTe(erp, erp) },
2566 #endif /* IEEE8021X_EAPOL */
2567 #ifdef CONFIG_WEP
2568 	{ FUNC_KEY(wep_key0) },
2569 	{ FUNC_KEY(wep_key1) },
2570 	{ FUNC_KEY(wep_key2) },
2571 	{ FUNC_KEY(wep_key3) },
2572 	{ INT(wep_tx_keyidx) },
2573 #endif /* CONFIG_WEP */
2574 #ifndef EXT_CODE_CROP
2575 	{ INT(priority) },
2576 #ifdef IEEE8021X_EAPOL
2577 	{ INT(eap_workaround) },
2578 	{ STRe(pac_file, pac_file) },
2579 	{ INTe(fragment_size, fragment_size) },
2580 	{ INTe(ocsp, cert.ocsp) },
2581 	{ INTe(ocsp2, phase2_cert.ocsp) },
2582 #endif /* IEEE8021X_EAPOL */
2583 #endif /* EXT_CODE_CROP */
2584 #ifdef CONFIG_MESH
2585 	{ INT_RANGE(mode, 0, 5) },
2586 #ifndef EXT_CODE_CROP
2587 	{ INT_RANGE(no_auto_peer, 0, 1) },
2588 	{ INT_RANGE(mesh_fwding, 0, 1) },
2589 	{ INT_RANGE(mesh_rssi_threshold, -255, 1) },
2590 #endif /* EXT_CODE_CROP */
2591 #else /* CONFIG_MESH */
2592 	{ INT_RANGE(mode, 0, 4) },
2593 #endif /* CONFIG_MESH */
2594 #ifndef EXT_CODE_CROP
2595 	{ INT_RANGE(proactive_key_caching, 0, 1) },
2596 	{ INT_RANGE(disabled, 0, 2) },
2597 	{ STR(id_str) },
2598 #endif /* EXT_CODE_CROP */
2599 	{ INT_RANGE(ieee80211w, 0, 2) },
2600 #ifdef CONFIG_OCV
2601 	{ FUNC(ocv) },
2602 #endif /* CONFIG_OCV */
2603 #ifndef EXT_CODE_CROP
2604 	{ FUNC(peerkey) /* obsolete - removed */ },
2605 	{ INT_RANGE(mixed_cell, 0, 1) },
2606 #endif /* EXT_CODE_CROP */
2607 	{ INT_RANGE(frequency, 0, 70200) },
2608 #ifndef EXT_CODE_CROP
2609 	{ INT_RANGE(fixed_freq, 0, 1) },
2610 	{ INT_RANGE(enable_edmg, 0, 1) },
2611 	{ INT_RANGE(edmg_channel, 9, 13) },
2612 #ifdef CONFIG_ACS
2613 	{ INT_RANGE(acs, 0, 1) },
2614 #endif /* CONFIG_ACS */
2615 #ifdef CONFIG_MESH
2616 	{ FUNC(mesh_basic_rates) },
2617 	{ INT(dot11MeshMaxRetries) },
2618 	{ INT(dot11MeshRetryTimeout) },
2619 	{ INT(dot11MeshConfirmTimeout) },
2620 	{ INT(dot11MeshHoldingTimeout) },
2621 #endif /* CONFIG_MESH */
2622 	{ INT(wpa_ptk_rekey) },
2623 	{ INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
2624 	{ INT(group_rekey) },
2625 	{ STR(bgscan) },
2626 #endif // EXT_CODE_CROP
2627 	{ INT_RANGE(ignore_broadcast_ssid, 0, 2) },
2628 #ifdef CONFIG_P2P
2629 	{ FUNC(go_p2p_dev_addr) },
2630 	{ FUNC(p2p_client_list) },
2631 	{ FUNC(psk_list) },
2632 #endif /* CONFIG_P2P */
2633 #ifdef CONFIG_HT_OVERRIDES
2634 	{ INT_RANGE(disable_ht, 0, 1) },
2635 	{ INT_RANGE(disable_ht40, -1, 1) },
2636 	{ INT_RANGE(disable_sgi, 0, 1) },
2637 	{ INT_RANGE(disable_ldpc, 0, 1) },
2638 	{ INT_RANGE(ht40_intolerant, 0, 1) },
2639 	{ INT_RANGE(tx_stbc, -1, 1) },
2640 	{ INT_RANGE(rx_stbc, -1, 3) },
2641 	{ INT_RANGE(disable_max_amsdu, -1, 1) },
2642 	{ INT_RANGE(ampdu_factor, -1, 3) },
2643 	{ INT_RANGE(ampdu_density, -1, 7) },
2644 	{ STR(ht_mcs) },
2645 #endif /* CONFIG_HT_OVERRIDES */
2646 #ifdef CONFIG_VHT_OVERRIDES
2647 	{ INT_RANGE(disable_vht, 0, 1) },
2648 	{ INT(vht_capa) },
2649 	{ INT(vht_capa_mask) },
2650 	{ INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2651 	{ INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2652 	{ INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2653 	{ INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2654 	{ INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2655 	{ INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2656 	{ INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2657 	{ INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2658 	{ INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2659 	{ INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2660 	{ INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2661 	{ INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2662 	{ INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2663 	{ INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2664 	{ INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2665 	{ INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2666 #endif /* CONFIG_VHT_OVERRIDES */
2667 #ifdef CONFIG_HE_OVERRIDES
2668 	{ INT_RANGE(disable_he, 0, 1)},
2669 #endif /* CONFIG_HE_OVERRIDES */
2670 #ifndef EXT_CODE_CROP
2671 	{ INT(ap_max_inactivity) },
2672 	{ INT(dtim_period) },
2673 	{ INT(beacon_int) },
2674 #endif /* EXT_CODE_CROP */
2675 #ifdef CONFIG_MACSEC
2676 	{ INT_RANGE(macsec_policy, 0, 1) },
2677 	{ INT_RANGE(macsec_integ_only, 0, 1) },
2678 	{ INT_RANGE(macsec_replay_protect, 0, 1) },
2679 	{ INT(macsec_replay_window) },
2680 	{ INT_RANGE(macsec_port, 1, 65534) },
2681 	{ INT_RANGE(mka_priority, 0, 255) },
2682 	{ FUNC_KEY(mka_cak) },
2683 	{ FUNC_KEY(mka_ckn) },
2684 #endif /* CONFIG_MACSEC */
2685 #ifdef CONFIG_HS20
2686 	{ INT(update_identifier) },
2687 #ifndef EXT_CODE_CROP
2688 	{ STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
2689 #endif /* EXT_CODE_CROP */
2690 #endif /* CONFIG_HS20 */
2691 #ifndef EXT_CODE_CROP
2692 	{ INT_RANGE(mac_addr, 0, 2) },
2693 	{ INT_RANGE(pbss, 0, 2) },
2694 	{ INT_RANGE(wps_disabled, 0, 1) },
2695 	{ INT_RANGE(fils_dh_group, 0, 65535) },
2696 #endif /* EXT_CODE_CROP */
2697 #ifdef CONFIG_DPP
2698 	{ STR(dpp_connector) },
2699 	{ STR_LEN(dpp_netaccesskey) },
2700 	{ INT(dpp_netaccesskey_expiry) },
2701 	{ STR_LEN(dpp_csign) },
2702 	{ STR_LEN(dpp_pp_key) },
2703 	{ INT_RANGE(dpp_pfs, 0, 2) },
2704 #endif /* CONFIG_DPP */
2705 	{ INT_RANGE(owe_group, 0, 65535) },
2706 	{ INT_RANGE(owe_only, 0, 1) },
2707 	{ INT_RANGE(owe_ptk_workaround, 0, 1) },
2708 	{ INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
2709 	{ INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
2710 	{ INT_RANGE(beacon_prot, 0, 1) },
2711 	{ INT_RANGE(transition_disable, 0, 255) },
2712 	{ INT_RANGE(sae_pk, 0, 2) },
2713 };
2714 
2715 #undef OFFSET
2716 #undef _STR
2717 #undef STR
2718 #undef STR_KEY
2719 #undef _STR_LEN
2720 #undef STR_LEN
2721 #undef STR_LEN_KEY
2722 #undef _STR_RANGE
2723 #undef STR_RANGE
2724 #undef STR_RANGE_KEY
2725 #undef _INT
2726 #undef INT
2727 #undef INT_RANGE
2728 #undef _FUNC
2729 #undef FUNC
2730 #undef FUNC_KEY
2731 #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
2732 
2733 #ifndef EXT_CODE_CROP
2734 /**
2735  * wpa_config_add_prio_network - Add a network to priority lists
2736  * @config: Configuration data from wpa_config_read()
2737  * @ssid: Pointer to the network configuration to be added to the list
2738  * Returns: 0 on success, -1 on failure
2739  *
2740  * This function is used to add a network block to the priority list of
2741  * networks. This must be called for each network when reading in the full
2742  * configuration. In addition, this can be used indirectly when updating
2743  * priorities by calling wpa_config_update_prio_list().
2744  */
wpa_config_add_prio_network(struct wpa_config * config,struct wpa_ssid * ssid)2745 int wpa_config_add_prio_network(struct wpa_config *config,
2746 				struct wpa_ssid *ssid)
2747 {
2748 	size_t prio;
2749 	struct wpa_ssid *prev, **nlist;
2750 
2751 	/*
2752 	 * Add to an existing priority list if one is available for the
2753 	 * configured priority level for this network.
2754 	 */
2755 	for (prio = 0; prio < config->num_prio; prio++) {
2756 		prev = config->pssid[prio];
2757 		if (prev->priority == ssid->priority) {
2758 			while (prev->pnext)
2759 				prev = prev->pnext;
2760 			prev->pnext = ssid;
2761 			return 0;
2762 		}
2763 	}
2764 
2765 	/* First network for this priority - add a new priority list */
2766 	nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2767 				 sizeof(struct wpa_ssid *));
2768 	if (nlist == NULL)
2769 		return -1;
2770 
2771 	for (prio = 0; prio < config->num_prio; prio++) {
2772 		if (nlist[prio]->priority < ssid->priority) {
2773 			os_memmove(&nlist[prio + 1], &nlist[prio],
2774 				   (config->num_prio - prio) *
2775 				   sizeof(struct wpa_ssid *));
2776 			break;
2777 		}
2778 	}
2779 
2780 	nlist[prio] = ssid;
2781 	config->num_prio++;
2782 	config->pssid = nlist;
2783 
2784 	return 0;
2785 }
2786 
2787 
2788 /**
2789  * wpa_config_update_prio_list - Update network priority list
2790  * @config: Configuration data from wpa_config_read()
2791  * Returns: 0 on success, -1 on failure
2792  *
2793  * This function is called to update the priority list of networks in the
2794  * configuration when a network is being added or removed. This is also called
2795  * if a priority for a network is changed.
2796  */
wpa_config_update_prio_list(struct wpa_config * config)2797 int wpa_config_update_prio_list(struct wpa_config *config)
2798 {
2799 	struct wpa_ssid *ssid;
2800 	int ret = 0;
2801 
2802 	os_free(config->pssid);
2803 	config->pssid = NULL;
2804 	config->num_prio = 0;
2805 
2806 	ssid = config->ssid;
2807 	while (ssid) {
2808 		ssid->pnext = NULL;
2809 		if (wpa_config_add_prio_network(config, ssid) < 0)
2810 			ret = -1;
2811 		ssid = ssid->next;
2812 	}
2813 
2814 	return ret;
2815 }
2816 #endif /* EXT_CODE_CROP */
2817 
2818 #ifdef IEEE8021X_EAPOL
2819 
eap_peer_config_free_cert(struct eap_peer_cert_config * cert)2820 static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2821 {
2822 	os_free(cert->ca_cert);
2823 	os_free(cert->ca_path);
2824 	os_free(cert->client_cert);
2825 	os_free(cert->private_key);
2826 	str_clear_free(cert->private_key_passwd);
2827 	os_free(cert->dh_file);
2828 	os_free(cert->subject_match);
2829 	os_free(cert->check_cert_subject);
2830 	os_free(cert->altsubject_match);
2831 	os_free(cert->domain_suffix_match);
2832 	os_free(cert->domain_match);
2833 	str_clear_free(cert->pin);
2834 	os_free(cert->engine_id);
2835 	os_free(cert->key_id);
2836 	os_free(cert->cert_id);
2837 	os_free(cert->ca_cert_id);
2838 }
2839 
2840 
eap_peer_config_free(struct eap_peer_config * eap)2841 static void eap_peer_config_free(struct eap_peer_config *eap)
2842 {
2843 	os_free(eap->eap_methods);
2844 	bin_clear_free(eap->identity, eap->identity_len);
2845 	os_free(eap->anonymous_identity);
2846 	os_free(eap->imsi_identity);
2847 	os_free(eap->machine_identity);
2848 	bin_clear_free(eap->password, eap->password_len);
2849 	bin_clear_free(eap->machine_password, eap->machine_password_len);
2850 	eap_peer_config_free_cert(&eap->cert);
2851 	eap_peer_config_free_cert(&eap->phase2_cert);
2852 	eap_peer_config_free_cert(&eap->machine_cert);
2853 	os_free(eap->phase1);
2854 	os_free(eap->phase2);
2855 	os_free(eap->machine_phase2);
2856 	os_free(eap->pcsc);
2857 	os_free(eap->otp);
2858 	os_free(eap->pending_req_otp);
2859 	os_free(eap->pac_file);
2860 	bin_clear_free(eap->new_password, eap->new_password_len);
2861 	str_clear_free(eap->external_sim_resp);
2862 	os_free(eap->openssl_ciphers);
2863 }
2864 
2865 #endif /* IEEE8021X_EAPOL */
2866 
2867 
2868 /**
2869  * wpa_config_free_ssid - Free network/ssid configuration data
2870  * @ssid: Configuration data for the network
2871  *
2872  * This function frees all resources allocated for the network configuration
2873  * data.
2874  */
wpa_config_free_ssid(struct wpa_ssid * ssid)2875 void wpa_config_free_ssid(struct wpa_ssid *ssid)
2876 {
2877 	struct psk_list_entry *psk;
2878 
2879 	os_free(ssid->ssid);
2880 	str_clear_free(ssid->passphrase);
2881 	os_free(ssid->ext_psk);
2882 	str_clear_free(ssid->sae_password);
2883 	os_free(ssid->sae_password_id);
2884 #ifdef IEEE8021X_EAPOL
2885 	eap_peer_config_free(&ssid->eap);
2886 #endif /* IEEE8021X_EAPOL */
2887 	os_free(ssid->id_str);
2888 	os_free(ssid->scan_freq);
2889 	os_free(ssid->freq_list);
2890 	os_free(ssid->bgscan);
2891 	os_free(ssid->p2p_client_list);
2892 	os_free(ssid->bssid_ignore);
2893 	os_free(ssid->bssid_accept);
2894 #ifdef CONFIG_HT_OVERRIDES
2895 	os_free(ssid->ht_mcs);
2896 #endif /* CONFIG_HT_OVERRIDES */
2897 #ifdef CONFIG_MESH
2898 	os_free(ssid->mesh_basic_rates);
2899 #endif /* CONFIG_MESH */
2900 #ifdef CONFIG_HS20
2901 	os_free(ssid->roaming_consortium_selection);
2902 #endif /* CONFIG_HS20 */
2903 	os_free(ssid->dpp_connector);
2904 	bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2905 	os_free(ssid->dpp_csign);
2906 	os_free(ssid->dpp_pp_key);
2907 	while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2908 				    list))) {
2909 		dl_list_del(&psk->list);
2910 		bin_clear_free(psk, sizeof(*psk));
2911 	}
2912 #ifdef CONFIG_SAE
2913 	sae_deinit_pt(ssid->pt);
2914 #endif /* CONFIG_SAE */
2915 	bin_clear_free(ssid, sizeof(*ssid));
2916 }
2917 
2918 
wpa_config_free_cred(struct wpa_cred * cred)2919 void wpa_config_free_cred(struct wpa_cred *cred)
2920 {
2921 	size_t i;
2922 
2923 	os_free(cred->realm);
2924 	str_clear_free(cred->username);
2925 	str_clear_free(cred->password);
2926 	os_free(cred->ca_cert);
2927 	os_free(cred->client_cert);
2928 	os_free(cred->private_key);
2929 	str_clear_free(cred->private_key_passwd);
2930 	os_free(cred->engine_id);
2931 	os_free(cred->ca_cert_id);
2932 	os_free(cred->cert_id);
2933 	os_free(cred->key_id);
2934 	os_free(cred->imsi);
2935 	str_clear_free(cred->milenage);
2936 	for (i = 0; i < cred->num_domain; i++)
2937 		os_free(cred->domain[i]);
2938 	os_free(cred->domain);
2939 	os_free(cred->domain_suffix_match);
2940 	os_free(cred->eap_method);
2941 	os_free(cred->phase1);
2942 	os_free(cred->phase2);
2943 	os_free(cred->excluded_ssid);
2944 	os_free(cred->roaming_partner);
2945 	os_free(cred->provisioning_sp);
2946 	for (i = 0; i < cred->num_req_conn_capab; i++)
2947 		os_free(cred->req_conn_capab_port[i]);
2948 	os_free(cred->req_conn_capab_port);
2949 	os_free(cred->req_conn_capab_proto);
2950 	os_free(cred);
2951 }
2952 
2953 
wpa_config_flush_blobs(struct wpa_config * config)2954 void wpa_config_flush_blobs(struct wpa_config *config)
2955 {
2956 #ifndef CONFIG_NO_CONFIG_BLOBS
2957 	struct wpa_config_blob *blob, *prev;
2958 
2959 	blob = config->blobs;
2960 	config->blobs = NULL;
2961 	while (blob) {
2962 		prev = blob;
2963 		blob = blob->next;
2964 		wpa_config_free_blob(prev);
2965 	}
2966 #endif /* CONFIG_NO_CONFIG_BLOBS */
2967 }
2968 
2969 
2970 /**
2971  * wpa_config_free - Free configuration data
2972  * @config: Configuration data from wpa_config_read()
2973  *
2974  * This function frees all resources allocated for the configuration data by
2975  * wpa_config_read().
2976  */
wpa_config_free(struct wpa_config * config)2977 void wpa_config_free(struct wpa_config *config)
2978 {
2979 	struct wpa_ssid *ssid, *prev = NULL;
2980 	struct wpa_cred *cred, *cprev;
2981 	int i;
2982 
2983 	ssid = config->ssid;
2984 	while (ssid) {
2985 		prev = ssid;
2986 		ssid = ssid->next;
2987 		wpa_config_free_ssid(prev);
2988 	}
2989 
2990 	cred = config->cred;
2991 	while (cred) {
2992 		cprev = cred;
2993 		cred = cred->next;
2994 		wpa_config_free_cred(cprev);
2995 	}
2996 
2997 	wpa_config_flush_blobs(config);
2998 
2999 	wpabuf_free(config->wps_vendor_ext_m1);
3000 	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
3001 		wpabuf_free(config->wps_vendor_ext[i]);
3002 	os_free(config->ctrl_interface);
3003 	os_free(config->ctrl_interface_group);
3004 	os_free(config->opensc_engine_path);
3005 	os_free(config->pkcs11_engine_path);
3006 	os_free(config->pkcs11_module_path);
3007 	os_free(config->openssl_ciphers);
3008 	os_free(config->pcsc_reader);
3009 	str_clear_free(config->pcsc_pin);
3010 	os_free(config->driver_param);
3011 	os_free(config->device_name);
3012 	os_free(config->manufacturer);
3013 	os_free(config->model_name);
3014 	os_free(config->model_number);
3015 	os_free(config->serial_number);
3016 	os_free(config->config_methods);
3017 	os_free(config->p2p_ssid_postfix);
3018 	os_free(config->pssid);
3019 	os_free(config->p2p_pref_chan);
3020 	os_free(config->p2p_no_go_freq.range);
3021 	os_free(config->autoscan);
3022 	os_free(config->freq_list);
3023 	os_free(config->initial_freq_list);
3024 	wpabuf_free(config->wps_nfc_dh_pubkey);
3025 	wpabuf_free(config->wps_nfc_dh_privkey);
3026 	wpabuf_free(config->wps_nfc_dev_pw);
3027 	os_free(config->ext_password_backend);
3028 	os_free(config->sae_groups);
3029 	wpabuf_free(config->ap_vendor_elements);
3030 	wpabuf_free(config->ap_assocresp_elements);
3031 	os_free(config->osu_dir);
3032 	os_free(config->bgscan);
3033 	os_free(config->wowlan_triggers);
3034 	os_free(config->fst_group_id);
3035 	os_free(config->sched_scan_plans);
3036 #ifdef CONFIG_MBO
3037 	os_free(config->non_pref_chan);
3038 #endif /* CONFIG_MBO */
3039 	os_free(config->dpp_name);
3040 	os_free(config->dpp_mud_url);
3041 
3042 	os_free(config);
3043 }
3044 
3045 
3046 /**
3047  * wpa_config_foreach_network - Iterate over each configured network
3048  * @config: Configuration data from wpa_config_read()
3049  * @func: Callback function to process each network
3050  * @arg: Opaque argument to pass to callback function
3051  *
3052  * Iterate over the set of configured networks calling the specified
3053  * function for each item. We guard against callbacks removing the
3054  * supplied network.
3055  */
wpa_config_foreach_network(struct wpa_config * config,void (* func)(void *,struct wpa_ssid *),void * arg)3056 void wpa_config_foreach_network(struct wpa_config *config,
3057 				void (*func)(void *, struct wpa_ssid *),
3058 				void *arg)
3059 {
3060 	struct wpa_ssid *ssid, *next;
3061 
3062 	ssid = config->ssid;
3063 	while (ssid) {
3064 		next = ssid->next;
3065 		func(arg, ssid);
3066 		ssid = next;
3067 	}
3068 }
3069 
3070 
3071 /**
3072  * wpa_config_get_network - Get configured network based on id
3073  * @config: Configuration data from wpa_config_read()
3074  * @id: Unique network id to search for
3075  * Returns: Network configuration or %NULL if not found
3076  */
wpa_config_get_network(struct wpa_config * config,int id)3077 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3078 {
3079 	struct wpa_ssid *ssid;
3080 
3081 	ssid = config->ssid;
3082 	while (ssid) {
3083 		if (id == ssid->id)
3084 			break;
3085 		ssid = ssid->next;
3086 	}
3087 
3088 	return ssid;
3089 }
3090 
3091 
3092 /**
3093  * wpa_config_add_network - Add a new network with empty configuration
3094  * @config: Configuration data from wpa_config_read()
3095  * Returns: The new network configuration or %NULL if operation failed
3096  */
wpa_config_add_network(struct wpa_config * config)3097 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3098 {
3099 	int id;
3100 	struct wpa_ssid *ssid, *last = NULL;
3101 
3102 	id = -1;
3103 	ssid = config->ssid;
3104 	while (ssid) {
3105 		if (ssid->id > id)
3106 			id = ssid->id;
3107 		last = ssid;
3108 		ssid = ssid->next;
3109 	}
3110 	id++;
3111 
3112 	ssid = os_zalloc(sizeof(*ssid));
3113 	if (ssid == NULL)
3114 		return NULL;
3115 	ssid->id = id;
3116 #ifdef CONFIG_P2P
3117 	dl_list_init(&ssid->psk_list);
3118 #endif /* CONFIG_P2P */
3119 	if (last)
3120 		last->next = ssid;
3121 	else
3122 		config->ssid = ssid;
3123 #ifndef EXT_CODE_CROP
3124 	wpa_config_update_prio_list(config);
3125 #endif /* EXT_CODE_CROP */
3126 	return ssid;
3127 }
3128 
3129 
3130 /**
3131  * wpa_config_remove_network - Remove a configured network based on id
3132  * @config: Configuration data from wpa_config_read()
3133  * @id: Unique network id to search for
3134  * Returns: 0 on success, or -1 if the network was not found
3135  */
wpa_config_remove_network(struct wpa_config * config,int id)3136 int wpa_config_remove_network(struct wpa_config *config, int id)
3137 {
3138 	struct wpa_ssid *ssid, *prev = NULL;
3139 
3140 	ssid = config->ssid;
3141 	while (ssid) {
3142 		if (id == ssid->id)
3143 			break;
3144 		prev = ssid;
3145 		ssid = ssid->next;
3146 	}
3147 
3148 	if (ssid == NULL)
3149 		return -1;
3150 
3151 	if (prev)
3152 		prev->next = ssid->next;
3153 	else
3154 		config->ssid = ssid->next;
3155 #ifndef EXT_CODE_CROP
3156 	wpa_config_update_prio_list(config);
3157 #endif /* EXT_CODE_CROP */
3158 	wpa_config_free_ssid(ssid);
3159 	return 0;
3160 }
3161 
3162 
3163 /**
3164  * wpa_config_set_network_defaults - Set network default values
3165  * @ssid: Pointer to network configuration data
3166  */
wpa_config_set_network_defaults(struct wpa_ssid * ssid)3167 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3168 {
3169 	ssid->proto = DEFAULT_PROTO;
3170 	ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3171 	ssid->group_cipher = DEFAULT_GROUP;
3172 	ssid->key_mgmt = DEFAULT_KEY_MGMT;
3173 	ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
3174 #ifndef EXT_CODE_CROP
3175 	ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
3176 #endif /* EXT_CODE_CROP */
3177 	ssid->ht = 1;
3178 	ssid->vht = 1;
3179 	ssid->he = 1;
3180 #ifdef IEEE8021X_EAPOL
3181 	ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3182 	ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3183 	ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
3184 	ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
3185 #endif /* IEEE8021X_EAPOL */
3186 #ifdef CONFIG_MESH
3187 	ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3188 	ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3189 	ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3190 	ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
3191 	ssid->mesh_fwding = DEFAULT_MESH_FWDING;
3192 	ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
3193 #endif /* CONFIG_MESH */
3194 #ifdef CONFIG_HT_OVERRIDES
3195 	ssid->disable_ht = DEFAULT_DISABLE_HT;
3196 	ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
3197 	ssid->disable_sgi = DEFAULT_DISABLE_SGI;
3198 	ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
3199 	ssid->tx_stbc = DEFAULT_TX_STBC;
3200 	ssid->rx_stbc = DEFAULT_RX_STBC;
3201 	ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3202 	ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3203 	ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3204 #endif /* CONFIG_HT_OVERRIDES */
3205 #ifdef CONFIG_VHT_OVERRIDES
3206 	ssid->vht_rx_mcs_nss_1 = -1;
3207 	ssid->vht_rx_mcs_nss_2 = -1;
3208 	ssid->vht_rx_mcs_nss_3 = -1;
3209 	ssid->vht_rx_mcs_nss_4 = -1;
3210 	ssid->vht_rx_mcs_nss_5 = -1;
3211 	ssid->vht_rx_mcs_nss_6 = -1;
3212 	ssid->vht_rx_mcs_nss_7 = -1;
3213 	ssid->vht_rx_mcs_nss_8 = -1;
3214 	ssid->vht_tx_mcs_nss_1 = -1;
3215 	ssid->vht_tx_mcs_nss_2 = -1;
3216 	ssid->vht_tx_mcs_nss_3 = -1;
3217 	ssid->vht_tx_mcs_nss_4 = -1;
3218 	ssid->vht_tx_mcs_nss_5 = -1;
3219 	ssid->vht_tx_mcs_nss_6 = -1;
3220 	ssid->vht_tx_mcs_nss_7 = -1;
3221 	ssid->vht_tx_mcs_nss_8 = -1;
3222 #endif /* CONFIG_VHT_OVERRIDES */
3223 	ssid->proactive_key_caching = -1;
3224 	ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
3225 	ssid->sae_pwe = DEFAULT_SAE_PWE;
3226 #ifdef CONFIG_MACSEC
3227 	ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3228 #endif /* CONFIG_MACSEC */
3229 	ssid->mac_addr = -1;
3230 	ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
3231 }
3232 
3233 
3234 /**
3235  * wpa_config_set - Set a variable in network configuration
3236  * @ssid: Pointer to network configuration data
3237  * @var: Variable name, e.g., "ssid"
3238  * @value: Variable value
3239  * @line: Line number in configuration file or 0 if not used
3240  * Returns: 0 on success with possible change in the value, 1 on success with
3241  * no change to previously configured value, or -1 on failure
3242  *
3243  * This function can be used to set network configuration variables based on
3244  * both the configuration file and management interface input. The value
3245  * parameter must be in the same format as the text-based configuration file is
3246  * using. For example, strings are using double quotation marks.
3247  */
wpa_config_set(struct wpa_ssid * ssid,const char * var,const char * value,int line)3248 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3249 		   int line)
3250 {
3251 	size_t i;
3252 	int ret = 0;
3253 
3254 	if (ssid == NULL || var == NULL || value == NULL)
3255 		return -1;
3256 
3257 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
3258 		const struct parse_data *field = &ssid_fields[i];
3259 		if (os_strcmp(var, field->name) != 0)
3260 			continue;
3261 
3262 		ret = field->parser(field, ssid, line, value);
3263 		if (ret < 0) {
3264 			if (line) {
3265 				wpa_printf(MSG_ERROR, "Line %d: failed to "
3266 					   "parse %s '%s'.", line, var, value);
3267 			}
3268 			ret = -1;
3269 		}
3270 #ifdef CONFIG_SAE
3271 		if (os_strcmp(var, "ssid") == 0 ||
3272 		    os_strcmp(var, "psk") == 0 ||
3273 		    os_strcmp(var, "sae_password") == 0 ||
3274 		    os_strcmp(var, "sae_password_id") == 0) {
3275 			sae_deinit_pt(ssid->pt);
3276 			ssid->pt = NULL;
3277 		}
3278 #endif /* CONFIG_SAE */
3279 		break;
3280 	}
3281 	if (i == NUM_SSID_FIELDS) {
3282 		if (line) {
3283 			wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3284 				   "'%s'.", line, var);
3285 		}
3286 		ret = -1;
3287 	}
3288 	ssid->was_recently_reconfigured = true;
3289 
3290 	return ret;
3291 }
3292 
3293 
wpa_config_set_quoted(struct wpa_ssid * ssid,const char * var,const char * value)3294 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3295 			  const char *value)
3296 {
3297 	size_t len;
3298 	char *buf;
3299 	int ret;
3300 
3301 	len = os_strlen(value);
3302 	buf = os_malloc(len + 3);
3303 	if (buf == NULL)
3304 		return -1;
3305 	buf[0] = '"';
3306 	os_memcpy(buf + 1, value, len);
3307 	buf[len + 1] = '"';
3308 	buf[len + 2] = '\0';
3309 	ret = wpa_config_set(ssid, var, buf, 0);
3310 	os_free(buf);
3311 	return ret;
3312 }
3313 
3314 
3315 /**
3316  * wpa_config_get_all - Get all options from network configuration
3317  * @ssid: Pointer to network configuration data
3318  * @get_keys: Determines if keys/passwords will be included in returned list
3319  *	(if they may be exported)
3320  * Returns: %NULL terminated list of all set keys and their values in the form
3321  * of [key1, val1, key2, val2, ... , NULL]
3322  *
3323  * This function can be used to get list of all configured network properties.
3324  * The caller is responsible for freeing the returned list and all its
3325  * elements.
3326  */
wpa_config_get_all(struct wpa_ssid * ssid,int get_keys)3327 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3328 {
3329 #ifdef NO_CONFIG_WRITE
3330 	return NULL;
3331 #else /* NO_CONFIG_WRITE */
3332 	const struct parse_data *field;
3333 	char *key, *value;
3334 	size_t i;
3335 	char **props;
3336 	int fields_num;
3337 
3338 	get_keys = get_keys && ssid->export_keys;
3339 
3340 	props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
3341 	if (!props)
3342 		return NULL;
3343 
3344 	fields_num = 0;
3345 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
3346 		field = &ssid_fields[i];
3347 		if (field->key_data && !get_keys)
3348 			continue;
3349 		value = field->writer(field, ssid);
3350 		if (value == NULL)
3351 			continue;
3352 		if (os_strlen(value) == 0) {
3353 			os_free(value);
3354 			continue;
3355 		}
3356 
3357 		key = os_strdup(field->name);
3358 		if (key == NULL) {
3359 			os_free(value);
3360 			goto err;
3361 		}
3362 
3363 		props[fields_num * 2] = key;
3364 		props[fields_num * 2 + 1] = value;
3365 
3366 		fields_num++;
3367 	}
3368 
3369 	return props;
3370 
3371 err:
3372 	for (i = 0; props[i]; i++)
3373 		os_free(props[i]);
3374 	os_free(props);
3375 	return NULL;
3376 #endif /* NO_CONFIG_WRITE */
3377 }
3378 
3379 
3380 #ifndef NO_CONFIG_WRITE
3381 /**
3382  * wpa_config_get - Get a variable in network configuration
3383  * @ssid: Pointer to network configuration data
3384  * @var: Variable name, e.g., "ssid"
3385  * Returns: Value of the variable or %NULL on failure
3386  *
3387  * This function can be used to get network configuration variables. The
3388  * returned value is a copy of the configuration variable in text format, i.e,.
3389  * the same format that the text-based configuration file and wpa_config_set()
3390  * are using for the value. The caller is responsible for freeing the returned
3391  * value.
3392  */
wpa_config_get(struct wpa_ssid * ssid,const char * var)3393 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3394 {
3395 	size_t i;
3396 
3397 	if (ssid == NULL || var == NULL)
3398 		return NULL;
3399 
3400 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
3401 		const struct parse_data *field = &ssid_fields[i];
3402 		if (os_strcmp(var, field->name) == 0) {
3403 			char *ret = field->writer(field, ssid);
3404 
3405 			if (ret && has_newline(ret)) {
3406 				wpa_printf(MSG_ERROR,
3407 					   "Found newline in value for %s; not returning it",
3408 					   var);
3409 				os_free(ret);
3410 				ret = NULL;
3411 			}
3412 
3413 			return ret;
3414 		}
3415 	}
3416 
3417 	return NULL;
3418 }
3419 
3420 
3421 /**
3422  * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3423  * @ssid: Pointer to network configuration data
3424  * @var: Variable name, e.g., "ssid"
3425  * Returns: Value of the variable or %NULL on failure
3426  *
3427  * This function can be used to get network configuration variable like
3428  * wpa_config_get(). The only difference is that this functions does not expose
3429  * key/password material from the configuration. In case a key/password field
3430  * is requested, the returned value is an empty string or %NULL if the variable
3431  * is not set or "*" if the variable is set (regardless of its value). The
3432  * returned value is a copy of the configuration variable in text format, i.e,.
3433  * the same format that the text-based configuration file and wpa_config_set()
3434  * are using for the value. The caller is responsible for freeing the returned
3435  * value.
3436  */
wpa_config_get_no_key(struct wpa_ssid * ssid,const char * var)3437 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3438 {
3439 	size_t i;
3440 
3441 	if (ssid == NULL || var == NULL)
3442 		return NULL;
3443 
3444 	for (i = 0; i < NUM_SSID_FIELDS; i++) {
3445 		const struct parse_data *field = &ssid_fields[i];
3446 		if (os_strcmp(var, field->name) == 0) {
3447 			char *res = field->writer(field, ssid);
3448 			if (field->key_data) {
3449 				if (res && res[0]) {
3450 					wpa_printf(MSG_DEBUG, "Do not allow "
3451 						   "key_data field to be "
3452 						   "exposed");
3453 					str_clear_free(res);
3454 					return os_strdup("*");
3455 				}
3456 
3457 				os_free(res);
3458 				return NULL;
3459 			}
3460 			return res;
3461 		}
3462 	}
3463 
3464 	return NULL;
3465 }
3466 #endif /* NO_CONFIG_WRITE */
3467 
3468 
3469 /**
3470  * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3471  * @ssid: Pointer to network configuration data
3472  *
3473  * This function must be called to update WPA PSK when either SSID or the
3474  * passphrase has changed for the network configuration.
3475  */
wpa_config_update_psk(struct wpa_ssid * ssid)3476 void wpa_config_update_psk(struct wpa_ssid *ssid)
3477 {
3478 #ifndef CONFIG_NO_PBKDF2
3479 	pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3480 		    ssid->psk, PMK_LEN);
3481 	wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3482 			ssid->psk, PMK_LEN);
3483 	ssid->psk_set = 1;
3484 #endif /* CONFIG_NO_PBKDF2 */
3485 }
3486 
3487 #ifndef EXT_CODE_CROP
wpa_config_set_cred_req_conn_capab(struct wpa_cred * cred,const char * value)3488 static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3489 					      const char *value)
3490 {
3491 	u8 *proto;
3492 	int **port;
3493 	int *ports, *nports;
3494 	const char *pos;
3495 	unsigned int num_ports;
3496 
3497 	proto = os_realloc_array(cred->req_conn_capab_proto,
3498 				 cred->num_req_conn_capab + 1, sizeof(u8));
3499 	if (proto == NULL)
3500 		return -1;
3501 	cred->req_conn_capab_proto = proto;
3502 
3503 	port = os_realloc_array(cred->req_conn_capab_port,
3504 				cred->num_req_conn_capab + 1, sizeof(int *));
3505 	if (port == NULL)
3506 		return -1;
3507 	cred->req_conn_capab_port = port;
3508 
3509 	proto[cred->num_req_conn_capab] = atoi(value);
3510 
3511 	pos = os_strchr(value, ':');
3512 	if (pos == NULL) {
3513 		port[cred->num_req_conn_capab] = NULL;
3514 		cred->num_req_conn_capab++;
3515 		return 0;
3516 	}
3517 	pos++;
3518 
3519 	ports = NULL;
3520 	num_ports = 0;
3521 
3522 	while (*pos) {
3523 		nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3524 		if (nports == NULL) {
3525 			os_free(ports);
3526 			return -1;
3527 		}
3528 		ports = nports;
3529 		ports[num_ports++] = atoi(pos);
3530 
3531 		pos = os_strchr(pos, ',');
3532 		if (pos == NULL)
3533 			break;
3534 		pos++;
3535 	}
3536 
3537 	nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3538 	if (nports == NULL) {
3539 		os_free(ports);
3540 		return -1;
3541 	}
3542 	ports = nports;
3543 	ports[num_ports] = -1;
3544 
3545 	port[cred->num_req_conn_capab] = ports;
3546 	cred->num_req_conn_capab++;
3547 	return 0;
3548 }
3549 
3550 
wpa_config_set_cred_roaming_consortiums(struct wpa_cred * cred,const char * value)3551 static int wpa_config_set_cred_roaming_consortiums(struct wpa_cred *cred,
3552 						   const char *value)
3553 {
3554 	u8 roaming_consortiums[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3555 	size_t roaming_consortiums_len[MAX_ROAMING_CONS];
3556 	unsigned int num_roaming_consortiums = 0;
3557 	const char *pos, *end;
3558 	size_t len;
3559 
3560 	os_memset(roaming_consortiums, 0, sizeof(roaming_consortiums));
3561 	os_memset(roaming_consortiums_len, 0, sizeof(roaming_consortiums_len));
3562 
3563 	for (pos = value;;) {
3564 		end = os_strchr(pos, ',');
3565 		len = end ? (size_t) (end - pos) : os_strlen(pos);
3566 		if (!end && len == 0)
3567 			break;
3568 		if (len == 0 || (len & 1) != 0 ||
3569 		    len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3570 		    hexstr2bin(pos,
3571 			       roaming_consortiums[num_roaming_consortiums],
3572 			       len / 2) < 0) {
3573 			wpa_printf(MSG_INFO,
3574 				   "Invalid roaming_consortiums entry: %s",
3575 				   pos);
3576 			return -1;
3577 		}
3578 		roaming_consortiums_len[num_roaming_consortiums] = len / 2;
3579 		num_roaming_consortiums++;
3580 
3581 		if (!end)
3582 			break;
3583 
3584 		if (num_roaming_consortiums >= MAX_ROAMING_CONS) {
3585 			wpa_printf(MSG_INFO,
3586 				   "Too many roaming_consortiums OIs");
3587 			return -1;
3588 		}
3589 
3590 		pos = end + 1;
3591 	}
3592 
3593 	os_memcpy(cred->roaming_consortiums, roaming_consortiums,
3594 		  sizeof(roaming_consortiums));
3595 	os_memcpy(cred->roaming_consortiums_len, roaming_consortiums_len,
3596 		  sizeof(roaming_consortiums_len));
3597 	cred->num_roaming_consortiums = num_roaming_consortiums;
3598 
3599 	return 0;
3600 }
3601 
3602 
wpa_config_set_cred(struct wpa_cred * cred,const char * var,const char * value,int line)3603 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3604 			const char *value, int line)
3605 {
3606 	char *val;
3607 	size_t len;
3608 	int res;
3609 
3610 	if (os_strcmp(var, "temporary") == 0) {
3611 		cred->temporary = atoi(value);
3612 		return 0;
3613 	}
3614 
3615 	if (os_strcmp(var, "priority") == 0) {
3616 		cred->priority = atoi(value);
3617 		return 0;
3618 	}
3619 
3620 	if (os_strcmp(var, "sp_priority") == 0) {
3621 		int prio = atoi(value);
3622 		if (prio < 0 || prio > 255)
3623 			return -1;
3624 		cred->sp_priority = prio;
3625 		return 0;
3626 	}
3627 
3628 	if (os_strcmp(var, "pcsc") == 0) {
3629 		cred->pcsc = atoi(value);
3630 		return 0;
3631 	}
3632 
3633 	if (os_strcmp(var, "eap") == 0) {
3634 		struct eap_method_type method;
3635 		method.method = eap_peer_get_type(value, &method.vendor);
3636 		if (method.vendor == EAP_VENDOR_IETF &&
3637 		    method.method == EAP_TYPE_NONE) {
3638 			wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3639 				   "for a credential", line, value);
3640 			return -1;
3641 		}
3642 		os_free(cred->eap_method);
3643 		cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3644 		if (cred->eap_method == NULL)
3645 			return -1;
3646 		os_memcpy(cred->eap_method, &method, sizeof(method));
3647 		return 0;
3648 	}
3649 
3650 	if (os_strcmp(var, "password") == 0 &&
3651 	    os_strncmp(value, "ext:", 4) == 0) {
3652 		if (has_newline(value))
3653 			return -1;
3654 		str_clear_free(cred->password);
3655 		cred->password = os_strdup(value);
3656 		cred->ext_password = 1;
3657 		return 0;
3658 	}
3659 
3660 	if (os_strcmp(var, "update_identifier") == 0) {
3661 		cred->update_identifier = atoi(value);
3662 		return 0;
3663 	}
3664 
3665 	if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3666 		cred->min_dl_bandwidth_home = atoi(value);
3667 		return 0;
3668 	}
3669 
3670 	if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3671 		cred->min_ul_bandwidth_home = atoi(value);
3672 		return 0;
3673 	}
3674 
3675 	if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3676 		cred->min_dl_bandwidth_roaming = atoi(value);
3677 		return 0;
3678 	}
3679 
3680 	if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3681 		cred->min_ul_bandwidth_roaming = atoi(value);
3682 		return 0;
3683 	}
3684 
3685 	if (os_strcmp(var, "max_bss_load") == 0) {
3686 		cred->max_bss_load = atoi(value);
3687 		return 0;
3688 	}
3689 
3690 	if (os_strcmp(var, "req_conn_capab") == 0)
3691 		return wpa_config_set_cred_req_conn_capab(cred, value);
3692 
3693 	if (os_strcmp(var, "ocsp") == 0) {
3694 		cred->ocsp = atoi(value);
3695 		return 0;
3696 	}
3697 
3698 	if (os_strcmp(var, "sim_num") == 0) {
3699 		cred->sim_num = atoi(value);
3700 		return 0;
3701 	}
3702 
3703 	if (os_strcmp(var, "engine") == 0) {
3704 		cred->engine = atoi(value);
3705 		return 0;
3706 	}
3707 
3708 	val = wpa_config_parse_string(value, &len);
3709 	if (val == NULL ||
3710 	    (os_strcmp(var, "excluded_ssid") != 0 &&
3711 	     os_strcmp(var, "roaming_consortium") != 0 &&
3712 	     os_strcmp(var, "required_roaming_consortium") != 0 &&
3713 	     has_newline(val))) {
3714 		wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3715 			   "value '%s'.", line, var, value);
3716 		os_free(val);
3717 		return -1;
3718 	}
3719 
3720 	if (os_strcmp(var, "realm") == 0) {
3721 		os_free(cred->realm);
3722 		cred->realm = val;
3723 		return 0;
3724 	}
3725 
3726 	if (os_strcmp(var, "username") == 0) {
3727 		str_clear_free(cred->username);
3728 		cred->username = val;
3729 		return 0;
3730 	}
3731 
3732 	if (os_strcmp(var, "password") == 0) {
3733 		str_clear_free(cred->password);
3734 		cred->password = val;
3735 		cred->ext_password = 0;
3736 		return 0;
3737 	}
3738 
3739 	if (os_strcmp(var, "ca_cert") == 0) {
3740 		os_free(cred->ca_cert);
3741 		cred->ca_cert = val;
3742 		return 0;
3743 	}
3744 
3745 	if (os_strcmp(var, "client_cert") == 0) {
3746 		os_free(cred->client_cert);
3747 		cred->client_cert = val;
3748 		return 0;
3749 	}
3750 
3751 	if (os_strcmp(var, "private_key") == 0) {
3752 		os_free(cred->private_key);
3753 		cred->private_key = val;
3754 		return 0;
3755 	}
3756 
3757 	if (os_strcmp(var, "private_key_passwd") == 0) {
3758 		str_clear_free(cred->private_key_passwd);
3759 		cred->private_key_passwd = val;
3760 		return 0;
3761 	}
3762 
3763 	if (os_strcmp(var, "engine_id") == 0) {
3764 		os_free(cred->engine_id);
3765 		cred->engine_id = val;
3766 		return 0;
3767 	}
3768 
3769 	if (os_strcmp(var, "ca_cert_id") == 0) {
3770 		os_free(cred->ca_cert_id);
3771 		cred->ca_cert_id = val;
3772 		return 0;
3773 	}
3774 
3775 	if (os_strcmp(var, "cert_id") == 0) {
3776 		os_free(cred->cert_id);
3777 		cred->cert_id = val;
3778 		return 0;
3779 	}
3780 
3781 	if (os_strcmp(var, "key_id") == 0) {
3782 		os_free(cred->key_id);
3783 		cred->key_id = val;
3784 		return 0;
3785 	}
3786 
3787 	if (os_strcmp(var, "imsi") == 0) {
3788 		os_free(cred->imsi);
3789 		cred->imsi = val;
3790 		return 0;
3791 	}
3792 
3793 	if (os_strcmp(var, "milenage") == 0) {
3794 		str_clear_free(cred->milenage);
3795 		cred->milenage = val;
3796 		return 0;
3797 	}
3798 
3799 	if (os_strcmp(var, "domain_suffix_match") == 0) {
3800 		os_free(cred->domain_suffix_match);
3801 		cred->domain_suffix_match = val;
3802 		return 0;
3803 	}
3804 
3805 	if (os_strcmp(var, "domain") == 0) {
3806 		char **new_domain;
3807 		new_domain = os_realloc_array(cred->domain,
3808 					      cred->num_domain + 1,
3809 					      sizeof(char *));
3810 		if (new_domain == NULL) {
3811 			os_free(val);
3812 			return -1;
3813 		}
3814 		new_domain[cred->num_domain++] = val;
3815 		cred->domain = new_domain;
3816 		return 0;
3817 	}
3818 
3819 	if (os_strcmp(var, "phase1") == 0) {
3820 		os_free(cred->phase1);
3821 		cred->phase1 = val;
3822 		return 0;
3823 	}
3824 
3825 	if (os_strcmp(var, "phase2") == 0) {
3826 		os_free(cred->phase2);
3827 		cred->phase2 = val;
3828 		return 0;
3829 	}
3830 
3831 	if (os_strcmp(var, "roaming_consortium") == 0) {
3832 		if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3833 			wpa_printf(MSG_ERROR, "Line %d: invalid "
3834 				   "roaming_consortium length %d (3..15 "
3835 				   "expected)", line, (int) len);
3836 			os_free(val);
3837 			return -1;
3838 		}
3839 		os_memcpy(cred->roaming_consortium, val, len);
3840 		cred->roaming_consortium_len = len;
3841 		os_free(val);
3842 		return 0;
3843 	}
3844 
3845 	if (os_strcmp(var, "required_roaming_consortium") == 0) {
3846 		if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3847 		{
3848 			wpa_printf(MSG_ERROR, "Line %d: invalid "
3849 				   "required_roaming_consortium length %d "
3850 				   "(3..15 expected)", line, (int) len);
3851 			os_free(val);
3852 			return -1;
3853 		}
3854 		os_memcpy(cred->required_roaming_consortium, val, len);
3855 		cred->required_roaming_consortium_len = len;
3856 		os_free(val);
3857 		return 0;
3858 	}
3859 
3860 	if (os_strcmp(var, "roaming_consortiums") == 0) {
3861 		res = wpa_config_set_cred_roaming_consortiums(cred, val);
3862 		if (res < 0)
3863 			wpa_printf(MSG_ERROR,
3864 				   "Line %d: invalid roaming_consortiums",
3865 				   line);
3866 		os_free(val);
3867 		return res;
3868 	}
3869 
3870 	if (os_strcmp(var, "excluded_ssid") == 0) {
3871 		struct excluded_ssid *e;
3872 
3873 		if (len > SSID_MAX_LEN) {
3874 			wpa_printf(MSG_ERROR, "Line %d: invalid "
3875 				   "excluded_ssid length %d", line, (int) len);
3876 			os_free(val);
3877 			return -1;
3878 		}
3879 
3880 		e = os_realloc_array(cred->excluded_ssid,
3881 				     cred->num_excluded_ssid + 1,
3882 				     sizeof(struct excluded_ssid));
3883 		if (e == NULL) {
3884 			os_free(val);
3885 			return -1;
3886 		}
3887 		cred->excluded_ssid = e;
3888 
3889 		e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3890 		os_memcpy(e->ssid, val, len);
3891 		e->ssid_len = len;
3892 
3893 		os_free(val);
3894 
3895 		return 0;
3896 	}
3897 
3898 	if (os_strcmp(var, "roaming_partner") == 0) {
3899 		struct roaming_partner *p;
3900 		char *pos;
3901 
3902 		p = os_realloc_array(cred->roaming_partner,
3903 				     cred->num_roaming_partner + 1,
3904 				     sizeof(struct roaming_partner));
3905 		if (p == NULL) {
3906 			os_free(val);
3907 			return -1;
3908 		}
3909 		cred->roaming_partner = p;
3910 
3911 		p = &cred->roaming_partner[cred->num_roaming_partner];
3912 
3913 		pos = os_strchr(val, ',');
3914 		if (pos == NULL) {
3915 			os_free(val);
3916 			return -1;
3917 		}
3918 		*pos++ = '\0';
3919 		if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3920 			os_free(val);
3921 			return -1;
3922 		}
3923 		os_memcpy(p->fqdn, val, pos - val);
3924 
3925 		p->exact_match = atoi(pos);
3926 
3927 		pos = os_strchr(pos, ',');
3928 		if (pos == NULL) {
3929 			os_free(val);
3930 			return -1;
3931 		}
3932 		*pos++ = '\0';
3933 
3934 		p->priority = atoi(pos);
3935 
3936 		pos = os_strchr(pos, ',');
3937 		if (pos == NULL) {
3938 			os_free(val);
3939 			return -1;
3940 		}
3941 		*pos++ = '\0';
3942 
3943 		if (os_strlen(pos) >= sizeof(p->country)) {
3944 			os_free(val);
3945 			return -1;
3946 		}
3947 		os_memcpy(p->country, pos, os_strlen(pos) + 1);
3948 
3949 		cred->num_roaming_partner++;
3950 		os_free(val);
3951 
3952 		return 0;
3953 	}
3954 
3955 	if (os_strcmp(var, "provisioning_sp") == 0) {
3956 		os_free(cred->provisioning_sp);
3957 		cred->provisioning_sp = val;
3958 		return 0;
3959 	}
3960 
3961 	if (line) {
3962 		wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3963 			   line, var);
3964 	}
3965 
3966 	os_free(val);
3967 
3968 	return -1;
3969 }
3970 
3971 
alloc_int_str(int val)3972 static char * alloc_int_str(int val)
3973 {
3974 	const unsigned int bufsize = 20;
3975 	char *buf;
3976 	int res;
3977 
3978 	buf = os_malloc(bufsize);
3979 	if (buf == NULL)
3980 		return NULL;
3981 	res = os_snprintf(buf, bufsize, "%d", val);
3982 	if (os_snprintf_error(bufsize, res)) {
3983 		os_free(buf);
3984 		buf = NULL;
3985 	}
3986 	return buf;
3987 }
3988 
3989 
alloc_strdup(const char * str)3990 static char * alloc_strdup(const char *str)
3991 {
3992 	if (str == NULL)
3993 		return NULL;
3994 	return os_strdup(str);
3995 }
3996 
3997 
wpa_config_get_cred_no_key(struct wpa_cred * cred,const char * var)3998 char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3999 {
4000 	if (os_strcmp(var, "temporary") == 0)
4001 		return alloc_int_str(cred->temporary);
4002 
4003 	if (os_strcmp(var, "priority") == 0)
4004 		return alloc_int_str(cred->priority);
4005 
4006 	if (os_strcmp(var, "sp_priority") == 0)
4007 		return alloc_int_str(cred->sp_priority);
4008 
4009 	if (os_strcmp(var, "pcsc") == 0)
4010 		return alloc_int_str(cred->pcsc);
4011 
4012 	if (os_strcmp(var, "eap") == 0) {
4013 		if (!cred->eap_method)
4014 			return NULL;
4015 		return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4016 						 cred->eap_method[0].method));
4017 	}
4018 
4019 	if (os_strcmp(var, "update_identifier") == 0)
4020 		return alloc_int_str(cred->update_identifier);
4021 
4022 	if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4023 		return alloc_int_str(cred->min_dl_bandwidth_home);
4024 
4025 	if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4026 		return alloc_int_str(cred->min_ul_bandwidth_home);
4027 
4028 	if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4029 		return alloc_int_str(cred->min_dl_bandwidth_roaming);
4030 
4031 	if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4032 		return alloc_int_str(cred->min_ul_bandwidth_roaming);
4033 
4034 	if (os_strcmp(var, "max_bss_load") == 0)
4035 		return alloc_int_str(cred->max_bss_load);
4036 
4037 	if (os_strcmp(var, "req_conn_capab") == 0) {
4038 		unsigned int i;
4039 		char *buf, *end, *pos;
4040 		int ret;
4041 
4042 		if (!cred->num_req_conn_capab)
4043 			return NULL;
4044 
4045 		buf = os_malloc(4000);
4046 		if (buf == NULL)
4047 			return NULL;
4048 		pos = buf;
4049 		end = pos + 4000;
4050 		for (i = 0; i < cred->num_req_conn_capab; i++) {
4051 			int *ports;
4052 
4053 			ret = os_snprintf(pos, end - pos, "%s%u",
4054 					  i > 0 ? "\n" : "",
4055 					  cred->req_conn_capab_proto[i]);
4056 			if (os_snprintf_error(end - pos, ret))
4057 				return buf;
4058 			pos += ret;
4059 
4060 			ports = cred->req_conn_capab_port[i];
4061 			if (ports) {
4062 				int j;
4063 				for (j = 0; ports[j] != -1; j++) {
4064 					ret = os_snprintf(pos, end - pos,
4065 							  "%s%d",
4066 							  j > 0 ? "," : ":",
4067 							  ports[j]);
4068 					if (os_snprintf_error(end - pos, ret))
4069 						return buf;
4070 					pos += ret;
4071 				}
4072 			}
4073 		}
4074 
4075 		return buf;
4076 	}
4077 
4078 	if (os_strcmp(var, "ocsp") == 0)
4079 		return alloc_int_str(cred->ocsp);
4080 
4081 	if (os_strcmp(var, "realm") == 0)
4082 		return alloc_strdup(cred->realm);
4083 
4084 	if (os_strcmp(var, "username") == 0)
4085 		return alloc_strdup(cred->username);
4086 
4087 	if (os_strcmp(var, "password") == 0) {
4088 		if (!cred->password)
4089 			return NULL;
4090 		return alloc_strdup("*");
4091 	}
4092 
4093 	if (os_strcmp(var, "ca_cert") == 0)
4094 		return alloc_strdup(cred->ca_cert);
4095 
4096 	if (os_strcmp(var, "client_cert") == 0)
4097 		return alloc_strdup(cred->client_cert);
4098 
4099 	if (os_strcmp(var, "private_key") == 0)
4100 		return alloc_strdup(cred->private_key);
4101 
4102 	if (os_strcmp(var, "private_key_passwd") == 0) {
4103 		if (!cred->private_key_passwd)
4104 			return NULL;
4105 		return alloc_strdup("*");
4106 	}
4107 
4108 	if (os_strcmp(var, "imsi") == 0)
4109 		return alloc_strdup(cred->imsi);
4110 
4111 	if (os_strcmp(var, "milenage") == 0) {
4112 		if (!(cred->milenage))
4113 			return NULL;
4114 		return alloc_strdup("*");
4115 	}
4116 
4117 	if (os_strcmp(var, "domain_suffix_match") == 0)
4118 		return alloc_strdup(cred->domain_suffix_match);
4119 
4120 	if (os_strcmp(var, "domain") == 0) {
4121 		unsigned int i;
4122 		char *buf, *end, *pos;
4123 		int ret;
4124 
4125 		if (!cred->num_domain)
4126 			return NULL;
4127 
4128 		buf = os_malloc(4000);
4129 		if (buf == NULL)
4130 			return NULL;
4131 		pos = buf;
4132 		end = pos + 4000;
4133 
4134 		for (i = 0; i < cred->num_domain; i++) {
4135 			ret = os_snprintf(pos, end - pos, "%s%s",
4136 					  i > 0 ? "\n" : "", cred->domain[i]);
4137 			if (os_snprintf_error(end - pos, ret))
4138 				return buf;
4139 			pos += ret;
4140 		}
4141 
4142 		return buf;
4143 	}
4144 
4145 	if (os_strcmp(var, "phase1") == 0)
4146 		return alloc_strdup(cred->phase1);
4147 
4148 	if (os_strcmp(var, "phase2") == 0)
4149 		return alloc_strdup(cred->phase2);
4150 
4151 	if (os_strcmp(var, "roaming_consortium") == 0) {
4152 		size_t buflen;
4153 		char *buf;
4154 
4155 		if (!cred->roaming_consortium_len)
4156 			return NULL;
4157 		buflen = cred->roaming_consortium_len * 2 + 1;
4158 		buf = os_malloc(buflen);
4159 		if (buf == NULL)
4160 			return NULL;
4161 		wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
4162 				 cred->roaming_consortium_len);
4163 		return buf;
4164 	}
4165 
4166 	if (os_strcmp(var, "required_roaming_consortium") == 0) {
4167 		size_t buflen;
4168 		char *buf;
4169 
4170 		if (!cred->required_roaming_consortium_len)
4171 			return NULL;
4172 		buflen = cred->required_roaming_consortium_len * 2 + 1;
4173 		buf = os_malloc(buflen);
4174 		if (buf == NULL)
4175 			return NULL;
4176 		wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
4177 				 cred->required_roaming_consortium_len);
4178 		return buf;
4179 	}
4180 
4181 	if (os_strcmp(var, "roaming_consortiums") == 0) {
4182 		size_t buflen;
4183 		char *buf, *pos;
4184 		size_t i;
4185 
4186 		if (!cred->num_roaming_consortiums)
4187 			return NULL;
4188 		buflen = cred->num_roaming_consortiums *
4189 			MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4190 		buf = os_malloc(buflen);
4191 		if (!buf)
4192 			return NULL;
4193 		pos = buf;
4194 		for (i = 0; i < cred->num_roaming_consortiums; i++) {
4195 			if (i > 0)
4196 				*pos++ = ',';
4197 			pos += wpa_snprintf_hex(
4198 				pos, buf + buflen - pos,
4199 				cred->roaming_consortiums[i],
4200 				cred->roaming_consortiums_len[i]);
4201 		}
4202 		*pos = '\0';
4203 		return buf;
4204 	}
4205 
4206 	if (os_strcmp(var, "excluded_ssid") == 0) {
4207 		unsigned int i;
4208 		char *buf, *end, *pos;
4209 
4210 		if (!cred->num_excluded_ssid)
4211 			return NULL;
4212 
4213 		buf = os_malloc(4000);
4214 		if (buf == NULL)
4215 			return NULL;
4216 		pos = buf;
4217 		end = pos + 4000;
4218 
4219 		for (i = 0; i < cred->num_excluded_ssid; i++) {
4220 			struct excluded_ssid *e;
4221 			int ret;
4222 
4223 			e = &cred->excluded_ssid[i];
4224 			ret = os_snprintf(pos, end - pos, "%s%s",
4225 					  i > 0 ? "\n" : "",
4226 					  wpa_ssid_txt(e->ssid, e->ssid_len));
4227 			if (os_snprintf_error(end - pos, ret))
4228 				return buf;
4229 			pos += ret;
4230 		}
4231 
4232 		return buf;
4233 	}
4234 
4235 	if (os_strcmp(var, "roaming_partner") == 0) {
4236 		unsigned int i;
4237 		char *buf, *end, *pos;
4238 
4239 		if (!cred->num_roaming_partner)
4240 			return NULL;
4241 
4242 		buf = os_malloc(4000);
4243 		if (buf == NULL)
4244 			return NULL;
4245 		pos = buf;
4246 		end = pos + 4000;
4247 
4248 		for (i = 0; i < cred->num_roaming_partner; i++) {
4249 			struct roaming_partner *p;
4250 			int ret;
4251 
4252 			p = &cred->roaming_partner[i];
4253 			ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4254 					  i > 0 ? "\n" : "",
4255 					  p->fqdn, p->exact_match, p->priority,
4256 					  p->country);
4257 			if (os_snprintf_error(end - pos, ret))
4258 				return buf;
4259 			pos += ret;
4260 		}
4261 
4262 		return buf;
4263 	}
4264 
4265 	if (os_strcmp(var, "provisioning_sp") == 0)
4266 		return alloc_strdup(cred->provisioning_sp);
4267 
4268 	return NULL;
4269 }
4270 
4271 
wpa_config_get_cred(struct wpa_config * config,int id)4272 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4273 {
4274 	struct wpa_cred *cred;
4275 
4276 	cred = config->cred;
4277 	while (cred) {
4278 		if (id == cred->id)
4279 			break;
4280 		cred = cred->next;
4281 	}
4282 
4283 	return cred;
4284 }
4285 
4286 
wpa_config_add_cred(struct wpa_config * config)4287 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4288 {
4289 	int id;
4290 	struct wpa_cred *cred, *last = NULL;
4291 
4292 	id = -1;
4293 	cred = config->cred;
4294 	while (cred) {
4295 		if (cred->id > id)
4296 			id = cred->id;
4297 		last = cred;
4298 		cred = cred->next;
4299 	}
4300 	id++;
4301 
4302 	cred = os_zalloc(sizeof(*cred));
4303 	if (cred == NULL)
4304 		return NULL;
4305 	cred->id = id;
4306 	cred->sim_num = DEFAULT_USER_SELECTED_SIM;
4307 	if (last)
4308 		last->next = cred;
4309 	else
4310 		config->cred = cred;
4311 
4312 	return cred;
4313 }
4314 
4315 
wpa_config_remove_cred(struct wpa_config * config,int id)4316 int wpa_config_remove_cred(struct wpa_config *config, int id)
4317 {
4318 	struct wpa_cred *cred, *prev = NULL;
4319 
4320 	cred = config->cred;
4321 	while (cred) {
4322 		if (id == cred->id)
4323 			break;
4324 		prev = cred;
4325 		cred = cred->next;
4326 	}
4327 
4328 	if (cred == NULL)
4329 		return -1;
4330 
4331 	if (prev)
4332 		prev->next = cred->next;
4333 	else
4334 		config->cred = cred->next;
4335 
4336 	wpa_config_free_cred(cred);
4337 	return 0;
4338 }
4339 #endif /* EXT_CODE_CROP */
4340 
4341 #ifndef CONFIG_NO_CONFIG_BLOBS
4342 /**
4343  * wpa_config_get_blob - Get a named configuration blob
4344  * @config: Configuration data from wpa_config_read()
4345  * @name: Name of the blob
4346  * Returns: Pointer to blob data or %NULL if not found
4347  */
wpa_config_get_blob(struct wpa_config * config,const char * name)4348 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4349 						   const char *name)
4350 {
4351 	struct wpa_config_blob *blob = config->blobs;
4352 
4353 	while (blob) {
4354 		if (os_strcmp(blob->name, name) == 0)
4355 			return blob;
4356 		blob = blob->next;
4357 	}
4358 	return NULL;
4359 }
4360 
4361 
4362 /**
4363  * wpa_config_set_blob - Set or add a named configuration blob
4364  * @config: Configuration data from wpa_config_read()
4365  * @blob: New value for the blob
4366  *
4367  * Adds a new configuration blob or replaces the current value of an existing
4368  * blob.
4369  */
wpa_config_set_blob(struct wpa_config * config,struct wpa_config_blob * blob)4370 void wpa_config_set_blob(struct wpa_config *config,
4371 			 struct wpa_config_blob *blob)
4372 {
4373 	wpa_config_remove_blob(config, blob->name);
4374 	blob->next = config->blobs;
4375 	config->blobs = blob;
4376 }
4377 
4378 
4379 /**
4380  * wpa_config_free_blob - Free blob data
4381  * @blob: Pointer to blob to be freed
4382  */
wpa_config_free_blob(struct wpa_config_blob * blob)4383 void wpa_config_free_blob(struct wpa_config_blob *blob)
4384 {
4385 	if (blob) {
4386 		os_free(blob->name);
4387 		bin_clear_free(blob->data, blob->len);
4388 		os_free(blob);
4389 	}
4390 }
4391 
4392 
4393 /**
4394  * wpa_config_remove_blob - Remove a named configuration blob
4395  * @config: Configuration data from wpa_config_read()
4396  * @name: Name of the blob to remove
4397  * Returns: 0 if blob was removed or -1 if blob was not found
4398  */
wpa_config_remove_blob(struct wpa_config * config,const char * name)4399 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4400 {
4401 	struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4402 
4403 	while (pos) {
4404 		if (os_strcmp(pos->name, name) == 0) {
4405 			if (prev)
4406 				prev->next = pos->next;
4407 			else
4408 				config->blobs = pos->next;
4409 			wpa_config_free_blob(pos);
4410 			return 0;
4411 		}
4412 		prev = pos;
4413 		pos = pos->next;
4414 	}
4415 
4416 	return -1;
4417 }
4418 #endif /* CONFIG_NO_CONFIG_BLOBS */
4419 
4420 
4421 /**
4422  * wpa_config_alloc_empty - Allocate an empty configuration
4423  * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4424  * socket
4425  * @driver_param: Driver parameters
4426  * Returns: Pointer to allocated configuration data or %NULL on failure
4427  */
wpa_config_alloc_empty(const char * ctrl_interface,const char * driver_param)4428 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4429 					   const char *driver_param)
4430 {
4431 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
4432 
4433 	struct wpa_config *config;
4434 	const int aCWmin = 4, aCWmax = 10;
4435 	const struct hostapd_wmm_ac_params ac_bk =
4436 		{ aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4437 	const struct hostapd_wmm_ac_params ac_be =
4438 		{ aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4439 	const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
4440 		{ aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
4441 	const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
4442 		{ aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4443 	const struct hostapd_tx_queue_params txq_bk =
4444 		{ 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4445 	const struct hostapd_tx_queue_params txq_be =
4446 		{ 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4447 	const struct hostapd_tx_queue_params txq_vi =
4448 		{ 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4449 	const struct hostapd_tx_queue_params txq_vo =
4450 		{ 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4451 		  (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4452 #undef ecw2cw
4453 
4454 	config = os_zalloc(sizeof(*config));
4455 	if (config == NULL)
4456 		return NULL;
4457 	config->eapol_version = DEFAULT_EAPOL_VERSION;
4458 	config->ap_scan = DEFAULT_AP_SCAN;
4459 	config->user_mpm = DEFAULT_USER_MPM;
4460 	config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
4461 	config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
4462 	config->mesh_fwding = DEFAULT_MESH_FWDING;
4463 	config->dot11RSNASAERetransPeriod =
4464 		DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
4465 	config->fast_reauth = DEFAULT_FAST_REAUTH;
4466 	config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4467 	config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
4468 	config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
4469 	config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
4470 	config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
4471 	config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
4472 	config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4473 	config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4474 	config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4475 	config->max_num_sta = DEFAULT_MAX_NUM_STA;
4476 	config->ap_isolate = DEFAULT_AP_ISOLATE;
4477 	config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
4478 	config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
4479 	config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
4480 	config->wmm_ac_params[0] = ac_be;
4481 	config->wmm_ac_params[1] = ac_bk;
4482 	config->wmm_ac_params[2] = ac_vi;
4483 	config->wmm_ac_params[3] = ac_vo;
4484 	config->tx_queue[0] = txq_vo;
4485 	config->tx_queue[1] = txq_vi;
4486 	config->tx_queue[2] = txq_be;
4487 	config->tx_queue[3] = txq_bk;
4488 	config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
4489 	config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
4490 	config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
4491 	config->cert_in_cb = DEFAULT_CERT_IN_CB;
4492 	config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
4493 	config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
4494 #ifdef LOS_WPA_PATCH
4495 	/* add default pmf setting for soc. */
4496 	config->pmf = MGMT_FRAME_PROTECTION_OPTIONAL;
4497 	if (g_sta_opt_set.usr_pmf_set_flag == WPA_FLAG_ON) {
4498 		config->pmf = g_sta_opt_set.pmf;
4499 	} else {
4500 		g_sta_opt_set.pmf = config->pmf;
4501 	}
4502 #ifdef CONFIG_WPA3
4503 	if (g_sta_opt_set.sae_pwe <= WIFI_SAE_PWE_UNSPECIFIED) {
4504 		config->sae_pwe = WIFI_SAE_PWE_BOTH - 1;
4505 	} else {
4506 		config->sae_pwe = g_sta_opt_set.sae_pwe - 1;
4507 	}
4508 	if (g_sta_opt_set.sae_groups != NULL && g_sta_opt_set.sae_groups[0] != 0) {
4509 		config->sae_groups = g_sta_opt_set.sae_groups;
4510 	}
4511 #endif /* CONFIG_WPA3 */
4512 #endif /* LOS_WPA_PATCH */
4513 
4514 #ifdef CONFIG_MBO
4515 	config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
4516 	config->disassoc_imminent_rssi_threshold =
4517 		DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4518 	config->oce = DEFAULT_OCE_SUPPORT;
4519 #endif /* CONFIG_MBO */
4520 
4521 	if (ctrl_interface)
4522 		config->ctrl_interface = os_strdup(ctrl_interface);
4523 	if (driver_param)
4524 		config->driver_param = os_strdup(driver_param);
4525 	config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
4526 
4527 	return config;
4528 }
4529 
4530 
4531 #ifndef CONFIG_NO_STDOUT_DEBUG
4532 /**
4533  * wpa_config_debug_dump_networks - Debug dump of configured networks
4534  * @config: Configuration data from wpa_config_read()
4535  */
wpa_config_debug_dump_networks(struct wpa_config * config)4536 void wpa_config_debug_dump_networks(struct wpa_config *config)
4537 {
4538 	size_t prio;
4539 	struct wpa_ssid *ssid;
4540 
4541 	for (prio = 0; prio < config->num_prio; prio++) {
4542 		ssid = config->pssid[prio];
4543 		wpa_printf(MSG_DEBUG, "Priority group %d",
4544 			   ssid->priority);
4545 		while (ssid) {
4546 			wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
4547 				   ssid->id,
4548 				   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4549 			ssid = ssid->pnext;
4550 		}
4551 	}
4552 }
4553 #endif /* CONFIG_NO_STDOUT_DEBUG */
4554 
4555 #ifdef EXT_WPA_GLOBAL_CONFIG
4556 /**
4557  * Structure for global configuration parsing. This data is used to implement a
4558  * generic parser for the global interface configuration. The table of variables
4559  * is defined below in this file (global_fields[]).
4560  */
4561 struct global_parse_data {
4562 	/* Configuration variable name */
4563 	char *name;
4564 
4565 	/* Parser function for this variable. The parser functions return 0 or 1
4566 	 * to indicate success. Value 0 indicates that the parameter value may
4567 	 * have changed while value 1 means that the value did not change.
4568 	 * Error cases (failure to parse the string) are indicated by returning
4569 	 * -1. */
4570 	int (*parser)(const struct global_parse_data *data,
4571 		      struct wpa_config *config, int line, const char *value);
4572 
4573 	/* Getter function to print the variable in text format to buf. */
4574 	int (*get)(const char *name, struct wpa_config *config, long offset,
4575 		   char *buf, size_t buflen, int pretty_print);
4576 
4577 	/* Variable specific parameters for the parser. */
4578 	void *param1, *param2, *param3;
4579 
4580 	/* Indicates which configuration variable has changed. */
4581 	unsigned int changed_flag;
4582 };
4583 
4584 
wpa_global_config_parse_int(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4585 static int wpa_global_config_parse_int(const struct global_parse_data *data,
4586 				       struct wpa_config *config, int line,
4587 				       const char *pos)
4588 {
4589 	int val, *dst;
4590 	char *end;
4591 	bool same;
4592 
4593 	dst = (int *) (((u8 *) config) + (long) data->param1);
4594 	val = strtol(pos, &end, 0);
4595 	if (*end) {
4596 		wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4597 			   line, pos);
4598 		return -1;
4599 	}
4600 	same = *dst == val;
4601 	*dst = val;
4602 
4603 	wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4604 
4605 	if (data->param2 && *dst < (long) data->param2) {
4606 		wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4607 			   "min_value=%ld)", line, data->name, *dst,
4608 			   (long) data->param2);
4609 		*dst = (long) data->param2;
4610 		return -1;
4611 	}
4612 
4613 	if (data->param3 && *dst > (long) data->param3) {
4614 		wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4615 			   "max_value=%ld)", line, data->name, *dst,
4616 			   (long) data->param3);
4617 		*dst = (long) data->param3;
4618 		return -1;
4619 	}
4620 
4621 	return same;
4622 }
4623 
4624 
wpa_global_config_parse_str(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4625 static int wpa_global_config_parse_str(const struct global_parse_data *data,
4626 				       struct wpa_config *config, int line,
4627 				       const char *pos)
4628 {
4629 	size_t len, prev_len;
4630 	char **dst, *tmp;
4631 
4632 	len = os_strlen(pos);
4633 	if (data->param2 && len < (size_t) data->param2) {
4634 		wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4635 			   "min_len=%ld)", line, data->name,
4636 			   (unsigned long) len, (long) data->param2);
4637 		return -1;
4638 	}
4639 
4640 	if (data->param3 && len > (size_t) data->param3) {
4641 		wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4642 			   "max_len=%ld)", line, data->name,
4643 			   (unsigned long) len, (long) data->param3);
4644 		return -1;
4645 	}
4646 
4647 	if (has_newline(pos)) {
4648 		wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4649 			   line, data->name);
4650 		return -1;
4651 	}
4652 
4653 	dst = (char **) (((u8 *) config) + (long) data->param1);
4654 	if (*dst)
4655 		prev_len = os_strlen(*dst);
4656 	else
4657 		prev_len = 0;
4658 
4659 	/* No change to the previously configured value */
4660 	if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
4661 		return 1;
4662 
4663 	tmp = os_strdup(pos);
4664 	if (tmp == NULL)
4665 		return -1;
4666 
4667 	os_free(*dst);
4668 	*dst = tmp;
4669 	wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4670 
4671 	return 0;
4672 }
4673 
4674 
wpa_config_process_bgscan(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4675 static int wpa_config_process_bgscan(const struct global_parse_data *data,
4676 				     struct wpa_config *config, int line,
4677 				     const char *pos)
4678 {
4679 	size_t len;
4680 	char *tmp;
4681 	int res;
4682 
4683 	tmp = wpa_config_parse_string(pos, &len);
4684 	if (tmp == NULL) {
4685 		wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4686 			   line, data->name);
4687 		return -1;
4688 	}
4689 
4690 	res = wpa_global_config_parse_str(data, config, line, tmp);
4691 	os_free(tmp);
4692 	return res;
4693 }
4694 
4695 
wpa_global_config_parse_bin(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4696 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4697 				       struct wpa_config *config, int line,
4698 				       const char *pos)
4699 {
4700 	struct wpabuf **dst, *tmp;
4701 
4702 	tmp = wpabuf_parse_bin(pos);
4703 	if (!tmp)
4704 		return -1;
4705 
4706 	dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
4707 	if (wpabuf_cmp(*dst, tmp) == 0) {
4708 		wpabuf_free(tmp);
4709 		return 1;
4710 	}
4711 	wpabuf_free(*dst);
4712 	*dst = tmp;
4713 	wpa_printf(MSG_DEBUG, "%s", data->name);
4714 
4715 	return 0;
4716 }
4717 
4718 
wpa_config_process_freq_list(const struct global_parse_data * data,struct wpa_config * config,int line,const char * value)4719 static int wpa_config_process_freq_list(const struct global_parse_data *data,
4720 					struct wpa_config *config, int line,
4721 					const char *value)
4722 {
4723 	int *freqs;
4724 
4725 	freqs = wpa_config_parse_int_array(value);
4726 	if (freqs == NULL)
4727 		return -1;
4728 	if (freqs[0] == 0) {
4729 		os_free(freqs);
4730 		freqs = NULL;
4731 	}
4732 	os_free(config->freq_list);
4733 	config->freq_list = freqs;
4734 	return 0;
4735 }
4736 
4737 
4738 static int
wpa_config_process_initial_freq_list(const struct global_parse_data * data,struct wpa_config * config,int line,const char * value)4739 wpa_config_process_initial_freq_list(const struct global_parse_data *data,
4740 				     struct wpa_config *config, int line,
4741 				     const char *value)
4742 {
4743 	int *freqs;
4744 
4745 	freqs = wpa_config_parse_int_array(value);
4746 	if (!freqs)
4747 		return -1;
4748 	if (freqs[0] == 0) {
4749 		os_free(freqs);
4750 		freqs = NULL;
4751 	}
4752 	os_free(config->initial_freq_list);
4753 	config->initial_freq_list = freqs;
4754 	return 0;
4755 }
4756 
4757 
4758 #ifdef CONFIG_P2P
wpa_global_config_parse_ipv4(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4759 static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4760 					struct wpa_config *config, int line,
4761 					const char *pos)
4762 {
4763 	u32 *dst;
4764 	struct hostapd_ip_addr addr;
4765 
4766 	if (hostapd_parse_ip_addr(pos, &addr) < 0)
4767 		return -1;
4768 	if (addr.af != AF_INET)
4769 		return -1;
4770 
4771 	dst = (u32 *) (((u8 *) config) + (long) data->param1);
4772 	if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
4773 		return 1;
4774 	os_memcpy(dst, &addr.u.v4.s_addr, 4);
4775 	wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4776 		   WPA_GET_BE32((u8 *) dst));
4777 
4778 	return 0;
4779 }
4780 #endif /* CONFIG_P2P */
4781 
4782 
wpa_config_process_country(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4783 static int wpa_config_process_country(const struct global_parse_data *data,
4784 				      struct wpa_config *config, int line,
4785 				      const char *pos)
4786 {
4787 	if (!pos[0] || !pos[1]) {
4788 		wpa_printf(MSG_DEBUG, "Invalid country set");
4789 		return -1;
4790 	}
4791 	if (pos[0] == config->country[0] && pos[1] == config->country[1])
4792 		return 1;
4793 	config->country[0] = pos[0];
4794 	config->country[1] = pos[1];
4795 	wpa_printf(MSG_DEBUG, "country='%c%c'",
4796 		   config->country[0], config->country[1]);
4797 	return 0;
4798 }
4799 
4800 
wpa_config_process_load_dynamic_eap(const struct global_parse_data * data,struct wpa_config * config,int line,const char * so)4801 static int wpa_config_process_load_dynamic_eap(
4802 	const struct global_parse_data *data, struct wpa_config *config,
4803 	int line, const char *so)
4804 {
4805 	int ret;
4806 	wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4807 	ret = eap_peer_method_load(so);
4808 	if (ret == -2) {
4809 		wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4810 			   "reloading.");
4811 	} else if (ret) {
4812 		wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4813 			   "method '%s'.", line, so);
4814 		return -1;
4815 	}
4816 
4817 	return 0;
4818 }
4819 
4820 
4821 #ifdef CONFIG_WPS
4822 
wpa_config_process_uuid(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4823 static int wpa_config_process_uuid(const struct global_parse_data *data,
4824 				   struct wpa_config *config, int line,
4825 				   const char *pos)
4826 {
4827 	char buf[40];
4828 	if (uuid_str2bin(pos, config->uuid)) {
4829 		wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4830 		return -1;
4831 	}
4832 	uuid_bin2str(config->uuid, buf, sizeof(buf));
4833 	wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4834 	return 0;
4835 }
4836 
4837 
wpa_config_process_device_type(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4838 static int wpa_config_process_device_type(
4839 	const struct global_parse_data *data,
4840 	struct wpa_config *config, int line, const char *pos)
4841 {
4842 	return wps_dev_type_str2bin(pos, config->device_type);
4843 }
4844 
4845 
wpa_config_process_os_version(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4846 static int wpa_config_process_os_version(const struct global_parse_data *data,
4847 					 struct wpa_config *config, int line,
4848 					 const char *pos)
4849 {
4850 	if (hexstr2bin(pos, config->os_version, 4)) {
4851 		wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4852 		return -1;
4853 	}
4854 	wpa_printf(MSG_DEBUG, "os_version=%08x",
4855 		   WPA_GET_BE32(config->os_version));
4856 	return 0;
4857 }
4858 
4859 
wpa_config_process_wps_vendor_ext_m1(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4860 static int wpa_config_process_wps_vendor_ext_m1(
4861 	const struct global_parse_data *data,
4862 	struct wpa_config *config, int line, const char *pos)
4863 {
4864 	struct wpabuf *tmp;
4865 	int len = os_strlen(pos) / 2;
4866 	u8 *p;
4867 
4868 	if (!len) {
4869 		wpa_printf(MSG_ERROR, "Line %d: "
4870 			   "invalid wps_vendor_ext_m1", line);
4871 		return -1;
4872 	}
4873 
4874 	tmp = wpabuf_alloc(len);
4875 	if (tmp) {
4876 		p = wpabuf_put(tmp, len);
4877 
4878 		if (hexstr2bin(pos, p, len)) {
4879 			wpa_printf(MSG_ERROR, "Line %d: "
4880 				   "invalid wps_vendor_ext_m1", line);
4881 			wpabuf_free(tmp);
4882 			return -1;
4883 		}
4884 
4885 		wpabuf_free(config->wps_vendor_ext_m1);
4886 		config->wps_vendor_ext_m1 = tmp;
4887 	} else {
4888 		wpa_printf(MSG_ERROR, "Can not allocate "
4889 			   "memory for wps_vendor_ext_m1");
4890 		return -1;
4891 	}
4892 
4893 	return 0;
4894 }
4895 
4896 #endif /* CONFIG_WPS */
4897 
4898 #ifdef CONFIG_P2P
wpa_config_process_sec_device_type(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4899 static int wpa_config_process_sec_device_type(
4900 	const struct global_parse_data *data,
4901 	struct wpa_config *config, int line, const char *pos)
4902 {
4903 	int idx;
4904 
4905 	if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4906 		wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4907 			   "items", line);
4908 		return -1;
4909 	}
4910 
4911 	idx = config->num_sec_device_types;
4912 
4913 	if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4914 		return -1;
4915 
4916 	config->num_sec_device_types++;
4917 	return 0;
4918 }
4919 
4920 
wpa_config_process_p2p_pref_chan(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4921 static int wpa_config_process_p2p_pref_chan(
4922 	const struct global_parse_data *data,
4923 	struct wpa_config *config, int line, const char *pos)
4924 {
4925 	struct p2p_channel *pref = NULL, *n;
4926 	size_t num = 0;
4927 	const char *pos2;
4928 	u8 op_class, chan;
4929 
4930 	/* format: class:chan,class:chan,... */
4931 
4932 	while (*pos) {
4933 		op_class = atoi(pos);
4934 		pos2 = os_strchr(pos, ':');
4935 		if (pos2 == NULL)
4936 			goto fail;
4937 		pos2++;
4938 		chan = atoi(pos2);
4939 
4940 		n = os_realloc_array(pref, num + 1,
4941 				     sizeof(struct p2p_channel));
4942 		if (n == NULL)
4943 			goto fail;
4944 		pref = n;
4945 		pref[num].op_class = op_class;
4946 		pref[num].chan = chan;
4947 		num++;
4948 
4949 		pos = os_strchr(pos2, ',');
4950 		if (pos == NULL)
4951 			break;
4952 		pos++;
4953 	}
4954 
4955 	os_free(config->p2p_pref_chan);
4956 	config->p2p_pref_chan = pref;
4957 	config->num_p2p_pref_chan = num;
4958 	wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4959 		    (u8 *) config->p2p_pref_chan,
4960 		    config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4961 
4962 	return 0;
4963 
4964 fail:
4965 	os_free(pref);
4966 	wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4967 	return -1;
4968 }
4969 
4970 
wpa_config_process_p2p_no_go_freq(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4971 static int wpa_config_process_p2p_no_go_freq(
4972 	const struct global_parse_data *data,
4973 	struct wpa_config *config, int line, const char *pos)
4974 {
4975 	int ret;
4976 
4977 	ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4978 	if (ret < 0) {
4979 		wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4980 		return -1;
4981 	}
4982 
4983 	wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4984 		   config->p2p_no_go_freq.num);
4985 
4986 	return 0;
4987 }
4988 
4989 
wpa_config_process_p2p_device_persistent_mac_addr(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)4990 static int wpa_config_process_p2p_device_persistent_mac_addr(
4991 	const struct global_parse_data *data,
4992 	struct wpa_config *config, int line, const char *pos)
4993 {
4994 	if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
4995 		wpa_printf(MSG_ERROR,
4996 			   "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
4997 			   line, pos);
4998 		return -1;
4999 	}
5000 
5001 	return 0;
5002 }
5003 
5004 #endif /* CONFIG_P2P */
5005 
5006 
wpa_config_process_hessid(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)5007 static int wpa_config_process_hessid(
5008 	const struct global_parse_data *data,
5009 	struct wpa_config *config, int line, const char *pos)
5010 {
5011 	if (hwaddr_aton2(pos, config->hessid) < 0) {
5012 		wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
5013 			   line, pos);
5014 		return -1;
5015 	}
5016 
5017 	return 0;
5018 }
5019 
5020 
wpa_config_process_sae_groups(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)5021 static int wpa_config_process_sae_groups(
5022 	const struct global_parse_data *data,
5023 	struct wpa_config *config, int line, const char *pos)
5024 {
5025 	int *groups = wpa_config_parse_int_array(pos);
5026 	if (groups == NULL) {
5027 		wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5028 			   line, pos);
5029 		return -1;
5030 	}
5031 
5032 	os_free(config->sae_groups);
5033 	config->sae_groups = groups;
5034 
5035 	return 0;
5036 }
5037 
5038 
wpa_config_process_ap_vendor_elements(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)5039 static int wpa_config_process_ap_vendor_elements(
5040 	const struct global_parse_data *data,
5041 	struct wpa_config *config, int line, const char *pos)
5042 {
5043 	struct wpabuf *tmp;
5044 
5045 	if (!*pos) {
5046 		wpabuf_free(config->ap_vendor_elements);
5047 		config->ap_vendor_elements = NULL;
5048 		return 0;
5049 	}
5050 
5051 	tmp = wpabuf_parse_bin(pos);
5052 	if (!tmp) {
5053 		wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5054 			   line);
5055 		return -1;
5056 	}
5057 	wpabuf_free(config->ap_vendor_elements);
5058 	config->ap_vendor_elements = tmp;
5059 
5060 	return 0;
5061 }
5062 
5063 
wpa_config_process_ap_assocresp_elements(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)5064 static int wpa_config_process_ap_assocresp_elements(
5065 	const struct global_parse_data *data,
5066 	struct wpa_config *config, int line, const char *pos)
5067 {
5068 	struct wpabuf *tmp;
5069 
5070 	if (!*pos) {
5071 		wpabuf_free(config->ap_assocresp_elements);
5072 		config->ap_assocresp_elements = NULL;
5073 		return 0;
5074 	}
5075 
5076 	tmp = wpabuf_parse_bin(pos);
5077 	if (!tmp) {
5078 		wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5079 			   line);
5080 		return -1;
5081 	}
5082 	wpabuf_free(config->ap_assocresp_elements);
5083 	config->ap_assocresp_elements = tmp;
5084 
5085 	return 0;
5086 }
5087 
5088 
5089 #ifdef CONFIG_CTRL_IFACE
wpa_config_process_no_ctrl_interface(const struct global_parse_data * data,struct wpa_config * config,int line,const char * pos)5090 static int wpa_config_process_no_ctrl_interface(
5091 	const struct global_parse_data *data,
5092 	struct wpa_config *config, int line, const char *pos)
5093 {
5094 	wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5095 	os_free(config->ctrl_interface);
5096 	config->ctrl_interface = NULL;
5097 	return 0;
5098 }
5099 #endif /* CONFIG_CTRL_IFACE */
5100 
5101 
wpa_config_get_int(const char * name,struct wpa_config * config,long offset,char * buf,size_t buflen,int pretty_print)5102 static int wpa_config_get_int(const char *name, struct wpa_config *config,
5103 			      long offset, char *buf, size_t buflen,
5104 			      int pretty_print)
5105 {
5106 	int *val = (int *) (((u8 *) config) + (long) offset);
5107 
5108 	if (pretty_print)
5109 		return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5110 	return os_snprintf(buf, buflen, "%d", *val);
5111 }
5112 
5113 
wpa_config_get_str(const char * name,struct wpa_config * config,long offset,char * buf,size_t buflen,int pretty_print)5114 static int wpa_config_get_str(const char *name, struct wpa_config *config,
5115 			      long offset, char *buf, size_t buflen,
5116 			      int pretty_print)
5117 {
5118 	char **val = (char **) (((u8 *) config) + (long) offset);
5119 	int res;
5120 
5121 	if (pretty_print)
5122 		res = os_snprintf(buf, buflen, "%s=%s\n", name,
5123 				  *val ? *val : "null");
5124 	else if (!*val)
5125 		return -1;
5126 	else
5127 		res = os_snprintf(buf, buflen, "%s", *val);
5128 	if (os_snprintf_error(buflen, res))
5129 		res = -1;
5130 
5131 	return res;
5132 }
5133 
5134 
5135 #ifdef CONFIG_P2P
wpa_config_get_ipv4(const char * name,struct wpa_config * config,long offset,char * buf,size_t buflen,int pretty_print)5136 static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5137 			       long offset, char *buf, size_t buflen,
5138 			       int pretty_print)
5139 {
5140 	void *val = ((u8 *) config) + (long) offset;
5141 	int res;
5142 	char addr[INET_ADDRSTRLEN];
5143 
5144 	if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5145 		return -1;
5146 
5147 	if (pretty_print)
5148 		res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5149 	else
5150 		res = os_snprintf(buf, buflen, "%s", addr);
5151 
5152 	if (os_snprintf_error(buflen, res))
5153 		res = -1;
5154 
5155 	return res;
5156 }
5157 #endif /* CONFIG_P2P */
5158 
5159 
5160 #ifdef OFFSET
5161 #undef OFFSET
5162 #endif /* OFFSET */
5163 /* OFFSET: Get offset of a variable within the wpa_config structure */
5164 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5165 
5166 #define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5167 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5168 #define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
5169 #define INT(f) _INT(f), NULL, NULL
5170 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
5171 #define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
5172 #define STR(f) _STR(f), NULL, NULL
5173 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
5174 #define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
5175 #define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4,  \
5176 	OFFSET(f), NULL, NULL
5177 
5178 static const struct global_parse_data global_fields[] = {
5179 #ifdef CONFIG_CTRL_IFACE
5180 	{ STR(ctrl_interface), 0 },
5181 	{ FUNC_NO_VAR(no_ctrl_interface), 0 },
5182 	{ STR(ctrl_interface_group), 0 } /* deprecated */,
5183 #endif /* CONFIG_CTRL_IFACE */
5184 #ifdef CONFIG_MACSEC
5185 	{ INT_RANGE(eapol_version, 1, 3), 0 },
5186 #else /* CONFIG_MACSEC */
5187 	{ INT_RANGE(eapol_version, 1, 2), 0 },
5188 #endif /* CONFIG_MACSEC */
5189 	{ INT(ap_scan), 0 },
5190 	{ FUNC(bgscan), CFG_CHANGED_BGSCAN },
5191 #ifdef CONFIG_MESH
5192 	{ INT(user_mpm), 0 },
5193 	{ INT_RANGE(max_peer_links, 0, 255), 0 },
5194 	{ INT(mesh_max_inactivity), 0 },
5195 	{ INT_RANGE(mesh_fwding, 0, 1), 0 },
5196 	{ INT(dot11RSNASAERetransPeriod), 0 },
5197 #endif /* CONFIG_MESH */
5198 	{ INT(disable_scan_offload), 0 },
5199 	{ INT(fast_reauth), 0 },
5200 	{ STR(opensc_engine_path), 0 },
5201 	{ STR(pkcs11_engine_path), 0 },
5202 	{ STR(pkcs11_module_path), 0 },
5203 	{ STR(openssl_ciphers), 0 },
5204 	{ STR(pcsc_reader), 0 },
5205 	{ STR(pcsc_pin), 0 },
5206 	{ INT(external_sim), 0 },
5207 	{ STR(driver_param), 0 },
5208 	{ INT(dot11RSNAConfigPMKLifetime), 0 },
5209 	{ INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5210 	{ INT(dot11RSNAConfigSATimeout), 0 },
5211 #ifndef CONFIG_NO_CONFIG_WRITE
5212 	{ INT(update_config), 0 },
5213 #endif /* CONFIG_NO_CONFIG_WRITE */
5214 	{ FUNC_NO_VAR(load_dynamic_eap), 0 },
5215 #ifdef CONFIG_WPS
5216 	{ FUNC(uuid), CFG_CHANGED_UUID },
5217 	{ INT_RANGE(auto_uuid, 0, 1), 0 },
5218 	{ STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5219 	  CFG_CHANGED_DEVICE_NAME },
5220 	{ STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5221 	{ STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5222 	{ STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5223 	{ STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5224 	{ FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5225 	{ FUNC(os_version), CFG_CHANGED_OS_VERSION },
5226 	{ STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5227 	{ INT_RANGE(wps_cred_processing, 0, 2), 0 },
5228 	{ INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
5229 	{ FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
5230 #endif /* CONFIG_WPS */
5231 #ifdef CONFIG_P2P
5232 	{ FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
5233 	{ INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5234 	{ INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5235 	{ INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5236 	{ INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
5237 	{ INT_RANGE(p2p_go_intent, 0, 15), 0 },
5238 	{ STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5239 	{ INT_RANGE(persistent_reconnect, 0, 1), 0 },
5240 	{ INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5241 	{ INT(p2p_group_idle), 0 },
5242 	{ INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
5243 	{ INT_RANGE(p2p_passphrase_len, 8, 63),
5244 	  CFG_CHANGED_P2P_PASSPHRASE_LEN },
5245 	{ FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
5246 	{ FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5247 	{ INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
5248 	{ INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
5249 	{ INT(p2p_go_ht40), 0 },
5250 	{ INT(p2p_go_vht), 0 },
5251 	{ INT(p2p_go_he), 0 },
5252 	{ INT(p2p_go_edmg), 0 },
5253 	{ INT(p2p_disabled), 0 },
5254 	{ INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
5255 	{ INT(p2p_no_group_iface), 0 },
5256 	{ INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
5257 	{ IPV4(ip_addr_go), 0 },
5258 	{ IPV4(ip_addr_mask), 0 },
5259 	{ IPV4(ip_addr_start), 0 },
5260 	{ IPV4(ip_addr_end), 0 },
5261 	{ INT_RANGE(p2p_cli_probe, 0, 1), 0 },
5262 	{ INT(p2p_device_random_mac_addr), 0 },
5263 	{ FUNC(p2p_device_persistent_mac_addr), 0 },
5264 	{ INT(p2p_interface_random_mac_addr), 0 },
5265 	{ INT(p2p_6ghz_disable), 0 },
5266 #endif /* CONFIG_P2P */
5267 	{ FUNC(country), CFG_CHANGED_COUNTRY },
5268 	{ INT(bss_max_count), 0 },
5269 	{ INT(bss_expiration_age), 0 },
5270 	{ INT(bss_expiration_scan_count), 0 },
5271 	{ INT_RANGE(filter_ssids, 0, 1), 0 },
5272 	{ INT_RANGE(filter_rssi, -100, 0), 0 },
5273 	{ INT(max_num_sta), 0 },
5274 	{ INT_RANGE(ap_isolate, 0, 1), 0 },
5275 	{ INT_RANGE(disassoc_low_ack, 0, 1), 0 },
5276 #ifdef CONFIG_HS20
5277 	{ INT_RANGE(hs20, 0, 1), 0 },
5278 #endif /* CONFIG_HS20 */
5279 	{ INT_RANGE(interworking, 0, 1), 0 },
5280 	{ FUNC(hessid), 0 },
5281 	{ INT_RANGE(access_network_type, 0, 15), 0 },
5282 	{ INT_RANGE(go_interworking, 0, 1), 0 },
5283 	{ INT_RANGE(go_access_network_type, 0, 15), 0 },
5284 	{ INT_RANGE(go_internet, 0, 1), 0 },
5285 	{ INT_RANGE(go_venue_group, 0, 255), 0 },
5286 	{ INT_RANGE(go_venue_type, 0, 255), 0 },
5287 	{ INT_RANGE(pbc_in_m1, 0, 1), 0 },
5288 	{ STR(autoscan), 0 },
5289 	{ INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5290 	  CFG_CHANGED_NFC_PASSWORD_TOKEN },
5291 	{ BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5292 	{ BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5293 	{ BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5294 	{ STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5295 	{ INT(p2p_go_max_inactivity), 0 },
5296 	{ INT_RANGE(auto_interworking, 0, 1), 0 },
5297 	{ INT(okc), 0 },
5298 	{ INT(pmf), 0 },
5299 	{ FUNC(sae_groups), 0 },
5300 	{ INT_RANGE(sae_pwe, 0, 3), 0 },
5301 	{ INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
5302 	{ INT(dtim_period), 0 },
5303 	{ INT(beacon_int), 0 },
5304 	{ FUNC(ap_assocresp_elements), 0 },
5305 	{ FUNC(ap_vendor_elements), 0 },
5306 	{ INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
5307 	{ FUNC(freq_list), 0 },
5308 	{ FUNC(initial_freq_list), 0},
5309 	{ INT(scan_cur_freq), 0 },
5310 	{ INT(scan_res_valid_for_connect), 0},
5311 	{ INT(sched_scan_interval), 0 },
5312 	{ INT(sched_scan_start_delay), 0 },
5313 	{ INT(tdls_external_control), 0},
5314 	{ STR(osu_dir), 0 },
5315 	{ STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
5316 	{ INT(p2p_search_delay), 0},
5317 	{ INT(mac_addr), 0 },
5318 	{ INT(rand_addr_lifetime), 0 },
5319 	{ INT(preassoc_mac_addr), 0 },
5320 	{ INT(key_mgmt_offload), 0},
5321 	{ INT(passive_scan), 0 },
5322 	{ INT(reassoc_same_bss_optim), 0 },
5323 	{ INT(wps_priority), 0},
5324 #ifdef CONFIG_FST
5325 	{ STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5326 	{ INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5327 	{ INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5328 #endif /* CONFIG_FST */
5329 	{ INT_RANGE(cert_in_cb, 0, 1), 0 },
5330 	{ INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
5331 	{ STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
5332 #ifdef CONFIG_MBO
5333 	{ STR(non_pref_chan), 0 },
5334 	{ INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5335 		    MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
5336 	{ INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5337 	{ INT_RANGE(oce, 0, 3), 0 },
5338 #endif /* CONFIG_MBO */
5339 	{ INT(gas_address3), 0 },
5340 	{ INT_RANGE(ftm_responder, 0, 1), 0 },
5341 	{ INT_RANGE(ftm_initiator, 0, 1), 0 },
5342 	{ INT(gas_rand_addr_lifetime), 0 },
5343 	{ INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
5344 #ifdef CONFIG_DPP
5345 	{ INT_RANGE(dpp_config_processing, 0, 2), 0 },
5346 	{ STR(dpp_name), 0 },
5347 	{ STR(dpp_mud_url), 0 },
5348 #endif /* CONFIG_DPP */
5349 	{ INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
5350 #ifdef CONFIG_WNM
5351 	{ INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
5352 	{ INT_RANGE(extended_key_id, 0, 1), 0 },
5353 #endif /* CONFIG_WNM */
5354 	{ INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
5355 #ifdef CONFIG_PASN
5356 #ifdef CONFIG_TESTING_OPTIONS
5357 	{ INT_RANGE(force_kdk_derivation, 0, 1), 0 },
5358 	{ INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
5359 #endif /* CONFIG_TESTING_OPTIONS */
5360 #endif /* CONFIG_PASN */
5361 };
5362 
5363 #undef FUNC
5364 #undef _INT
5365 #undef INT
5366 #undef INT_RANGE
5367 #undef _STR
5368 #undef STR
5369 #undef STR_RANGE
5370 #undef BIN
5371 #undef IPV4
5372 #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
5373 
5374 
wpa_config_dump_values(struct wpa_config * config,char * buf,size_t buflen)5375 int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5376 {
5377 	int result = 0;
5378 	size_t i;
5379 
5380 	for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5381 		const struct global_parse_data *field = &global_fields[i];
5382 		int tmp;
5383 
5384 		if (!field->get)
5385 			continue;
5386 
5387 		tmp = field->get(field->name, config, (long) field->param1,
5388 				 buf, buflen, 1);
5389 		if (tmp < 0)
5390 			return -1;
5391 		buf += tmp;
5392 		buflen -= tmp;
5393 		result += tmp;
5394 	}
5395 	return result;
5396 }
5397 
5398 
wpa_config_get_value(const char * name,struct wpa_config * config,char * buf,size_t buflen)5399 int wpa_config_get_value(const char *name, struct wpa_config *config,
5400 			 char *buf, size_t buflen)
5401 {
5402 	size_t i;
5403 
5404 	for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5405 		const struct global_parse_data *field = &global_fields[i];
5406 
5407 		if (os_strcmp(name, field->name) != 0)
5408 			continue;
5409 		if (!field->get)
5410 			break;
5411 		return field->get(name, config, (long) field->param1,
5412 				  buf, buflen, 0);
5413 	}
5414 
5415 	return -1;
5416 }
5417 
5418 
wpa_config_get_num_global_field_names(void)5419 int wpa_config_get_num_global_field_names(void)
5420 {
5421 	return NUM_GLOBAL_FIELDS;
5422 }
5423 
5424 
wpa_config_get_global_field_name(unsigned int i,int * no_var)5425 const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5426 {
5427 	if (i >= NUM_GLOBAL_FIELDS)
5428 		return NULL;
5429 
5430 	if (no_var)
5431 		*no_var = !global_fields[i].param1;
5432 	return global_fields[i].name;
5433 }
5434 
5435 
5436 /**
5437  * wpa_config_process_global - Set a variable in global configuration
5438  * @config: Pointer to global configuration data
5439  * @pos: Name and value in the format "{name}={value}"
5440  * @line: Line number in configuration file or 0 if not used
5441  * Returns: 0 on success with a possible change in value, 1 on success with no
5442  * change to previously configured value, or -1 on failure
5443  *
5444  * This function can be used to set global configuration variables based on
5445  * both the configuration file and management interface input. The value
5446  * parameter must be in the same format as the text-based configuration file is
5447  * using. For example, strings are using double quotation marks.
5448  */
wpa_config_process_global(struct wpa_config * config,char * pos,int line)5449 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5450 {
5451 	size_t i;
5452 	int ret = 0;
5453 
5454 	for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5455 		const struct global_parse_data *field = &global_fields[i];
5456 		size_t flen = os_strlen(field->name);
5457 		if (os_strncmp(pos, field->name, flen) != 0 ||
5458 		    pos[flen] != '=')
5459 			continue;
5460 
5461 		ret = field->parser(field, config, line, pos + flen + 1);
5462 		if (ret < 0) {
5463 			wpa_printf(MSG_ERROR, "Line %d: failed to "
5464 				   "parse '%s'.", line, pos);
5465 			ret = -1;
5466 		}
5467 		if (ret == 1)
5468 			break;
5469 		if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5470 			config->wps_nfc_pw_from_config = 1;
5471 		config->changed_parameters |= field->changed_flag;
5472 		break;
5473 	}
5474 	if (i == NUM_GLOBAL_FIELDS) {
5475 #ifdef CONFIG_AP
5476 		if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5477 			char *tmp = os_strchr(pos, '=');
5478 
5479 			if (!tmp) {
5480 				if (line < 0)
5481 					wpa_printf(MSG_ERROR,
5482 						   "Line %d: invalid line %s",
5483 						   line, pos);
5484 				return -1;
5485 			}
5486 			*tmp++ = '\0';
5487 			if (hostapd_config_tx_queue(config->tx_queue, pos,
5488 						    tmp)) {
5489 				wpa_printf(MSG_ERROR,
5490 					   "Line %d: invalid TX queue item",
5491 					   line);
5492 				return -1;
5493 			}
5494 		}
5495 
5496 		if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5497 			char *tmp = os_strchr(pos, '=');
5498 			if (tmp == NULL) {
5499 				if (line < 0)
5500 					return -1;
5501 				wpa_printf(MSG_ERROR, "Line %d: invalid line "
5502 					   "'%s'", line, pos);
5503 				return -1;
5504 			}
5505 			*tmp++ = '\0';
5506 			if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5507 						  tmp)) {
5508 				wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5509 					   "AC item", line);
5510 				return -1;
5511 			}
5512 			return ret;
5513 		}
5514 #endif /* CONFIG_AP */
5515 		if (line < 0)
5516 			return -1;
5517 		wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5518 			   line, pos);
5519 		ret = -1;
5520 	}
5521 
5522 	return ret;
5523 }
5524 #endif /* EXT_WPA_GLOBAL_CONFIG */
5525