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