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