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