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