Standardize on a single definition of auth_alg bitfield values
[hostap-gosc2009.git] / wpa_supplicant / config.c
blobda56dc8e3f5b0e52485d9d8d728488fc47e4d8fc
1 /*
2 * WPA Supplicant / Configuration parser and common functions
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto/sha1.h"
19 #include "rsn_supp/wpa.h"
20 #include "eap_peer/eap.h"
21 #include "config.h"
24 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25 #define NO_CONFIG_WRITE
26 #endif
29 * Structure for network configuration parsing. This data is used to implement
30 * a generic parser for each network block variable. The table of configuration
31 * variables is defined below in this file (ssid_fields[]).
33 struct parse_data {
34 /* Configuration variable name */
35 char *name;
37 /* Parser function for this variable */
38 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
39 int line, const char *value);
41 #ifndef NO_CONFIG_WRITE
42 /* Writer function (i.e., to get the variable in text format from
43 * internal presentation). */
44 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
45 #endif /* NO_CONFIG_WRITE */
47 /* Variable specific parameters for the parser. */
48 void *param1, *param2, *param3, *param4;
50 /* 0 = this variable can be included in debug output and ctrl_iface
51 * 1 = this variable contains key/private data and it must not be
52 * included in debug output unless explicitly requested. In
53 * addition, this variable will not be readable through the
54 * ctrl_iface.
56 int key_data;
60 static char * wpa_config_parse_string(const char *value, size_t *len)
62 if (*value == '"') {
63 const char *pos;
64 char *str;
65 value++;
66 pos = os_strrchr(value, '"');
67 if (pos == NULL || pos[1] != '\0')
68 return NULL;
69 *len = pos - value;
70 str = os_malloc(*len + 1);
71 if (str == NULL)
72 return NULL;
73 os_memcpy(str, value, *len);
74 str[*len] = '\0';
75 return str;
76 } else {
77 u8 *str;
78 size_t tlen, hlen = os_strlen(value);
79 if (hlen & 1)
80 return NULL;
81 tlen = hlen / 2;
82 str = os_malloc(tlen + 1);
83 if (str == NULL)
84 return NULL;
85 if (hexstr2bin(value, str, tlen)) {
86 os_free(str);
87 return NULL;
89 str[tlen] = '\0';
90 *len = tlen;
91 return (char *) str;
96 static int wpa_config_parse_str(const struct parse_data *data,
97 struct wpa_ssid *ssid,
98 int line, const char *value)
100 size_t res_len, *dst_len;
101 char **dst, *tmp;
103 if (os_strcmp(value, "NULL") == 0) {
104 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
105 data->name);
106 tmp = NULL;
107 res_len = 0;
108 goto set;
111 tmp = wpa_config_parse_string(value, &res_len);
112 if (tmp == NULL) {
113 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
114 line, data->name,
115 data->key_data ? "[KEY DATA REMOVED]" : value);
116 return -1;
119 if (data->key_data) {
120 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
121 (u8 *) tmp, res_len);
122 } else {
123 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
124 (u8 *) tmp, res_len);
127 if (data->param3 && res_len < (size_t) data->param3) {
128 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
129 "min_len=%ld)", line, data->name,
130 (unsigned long) res_len, (long) data->param3);
131 os_free(tmp);
132 return -1;
135 if (data->param4 && res_len > (size_t) data->param4) {
136 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
137 "max_len=%ld)", line, data->name,
138 (unsigned long) res_len, (long) data->param4);
139 os_free(tmp);
140 return -1;
143 set:
144 dst = (char **) (((u8 *) ssid) + (long) data->param1);
145 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
146 os_free(*dst);
147 *dst = tmp;
148 if (data->param2)
149 *dst_len = res_len;
151 return 0;
155 #ifndef NO_CONFIG_WRITE
156 static int is_hex(const u8 *data, size_t len)
158 size_t i;
160 for (i = 0; i < len; i++) {
161 if (data[i] < 32 || data[i] >= 127)
162 return 1;
164 return 0;
168 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
170 char *buf;
172 buf = os_malloc(len + 3);
173 if (buf == NULL)
174 return NULL;
175 buf[0] = '"';
176 os_memcpy(buf + 1, value, len);
177 buf[len + 1] = '"';
178 buf[len + 2] = '\0';
180 return buf;
184 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
186 char *buf;
188 buf = os_zalloc(2 * len + 1);
189 if (buf == NULL)
190 return NULL;
191 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
193 return buf;
197 static char * wpa_config_write_string(const u8 *value, size_t len)
199 if (value == NULL)
200 return NULL;
202 if (is_hex(value, len))
203 return wpa_config_write_string_hex(value, len);
204 else
205 return wpa_config_write_string_ascii(value, len);
209 static char * wpa_config_write_str(const struct parse_data *data,
210 struct wpa_ssid *ssid)
212 size_t len;
213 char **src;
215 src = (char **) (((u8 *) ssid) + (long) data->param1);
216 if (*src == NULL)
217 return NULL;
219 if (data->param2)
220 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
221 else
222 len = os_strlen(*src);
224 return wpa_config_write_string((const u8 *) *src, len);
226 #endif /* NO_CONFIG_WRITE */
229 static int wpa_config_parse_int(const struct parse_data *data,
230 struct wpa_ssid *ssid,
231 int line, const char *value)
233 int *dst;
235 dst = (int *) (((u8 *) ssid) + (long) data->param1);
236 *dst = atoi(value);
237 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
239 if (data->param3 && *dst < (long) data->param3) {
240 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
241 "min_value=%ld)", line, data->name, *dst,
242 (long) data->param3);
243 *dst = (long) data->param3;
244 return -1;
247 if (data->param4 && *dst > (long) data->param4) {
248 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
249 "max_value=%ld)", line, data->name, *dst,
250 (long) data->param4);
251 *dst = (long) data->param4;
252 return -1;
255 return 0;
259 #ifndef NO_CONFIG_WRITE
260 static char * wpa_config_write_int(const struct parse_data *data,
261 struct wpa_ssid *ssid)
263 int *src, res;
264 char *value;
266 src = (int *) (((u8 *) ssid) + (long) data->param1);
268 value = os_malloc(20);
269 if (value == NULL)
270 return NULL;
271 res = os_snprintf(value, 20, "%d", *src);
272 if (res < 0 || res >= 20) {
273 os_free(value);
274 return NULL;
276 value[20 - 1] = '\0';
277 return value;
279 #endif /* NO_CONFIG_WRITE */
282 static int wpa_config_parse_bssid(const struct parse_data *data,
283 struct wpa_ssid *ssid, int line,
284 const char *value)
286 if (hwaddr_aton(value, ssid->bssid)) {
287 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
288 line, value);
289 return -1;
291 ssid->bssid_set = 1;
292 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
293 return 0;
297 #ifndef NO_CONFIG_WRITE
298 static char * wpa_config_write_bssid(const struct parse_data *data,
299 struct wpa_ssid *ssid)
301 char *value;
302 int res;
304 if (!ssid->bssid_set)
305 return NULL;
307 value = os_malloc(20);
308 if (value == NULL)
309 return NULL;
310 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
311 if (res < 0 || res >= 20) {
312 os_free(value);
313 return NULL;
315 value[20 - 1] = '\0';
316 return value;
318 #endif /* NO_CONFIG_WRITE */
321 static int wpa_config_parse_psk(const struct parse_data *data,
322 struct wpa_ssid *ssid, int line,
323 const char *value)
325 if (*value == '"') {
326 #ifndef CONFIG_NO_PBKDF2
327 const char *pos;
328 size_t len;
330 value++;
331 pos = os_strrchr(value, '"');
332 if (pos)
333 len = pos - value;
334 else
335 len = os_strlen(value);
336 if (len < 8 || len > 63) {
337 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
338 "length %lu (expected: 8..63) '%s'.",
339 line, (unsigned long) len, value);
340 return -1;
342 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
343 (u8 *) value, len);
344 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
345 os_memcmp(ssid->passphrase, value, len) == 0)
346 return 0;
347 ssid->psk_set = 0;
348 os_free(ssid->passphrase);
349 ssid->passphrase = os_malloc(len + 1);
350 if (ssid->passphrase == NULL)
351 return -1;
352 os_memcpy(ssid->passphrase, value, len);
353 ssid->passphrase[len] = '\0';
354 return 0;
355 #else /* CONFIG_NO_PBKDF2 */
356 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
357 "supported.", line);
358 return -1;
359 #endif /* CONFIG_NO_PBKDF2 */
362 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
363 value[PMK_LEN * 2] != '\0') {
364 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
365 line, value);
366 return -1;
369 os_free(ssid->passphrase);
370 ssid->passphrase = NULL;
372 ssid->psk_set = 1;
373 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
374 return 0;
378 #ifndef NO_CONFIG_WRITE
379 static char * wpa_config_write_psk(const struct parse_data *data,
380 struct wpa_ssid *ssid)
382 if (ssid->passphrase)
383 return wpa_config_write_string_ascii(
384 (const u8 *) ssid->passphrase,
385 os_strlen(ssid->passphrase));
387 if (ssid->psk_set)
388 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
390 return NULL;
392 #endif /* NO_CONFIG_WRITE */
395 static int wpa_config_parse_proto(const struct parse_data *data,
396 struct wpa_ssid *ssid, int line,
397 const char *value)
399 int val = 0, last, errors = 0;
400 char *start, *end, *buf;
402 buf = os_strdup(value);
403 if (buf == NULL)
404 return -1;
405 start = buf;
407 while (*start != '\0') {
408 while (*start == ' ' || *start == '\t')
409 start++;
410 if (*start == '\0')
411 break;
412 end = start;
413 while (*end != ' ' && *end != '\t' && *end != '\0')
414 end++;
415 last = *end == '\0';
416 *end = '\0';
417 if (os_strcmp(start, "WPA") == 0)
418 val |= WPA_PROTO_WPA;
419 else if (os_strcmp(start, "RSN") == 0 ||
420 os_strcmp(start, "WPA2") == 0)
421 val |= WPA_PROTO_RSN;
422 else {
423 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
424 line, start);
425 errors++;
428 if (last)
429 break;
430 start = end + 1;
432 os_free(buf);
434 if (val == 0) {
435 wpa_printf(MSG_ERROR,
436 "Line %d: no proto values configured.", line);
437 errors++;
440 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
441 ssid->proto = val;
442 return errors ? -1 : 0;
446 #ifndef NO_CONFIG_WRITE
447 static char * wpa_config_write_proto(const struct parse_data *data,
448 struct wpa_ssid *ssid)
450 int first = 1, ret;
451 char *buf, *pos, *end;
453 pos = buf = os_zalloc(10);
454 if (buf == NULL)
455 return NULL;
456 end = buf + 10;
458 if (ssid->proto & WPA_PROTO_WPA) {
459 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
460 if (ret < 0 || ret >= end - pos)
461 return buf;
462 pos += ret;
463 first = 0;
466 if (ssid->proto & WPA_PROTO_RSN) {
467 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
468 if (ret < 0 || ret >= end - pos)
469 return buf;
470 pos += ret;
471 first = 0;
474 return buf;
476 #endif /* NO_CONFIG_WRITE */
479 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
480 struct wpa_ssid *ssid, int line,
481 const char *value)
483 int val = 0, last, errors = 0;
484 char *start, *end, *buf;
486 buf = os_strdup(value);
487 if (buf == NULL)
488 return -1;
489 start = buf;
491 while (*start != '\0') {
492 while (*start == ' ' || *start == '\t')
493 start++;
494 if (*start == '\0')
495 break;
496 end = start;
497 while (*end != ' ' && *end != '\t' && *end != '\0')
498 end++;
499 last = *end == '\0';
500 *end = '\0';
501 if (os_strcmp(start, "WPA-PSK") == 0)
502 val |= WPA_KEY_MGMT_PSK;
503 else if (os_strcmp(start, "WPA-EAP") == 0)
504 val |= WPA_KEY_MGMT_IEEE8021X;
505 else if (os_strcmp(start, "IEEE8021X") == 0)
506 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
507 else if (os_strcmp(start, "NONE") == 0)
508 val |= WPA_KEY_MGMT_NONE;
509 else if (os_strcmp(start, "WPA-NONE") == 0)
510 val |= WPA_KEY_MGMT_WPA_NONE;
511 #ifdef CONFIG_IEEE80211R
512 else if (os_strcmp(start, "FT-PSK") == 0)
513 val |= WPA_KEY_MGMT_FT_PSK;
514 else if (os_strcmp(start, "FT-EAP") == 0)
515 val |= WPA_KEY_MGMT_FT_IEEE8021X;
516 #endif /* CONFIG_IEEE80211R */
517 #ifdef CONFIG_IEEE80211W
518 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
519 val |= WPA_KEY_MGMT_PSK_SHA256;
520 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
521 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
522 #endif /* CONFIG_IEEE80211W */
523 #ifdef CONFIG_WPS
524 else if (os_strcmp(start, "WPS") == 0)
525 val |= WPA_KEY_MGMT_WPS;
526 #endif /* CONFIG_WPS */
527 else {
528 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
529 line, start);
530 errors++;
533 if (last)
534 break;
535 start = end + 1;
537 os_free(buf);
539 if (val == 0) {
540 wpa_printf(MSG_ERROR,
541 "Line %d: no key_mgmt values configured.", line);
542 errors++;
545 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
546 ssid->key_mgmt = val;
547 return errors ? -1 : 0;
551 #ifndef NO_CONFIG_WRITE
552 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
553 struct wpa_ssid *ssid)
555 char *buf, *pos, *end;
556 int ret;
558 pos = buf = os_zalloc(50);
559 if (buf == NULL)
560 return NULL;
561 end = buf + 50;
563 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
564 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
565 pos == buf ? "" : " ");
566 if (ret < 0 || ret >= end - pos) {
567 end[-1] = '\0';
568 return buf;
570 pos += ret;
573 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
574 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
575 pos == buf ? "" : " ");
576 if (ret < 0 || ret >= end - pos) {
577 end[-1] = '\0';
578 return buf;
580 pos += ret;
583 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
584 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
585 pos == buf ? "" : " ");
586 if (ret < 0 || ret >= end - pos) {
587 end[-1] = '\0';
588 return buf;
590 pos += ret;
593 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
594 ret = os_snprintf(pos, end - pos, "%sNONE",
595 pos == buf ? "" : " ");
596 if (ret < 0 || ret >= end - pos) {
597 end[-1] = '\0';
598 return buf;
600 pos += ret;
603 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
604 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
605 pos == buf ? "" : " ");
606 if (ret < 0 || ret >= end - pos) {
607 end[-1] = '\0';
608 return buf;
610 pos += ret;
613 #ifdef CONFIG_IEEE80211R
614 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
615 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
616 pos == buf ? "" : " ");
618 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
619 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
620 pos == buf ? "" : " ");
621 #endif /* CONFIG_IEEE80211R */
623 #ifdef CONFIG_IEEE80211W
624 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
625 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
626 pos == buf ? "" : " ");
628 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
629 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
630 pos == buf ? "" : " ");
631 #endif /* CONFIG_IEEE80211W */
633 #ifdef CONFIG_WPS
634 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
635 pos += os_snprintf(pos, end - pos, "%sWPS",
636 pos == buf ? "" : " ");
637 #endif /* CONFIG_WPS */
639 return buf;
641 #endif /* NO_CONFIG_WRITE */
644 static int wpa_config_parse_cipher(int line, const char *value)
646 int val = 0, last;
647 char *start, *end, *buf;
649 buf = os_strdup(value);
650 if (buf == NULL)
651 return -1;
652 start = buf;
654 while (*start != '\0') {
655 while (*start == ' ' || *start == '\t')
656 start++;
657 if (*start == '\0')
658 break;
659 end = start;
660 while (*end != ' ' && *end != '\t' && *end != '\0')
661 end++;
662 last = *end == '\0';
663 *end = '\0';
664 if (os_strcmp(start, "CCMP") == 0)
665 val |= WPA_CIPHER_CCMP;
666 else if (os_strcmp(start, "TKIP") == 0)
667 val |= WPA_CIPHER_TKIP;
668 else if (os_strcmp(start, "WEP104") == 0)
669 val |= WPA_CIPHER_WEP104;
670 else if (os_strcmp(start, "WEP40") == 0)
671 val |= WPA_CIPHER_WEP40;
672 else if (os_strcmp(start, "NONE") == 0)
673 val |= WPA_CIPHER_NONE;
674 else {
675 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
676 line, start);
677 os_free(buf);
678 return -1;
681 if (last)
682 break;
683 start = end + 1;
685 os_free(buf);
687 if (val == 0) {
688 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
689 line);
690 return -1;
692 return val;
696 #ifndef NO_CONFIG_WRITE
697 static char * wpa_config_write_cipher(int cipher)
699 char *buf, *pos, *end;
700 int ret;
702 pos = buf = os_zalloc(50);
703 if (buf == NULL)
704 return NULL;
705 end = buf + 50;
707 if (cipher & WPA_CIPHER_CCMP) {
708 ret = os_snprintf(pos, end - pos, "%sCCMP",
709 pos == buf ? "" : " ");
710 if (ret < 0 || ret >= end - pos) {
711 end[-1] = '\0';
712 return buf;
714 pos += ret;
717 if (cipher & WPA_CIPHER_TKIP) {
718 ret = os_snprintf(pos, end - pos, "%sTKIP",
719 pos == buf ? "" : " ");
720 if (ret < 0 || ret >= end - pos) {
721 end[-1] = '\0';
722 return buf;
724 pos += ret;
727 if (cipher & WPA_CIPHER_WEP104) {
728 ret = os_snprintf(pos, end - pos, "%sWEP104",
729 pos == buf ? "" : " ");
730 if (ret < 0 || ret >= end - pos) {
731 end[-1] = '\0';
732 return buf;
734 pos += ret;
737 if (cipher & WPA_CIPHER_WEP40) {
738 ret = os_snprintf(pos, end - pos, "%sWEP40",
739 pos == buf ? "" : " ");
740 if (ret < 0 || ret >= end - pos) {
741 end[-1] = '\0';
742 return buf;
744 pos += ret;
747 if (cipher & WPA_CIPHER_NONE) {
748 ret = os_snprintf(pos, end - pos, "%sNONE",
749 pos == buf ? "" : " ");
750 if (ret < 0 || ret >= end - pos) {
751 end[-1] = '\0';
752 return buf;
754 pos += ret;
757 return buf;
759 #endif /* NO_CONFIG_WRITE */
762 static int wpa_config_parse_pairwise(const struct parse_data *data,
763 struct wpa_ssid *ssid, int line,
764 const char *value)
766 int val;
767 val = wpa_config_parse_cipher(line, value);
768 if (val == -1)
769 return -1;
770 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
771 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
772 "(0x%x).", line, val);
773 return -1;
776 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
777 ssid->pairwise_cipher = val;
778 return 0;
782 #ifndef NO_CONFIG_WRITE
783 static char * wpa_config_write_pairwise(const struct parse_data *data,
784 struct wpa_ssid *ssid)
786 return wpa_config_write_cipher(ssid->pairwise_cipher);
788 #endif /* NO_CONFIG_WRITE */
791 static int wpa_config_parse_group(const struct parse_data *data,
792 struct wpa_ssid *ssid, int line,
793 const char *value)
795 int val;
796 val = wpa_config_parse_cipher(line, value);
797 if (val == -1)
798 return -1;
799 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
800 WPA_CIPHER_WEP40)) {
801 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
802 "(0x%x).", line, val);
803 return -1;
806 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
807 ssid->group_cipher = val;
808 return 0;
812 #ifndef NO_CONFIG_WRITE
813 static char * wpa_config_write_group(const struct parse_data *data,
814 struct wpa_ssid *ssid)
816 return wpa_config_write_cipher(ssid->group_cipher);
818 #endif /* NO_CONFIG_WRITE */
821 static int wpa_config_parse_auth_alg(const struct parse_data *data,
822 struct wpa_ssid *ssid, int line,
823 const char *value)
825 int val = 0, last, errors = 0;
826 char *start, *end, *buf;
828 buf = os_strdup(value);
829 if (buf == NULL)
830 return -1;
831 start = buf;
833 while (*start != '\0') {
834 while (*start == ' ' || *start == '\t')
835 start++;
836 if (*start == '\0')
837 break;
838 end = start;
839 while (*end != ' ' && *end != '\t' && *end != '\0')
840 end++;
841 last = *end == '\0';
842 *end = '\0';
843 if (os_strcmp(start, "OPEN") == 0)
844 val |= WPA_AUTH_ALG_OPEN;
845 else if (os_strcmp(start, "SHARED") == 0)
846 val |= WPA_AUTH_ALG_SHARED;
847 else if (os_strcmp(start, "LEAP") == 0)
848 val |= WPA_AUTH_ALG_LEAP;
849 else {
850 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
851 line, start);
852 errors++;
855 if (last)
856 break;
857 start = end + 1;
859 os_free(buf);
861 if (val == 0) {
862 wpa_printf(MSG_ERROR,
863 "Line %d: no auth_alg values configured.", line);
864 errors++;
867 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
868 ssid->auth_alg = val;
869 return errors ? -1 : 0;
873 #ifndef NO_CONFIG_WRITE
874 static char * wpa_config_write_auth_alg(const struct parse_data *data,
875 struct wpa_ssid *ssid)
877 char *buf, *pos, *end;
878 int ret;
880 pos = buf = os_zalloc(30);
881 if (buf == NULL)
882 return NULL;
883 end = buf + 30;
885 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
886 ret = os_snprintf(pos, end - pos, "%sOPEN",
887 pos == buf ? "" : " ");
888 if (ret < 0 || ret >= end - pos) {
889 end[-1] = '\0';
890 return buf;
892 pos += ret;
895 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
896 ret = os_snprintf(pos, end - pos, "%sSHARED",
897 pos == buf ? "" : " ");
898 if (ret < 0 || ret >= end - pos) {
899 end[-1] = '\0';
900 return buf;
902 pos += ret;
905 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
906 ret = os_snprintf(pos, end - pos, "%sLEAP",
907 pos == buf ? "" : " ");
908 if (ret < 0 || ret >= end - pos) {
909 end[-1] = '\0';
910 return buf;
912 pos += ret;
915 return buf;
917 #endif /* NO_CONFIG_WRITE */
920 static int wpa_config_parse_scan_freq(const struct parse_data *data,
921 struct wpa_ssid *ssid, int line,
922 const char *value)
924 int *freqs;
925 size_t used, len;
926 const char *pos;
928 used = 0;
929 len = 10;
930 freqs = os_zalloc((len + 1) * sizeof(int));
931 if (freqs == NULL)
932 return -1;
934 pos = value;
935 while (pos) {
936 while (*pos == ' ')
937 pos++;
938 if (used == len) {
939 int *n;
940 size_t i;
941 n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
942 if (n == NULL) {
943 os_free(freqs);
944 return -1;
946 for (i = len; i <= len * 2; i++)
947 n[i] = 0;
948 freqs = n;
949 len *= 2;
952 freqs[used] = atoi(pos);
953 if (freqs[used] == 0)
954 break;
955 used++;
956 pos = os_strchr(pos + 1, ' ');
959 os_free(ssid->scan_freq);
960 ssid->scan_freq = freqs;
962 return 0;
966 #ifndef NO_CONFIG_WRITE
967 static char * wpa_config_write_scan_freq(const struct parse_data *data,
968 struct wpa_ssid *ssid)
970 char *buf, *pos, *end;
971 int i, ret;
972 size_t count;
974 if (ssid->scan_freq == NULL)
975 return NULL;
977 count = 0;
978 for (i = 0; ssid->scan_freq[i]; i++)
979 count++;
981 pos = buf = os_zalloc(10 * count + 1);
982 if (buf == NULL)
983 return NULL;
984 end = buf + 10 * count + 1;
986 for (i = 0; ssid->scan_freq[i]; i++) {
987 ret = os_snprintf(pos, end - pos, "%s%u",
988 i == 0 ? "" : " ", ssid->scan_freq[i]);
989 if (ret < 0 || ret >= end - pos) {
990 end[-1] = '\0';
991 return buf;
993 pos += ret;
996 return buf;
998 #endif /* NO_CONFIG_WRITE */
1001 #ifdef IEEE8021X_EAPOL
1002 static int wpa_config_parse_eap(const struct parse_data *data,
1003 struct wpa_ssid *ssid, int line,
1004 const char *value)
1006 int last, errors = 0;
1007 char *start, *end, *buf;
1008 struct eap_method_type *methods = NULL, *tmp;
1009 size_t num_methods = 0;
1011 buf = os_strdup(value);
1012 if (buf == NULL)
1013 return -1;
1014 start = buf;
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;
1027 methods = os_realloc(methods,
1028 (num_methods + 1) * sizeof(*methods));
1029 if (methods == NULL) {
1030 os_free(tmp);
1031 os_free(buf);
1032 return -1;
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;
1055 os_free(buf);
1057 tmp = methods;
1058 methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1059 if (methods == NULL) {
1060 os_free(tmp);
1061 return -1;
1063 methods[num_methods].vendor = EAP_VENDOR_IETF;
1064 methods[num_methods].method = EAP_TYPE_NONE;
1065 num_methods++;
1067 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1068 (u8 *) methods, num_methods * sizeof(*methods));
1069 ssid->eap.eap_methods = methods;
1070 return errors ? -1 : 0;
1074 static char * wpa_config_write_eap(const struct parse_data *data,
1075 struct wpa_ssid *ssid)
1077 int i, ret;
1078 char *buf, *pos, *end;
1079 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1080 const char *name;
1082 if (eap_methods == NULL)
1083 return NULL;
1085 pos = buf = os_zalloc(100);
1086 if (buf == NULL)
1087 return NULL;
1088 end = buf + 100;
1090 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1091 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1092 name = eap_get_name(eap_methods[i].vendor,
1093 eap_methods[i].method);
1094 if (name) {
1095 ret = os_snprintf(pos, end - pos, "%s%s",
1096 pos == buf ? "" : " ", name);
1097 if (ret < 0 || ret >= end - pos)
1098 break;
1099 pos += ret;
1103 end[-1] = '\0';
1105 return buf;
1109 static int wpa_config_parse_password(const struct parse_data *data,
1110 struct wpa_ssid *ssid, int line,
1111 const char *value)
1113 u8 *hash;
1115 if (os_strcmp(value, "NULL") == 0) {
1116 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1117 os_free(ssid->eap.password);
1118 ssid->eap.password = NULL;
1119 ssid->eap.password_len = 0;
1120 return 0;
1123 if (os_strncmp(value, "hash:", 5) != 0) {
1124 char *tmp;
1125 size_t res_len;
1127 tmp = wpa_config_parse_string(value, &res_len);
1128 if (tmp == NULL) {
1129 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1130 "password.", line);
1131 return -1;
1133 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1134 (u8 *) tmp, res_len);
1136 os_free(ssid->eap.password);
1137 ssid->eap.password = (u8 *) tmp;
1138 ssid->eap.password_len = res_len;
1139 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1141 return 0;
1145 /* NtPasswordHash: hash:<32 hex digits> */
1146 if (os_strlen(value + 5) != 2 * 16) {
1147 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1148 "(expected 32 hex digits)", line);
1149 return -1;
1152 hash = os_malloc(16);
1153 if (hash == NULL)
1154 return -1;
1156 if (hexstr2bin(value + 5, hash, 16)) {
1157 os_free(hash);
1158 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1159 return -1;
1162 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1164 os_free(ssid->eap.password);
1165 ssid->eap.password = hash;
1166 ssid->eap.password_len = 16;
1167 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1169 return 0;
1173 static char * wpa_config_write_password(const struct parse_data *data,
1174 struct wpa_ssid *ssid)
1176 char *buf;
1178 if (ssid->eap.password == NULL)
1179 return NULL;
1181 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1182 return wpa_config_write_string(
1183 ssid->eap.password, ssid->eap.password_len);
1186 buf = os_malloc(5 + 32 + 1);
1187 if (buf == NULL)
1188 return NULL;
1190 os_memcpy(buf, "hash:", 5);
1191 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1193 return buf;
1195 #endif /* IEEE8021X_EAPOL */
1198 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1199 const char *value, int idx)
1201 char *buf, title[20];
1202 int res;
1204 buf = wpa_config_parse_string(value, len);
1205 if (buf == NULL) {
1206 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1207 line, idx, value);
1208 return -1;
1210 if (*len > MAX_WEP_KEY_LEN) {
1211 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1212 line, idx, value);
1213 os_free(buf);
1214 return -1;
1216 os_memcpy(key, buf, *len);
1217 os_free(buf);
1218 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1219 if (res >= 0 && (size_t) res < sizeof(title))
1220 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1221 return 0;
1225 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1226 struct wpa_ssid *ssid, int line,
1227 const char *value)
1229 return wpa_config_parse_wep_key(ssid->wep_key[0],
1230 &ssid->wep_key_len[0], line,
1231 value, 0);
1235 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1236 struct wpa_ssid *ssid, int line,
1237 const char *value)
1239 return wpa_config_parse_wep_key(ssid->wep_key[1],
1240 &ssid->wep_key_len[1], line,
1241 value, 1);
1245 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1246 struct wpa_ssid *ssid, int line,
1247 const char *value)
1249 return wpa_config_parse_wep_key(ssid->wep_key[2],
1250 &ssid->wep_key_len[2], line,
1251 value, 2);
1255 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1256 struct wpa_ssid *ssid, int line,
1257 const char *value)
1259 return wpa_config_parse_wep_key(ssid->wep_key[3],
1260 &ssid->wep_key_len[3], line,
1261 value, 3);
1265 #ifndef NO_CONFIG_WRITE
1266 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1268 if (ssid->wep_key_len[idx] == 0)
1269 return NULL;
1270 return wpa_config_write_string(ssid->wep_key[idx],
1271 ssid->wep_key_len[idx]);
1275 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1276 struct wpa_ssid *ssid)
1278 return wpa_config_write_wep_key(ssid, 0);
1282 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1283 struct wpa_ssid *ssid)
1285 return wpa_config_write_wep_key(ssid, 1);
1289 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1290 struct wpa_ssid *ssid)
1292 return wpa_config_write_wep_key(ssid, 2);
1296 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1297 struct wpa_ssid *ssid)
1299 return wpa_config_write_wep_key(ssid, 3);
1301 #endif /* NO_CONFIG_WRITE */
1304 /* Helper macros for network block parser */
1306 #ifdef OFFSET
1307 #undef OFFSET
1308 #endif /* OFFSET */
1309 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1310 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1312 /* STR: Define a string variable for an ASCII string; f = field name */
1313 #ifdef NO_CONFIG_WRITE
1314 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1315 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1316 #else /* NO_CONFIG_WRITE */
1317 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1318 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1319 #endif /* NO_CONFIG_WRITE */
1320 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1321 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1322 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1323 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1325 /* STR_LEN: Define a string variable with a separate variable for storing the
1326 * data length. Unlike STR(), this can be used to store arbitrary binary data
1327 * (i.e., even nul termination character). */
1328 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1329 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1330 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1331 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1332 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1334 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1335 * explicitly specified. */
1336 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1337 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1338 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1340 #ifdef NO_CONFIG_WRITE
1341 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1342 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1343 #else /* NO_CONFIG_WRITE */
1344 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1345 OFFSET(f), (void *) 0
1346 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1347 OFFSET(eap.f), (void *) 0
1348 #endif /* NO_CONFIG_WRITE */
1350 /* INT: Define an integer variable */
1351 #define INT(f) _INT(f), NULL, NULL, 0
1352 #define INTe(f) _INTe(f), NULL, NULL, 0
1354 /* INT_RANGE: Define an integer variable with allowed value range */
1355 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1357 /* FUNC: Define a configuration variable that uses a custom function for
1358 * parsing and writing the value. */
1359 #ifdef NO_CONFIG_WRITE
1360 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1361 #else /* NO_CONFIG_WRITE */
1362 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1363 NULL, NULL, NULL, NULL
1364 #endif /* NO_CONFIG_WRITE */
1365 #define FUNC(f) _FUNC(f), 0
1366 #define FUNC_KEY(f) _FUNC(f), 1
1369 * Table of network configuration variables. This table is used to parse each
1370 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1371 * that is inside a network block.
1373 * This table is generated using the helper macros defined above and with
1374 * generous help from the C pre-processor. The field name is stored as a string
1375 * into .name and for STR and INT types, the offset of the target buffer within
1376 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1377 * offset to the field containing the length of the configuration variable.
1378 * .param3 and .param4 can be used to mark the allowed range (length for STR
1379 * and value for INT).
1381 * For each configuration line in wpa_supplicant.conf, the parser goes through
1382 * this table and select the entry that matches with the field name. The parser
1383 * function (.parser) is then called to parse the actual value of the field.
1385 * This kind of mechanism makes it easy to add new configuration parameters,
1386 * since only one line needs to be added into this table and into the
1387 * struct wpa_ssid definition if the new variable is either a string or
1388 * integer. More complex types will need to use their own parser and writer
1389 * functions.
1391 static const struct parse_data ssid_fields[] = {
1392 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1393 { INT_RANGE(scan_ssid, 0, 1) },
1394 { FUNC(bssid) },
1395 { FUNC_KEY(psk) },
1396 { FUNC(proto) },
1397 { FUNC(key_mgmt) },
1398 { FUNC(pairwise) },
1399 { FUNC(group) },
1400 { FUNC(auth_alg) },
1401 { FUNC(scan_freq) },
1402 #ifdef IEEE8021X_EAPOL
1403 { FUNC(eap) },
1404 { STR_LENe(identity) },
1405 { STR_LENe(anonymous_identity) },
1406 { FUNC_KEY(password) },
1407 { STRe(ca_cert) },
1408 { STRe(ca_path) },
1409 { STRe(client_cert) },
1410 { STRe(private_key) },
1411 { STR_KEYe(private_key_passwd) },
1412 { STRe(dh_file) },
1413 { STRe(subject_match) },
1414 { STRe(altsubject_match) },
1415 { STRe(ca_cert2) },
1416 { STRe(ca_path2) },
1417 { STRe(client_cert2) },
1418 { STRe(private_key2) },
1419 { STR_KEYe(private_key2_passwd) },
1420 { STRe(dh_file2) },
1421 { STRe(subject_match2) },
1422 { STRe(altsubject_match2) },
1423 { STRe(phase1) },
1424 { STRe(phase2) },
1425 { STRe(pcsc) },
1426 { STR_KEYe(pin) },
1427 { STRe(engine_id) },
1428 { STRe(key_id) },
1429 { STRe(cert_id) },
1430 { STRe(ca_cert_id) },
1431 { STR_KEYe(pin2) },
1432 { STRe(engine2_id) },
1433 { STRe(key2_id) },
1434 { STRe(cert2_id) },
1435 { STRe(ca_cert2_id) },
1436 { INTe(engine) },
1437 { INTe(engine2) },
1438 { INT(eapol_flags) },
1439 #endif /* IEEE8021X_EAPOL */
1440 { FUNC_KEY(wep_key0) },
1441 { FUNC_KEY(wep_key1) },
1442 { FUNC_KEY(wep_key2) },
1443 { FUNC_KEY(wep_key3) },
1444 { INT(wep_tx_keyidx) },
1445 { INT(priority) },
1446 #ifdef IEEE8021X_EAPOL
1447 { INT(eap_workaround) },
1448 { STRe(pac_file) },
1449 { INTe(fragment_size) },
1450 #endif /* IEEE8021X_EAPOL */
1451 { INT_RANGE(mode, 0, 2) },
1452 { INT_RANGE(proactive_key_caching, 0, 1) },
1453 { INT_RANGE(disabled, 0, 1) },
1454 { STR(id_str) },
1455 #ifdef CONFIG_IEEE80211W
1456 { INT_RANGE(ieee80211w, 0, 2) },
1457 #endif /* CONFIG_IEEE80211W */
1458 { INT_RANGE(peerkey, 0, 1) },
1459 { INT_RANGE(mixed_cell, 0, 1) },
1460 { INT_RANGE(frequency, 0, 10000) },
1461 { INT(wpa_ptk_rekey) },
1462 { STR(bgscan) },
1465 #undef OFFSET
1466 #undef _STR
1467 #undef STR
1468 #undef STR_KEY
1469 #undef _STR_LEN
1470 #undef STR_LEN
1471 #undef STR_LEN_KEY
1472 #undef _STR_RANGE
1473 #undef STR_RANGE
1474 #undef STR_RANGE_KEY
1475 #undef _INT
1476 #undef INT
1477 #undef INT_RANGE
1478 #undef _FUNC
1479 #undef FUNC
1480 #undef FUNC_KEY
1481 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1485 * wpa_config_add_prio_network - Add a network to priority lists
1486 * @config: Configuration data from wpa_config_read()
1487 * @ssid: Pointer to the network configuration to be added to the list
1488 * Returns: 0 on success, -1 on failure
1490 * This function is used to add a network block to the priority list of
1491 * networks. This must be called for each network when reading in the full
1492 * configuration. In addition, this can be used indirectly when updating
1493 * priorities by calling wpa_config_update_prio_list().
1495 int wpa_config_add_prio_network(struct wpa_config *config,
1496 struct wpa_ssid *ssid)
1498 int prio;
1499 struct wpa_ssid *prev, **nlist;
1502 * Add to an existing priority list if one is available for the
1503 * configured priority level for this network.
1505 for (prio = 0; prio < config->num_prio; prio++) {
1506 prev = config->pssid[prio];
1507 if (prev->priority == ssid->priority) {
1508 while (prev->pnext)
1509 prev = prev->pnext;
1510 prev->pnext = ssid;
1511 return 0;
1515 /* First network for this priority - add a new priority list */
1516 nlist = os_realloc(config->pssid,
1517 (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1518 if (nlist == NULL)
1519 return -1;
1521 for (prio = 0; prio < config->num_prio; prio++) {
1522 if (nlist[prio]->priority < ssid->priority)
1523 break;
1526 os_memmove(&nlist[prio + 1], &nlist[prio],
1527 (config->num_prio - prio) * sizeof(struct wpa_ssid *));
1529 nlist[prio] = ssid;
1530 config->num_prio++;
1531 config->pssid = nlist;
1533 return 0;
1538 * wpa_config_update_prio_list - Update network priority list
1539 * @config: Configuration data from wpa_config_read()
1540 * Returns: 0 on success, -1 on failure
1542 * This function is called to update the priority list of networks in the
1543 * configuration when a network is being added or removed. This is also called
1544 * if a priority for a network is changed.
1546 static int wpa_config_update_prio_list(struct wpa_config *config)
1548 struct wpa_ssid *ssid;
1549 int ret = 0;
1551 os_free(config->pssid);
1552 config->pssid = NULL;
1553 config->num_prio = 0;
1555 ssid = config->ssid;
1556 while (ssid) {
1557 ssid->pnext = NULL;
1558 if (wpa_config_add_prio_network(config, ssid) < 0)
1559 ret = -1;
1560 ssid = ssid->next;
1563 return ret;
1567 #ifdef IEEE8021X_EAPOL
1568 static void eap_peer_config_free(struct eap_peer_config *eap)
1570 os_free(eap->eap_methods);
1571 os_free(eap->identity);
1572 os_free(eap->anonymous_identity);
1573 os_free(eap->password);
1574 os_free(eap->ca_cert);
1575 os_free(eap->ca_path);
1576 os_free(eap->client_cert);
1577 os_free(eap->private_key);
1578 os_free(eap->private_key_passwd);
1579 os_free(eap->dh_file);
1580 os_free(eap->subject_match);
1581 os_free(eap->altsubject_match);
1582 os_free(eap->ca_cert2);
1583 os_free(eap->ca_path2);
1584 os_free(eap->client_cert2);
1585 os_free(eap->private_key2);
1586 os_free(eap->private_key2_passwd);
1587 os_free(eap->dh_file2);
1588 os_free(eap->subject_match2);
1589 os_free(eap->altsubject_match2);
1590 os_free(eap->phase1);
1591 os_free(eap->phase2);
1592 os_free(eap->pcsc);
1593 os_free(eap->pin);
1594 os_free(eap->engine_id);
1595 os_free(eap->key_id);
1596 os_free(eap->cert_id);
1597 os_free(eap->ca_cert_id);
1598 os_free(eap->key2_id);
1599 os_free(eap->cert2_id);
1600 os_free(eap->ca_cert2_id);
1601 os_free(eap->pin2);
1602 os_free(eap->engine2_id);
1603 os_free(eap->otp);
1604 os_free(eap->pending_req_otp);
1605 os_free(eap->pac_file);
1606 os_free(eap->new_password);
1608 #endif /* IEEE8021X_EAPOL */
1612 * wpa_config_free_ssid - Free network/ssid configuration data
1613 * @ssid: Configuration data for the network
1615 * This function frees all resources allocated for the network configuration
1616 * data.
1618 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1620 os_free(ssid->ssid);
1621 os_free(ssid->passphrase);
1622 #ifdef IEEE8021X_EAPOL
1623 eap_peer_config_free(&ssid->eap);
1624 #endif /* IEEE8021X_EAPOL */
1625 os_free(ssid->id_str);
1626 os_free(ssid->scan_freq);
1627 os_free(ssid->bgscan);
1628 os_free(ssid);
1633 * wpa_config_free - Free configuration data
1634 * @config: Configuration data from wpa_config_read()
1636 * This function frees all resources allocated for the configuration data by
1637 * wpa_config_read().
1639 void wpa_config_free(struct wpa_config *config)
1641 #ifndef CONFIG_NO_CONFIG_BLOBS
1642 struct wpa_config_blob *blob, *prevblob;
1643 #endif /* CONFIG_NO_CONFIG_BLOBS */
1644 struct wpa_ssid *ssid, *prev = NULL;
1645 ssid = config->ssid;
1646 while (ssid) {
1647 prev = ssid;
1648 ssid = ssid->next;
1649 wpa_config_free_ssid(prev);
1652 #ifndef CONFIG_NO_CONFIG_BLOBS
1653 blob = config->blobs;
1654 prevblob = NULL;
1655 while (blob) {
1656 prevblob = blob;
1657 blob = blob->next;
1658 wpa_config_free_blob(prevblob);
1660 #endif /* CONFIG_NO_CONFIG_BLOBS */
1662 os_free(config->ctrl_interface);
1663 os_free(config->ctrl_interface_group);
1664 os_free(config->opensc_engine_path);
1665 os_free(config->pkcs11_engine_path);
1666 os_free(config->pkcs11_module_path);
1667 os_free(config->driver_param);
1668 os_free(config->device_name);
1669 os_free(config->manufacturer);
1670 os_free(config->model_name);
1671 os_free(config->model_number);
1672 os_free(config->serial_number);
1673 os_free(config->device_type);
1674 os_free(config->config_methods);
1675 os_free(config->pssid);
1676 os_free(config);
1681 * wpa_config_get_network - Get configured network based on id
1682 * @config: Configuration data from wpa_config_read()
1683 * @id: Unique network id to search for
1684 * Returns: Network configuration or %NULL if not found
1686 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1688 struct wpa_ssid *ssid;
1690 ssid = config->ssid;
1691 while (ssid) {
1692 if (id == ssid->id)
1693 break;
1694 ssid = ssid->next;
1697 return ssid;
1702 * wpa_config_add_network - Add a new network with empty configuration
1703 * @config: Configuration data from wpa_config_read()
1704 * Returns: The new network configuration or %NULL if operation failed
1706 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1708 int id;
1709 struct wpa_ssid *ssid, *last = NULL;
1711 id = -1;
1712 ssid = config->ssid;
1713 while (ssid) {
1714 if (ssid->id > id)
1715 id = ssid->id;
1716 last = ssid;
1717 ssid = ssid->next;
1719 id++;
1721 ssid = os_zalloc(sizeof(*ssid));
1722 if (ssid == NULL)
1723 return NULL;
1724 ssid->id = id;
1725 if (last)
1726 last->next = ssid;
1727 else
1728 config->ssid = ssid;
1730 wpa_config_update_prio_list(config);
1732 return ssid;
1737 * wpa_config_remove_network - Remove a configured network based on id
1738 * @config: Configuration data from wpa_config_read()
1739 * @id: Unique network id to search for
1740 * Returns: 0 on success, or -1 if the network was not found
1742 int wpa_config_remove_network(struct wpa_config *config, int id)
1744 struct wpa_ssid *ssid, *prev = NULL;
1746 ssid = config->ssid;
1747 while (ssid) {
1748 if (id == ssid->id)
1749 break;
1750 prev = ssid;
1751 ssid = ssid->next;
1754 if (ssid == NULL)
1755 return -1;
1757 if (prev)
1758 prev->next = ssid->next;
1759 else
1760 config->ssid = ssid->next;
1762 wpa_config_update_prio_list(config);
1763 wpa_config_free_ssid(ssid);
1764 return 0;
1769 * wpa_config_set_network_defaults - Set network default values
1770 * @ssid: Pointer to network configuration data
1772 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1774 ssid->proto = DEFAULT_PROTO;
1775 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1776 ssid->group_cipher = DEFAULT_GROUP;
1777 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1778 #ifdef IEEE8021X_EAPOL
1779 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1780 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1781 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1782 #endif /* IEEE8021X_EAPOL */
1787 * wpa_config_set - Set a variable in network configuration
1788 * @ssid: Pointer to network configuration data
1789 * @var: Variable name, e.g., "ssid"
1790 * @value: Variable value
1791 * @line: Line number in configuration file or 0 if not used
1792 * Returns: 0 on success, -1 on failure
1794 * This function can be used to set network configuration variables based on
1795 * both the configuration file and management interface input. The value
1796 * parameter must be in the same format as the text-based configuration file is
1797 * using. For example, strings are using double quotation marks.
1799 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1800 int line)
1802 size_t i;
1803 int ret = 0;
1805 if (ssid == NULL || var == NULL || value == NULL)
1806 return -1;
1808 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1809 const struct parse_data *field = &ssid_fields[i];
1810 if (os_strcmp(var, field->name) != 0)
1811 continue;
1813 if (field->parser(field, ssid, line, value)) {
1814 if (line) {
1815 wpa_printf(MSG_ERROR, "Line %d: failed to "
1816 "parse %s '%s'.", line, var, value);
1818 ret = -1;
1820 break;
1822 if (i == NUM_SSID_FIELDS) {
1823 if (line) {
1824 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
1825 "'%s'.", line, var);
1827 ret = -1;
1830 return ret;
1835 * wpa_config_get_all - Get all options from network configuration
1836 * @ssid: Pointer to network configuration data
1837 * @get_keys: Determines if keys/passwords will be included in returned list
1838 * Returns: %NULL terminated list of all set keys and their values in the form
1839 * of [key1, val1, key2, val2, ... , NULL]
1841 * This function can be used to get list of all configured network properties.
1842 * The caller is responsible for freeing the returned list and all its
1843 * elements.
1845 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
1847 const struct parse_data *field;
1848 char *key, *value;
1849 size_t i;
1850 char **props;
1851 int fields_num;
1853 props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
1854 if (!props)
1855 return NULL;
1857 fields_num = 0;
1858 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1859 field = &ssid_fields[i];
1860 if (field->key_data && !get_keys)
1861 continue;
1862 value = field->writer(field, ssid);
1863 if (value == NULL)
1864 continue;
1865 if (os_strlen(value) == 0) {
1866 os_free(value);
1867 continue;
1870 key = os_strdup(field->name);
1871 if (key == NULL) {
1872 os_free(value);
1873 goto err;
1876 props[fields_num * 2] = key;
1877 props[fields_num * 2 + 1] = value;
1879 fields_num++;
1882 return props;
1884 err:
1885 value = *props;
1886 while (value)
1887 os_free(value++);
1888 os_free(props);
1889 return NULL;
1893 #ifndef NO_CONFIG_WRITE
1895 * wpa_config_get - Get a variable in network configuration
1896 * @ssid: Pointer to network configuration data
1897 * @var: Variable name, e.g., "ssid"
1898 * Returns: Value of the variable or %NULL on failure
1900 * This function can be used to get network configuration variables. The
1901 * returned value is a copy of the configuration variable in text format, i.e,.
1902 * the same format that the text-based configuration file and wpa_config_set()
1903 * are using for the value. The caller is responsible for freeing the returned
1904 * value.
1906 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
1908 size_t i;
1910 if (ssid == NULL || var == NULL)
1911 return NULL;
1913 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1914 const struct parse_data *field = &ssid_fields[i];
1915 if (os_strcmp(var, field->name) == 0)
1916 return field->writer(field, ssid);
1919 return NULL;
1924 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
1925 * @ssid: Pointer to network configuration data
1926 * @var: Variable name, e.g., "ssid"
1927 * Returns: Value of the variable or %NULL on failure
1929 * This function can be used to get network configuration variable like
1930 * wpa_config_get(). The only difference is that this functions does not expose
1931 * key/password material from the configuration. In case a key/password field
1932 * is requested, the returned value is an empty string or %NULL if the variable
1933 * is not set or "*" if the variable is set (regardless of its value). The
1934 * returned value is a copy of the configuration variable in text format, i.e,.
1935 * the same format that the text-based configuration file and wpa_config_set()
1936 * are using for the value. The caller is responsible for freeing the returned
1937 * value.
1939 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
1941 size_t i;
1943 if (ssid == NULL || var == NULL)
1944 return NULL;
1946 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1947 const struct parse_data *field = &ssid_fields[i];
1948 if (os_strcmp(var, field->name) == 0) {
1949 char *res = field->writer(field, ssid);
1950 if (field->key_data) {
1951 if (res && res[0]) {
1952 wpa_printf(MSG_DEBUG, "Do not allow "
1953 "key_data field to be "
1954 "exposed");
1955 os_free(res);
1956 return os_strdup("*");
1959 os_free(res);
1960 return NULL;
1962 return res;
1966 return NULL;
1968 #endif /* NO_CONFIG_WRITE */
1972 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
1973 * @ssid: Pointer to network configuration data
1975 * This function must be called to update WPA PSK when either SSID or the
1976 * passphrase has changed for the network configuration.
1978 void wpa_config_update_psk(struct wpa_ssid *ssid)
1980 #ifndef CONFIG_NO_PBKDF2
1981 pbkdf2_sha1(ssid->passphrase,
1982 (char *) ssid->ssid, ssid->ssid_len, 4096,
1983 ssid->psk, PMK_LEN);
1984 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1985 ssid->psk, PMK_LEN);
1986 ssid->psk_set = 1;
1987 #endif /* CONFIG_NO_PBKDF2 */
1991 #ifndef CONFIG_NO_CONFIG_BLOBS
1993 * wpa_config_get_blob - Get a named configuration blob
1994 * @config: Configuration data from wpa_config_read()
1995 * @name: Name of the blob
1996 * Returns: Pointer to blob data or %NULL if not found
1998 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
1999 const char *name)
2001 struct wpa_config_blob *blob = config->blobs;
2003 while (blob) {
2004 if (os_strcmp(blob->name, name) == 0)
2005 return blob;
2006 blob = blob->next;
2008 return NULL;
2013 * wpa_config_set_blob - Set or add a named configuration blob
2014 * @config: Configuration data from wpa_config_read()
2015 * @blob: New value for the blob
2017 * Adds a new configuration blob or replaces the current value of an existing
2018 * blob.
2020 void wpa_config_set_blob(struct wpa_config *config,
2021 struct wpa_config_blob *blob)
2023 wpa_config_remove_blob(config, blob->name);
2024 blob->next = config->blobs;
2025 config->blobs = blob;
2030 * wpa_config_free_blob - Free blob data
2031 * @blob: Pointer to blob to be freed
2033 void wpa_config_free_blob(struct wpa_config_blob *blob)
2035 if (blob) {
2036 os_free(blob->name);
2037 os_free(blob->data);
2038 os_free(blob);
2044 * wpa_config_remove_blob - Remove a named configuration blob
2045 * @config: Configuration data from wpa_config_read()
2046 * @name: Name of the blob to remove
2047 * Returns: 0 if blob was removed or -1 if blob was not found
2049 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2051 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2053 while (pos) {
2054 if (os_strcmp(pos->name, name) == 0) {
2055 if (prev)
2056 prev->next = pos->next;
2057 else
2058 config->blobs = pos->next;
2059 wpa_config_free_blob(pos);
2060 return 0;
2062 prev = pos;
2063 pos = pos->next;
2066 return -1;
2068 #endif /* CONFIG_NO_CONFIG_BLOBS */
2072 * wpa_config_alloc_empty - Allocate an empty configuration
2073 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2074 * socket
2075 * @driver_param: Driver parameters
2076 * Returns: Pointer to allocated configuration data or %NULL on failure
2078 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2079 const char *driver_param)
2081 struct wpa_config *config;
2083 config = os_zalloc(sizeof(*config));
2084 if (config == NULL)
2085 return NULL;
2086 config->eapol_version = DEFAULT_EAPOL_VERSION;
2087 config->ap_scan = DEFAULT_AP_SCAN;
2088 config->fast_reauth = DEFAULT_FAST_REAUTH;
2090 if (ctrl_interface)
2091 config->ctrl_interface = os_strdup(ctrl_interface);
2092 if (driver_param)
2093 config->driver_param = os_strdup(driver_param);
2095 return config;
2099 #ifndef CONFIG_NO_STDOUT_DEBUG
2101 * wpa_config_debug_dump_networks - Debug dump of configured networks
2102 * @config: Configuration data from wpa_config_read()
2104 void wpa_config_debug_dump_networks(struct wpa_config *config)
2106 int prio;
2107 struct wpa_ssid *ssid;
2109 for (prio = 0; prio < config->num_prio; prio++) {
2110 ssid = config->pssid[prio];
2111 wpa_printf(MSG_DEBUG, "Priority group %d",
2112 ssid->priority);
2113 while (ssid) {
2114 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2115 ssid->id,
2116 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2117 ssid = ssid->pnext;
2121 #endif /* CONFIG_NO_STDOUT_DEBUG */