]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/config.c
P2P: Avoid segfault on AP deinit after failed AP start
[thirdparty/hostap.git] / wpa_supplicant / config.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Configuration parser and common functions
56586197 3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
6fc6879b
JM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
121adf9c 18#include "utils/uuid.h"
03da66bd 19#include "crypto/sha1.h"
3acb5005 20#include "rsn_supp/wpa.h"
6fc6879b
JM
21#include "eap_peer/eap.h"
22#include "config.h"
23
24
25#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
26#define NO_CONFIG_WRITE
27#endif
28
29/*
30 * Structure for network configuration parsing. This data is used to implement
31 * a generic parser for each network block variable. The table of configuration
32 * variables is defined below in this file (ssid_fields[]).
33 */
34struct parse_data {
35 /* Configuration variable name */
36 char *name;
37
38 /* Parser function for this variable */
39 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
40 int line, const char *value);
41
42#ifndef NO_CONFIG_WRITE
43 /* Writer function (i.e., to get the variable in text format from
44 * internal presentation). */
45 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
46#endif /* NO_CONFIG_WRITE */
47
48 /* Variable specific parameters for the parser. */
49 void *param1, *param2, *param3, *param4;
50
51 /* 0 = this variable can be included in debug output and ctrl_iface
52 * 1 = this variable contains key/private data and it must not be
53 * included in debug output unless explicitly requested. In
54 * addition, this variable will not be readable through the
55 * ctrl_iface.
56 */
57 int key_data;
58};
59
60
61static char * wpa_config_parse_string(const char *value, size_t *len)
62{
63 if (*value == '"') {
e237a6b0
JM
64 const char *pos;
65 char *str;
6fc6879b
JM
66 value++;
67 pos = os_strrchr(value, '"');
68 if (pos == NULL || pos[1] != '\0')
69 return NULL;
e237a6b0
JM
70 *len = pos - value;
71 str = os_malloc(*len + 1);
72 if (str == NULL)
73 return NULL;
74 os_memcpy(str, value, *len);
75 str[*len] = '\0';
76 return str;
6fc6879b
JM
77 } else {
78 u8 *str;
79 size_t tlen, hlen = os_strlen(value);
80 if (hlen & 1)
81 return NULL;
82 tlen = hlen / 2;
83 str = os_malloc(tlen + 1);
84 if (str == NULL)
85 return NULL;
86 if (hexstr2bin(value, str, tlen)) {
87 os_free(str);
88 return NULL;
89 }
90 str[tlen] = '\0';
91 *len = tlen;
92 return (char *) str;
93 }
94}
95
96
97static int wpa_config_parse_str(const struct parse_data *data,
98 struct wpa_ssid *ssid,
99 int line, const char *value)
100{
101 size_t res_len, *dst_len;
102 char **dst, *tmp;
103
b56c0546
JM
104 if (os_strcmp(value, "NULL") == 0) {
105 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
106 data->name);
107 tmp = NULL;
108 res_len = 0;
109 goto set;
110 }
111
6fc6879b
JM
112 tmp = wpa_config_parse_string(value, &res_len);
113 if (tmp == NULL) {
114 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
115 line, data->name,
116 data->key_data ? "[KEY DATA REMOVED]" : value);
117 return -1;
118 }
119
120 if (data->key_data) {
121 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
122 (u8 *) tmp, res_len);
123 } else {
124 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
125 (u8 *) tmp, res_len);
126 }
127
128 if (data->param3 && res_len < (size_t) data->param3) {
129 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
130 "min_len=%ld)", line, data->name,
131 (unsigned long) res_len, (long) data->param3);
132 os_free(tmp);
133 return -1;
134 }
135
136 if (data->param4 && res_len > (size_t) data->param4) {
137 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
138 "max_len=%ld)", line, data->name,
139 (unsigned long) res_len, (long) data->param4);
140 os_free(tmp);
141 return -1;
142 }
143
b56c0546 144set:
6fc6879b
JM
145 dst = (char **) (((u8 *) ssid) + (long) data->param1);
146 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
147 os_free(*dst);
148 *dst = tmp;
149 if (data->param2)
150 *dst_len = res_len;
151
152 return 0;
153}
154
155
156#ifndef NO_CONFIG_WRITE
157static int is_hex(const u8 *data, size_t len)
158{
159 size_t i;
160
161 for (i = 0; i < len; i++) {
162 if (data[i] < 32 || data[i] >= 127)
163 return 1;
164 }
165 return 0;
166}
167
168
169static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
170{
171 char *buf;
172
173 buf = os_malloc(len + 3);
174 if (buf == NULL)
175 return NULL;
176 buf[0] = '"';
177 os_memcpy(buf + 1, value, len);
178 buf[len + 1] = '"';
179 buf[len + 2] = '\0';
180
181 return buf;
182}
183
184
185static char * wpa_config_write_string_hex(const u8 *value, size_t len)
186{
187 char *buf;
188
189 buf = os_zalloc(2 * len + 1);
190 if (buf == NULL)
191 return NULL;
192 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
193
194 return buf;
195}
196
197
198static char * wpa_config_write_string(const u8 *value, size_t len)
199{
200 if (value == NULL)
201 return NULL;
202
203 if (is_hex(value, len))
204 return wpa_config_write_string_hex(value, len);
205 else
206 return wpa_config_write_string_ascii(value, len);
207}
208
209
210static char * wpa_config_write_str(const struct parse_data *data,
211 struct wpa_ssid *ssid)
212{
213 size_t len;
214 char **src;
215
216 src = (char **) (((u8 *) ssid) + (long) data->param1);
217 if (*src == NULL)
218 return NULL;
219
220 if (data->param2)
221 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
222 else
223 len = os_strlen(*src);
224
225 return wpa_config_write_string((const u8 *) *src, len);
226}
227#endif /* NO_CONFIG_WRITE */
228
229
230static int wpa_config_parse_int(const struct parse_data *data,
231 struct wpa_ssid *ssid,
232 int line, const char *value)
233{
234 int *dst;
235
236 dst = (int *) (((u8 *) ssid) + (long) data->param1);
237 *dst = atoi(value);
238 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
239
240 if (data->param3 && *dst < (long) data->param3) {
241 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
242 "min_value=%ld)", line, data->name, *dst,
243 (long) data->param3);
244 *dst = (long) data->param3;
245 return -1;
246 }
247
248 if (data->param4 && *dst > (long) data->param4) {
249 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
250 "max_value=%ld)", line, data->name, *dst,
251 (long) data->param4);
252 *dst = (long) data->param4;
253 return -1;
254 }
255
256 return 0;
257}
258
259
260#ifndef NO_CONFIG_WRITE
261static char * wpa_config_write_int(const struct parse_data *data,
262 struct wpa_ssid *ssid)
263{
264 int *src, res;
265 char *value;
266
267 src = (int *) (((u8 *) ssid) + (long) data->param1);
268
269 value = os_malloc(20);
270 if (value == NULL)
271 return NULL;
272 res = os_snprintf(value, 20, "%d", *src);
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_bssid(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
285 const char *value)
286{
287 if (hwaddr_aton(value, ssid->bssid)) {
288 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
289 line, value);
290 return -1;
291 }
292 ssid->bssid_set = 1;
293 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
294 return 0;
295}
296
297
298#ifndef NO_CONFIG_WRITE
299static char * wpa_config_write_bssid(const struct parse_data *data,
300 struct wpa_ssid *ssid)
301{
302 char *value;
303 int res;
304
305 if (!ssid->bssid_set)
306 return NULL;
307
308 value = os_malloc(20);
309 if (value == NULL)
310 return NULL;
311 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
312 if (res < 0 || res >= 20) {
313 os_free(value);
314 return NULL;
315 }
316 value[20 - 1] = '\0';
317 return value;
318}
319#endif /* NO_CONFIG_WRITE */
320
321
322static int wpa_config_parse_psk(const struct parse_data *data,
323 struct wpa_ssid *ssid, int line,
324 const char *value)
325{
326 if (*value == '"') {
327#ifndef CONFIG_NO_PBKDF2
328 const char *pos;
329 size_t len;
330
331 value++;
332 pos = os_strrchr(value, '"');
333 if (pos)
334 len = pos - value;
335 else
336 len = os_strlen(value);
337 if (len < 8 || len > 63) {
338 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
339 "length %lu (expected: 8..63) '%s'.",
340 line, (unsigned long) len, value);
341 return -1;
342 }
343 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
344 (u8 *) value, len);
345 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
346 os_memcmp(ssid->passphrase, value, len) == 0)
347 return 0;
348 ssid->psk_set = 0;
349 os_free(ssid->passphrase);
350 ssid->passphrase = os_malloc(len + 1);
351 if (ssid->passphrase == NULL)
352 return -1;
353 os_memcpy(ssid->passphrase, value, len);
354 ssid->passphrase[len] = '\0';
355 return 0;
356#else /* CONFIG_NO_PBKDF2 */
357 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
358 "supported.", line);
359 return -1;
360#endif /* CONFIG_NO_PBKDF2 */
361 }
362
363 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
364 value[PMK_LEN * 2] != '\0') {
365 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
366 line, value);
367 return -1;
368 }
369
370 os_free(ssid->passphrase);
371 ssid->passphrase = NULL;
372
373 ssid->psk_set = 1;
374 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
375 return 0;
376}
377
378
379#ifndef NO_CONFIG_WRITE
380static char * wpa_config_write_psk(const struct parse_data *data,
381 struct wpa_ssid *ssid)
382{
383 if (ssid->passphrase)
384 return wpa_config_write_string_ascii(
385 (const u8 *) ssid->passphrase,
386 os_strlen(ssid->passphrase));
387
388 if (ssid->psk_set)
389 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
390
391 return NULL;
392}
393#endif /* NO_CONFIG_WRITE */
394
395
396static int wpa_config_parse_proto(const struct parse_data *data,
397 struct wpa_ssid *ssid, int line,
398 const char *value)
399{
400 int val = 0, last, errors = 0;
401 char *start, *end, *buf;
402
403 buf = os_strdup(value);
404 if (buf == NULL)
405 return -1;
406 start = buf;
407
408 while (*start != '\0') {
409 while (*start == ' ' || *start == '\t')
410 start++;
411 if (*start == '\0')
412 break;
413 end = start;
414 while (*end != ' ' && *end != '\t' && *end != '\0')
415 end++;
416 last = *end == '\0';
417 *end = '\0';
418 if (os_strcmp(start, "WPA") == 0)
419 val |= WPA_PROTO_WPA;
420 else if (os_strcmp(start, "RSN") == 0 ||
421 os_strcmp(start, "WPA2") == 0)
422 val |= WPA_PROTO_RSN;
423 else {
424 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
425 line, start);
426 errors++;
427 }
428
429 if (last)
430 break;
431 start = end + 1;
432 }
433 os_free(buf);
434
435 if (val == 0) {
436 wpa_printf(MSG_ERROR,
437 "Line %d: no proto values configured.", line);
438 errors++;
439 }
440
441 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
442 ssid->proto = val;
443 return errors ? -1 : 0;
444}
445
446
447#ifndef NO_CONFIG_WRITE
448static char * wpa_config_write_proto(const struct parse_data *data,
449 struct wpa_ssid *ssid)
450{
451 int first = 1, ret;
452 char *buf, *pos, *end;
453
454 pos = buf = os_zalloc(10);
455 if (buf == NULL)
456 return NULL;
457 end = buf + 10;
458
459 if (ssid->proto & WPA_PROTO_WPA) {
460 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
461 if (ret < 0 || ret >= end - pos)
462 return buf;
463 pos += ret;
464 first = 0;
465 }
466
467 if (ssid->proto & WPA_PROTO_RSN) {
468 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
469 if (ret < 0 || ret >= end - pos)
470 return buf;
471 pos += ret;
472 first = 0;
473 }
474
475 return buf;
476}
477#endif /* NO_CONFIG_WRITE */
478
479
480static int wpa_config_parse_key_mgmt(const struct parse_data *data,
481 struct wpa_ssid *ssid, int line,
482 const char *value)
483{
484 int val = 0, last, errors = 0;
485 char *start, *end, *buf;
486
487 buf = os_strdup(value);
488 if (buf == NULL)
489 return -1;
490 start = buf;
491
492 while (*start != '\0') {
493 while (*start == ' ' || *start == '\t')
494 start++;
495 if (*start == '\0')
496 break;
497 end = start;
498 while (*end != ' ' && *end != '\t' && *end != '\0')
499 end++;
500 last = *end == '\0';
501 *end = '\0';
502 if (os_strcmp(start, "WPA-PSK") == 0)
503 val |= WPA_KEY_MGMT_PSK;
504 else if (os_strcmp(start, "WPA-EAP") == 0)
505 val |= WPA_KEY_MGMT_IEEE8021X;
506 else if (os_strcmp(start, "IEEE8021X") == 0)
507 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
508 else if (os_strcmp(start, "NONE") == 0)
509 val |= WPA_KEY_MGMT_NONE;
510 else if (os_strcmp(start, "WPA-NONE") == 0)
511 val |= WPA_KEY_MGMT_WPA_NONE;
512#ifdef CONFIG_IEEE80211R
513 else if (os_strcmp(start, "FT-PSK") == 0)
514 val |= WPA_KEY_MGMT_FT_PSK;
515 else if (os_strcmp(start, "FT-EAP") == 0)
516 val |= WPA_KEY_MGMT_FT_IEEE8021X;
517#endif /* CONFIG_IEEE80211R */
56586197
JM
518#ifdef CONFIG_IEEE80211W
519 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
520 val |= WPA_KEY_MGMT_PSK_SHA256;
521 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
522 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
523#endif /* CONFIG_IEEE80211W */
ad08c363
JM
524#ifdef CONFIG_WPS
525 else if (os_strcmp(start, "WPS") == 0)
526 val |= WPA_KEY_MGMT_WPS;
527#endif /* CONFIG_WPS */
6fc6879b
JM
528 else {
529 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
530 line, start);
531 errors++;
532 }
533
534 if (last)
535 break;
536 start = end + 1;
537 }
538 os_free(buf);
539
540 if (val == 0) {
541 wpa_printf(MSG_ERROR,
542 "Line %d: no key_mgmt values configured.", line);
543 errors++;
544 }
545
546 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
547 ssid->key_mgmt = val;
548 return errors ? -1 : 0;
549}
550
551
552#ifndef NO_CONFIG_WRITE
553static char * wpa_config_write_key_mgmt(const struct parse_data *data,
554 struct wpa_ssid *ssid)
555{
556 char *buf, *pos, *end;
557 int ret;
558
559 pos = buf = os_zalloc(50);
560 if (buf == NULL)
561 return NULL;
562 end = buf + 50;
563
564 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
565 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
566 pos == buf ? "" : " ");
567 if (ret < 0 || ret >= end - pos) {
568 end[-1] = '\0';
569 return buf;
570 }
571 pos += ret;
572 }
573
574 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
575 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
576 pos == buf ? "" : " ");
577 if (ret < 0 || ret >= end - pos) {
578 end[-1] = '\0';
579 return buf;
580 }
581 pos += ret;
582 }
583
584 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
585 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
586 pos == buf ? "" : " ");
587 if (ret < 0 || ret >= end - pos) {
588 end[-1] = '\0';
589 return buf;
590 }
591 pos += ret;
592 }
593
594 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
595 ret = os_snprintf(pos, end - pos, "%sNONE",
596 pos == buf ? "" : " ");
597 if (ret < 0 || ret >= end - pos) {
598 end[-1] = '\0';
599 return buf;
600 }
601 pos += ret;
602 }
603
604 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
605 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
606 pos == buf ? "" : " ");
607 if (ret < 0 || ret >= end - pos) {
608 end[-1] = '\0';
609 return buf;
610 }
611 pos += ret;
612 }
613
614#ifdef CONFIG_IEEE80211R
615 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
616 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
617 pos == buf ? "" : " ");
618
619 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
620 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
621 pos == buf ? "" : " ");
622#endif /* CONFIG_IEEE80211R */
623
56586197
JM
624#ifdef CONFIG_IEEE80211W
625 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
626 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
627 pos == buf ? "" : " ");
628
629 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
630 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
631 pos == buf ? "" : " ");
632#endif /* CONFIG_IEEE80211W */
633
728fae16
JM
634#ifdef CONFIG_WPS
635 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
636 pos += os_snprintf(pos, end - pos, "%sWPS",
637 pos == buf ? "" : " ");
638#endif /* CONFIG_WPS */
639
6fc6879b
JM
640 return buf;
641}
642#endif /* NO_CONFIG_WRITE */
643
644
645static int wpa_config_parse_cipher(int line, const char *value)
646{
647 int val = 0, last;
648 char *start, *end, *buf;
649
650 buf = os_strdup(value);
651 if (buf == NULL)
652 return -1;
653 start = buf;
654
655 while (*start != '\0') {
656 while (*start == ' ' || *start == '\t')
657 start++;
658 if (*start == '\0')
659 break;
660 end = start;
661 while (*end != ' ' && *end != '\t' && *end != '\0')
662 end++;
663 last = *end == '\0';
664 *end = '\0';
665 if (os_strcmp(start, "CCMP") == 0)
666 val |= WPA_CIPHER_CCMP;
667 else if (os_strcmp(start, "TKIP") == 0)
668 val |= WPA_CIPHER_TKIP;
669 else if (os_strcmp(start, "WEP104") == 0)
670 val |= WPA_CIPHER_WEP104;
671 else if (os_strcmp(start, "WEP40") == 0)
672 val |= WPA_CIPHER_WEP40;
673 else if (os_strcmp(start, "NONE") == 0)
674 val |= WPA_CIPHER_NONE;
675 else {
676 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
677 line, start);
678 os_free(buf);
679 return -1;
680 }
681
682 if (last)
683 break;
684 start = end + 1;
685 }
686 os_free(buf);
687
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{
700 char *buf, *pos, *end;
701 int ret;
702
703 pos = buf = os_zalloc(50);
704 if (buf == NULL)
705 return NULL;
706 end = buf + 50;
707
708 if (cipher & WPA_CIPHER_CCMP) {
709 ret = os_snprintf(pos, end - pos, "%sCCMP",
710 pos == buf ? "" : " ");
711 if (ret < 0 || ret >= end - pos) {
712 end[-1] = '\0';
713 return buf;
714 }
715 pos += ret;
716 }
717
718 if (cipher & WPA_CIPHER_TKIP) {
719 ret = os_snprintf(pos, end - pos, "%sTKIP",
720 pos == buf ? "" : " ");
721 if (ret < 0 || ret >= end - pos) {
722 end[-1] = '\0';
723 return buf;
724 }
725 pos += ret;
726 }
727
728 if (cipher & WPA_CIPHER_WEP104) {
729 ret = os_snprintf(pos, end - pos, "%sWEP104",
730 pos == buf ? "" : " ");
731 if (ret < 0 || ret >= end - pos) {
732 end[-1] = '\0';
733 return buf;
734 }
735 pos += ret;
736 }
737
738 if (cipher & WPA_CIPHER_WEP40) {
739 ret = os_snprintf(pos, end - pos, "%sWEP40",
740 pos == buf ? "" : " ");
741 if (ret < 0 || ret >= end - pos) {
742 end[-1] = '\0';
743 return buf;
744 }
745 pos += ret;
746 }
747
748 if (cipher & WPA_CIPHER_NONE) {
749 ret = os_snprintf(pos, end - pos, "%sNONE",
750 pos == buf ? "" : " ");
751 if (ret < 0 || ret >= end - pos) {
752 end[-1] = '\0';
753 return buf;
754 }
755 pos += ret;
756 }
757
758 return buf;
759}
760#endif /* NO_CONFIG_WRITE */
761
762
763static int wpa_config_parse_pairwise(const struct parse_data *data,
764 struct wpa_ssid *ssid, int line,
765 const char *value)
766{
767 int val;
768 val = wpa_config_parse_cipher(line, value);
769 if (val == -1)
770 return -1;
771 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
772 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
773 "(0x%x).", line, val);
774 return -1;
775 }
776
777 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
778 ssid->pairwise_cipher = val;
779 return 0;
780}
781
782
783#ifndef NO_CONFIG_WRITE
784static char * wpa_config_write_pairwise(const struct parse_data *data,
785 struct wpa_ssid *ssid)
786{
787 return wpa_config_write_cipher(ssid->pairwise_cipher);
788}
789#endif /* NO_CONFIG_WRITE */
790
791
792static int wpa_config_parse_group(const struct parse_data *data,
793 struct wpa_ssid *ssid, int line,
794 const char *value)
795{
796 int val;
797 val = wpa_config_parse_cipher(line, value);
798 if (val == -1)
799 return -1;
800 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
801 WPA_CIPHER_WEP40)) {
802 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
803 "(0x%x).", line, val);
804 return -1;
805 }
806
807 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
808 ssid->group_cipher = val;
809 return 0;
810}
811
812
813#ifndef NO_CONFIG_WRITE
814static char * wpa_config_write_group(const struct parse_data *data,
815 struct wpa_ssid *ssid)
816{
817 return wpa_config_write_cipher(ssid->group_cipher);
818}
819#endif /* NO_CONFIG_WRITE */
820
821
822static int wpa_config_parse_auth_alg(const struct parse_data *data,
823 struct wpa_ssid *ssid, int line,
824 const char *value)
825{
826 int val = 0, last, errors = 0;
827 char *start, *end, *buf;
828
829 buf = os_strdup(value);
830 if (buf == NULL)
831 return -1;
832 start = buf;
833
834 while (*start != '\0') {
835 while (*start == ' ' || *start == '\t')
836 start++;
837 if (*start == '\0')
838 break;
839 end = start;
840 while (*end != ' ' && *end != '\t' && *end != '\0')
841 end++;
842 last = *end == '\0';
843 *end = '\0';
844 if (os_strcmp(start, "OPEN") == 0)
845 val |= WPA_AUTH_ALG_OPEN;
846 else if (os_strcmp(start, "SHARED") == 0)
847 val |= WPA_AUTH_ALG_SHARED;
848 else if (os_strcmp(start, "LEAP") == 0)
849 val |= WPA_AUTH_ALG_LEAP;
850 else {
851 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
852 line, start);
853 errors++;
854 }
855
856 if (last)
857 break;
858 start = end + 1;
859 }
860 os_free(buf);
861
862 if (val == 0) {
863 wpa_printf(MSG_ERROR,
864 "Line %d: no auth_alg values configured.", line);
865 errors++;
866 }
867
868 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
869 ssid->auth_alg = val;
870 return errors ? -1 : 0;
871}
872
873
874#ifndef NO_CONFIG_WRITE
875static char * wpa_config_write_auth_alg(const struct parse_data *data,
876 struct wpa_ssid *ssid)
877{
878 char *buf, *pos, *end;
879 int ret;
880
881 pos = buf = os_zalloc(30);
882 if (buf == NULL)
883 return NULL;
884 end = buf + 30;
885
886 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
887 ret = os_snprintf(pos, end - pos, "%sOPEN",
888 pos == buf ? "" : " ");
889 if (ret < 0 || ret >= end - pos) {
890 end[-1] = '\0';
891 return buf;
892 }
893 pos += ret;
894 }
895
896 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
897 ret = os_snprintf(pos, end - pos, "%sSHARED",
898 pos == buf ? "" : " ");
899 if (ret < 0 || ret >= end - pos) {
900 end[-1] = '\0';
901 return buf;
902 }
903 pos += ret;
904 }
905
906 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
907 ret = os_snprintf(pos, end - pos, "%sLEAP",
908 pos == buf ? "" : " ");
909 if (ret < 0 || ret >= end - pos) {
910 end[-1] = '\0';
911 return buf;
912 }
913 pos += ret;
914 }
915
916 return buf;
917}
918#endif /* NO_CONFIG_WRITE */
919
920
b766a9a2
JM
921static int * wpa_config_parse_freqs(const struct parse_data *data,
922 struct wpa_ssid *ssid, int line,
923 const char *value)
d3a98225
JM
924{
925 int *freqs;
926 size_t used, len;
927 const char *pos;
928
929 used = 0;
930 len = 10;
931 freqs = os_zalloc((len + 1) * sizeof(int));
932 if (freqs == NULL)
b766a9a2 933 return NULL;
d3a98225
JM
934
935 pos = value;
936 while (pos) {
937 while (*pos == ' ')
938 pos++;
939 if (used == len) {
940 int *n;
941 size_t i;
942 n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
943 if (n == NULL) {
944 os_free(freqs);
b766a9a2 945 return NULL;
d3a98225
JM
946 }
947 for (i = len; i <= len * 2; i++)
948 n[i] = 0;
949 freqs = n;
950 len *= 2;
951 }
952
953 freqs[used] = atoi(pos);
954 if (freqs[used] == 0)
955 break;
956 used++;
957 pos = os_strchr(pos + 1, ' ');
958 }
959
b766a9a2
JM
960 return freqs;
961}
962
963
964static int wpa_config_parse_scan_freq(const struct parse_data *data,
965 struct wpa_ssid *ssid, int line,
966 const char *value)
967{
968 int *freqs;
969
970 freqs = wpa_config_parse_freqs(data, ssid, line, value);
971 if (freqs == NULL)
972 return -1;
d3a98225
JM
973 os_free(ssid->scan_freq);
974 ssid->scan_freq = freqs;
975
976 return 0;
977}
978
979
b766a9a2
JM
980static int wpa_config_parse_freq_list(const struct parse_data *data,
981 struct wpa_ssid *ssid, int line,
982 const char *value)
983{
984 int *freqs;
985
986 freqs = wpa_config_parse_freqs(data, ssid, line, value);
987 if (freqs == NULL)
988 return -1;
989 os_free(ssid->freq_list);
990 ssid->freq_list = freqs;
991
992 return 0;
993}
994
995
d3a98225 996#ifndef NO_CONFIG_WRITE
b766a9a2
JM
997static char * wpa_config_write_freqs(const struct parse_data *data,
998 const int *freqs)
d3a98225
JM
999{
1000 char *buf, *pos, *end;
1001 int i, ret;
1002 size_t count;
1003
b766a9a2 1004 if (freqs == NULL)
d3a98225
JM
1005 return NULL;
1006
1007 count = 0;
b766a9a2 1008 for (i = 0; freqs[i]; i++)
d3a98225
JM
1009 count++;
1010
1011 pos = buf = os_zalloc(10 * count + 1);
1012 if (buf == NULL)
1013 return NULL;
1014 end = buf + 10 * count + 1;
1015
b766a9a2 1016 for (i = 0; freqs[i]; i++) {
d3a98225 1017 ret = os_snprintf(pos, end - pos, "%s%u",
b766a9a2 1018 i == 0 ? "" : " ", freqs[i]);
d3a98225
JM
1019 if (ret < 0 || ret >= end - pos) {
1020 end[-1] = '\0';
1021 return buf;
1022 }
1023 pos += ret;
1024 }
1025
1026 return buf;
1027}
b766a9a2
JM
1028
1029
1030static char * wpa_config_write_scan_freq(const struct parse_data *data,
1031 struct wpa_ssid *ssid)
1032{
1033 return wpa_config_write_freqs(data, ssid->scan_freq);
1034}
1035
1036
1037static char * wpa_config_write_freq_list(const struct parse_data *data,
1038 struct wpa_ssid *ssid)
1039{
1040 return wpa_config_write_freqs(data, ssid->freq_list);
1041}
d3a98225
JM
1042#endif /* NO_CONFIG_WRITE */
1043
1044
6fc6879b
JM
1045#ifdef IEEE8021X_EAPOL
1046static int wpa_config_parse_eap(const struct parse_data *data,
1047 struct wpa_ssid *ssid, int line,
1048 const char *value)
1049{
1050 int last, errors = 0;
1051 char *start, *end, *buf;
1052 struct eap_method_type *methods = NULL, *tmp;
1053 size_t num_methods = 0;
1054
1055 buf = os_strdup(value);
1056 if (buf == NULL)
1057 return -1;
1058 start = buf;
1059
1060 while (*start != '\0') {
1061 while (*start == ' ' || *start == '\t')
1062 start++;
1063 if (*start == '\0')
1064 break;
1065 end = start;
1066 while (*end != ' ' && *end != '\t' && *end != '\0')
1067 end++;
1068 last = *end == '\0';
1069 *end = '\0';
1070 tmp = methods;
1071 methods = os_realloc(methods,
1072 (num_methods + 1) * sizeof(*methods));
1073 if (methods == NULL) {
1074 os_free(tmp);
1075 os_free(buf);
1076 return -1;
1077 }
1078 methods[num_methods].method = eap_peer_get_type(
1079 start, &methods[num_methods].vendor);
1080 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1081 methods[num_methods].method == EAP_TYPE_NONE) {
1082 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1083 "'%s'", line, start);
1084 wpa_printf(MSG_ERROR, "You may need to add support for"
1085 " this EAP method during wpa_supplicant\n"
1086 "build time configuration.\n"
1087 "See README for more information.");
1088 errors++;
1089 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1090 methods[num_methods].method == EAP_TYPE_LEAP)
1091 ssid->leap++;
1092 else
1093 ssid->non_leap++;
1094 num_methods++;
1095 if (last)
1096 break;
1097 start = end + 1;
1098 }
1099 os_free(buf);
1100
1101 tmp = methods;
1102 methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1103 if (methods == NULL) {
1104 os_free(tmp);
1105 return -1;
1106 }
1107 methods[num_methods].vendor = EAP_VENDOR_IETF;
1108 methods[num_methods].method = EAP_TYPE_NONE;
1109 num_methods++;
1110
1111 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1112 (u8 *) methods, num_methods * sizeof(*methods));
1113 ssid->eap.eap_methods = methods;
1114 return errors ? -1 : 0;
1115}
1116
1117
1118static char * wpa_config_write_eap(const struct parse_data *data,
1119 struct wpa_ssid *ssid)
1120{
1121 int i, ret;
1122 char *buf, *pos, *end;
1123 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1124 const char *name;
1125
1126 if (eap_methods == NULL)
1127 return NULL;
1128
1129 pos = buf = os_zalloc(100);
1130 if (buf == NULL)
1131 return NULL;
1132 end = buf + 100;
1133
1134 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1135 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1136 name = eap_get_name(eap_methods[i].vendor,
1137 eap_methods[i].method);
1138 if (name) {
1139 ret = os_snprintf(pos, end - pos, "%s%s",
1140 pos == buf ? "" : " ", name);
1141 if (ret < 0 || ret >= end - pos)
1142 break;
1143 pos += ret;
1144 }
1145 }
1146
1147 end[-1] = '\0';
1148
1149 return buf;
1150}
1151
1152
1153static int wpa_config_parse_password(const struct parse_data *data,
1154 struct wpa_ssid *ssid, int line,
1155 const char *value)
1156{
1157 u8 *hash;
1158
b56c0546
JM
1159 if (os_strcmp(value, "NULL") == 0) {
1160 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1161 os_free(ssid->eap.password);
1162 ssid->eap.password = NULL;
1163 ssid->eap.password_len = 0;
1164 return 0;
1165 }
1166
6fc6879b
JM
1167 if (os_strncmp(value, "hash:", 5) != 0) {
1168 char *tmp;
1169 size_t res_len;
1170
1171 tmp = wpa_config_parse_string(value, &res_len);
1172 if (tmp == NULL) {
1173 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1174 "password.", line);
1175 return -1;
1176 }
556f5a2a
HS
1177 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1178 (u8 *) tmp, res_len);
6fc6879b
JM
1179
1180 os_free(ssid->eap.password);
1181 ssid->eap.password = (u8 *) tmp;
1182 ssid->eap.password_len = res_len;
1183 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1184
1185 return 0;
1186 }
1187
1188
1189 /* NtPasswordHash: hash:<32 hex digits> */
1190 if (os_strlen(value + 5) != 2 * 16) {
1191 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1192 "(expected 32 hex digits)", line);
1193 return -1;
1194 }
1195
1196 hash = os_malloc(16);
1197 if (hash == NULL)
1198 return -1;
1199
1200 if (hexstr2bin(value + 5, hash, 16)) {
1201 os_free(hash);
1202 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1203 return -1;
1204 }
1205
1206 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1207
1208 os_free(ssid->eap.password);
1209 ssid->eap.password = hash;
1210 ssid->eap.password_len = 16;
1211 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1212
1213 return 0;
1214}
1215
1216
1217static char * wpa_config_write_password(const struct parse_data *data,
1218 struct wpa_ssid *ssid)
1219{
1220 char *buf;
1221
1222 if (ssid->eap.password == NULL)
1223 return NULL;
1224
1225 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1226 return wpa_config_write_string(
1227 ssid->eap.password, ssid->eap.password_len);
1228 }
1229
1230 buf = os_malloc(5 + 32 + 1);
1231 if (buf == NULL)
1232 return NULL;
1233
1234 os_memcpy(buf, "hash:", 5);
1235 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1236
1237 return buf;
1238}
1239#endif /* IEEE8021X_EAPOL */
1240
1241
1242static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1243 const char *value, int idx)
1244{
1245 char *buf, title[20];
1246 int res;
1247
1248 buf = wpa_config_parse_string(value, len);
1249 if (buf == NULL) {
1250 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1251 line, idx, value);
1252 return -1;
1253 }
1254 if (*len > MAX_WEP_KEY_LEN) {
1255 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1256 line, idx, value);
1257 os_free(buf);
1258 return -1;
1259 }
1260 os_memcpy(key, buf, *len);
1261 os_free(buf);
1262 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1263 if (res >= 0 && (size_t) res < sizeof(title))
1264 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1265 return 0;
1266}
1267
1268
1269static int wpa_config_parse_wep_key0(const struct parse_data *data,
1270 struct wpa_ssid *ssid, int line,
1271 const char *value)
1272{
1273 return wpa_config_parse_wep_key(ssid->wep_key[0],
1274 &ssid->wep_key_len[0], line,
1275 value, 0);
1276}
1277
1278
1279static int wpa_config_parse_wep_key1(const struct parse_data *data,
1280 struct wpa_ssid *ssid, int line,
1281 const char *value)
1282{
1283 return wpa_config_parse_wep_key(ssid->wep_key[1],
1284 &ssid->wep_key_len[1], line,
1285 value, 1);
1286}
1287
1288
1289static int wpa_config_parse_wep_key2(const struct parse_data *data,
1290 struct wpa_ssid *ssid, int line,
1291 const char *value)
1292{
1293 return wpa_config_parse_wep_key(ssid->wep_key[2],
1294 &ssid->wep_key_len[2], line,
1295 value, 2);
1296}
1297
1298
1299static int wpa_config_parse_wep_key3(const struct parse_data *data,
1300 struct wpa_ssid *ssid, int line,
1301 const char *value)
1302{
1303 return wpa_config_parse_wep_key(ssid->wep_key[3],
1304 &ssid->wep_key_len[3], line,
1305 value, 3);
1306}
1307
1308
1309#ifndef NO_CONFIG_WRITE
1310static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1311{
1312 if (ssid->wep_key_len[idx] == 0)
1313 return NULL;
1314 return wpa_config_write_string(ssid->wep_key[idx],
1315 ssid->wep_key_len[idx]);
1316}
1317
1318
1319static char * wpa_config_write_wep_key0(const struct parse_data *data,
1320 struct wpa_ssid *ssid)
1321{
1322 return wpa_config_write_wep_key(ssid, 0);
1323}
1324
1325
1326static char * wpa_config_write_wep_key1(const struct parse_data *data,
1327 struct wpa_ssid *ssid)
1328{
1329 return wpa_config_write_wep_key(ssid, 1);
1330}
1331
1332
1333static char * wpa_config_write_wep_key2(const struct parse_data *data,
1334 struct wpa_ssid *ssid)
1335{
1336 return wpa_config_write_wep_key(ssid, 2);
1337}
1338
1339
1340static char * wpa_config_write_wep_key3(const struct parse_data *data,
1341 struct wpa_ssid *ssid)
1342{
1343 return wpa_config_write_wep_key(ssid, 3);
1344}
1345#endif /* NO_CONFIG_WRITE */
1346
1347
1348/* Helper macros for network block parser */
1349
1350#ifdef OFFSET
1351#undef OFFSET
1352#endif /* OFFSET */
1353/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1354#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1355
1356/* STR: Define a string variable for an ASCII string; f = field name */
1357#ifdef NO_CONFIG_WRITE
1358#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1359#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1360#else /* NO_CONFIG_WRITE */
1361#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1362#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1363#endif /* NO_CONFIG_WRITE */
1364#define STR(f) _STR(f), NULL, NULL, NULL, 0
1365#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1366#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1367#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1368
1369/* STR_LEN: Define a string variable with a separate variable for storing the
1370 * data length. Unlike STR(), this can be used to store arbitrary binary data
1371 * (i.e., even nul termination character). */
1372#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1373#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1374#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1375#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1376#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1377
1378/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1379 * explicitly specified. */
1380#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1381#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1382#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1383
1384#ifdef NO_CONFIG_WRITE
1385#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1386#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1387#else /* NO_CONFIG_WRITE */
1388#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1389 OFFSET(f), (void *) 0
1390#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1391 OFFSET(eap.f), (void *) 0
1392#endif /* NO_CONFIG_WRITE */
1393
1394/* INT: Define an integer variable */
1395#define INT(f) _INT(f), NULL, NULL, 0
1396#define INTe(f) _INTe(f), NULL, NULL, 0
1397
1398/* INT_RANGE: Define an integer variable with allowed value range */
1399#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1400
1401/* FUNC: Define a configuration variable that uses a custom function for
1402 * parsing and writing the value. */
1403#ifdef NO_CONFIG_WRITE
1404#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1405#else /* NO_CONFIG_WRITE */
1406#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1407 NULL, NULL, NULL, NULL
1408#endif /* NO_CONFIG_WRITE */
1409#define FUNC(f) _FUNC(f), 0
1410#define FUNC_KEY(f) _FUNC(f), 1
1411
1412/*
1413 * Table of network configuration variables. This table is used to parse each
1414 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1415 * that is inside a network block.
1416 *
1417 * This table is generated using the helper macros defined above and with
1418 * generous help from the C pre-processor. The field name is stored as a string
1419 * into .name and for STR and INT types, the offset of the target buffer within
1420 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1421 * offset to the field containing the length of the configuration variable.
1422 * .param3 and .param4 can be used to mark the allowed range (length for STR
1423 * and value for INT).
1424 *
1425 * For each configuration line in wpa_supplicant.conf, the parser goes through
1426 * this table and select the entry that matches with the field name. The parser
1427 * function (.parser) is then called to parse the actual value of the field.
1428 *
1429 * This kind of mechanism makes it easy to add new configuration parameters,
1430 * since only one line needs to be added into this table and into the
1431 * struct wpa_ssid definition if the new variable is either a string or
1432 * integer. More complex types will need to use their own parser and writer
1433 * functions.
1434 */
1435static const struct parse_data ssid_fields[] = {
1436 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1437 { INT_RANGE(scan_ssid, 0, 1) },
1438 { FUNC(bssid) },
1439 { FUNC_KEY(psk) },
1440 { FUNC(proto) },
1441 { FUNC(key_mgmt) },
1442 { FUNC(pairwise) },
1443 { FUNC(group) },
1444 { FUNC(auth_alg) },
d3a98225 1445 { FUNC(scan_freq) },
b766a9a2 1446 { FUNC(freq_list) },
6fc6879b
JM
1447#ifdef IEEE8021X_EAPOL
1448 { FUNC(eap) },
1449 { STR_LENe(identity) },
1450 { STR_LENe(anonymous_identity) },
556f5a2a 1451 { FUNC_KEY(password) },
6fc6879b
JM
1452 { STRe(ca_cert) },
1453 { STRe(ca_path) },
1454 { STRe(client_cert) },
1455 { STRe(private_key) },
1456 { STR_KEYe(private_key_passwd) },
1457 { STRe(dh_file) },
1458 { STRe(subject_match) },
1459 { STRe(altsubject_match) },
1460 { STRe(ca_cert2) },
1461 { STRe(ca_path2) },
1462 { STRe(client_cert2) },
1463 { STRe(private_key2) },
1464 { STR_KEYe(private_key2_passwd) },
1465 { STRe(dh_file2) },
1466 { STRe(subject_match2) },
1467 { STRe(altsubject_match2) },
1468 { STRe(phase1) },
1469 { STRe(phase2) },
1470 { STRe(pcsc) },
1471 { STR_KEYe(pin) },
1472 { STRe(engine_id) },
1473 { STRe(key_id) },
61ee0f71
DS
1474 { STRe(cert_id) },
1475 { STRe(ca_cert_id) },
98842d51
CL
1476 { STR_KEYe(pin2) },
1477 { STRe(engine2_id) },
61ee0f71
DS
1478 { STRe(key2_id) },
1479 { STRe(cert2_id) },
1480 { STRe(ca_cert2_id) },
6fc6879b 1481 { INTe(engine) },
98842d51 1482 { INTe(engine2) },
6fc6879b
JM
1483 { INT(eapol_flags) },
1484#endif /* IEEE8021X_EAPOL */
1485 { FUNC_KEY(wep_key0) },
1486 { FUNC_KEY(wep_key1) },
1487 { FUNC_KEY(wep_key2) },
1488 { FUNC_KEY(wep_key3) },
1489 { INT(wep_tx_keyidx) },
1490 { INT(priority) },
1491#ifdef IEEE8021X_EAPOL
1492 { INT(eap_workaround) },
1493 { STRe(pac_file) },
1494 { INTe(fragment_size) },
1495#endif /* IEEE8021X_EAPOL */
2c5d725c 1496 { INT_RANGE(mode, 0, 4) },
6fc6879b 1497 { INT_RANGE(proactive_key_caching, 0, 1) },
4dac0245 1498 { INT_RANGE(disabled, 0, 2) },
6fc6879b
JM
1499 { STR(id_str) },
1500#ifdef CONFIG_IEEE80211W
1501 { INT_RANGE(ieee80211w, 0, 2) },
1502#endif /* CONFIG_IEEE80211W */
1503 { INT_RANGE(peerkey, 0, 1) },
1504 { INT_RANGE(mixed_cell, 0, 1) },
581a8cde 1505 { INT_RANGE(frequency, 0, 10000) },
60b94c98
JM
1506 { INT(wpa_ptk_rekey) },
1507 { STR(bgscan) },
6fc6879b
JM
1508};
1509
1510#undef OFFSET
1511#undef _STR
1512#undef STR
1513#undef STR_KEY
1514#undef _STR_LEN
1515#undef STR_LEN
1516#undef STR_LEN_KEY
1517#undef _STR_RANGE
1518#undef STR_RANGE
1519#undef STR_RANGE_KEY
1520#undef _INT
1521#undef INT
1522#undef INT_RANGE
1523#undef _FUNC
1524#undef FUNC
1525#undef FUNC_KEY
1526#define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1527
1528
1529/**
1530 * wpa_config_add_prio_network - Add a network to priority lists
1531 * @config: Configuration data from wpa_config_read()
1532 * @ssid: Pointer to the network configuration to be added to the list
1533 * Returns: 0 on success, -1 on failure
1534 *
1535 * This function is used to add a network block to the priority list of
1536 * networks. This must be called for each network when reading in the full
1537 * configuration. In addition, this can be used indirectly when updating
1538 * priorities by calling wpa_config_update_prio_list().
1539 */
1540int wpa_config_add_prio_network(struct wpa_config *config,
1541 struct wpa_ssid *ssid)
1542{
1543 int prio;
1544 struct wpa_ssid *prev, **nlist;
1545
1546 /*
1547 * Add to an existing priority list if one is available for the
1548 * configured priority level for this network.
1549 */
1550 for (prio = 0; prio < config->num_prio; prio++) {
1551 prev = config->pssid[prio];
1552 if (prev->priority == ssid->priority) {
1553 while (prev->pnext)
1554 prev = prev->pnext;
1555 prev->pnext = ssid;
1556 return 0;
1557 }
1558 }
1559
1560 /* First network for this priority - add a new priority list */
1561 nlist = os_realloc(config->pssid,
1562 (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1563 if (nlist == NULL)
1564 return -1;
1565
1566 for (prio = 0; prio < config->num_prio; prio++) {
1567 if (nlist[prio]->priority < ssid->priority)
1568 break;
1569 }
1570
1571 os_memmove(&nlist[prio + 1], &nlist[prio],
1572 (config->num_prio - prio) * sizeof(struct wpa_ssid *));
1573
1574 nlist[prio] = ssid;
1575 config->num_prio++;
1576 config->pssid = nlist;
1577
1578 return 0;
1579}
1580
1581
1582/**
1583 * wpa_config_update_prio_list - Update network priority list
1584 * @config: Configuration data from wpa_config_read()
1585 * Returns: 0 on success, -1 on failure
1586 *
1587 * This function is called to update the priority list of networks in the
1588 * configuration when a network is being added or removed. This is also called
1589 * if a priority for a network is changed.
1590 */
aa53509f 1591int wpa_config_update_prio_list(struct wpa_config *config)
6fc6879b
JM
1592{
1593 struct wpa_ssid *ssid;
1594 int ret = 0;
1595
1596 os_free(config->pssid);
1597 config->pssid = NULL;
1598 config->num_prio = 0;
1599
1600 ssid = config->ssid;
1601 while (ssid) {
1602 ssid->pnext = NULL;
1603 if (wpa_config_add_prio_network(config, ssid) < 0)
1604 ret = -1;
1605 ssid = ssid->next;
1606 }
1607
1608 return ret;
1609}
1610
1611
1612#ifdef IEEE8021X_EAPOL
1613static void eap_peer_config_free(struct eap_peer_config *eap)
1614{
1615 os_free(eap->eap_methods);
1616 os_free(eap->identity);
1617 os_free(eap->anonymous_identity);
1618 os_free(eap->password);
1619 os_free(eap->ca_cert);
1620 os_free(eap->ca_path);
1621 os_free(eap->client_cert);
1622 os_free(eap->private_key);
1623 os_free(eap->private_key_passwd);
1624 os_free(eap->dh_file);
1625 os_free(eap->subject_match);
1626 os_free(eap->altsubject_match);
1627 os_free(eap->ca_cert2);
1628 os_free(eap->ca_path2);
1629 os_free(eap->client_cert2);
1630 os_free(eap->private_key2);
1631 os_free(eap->private_key2_passwd);
1632 os_free(eap->dh_file2);
1633 os_free(eap->subject_match2);
1634 os_free(eap->altsubject_match2);
1635 os_free(eap->phase1);
1636 os_free(eap->phase2);
1637 os_free(eap->pcsc);
1638 os_free(eap->pin);
1639 os_free(eap->engine_id);
1640 os_free(eap->key_id);
61ee0f71
DS
1641 os_free(eap->cert_id);
1642 os_free(eap->ca_cert_id);
1643 os_free(eap->key2_id);
1644 os_free(eap->cert2_id);
1645 os_free(eap->ca_cert2_id);
98842d51
CL
1646 os_free(eap->pin2);
1647 os_free(eap->engine2_id);
6fc6879b
JM
1648 os_free(eap->otp);
1649 os_free(eap->pending_req_otp);
1650 os_free(eap->pac_file);
1651 os_free(eap->new_password);
1652}
1653#endif /* IEEE8021X_EAPOL */
1654
1655
1656/**
1657 * wpa_config_free_ssid - Free network/ssid configuration data
1658 * @ssid: Configuration data for the network
1659 *
1660 * This function frees all resources allocated for the network configuration
1661 * data.
1662 */
1663void wpa_config_free_ssid(struct wpa_ssid *ssid)
1664{
1665 os_free(ssid->ssid);
1666 os_free(ssid->passphrase);
1667#ifdef IEEE8021X_EAPOL
1668 eap_peer_config_free(&ssid->eap);
1669#endif /* IEEE8021X_EAPOL */
1670 os_free(ssid->id_str);
d3a98225 1671 os_free(ssid->scan_freq);
b766a9a2 1672 os_free(ssid->freq_list);
60b94c98 1673 os_free(ssid->bgscan);
6fc6879b
JM
1674 os_free(ssid);
1675}
1676
1677
1678/**
1679 * wpa_config_free - Free configuration data
1680 * @config: Configuration data from wpa_config_read()
1681 *
1682 * This function frees all resources allocated for the configuration data by
1683 * wpa_config_read().
1684 */
1685void wpa_config_free(struct wpa_config *config)
1686{
1687#ifndef CONFIG_NO_CONFIG_BLOBS
1688 struct wpa_config_blob *blob, *prevblob;
1689#endif /* CONFIG_NO_CONFIG_BLOBS */
1690 struct wpa_ssid *ssid, *prev = NULL;
e3768e7c
JM
1691 int i;
1692
6fc6879b
JM
1693 ssid = config->ssid;
1694 while (ssid) {
1695 prev = ssid;
1696 ssid = ssid->next;
1697 wpa_config_free_ssid(prev);
1698 }
1699
1700#ifndef CONFIG_NO_CONFIG_BLOBS
1701 blob = config->blobs;
1702 prevblob = NULL;
1703 while (blob) {
1704 prevblob = blob;
1705 blob = blob->next;
1706 wpa_config_free_blob(prevblob);
1707 }
1708#endif /* CONFIG_NO_CONFIG_BLOBS */
1709
1710 os_free(config->ctrl_interface);
1711 os_free(config->ctrl_interface_group);
6fc6879b
JM
1712 os_free(config->opensc_engine_path);
1713 os_free(config->pkcs11_engine_path);
1714 os_free(config->pkcs11_module_path);
6fc6879b 1715 os_free(config->driver_param);
3c0b7aa4
JM
1716 os_free(config->device_name);
1717 os_free(config->manufacturer);
1718 os_free(config->model_name);
1719 os_free(config->model_number);
1720 os_free(config->serial_number);
1721 os_free(config->device_type);
e3768e7c
JM
1722 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++)
1723 os_free(config->sec_device_type[i]);
c0e4dd9e 1724 os_free(config->config_methods);
e3768e7c 1725 os_free(config->p2p_ssid_postfix);
6fc6879b
JM
1726 os_free(config->pssid);
1727 os_free(config);
1728}
1729
1730
1731/**
1732 * wpa_config_get_network - Get configured network based on id
1733 * @config: Configuration data from wpa_config_read()
1734 * @id: Unique network id to search for
1735 * Returns: Network configuration or %NULL if not found
1736 */
1737struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1738{
1739 struct wpa_ssid *ssid;
1740
1741 ssid = config->ssid;
1742 while (ssid) {
1743 if (id == ssid->id)
1744 break;
1745 ssid = ssid->next;
1746 }
1747
1748 return ssid;
1749}
1750
1751
1752/**
1753 * wpa_config_add_network - Add a new network with empty configuration
1754 * @config: Configuration data from wpa_config_read()
1755 * Returns: The new network configuration or %NULL if operation failed
1756 */
1757struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1758{
1759 int id;
1760 struct wpa_ssid *ssid, *last = NULL;
1761
1762 id = -1;
1763 ssid = config->ssid;
1764 while (ssid) {
1765 if (ssid->id > id)
1766 id = ssid->id;
1767 last = ssid;
1768 ssid = ssid->next;
1769 }
1770 id++;
1771
1772 ssid = os_zalloc(sizeof(*ssid));
1773 if (ssid == NULL)
1774 return NULL;
1775 ssid->id = id;
1776 if (last)
1777 last->next = ssid;
1778 else
1779 config->ssid = ssid;
1780
1781 wpa_config_update_prio_list(config);
1782
1783 return ssid;
1784}
1785
1786
1787/**
1788 * wpa_config_remove_network - Remove a configured network based on id
1789 * @config: Configuration data from wpa_config_read()
1790 * @id: Unique network id to search for
1791 * Returns: 0 on success, or -1 if the network was not found
1792 */
1793int wpa_config_remove_network(struct wpa_config *config, int id)
1794{
1795 struct wpa_ssid *ssid, *prev = NULL;
1796
1797 ssid = config->ssid;
1798 while (ssid) {
1799 if (id == ssid->id)
1800 break;
1801 prev = ssid;
1802 ssid = ssid->next;
1803 }
1804
1805 if (ssid == NULL)
1806 return -1;
1807
1808 if (prev)
1809 prev->next = ssid->next;
1810 else
1811 config->ssid = ssid->next;
1812
1813 wpa_config_update_prio_list(config);
1814 wpa_config_free_ssid(ssid);
1815 return 0;
1816}
1817
1818
1819/**
1820 * wpa_config_set_network_defaults - Set network default values
1821 * @ssid: Pointer to network configuration data
1822 */
1823void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1824{
1825 ssid->proto = DEFAULT_PROTO;
1826 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1827 ssid->group_cipher = DEFAULT_GROUP;
1828 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1829#ifdef IEEE8021X_EAPOL
1830 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1831 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1832 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1833#endif /* IEEE8021X_EAPOL */
1834}
1835
1836
1837/**
1838 * wpa_config_set - Set a variable in network configuration
1839 * @ssid: Pointer to network configuration data
1840 * @var: Variable name, e.g., "ssid"
1841 * @value: Variable value
1842 * @line: Line number in configuration file or 0 if not used
1843 * Returns: 0 on success, -1 on failure
1844 *
1845 * This function can be used to set network configuration variables based on
1846 * both the configuration file and management interface input. The value
1847 * parameter must be in the same format as the text-based configuration file is
1848 * using. For example, strings are using double quotation marks.
1849 */
1850int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1851 int line)
1852{
1853 size_t i;
1854 int ret = 0;
1855
1856 if (ssid == NULL || var == NULL || value == NULL)
1857 return -1;
1858
1859 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1860 const struct parse_data *field = &ssid_fields[i];
1861 if (os_strcmp(var, field->name) != 0)
1862 continue;
1863
1864 if (field->parser(field, ssid, line, value)) {
1865 if (line) {
1866 wpa_printf(MSG_ERROR, "Line %d: failed to "
1867 "parse %s '%s'.", line, var, value);
1868 }
1869 ret = -1;
1870 }
1871 break;
1872 }
1873 if (i == NUM_SSID_FIELDS) {
1874 if (line) {
1875 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
1876 "'%s'.", line, var);
1877 }
1878 ret = -1;
1879 }
1880
1881 return ret;
1882}
1883
1884
3d3d3056
WS
1885/**
1886 * wpa_config_get_all - Get all options from network configuration
1887 * @ssid: Pointer to network configuration data
1888 * @get_keys: Determines if keys/passwords will be included in returned list
1889 * Returns: %NULL terminated list of all set keys and their values in the form
1890 * of [key1, val1, key2, val2, ... , NULL]
1891 *
1892 * This function can be used to get list of all configured network properties.
1893 * The caller is responsible for freeing the returned list and all its
1894 * elements.
1895 */
1896char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
1897{
1898 const struct parse_data *field;
1899 char *key, *value;
1900 size_t i;
1901 char **props;
1902 int fields_num;
1903
1904 props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
1905 if (!props)
1906 return NULL;
1907
1908 fields_num = 0;
1909 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1910 field = &ssid_fields[i];
1911 if (field->key_data && !get_keys)
1912 continue;
1913 value = field->writer(field, ssid);
04746865 1914 if (value == NULL)
3d3d3056 1915 continue;
04746865
JM
1916 if (os_strlen(value) == 0) {
1917 os_free(value);
1918 continue;
1919 }
3d3d3056
WS
1920
1921 key = os_strdup(field->name);
04746865
JM
1922 if (key == NULL) {
1923 os_free(value);
3d3d3056 1924 goto err;
04746865 1925 }
3d3d3056
WS
1926
1927 props[fields_num * 2] = key;
1928 props[fields_num * 2 + 1] = value;
1929
1930 fields_num++;
1931 }
1932
1933 return props;
1934
1935err:
1936 value = *props;
1937 while (value)
1938 os_free(value++);
1939 os_free(props);
1940 return NULL;
1941}
1942
1943
6fc6879b
JM
1944#ifndef NO_CONFIG_WRITE
1945/**
1946 * wpa_config_get - Get a variable in network configuration
1947 * @ssid: Pointer to network configuration data
1948 * @var: Variable name, e.g., "ssid"
1949 * Returns: Value of the variable or %NULL on failure
1950 *
1951 * This function can be used to get network configuration variables. The
1952 * returned value is a copy of the configuration variable in text format, i.e,.
1953 * the same format that the text-based configuration file and wpa_config_set()
1954 * are using for the value. The caller is responsible for freeing the returned
1955 * value.
1956 */
1957char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
1958{
1959 size_t i;
1960
1961 if (ssid == NULL || var == NULL)
1962 return NULL;
1963
1964 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1965 const struct parse_data *field = &ssid_fields[i];
1966 if (os_strcmp(var, field->name) == 0)
1967 return field->writer(field, ssid);
1968 }
1969
1970 return NULL;
1971}
1972
1973
1974/**
1975 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
1976 * @ssid: Pointer to network configuration data
1977 * @var: Variable name, e.g., "ssid"
1978 * Returns: Value of the variable or %NULL on failure
1979 *
1980 * This function can be used to get network configuration variable like
1981 * wpa_config_get(). The only difference is that this functions does not expose
1982 * key/password material from the configuration. In case a key/password field
1983 * is requested, the returned value is an empty string or %NULL if the variable
1984 * is not set or "*" if the variable is set (regardless of its value). The
1985 * returned value is a copy of the configuration variable in text format, i.e,.
1986 * the same format that the text-based configuration file and wpa_config_set()
1987 * are using for the value. The caller is responsible for freeing the returned
1988 * value.
1989 */
1990char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
1991{
1992 size_t i;
1993
1994 if (ssid == NULL || var == NULL)
1995 return NULL;
1996
1997 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1998 const struct parse_data *field = &ssid_fields[i];
1999 if (os_strcmp(var, field->name) == 0) {
2000 char *res = field->writer(field, ssid);
2001 if (field->key_data) {
2002 if (res && res[0]) {
2003 wpa_printf(MSG_DEBUG, "Do not allow "
2004 "key_data field to be "
2005 "exposed");
2006 os_free(res);
2007 return os_strdup("*");
2008 }
2009
2010 os_free(res);
2011 return NULL;
2012 }
2013 return res;
2014 }
2015 }
2016
2017 return NULL;
2018}
2019#endif /* NO_CONFIG_WRITE */
2020
2021
2022/**
2023 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2024 * @ssid: Pointer to network configuration data
2025 *
2026 * This function must be called to update WPA PSK when either SSID or the
2027 * passphrase has changed for the network configuration.
2028 */
2029void wpa_config_update_psk(struct wpa_ssid *ssid)
2030{
2031#ifndef CONFIG_NO_PBKDF2
2032 pbkdf2_sha1(ssid->passphrase,
2033 (char *) ssid->ssid, ssid->ssid_len, 4096,
2034 ssid->psk, PMK_LEN);
2035 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2036 ssid->psk, PMK_LEN);
2037 ssid->psk_set = 1;
2038#endif /* CONFIG_NO_PBKDF2 */
2039}
2040
2041
2042#ifndef CONFIG_NO_CONFIG_BLOBS
2043/**
2044 * wpa_config_get_blob - Get a named configuration blob
2045 * @config: Configuration data from wpa_config_read()
2046 * @name: Name of the blob
2047 * Returns: Pointer to blob data or %NULL if not found
2048 */
2049const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2050 const char *name)
2051{
2052 struct wpa_config_blob *blob = config->blobs;
2053
2054 while (blob) {
2055 if (os_strcmp(blob->name, name) == 0)
2056 return blob;
2057 blob = blob->next;
2058 }
2059 return NULL;
2060}
2061
2062
2063/**
2064 * wpa_config_set_blob - Set or add a named configuration blob
2065 * @config: Configuration data from wpa_config_read()
2066 * @blob: New value for the blob
2067 *
2068 * Adds a new configuration blob or replaces the current value of an existing
2069 * blob.
2070 */
2071void wpa_config_set_blob(struct wpa_config *config,
2072 struct wpa_config_blob *blob)
2073{
2074 wpa_config_remove_blob(config, blob->name);
2075 blob->next = config->blobs;
2076 config->blobs = blob;
2077}
2078
2079
2080/**
2081 * wpa_config_free_blob - Free blob data
2082 * @blob: Pointer to blob to be freed
2083 */
2084void wpa_config_free_blob(struct wpa_config_blob *blob)
2085{
2086 if (blob) {
2087 os_free(blob->name);
2088 os_free(blob->data);
2089 os_free(blob);
2090 }
2091}
2092
2093
2094/**
2095 * wpa_config_remove_blob - Remove a named configuration blob
2096 * @config: Configuration data from wpa_config_read()
2097 * @name: Name of the blob to remove
2098 * Returns: 0 if blob was removed or -1 if blob was not found
2099 */
2100int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2101{
2102 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2103
2104 while (pos) {
2105 if (os_strcmp(pos->name, name) == 0) {
2106 if (prev)
2107 prev->next = pos->next;
2108 else
2109 config->blobs = pos->next;
2110 wpa_config_free_blob(pos);
2111 return 0;
2112 }
2113 prev = pos;
2114 pos = pos->next;
2115 }
2116
2117 return -1;
2118}
2119#endif /* CONFIG_NO_CONFIG_BLOBS */
2120
2121
2122/**
2123 * wpa_config_alloc_empty - Allocate an empty configuration
2124 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2125 * socket
2126 * @driver_param: Driver parameters
2127 * Returns: Pointer to allocated configuration data or %NULL on failure
2128 */
2129struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2130 const char *driver_param)
2131{
2132 struct wpa_config *config;
2133
2134 config = os_zalloc(sizeof(*config));
2135 if (config == NULL)
2136 return NULL;
2137 config->eapol_version = DEFAULT_EAPOL_VERSION;
2138 config->ap_scan = DEFAULT_AP_SCAN;
2139 config->fast_reauth = DEFAULT_FAST_REAUTH;
e3768e7c 2140 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
c9c38b09 2141 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
6fc6879b
JM
2142
2143 if (ctrl_interface)
2144 config->ctrl_interface = os_strdup(ctrl_interface);
2145 if (driver_param)
2146 config->driver_param = os_strdup(driver_param);
2147
2148 return config;
2149}
2150
2151
2152#ifndef CONFIG_NO_STDOUT_DEBUG
2153/**
2154 * wpa_config_debug_dump_networks - Debug dump of configured networks
2155 * @config: Configuration data from wpa_config_read()
2156 */
2157void wpa_config_debug_dump_networks(struct wpa_config *config)
2158{
2159 int prio;
2160 struct wpa_ssid *ssid;
2161
2162 for (prio = 0; prio < config->num_prio; prio++) {
2163 ssid = config->pssid[prio];
2164 wpa_printf(MSG_DEBUG, "Priority group %d",
2165 ssid->priority);
2166 while (ssid) {
2167 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2168 ssid->id,
2169 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2170 ssid = ssid->pnext;
2171 }
2172 }
2173}
2174#endif /* CONFIG_NO_STDOUT_DEBUG */
121adf9c
JM
2175
2176
2177struct global_parse_data {
2178 char *name;
2179 int (*parser)(const struct global_parse_data *data,
2180 struct wpa_config *config, int line, const char *value);
2181 void *param1, *param2, *param3;
1d47214a 2182 unsigned int changed_flag;
121adf9c
JM
2183};
2184
2185
2186static int wpa_global_config_parse_int(const struct global_parse_data *data,
2187 struct wpa_config *config, int line,
2188 const char *pos)
2189{
2190 int *dst;
2191 dst = (int *) (((u8 *) config) + (long) data->param1);
2192 *dst = atoi(pos);
2193 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2194
2195 if (data->param2 && *dst < (long) data->param2) {
2196 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2197 "min_value=%ld)", line, data->name, *dst,
2198 (long) data->param2);
2199 *dst = (long) data->param2;
2200 return -1;
2201 }
2202
2203 if (data->param3 && *dst > (long) data->param3) {
2204 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2205 "max_value=%ld)", line, data->name, *dst,
2206 (long) data->param3);
2207 *dst = (long) data->param3;
2208 return -1;
2209 }
2210
2211 return 0;
2212}
2213
2214
2215static int wpa_global_config_parse_str(const struct global_parse_data *data,
2216 struct wpa_config *config, int line,
2217 const char *pos)
2218{
2219 size_t len;
2220 char **dst, *tmp;
2221
2222 len = os_strlen(pos);
2223 if (data->param2 && len < (size_t) data->param2) {
2224 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2225 "min_len=%ld)", line, data->name,
2226 (unsigned long) len, (long) data->param2);
2227 return -1;
2228 }
2229
2230 if (data->param3 && len > (size_t) data->param3) {
2231 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2232 "max_len=%ld)", line, data->name,
2233 (unsigned long) len, (long) data->param3);
2234 return -1;
2235 }
2236
2237 tmp = os_strdup(pos);
2238 if (tmp == NULL)
2239 return -1;
2240
2241 dst = (char **) (((u8 *) config) + (long) data->param1);
2242 os_free(*dst);
2243 *dst = tmp;
2244 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2245
2246 return 0;
2247}
2248
2249
2250static int wpa_config_process_country(const struct global_parse_data *data,
2251 struct wpa_config *config, int line,
2252 const char *pos)
2253{
2254 if (!pos[0] || !pos[1]) {
2255 wpa_printf(MSG_DEBUG, "Invalid country set");
2256 return -1;
2257 }
2258 config->country[0] = pos[0];
2259 config->country[1] = pos[1];
2260 wpa_printf(MSG_DEBUG, "country='%c%c'",
2261 config->country[0], config->country[1]);
2262 return 0;
2263}
2264
2265
2266static int wpa_config_process_load_dynamic_eap(
2267 const struct global_parse_data *data, struct wpa_config *config,
2268 int line, const char *so)
2269{
2270 int ret;
2271 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2272 ret = eap_peer_method_load(so);
2273 if (ret == -2) {
2274 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2275 "reloading.");
2276 } else if (ret) {
2277 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2278 "method '%s'.", line, so);
2279 return -1;
2280 }
2281
2282 return 0;
2283}
2284
2285
2286#ifdef CONFIG_WPS
2287
2288static int wpa_config_process_uuid(const struct global_parse_data *data,
2289 struct wpa_config *config, int line,
2290 const char *pos)
2291{
2292 char buf[40];
2293 if (uuid_str2bin(pos, config->uuid)) {
2294 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2295 return -1;
2296 }
2297 uuid_bin2str(config->uuid, buf, sizeof(buf));
2298 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2299 return 0;
2300}
2301
2302
2303static int wpa_config_process_os_version(const struct global_parse_data *data,
2304 struct wpa_config *config, int line,
2305 const char *pos)
2306{
2307 if (hexstr2bin(pos, config->os_version, 4)) {
2308 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2309 return -1;
2310 }
2311 wpa_printf(MSG_DEBUG, "os_version=%08x",
2312 WPA_GET_BE32(config->os_version));
2313 return 0;
2314}
2315
2316#endif /* CONFIG_WPS */
2317
e3768e7c
JM
2318#ifdef CONFIG_P2P
2319static int wpa_config_process_sec_device_type(
2320 const struct global_parse_data *data,
2321 struct wpa_config *config, int line, const char *pos)
2322{
2323 int idx;
2324
2325 for (idx = 0; idx < MAX_SEC_DEVICE_TYPES; idx++)
2326 if (config->sec_device_type[idx] == NULL)
2327 break;
2328 if (idx == MAX_SEC_DEVICE_TYPES) {
2329 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2330 "items", line);
2331 return -1;
2332 }
2333
2334 config->sec_device_type[idx] = os_strdup(pos);
2335 if (config->sec_device_type[idx] == NULL)
2336 return -1;
2337 return 0;
2338}
2339#endif /* CONFIG_P2P */
2340
121adf9c
JM
2341
2342#ifdef OFFSET
2343#undef OFFSET
2344#endif /* OFFSET */
2345/* OFFSET: Get offset of a variable within the wpa_config structure */
2346#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2347
2348#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2349#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2350#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2351#define INT(f) _INT(f), NULL, NULL
2352#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2353#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2354#define STR(f) _STR(f), NULL, NULL
2355#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2356
2357static const struct global_parse_data global_fields[] = {
2358#ifdef CONFIG_CTRL_IFACE
1d47214a
JM
2359 { STR(ctrl_interface), 0 },
2360 { STR(ctrl_interface_group), 0 } /* deprecated */,
121adf9c 2361#endif /* CONFIG_CTRL_IFACE */
1d47214a
JM
2362 { INT_RANGE(eapol_version, 1, 2), 0 },
2363 { INT(ap_scan), 0 },
2364 { INT(fast_reauth), 0 },
2365 { STR(opensc_engine_path), 0 },
2366 { STR(pkcs11_engine_path), 0 },
2367 { STR(pkcs11_module_path), 0 },
2368 { STR(driver_param), 0 },
2369 { INT(dot11RSNAConfigPMKLifetime), 0 },
2370 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2371 { INT(dot11RSNAConfigSATimeout), 0 },
121adf9c 2372#ifndef CONFIG_NO_CONFIG_WRITE
1d47214a 2373 { INT(update_config), 0 },
121adf9c 2374#endif /* CONFIG_NO_CONFIG_WRITE */
1d47214a 2375 { FUNC_NO_VAR(load_dynamic_eap), 0 },
121adf9c 2376#ifdef CONFIG_WPS
1d47214a
JM
2377 { FUNC(uuid), CFG_CHANGED_UUID },
2378 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2379 { STR_RANGE(manufacturer, 0, 64), 0 },
2380 { STR_RANGE(model_name, 0, 32), 0 },
2381 { STR_RANGE(model_number, 0, 32), 0 },
2382 { STR_RANGE(serial_number, 0, 32), 0 },
2383 { STR(device_type), CFG_CHANGED_DEVICE_TYPE },
2384 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2385 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2386 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
121adf9c 2387#endif /* CONFIG_WPS */
e3768e7c
JM
2388#ifdef CONFIG_P2P
2389 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2390 { INT(p2p_listen_reg_class), 0 },
2391 { INT(p2p_listen_channel), 0 },
2392 { INT(p2p_oper_reg_class), 0 },
2393 { INT(p2p_oper_channel), 0 },
2394 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2395 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2396 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2397#endif /* CONFIG_P2P */
1d47214a
JM
2398 { FUNC(country), CFG_CHANGED_COUNTRY },
2399 { INT(bss_max_count), 0 },
2400 { INT_RANGE(filter_ssids, 0, 1), 0 }
121adf9c
JM
2401};
2402
2403#undef FUNC
2404#undef _INT
2405#undef INT
2406#undef INT_RANGE
2407#undef _STR
2408#undef STR
2409#undef STR_RANGE
2410#define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
2411
2412
2413int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
2414{
2415 size_t i;
2416 int ret = 0;
2417
2418 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
2419 const struct global_parse_data *field = &global_fields[i];
2420 size_t flen = os_strlen(field->name);
2421 if (os_strncmp(pos, field->name, flen) != 0 ||
2422 pos[flen] != '=')
2423 continue;
2424
2425 if (field->parser(field, config, line, pos + flen + 1)) {
2426 wpa_printf(MSG_ERROR, "Line %d: failed to "
2427 "parse '%s'.", line, pos);
2428 ret = -1;
2429 }
1d47214a 2430 config->changed_parameters |= field->changed_flag;
121adf9c
JM
2431 break;
2432 }
2433 if (i == NUM_GLOBAL_FIELDS) {
1d47214a
JM
2434 if (line < 0)
2435 return -1;
121adf9c
JM
2436 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
2437 line, pos);
2438 ret = -1;
2439 }
2440
2441 return ret;
2442}