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