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
12 * See README and COPYING for more details.
18 #include "crypto/sha1.h"
19 #include "rsn_supp/wpa.h"
20 #include "eap_peer/eap.h"
24 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25 #define NO_CONFIG_WRITE
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[]).
34 /* Configuration variable 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
60 static char * wpa_config_parse_string(const char *value
, size_t *len
)
66 pos
= os_strrchr(value
, '"');
67 if (pos
== NULL
|| pos
[1] != '\0')
70 str
= os_malloc(*len
+ 1);
73 os_memcpy(str
, value
, *len
);
78 size_t tlen
, hlen
= os_strlen(value
);
82 str
= os_malloc(tlen
+ 1);
85 if (hexstr2bin(value
, str
, tlen
)) {
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
;
103 if (os_strcmp(value
, "NULL") == 0) {
104 wpa_printf(MSG_DEBUG
, "Unset configuration string '%s'",
111 tmp
= wpa_config_parse_string(value
, &res_len
);
113 wpa_printf(MSG_ERROR
, "Line %d: failed to parse %s '%s'.",
115 data
->key_data
? "[KEY DATA REMOVED]" : value
);
119 if (data
->key_data
) {
120 wpa_hexdump_ascii_key(MSG_MSGDUMP
, data
->name
,
121 (u8
*) tmp
, res_len
);
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
);
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
);
144 dst
= (char **) (((u8
*) ssid
) + (long) data
->param1
);
145 dst_len
= (size_t *) (((u8
*) ssid
) + (long) data
->param2
);
155 #ifndef NO_CONFIG_WRITE
156 static int is_hex(const u8
*data
, size_t len
)
160 for (i
= 0; i
< len
; i
++) {
161 if (data
[i
] < 32 || data
[i
] >= 127)
168 static char * wpa_config_write_string_ascii(const u8
*value
, size_t len
)
172 buf
= os_malloc(len
+ 3);
176 os_memcpy(buf
+ 1, value
, len
);
184 static char * wpa_config_write_string_hex(const u8
*value
, size_t len
)
188 buf
= os_zalloc(2 * len
+ 1);
191 wpa_snprintf_hex(buf
, 2 * len
+ 1, value
, len
);
197 static char * wpa_config_write_string(const u8
*value
, size_t len
)
202 if (is_hex(value
, len
))
203 return wpa_config_write_string_hex(value
, len
);
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
)
215 src
= (char **) (((u8
*) ssid
) + (long) data
->param1
);
220 len
= *((size_t *) (((u8
*) ssid
) + (long) data
->param2
));
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
)
235 dst
= (int *) (((u8
*) ssid
) + (long) data
->param1
);
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
;
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
;
259 #ifndef NO_CONFIG_WRITE
260 static char * wpa_config_write_int(const struct parse_data
*data
,
261 struct wpa_ssid
*ssid
)
266 src
= (int *) (((u8
*) ssid
) + (long) data
->param1
);
268 value
= os_malloc(20);
271 res
= os_snprintf(value
, 20, "%d", *src
);
272 if (res
< 0 || res
>= 20) {
276 value
[20 - 1] = '\0';
279 #endif /* NO_CONFIG_WRITE */
282 static int wpa_config_parse_bssid(const struct parse_data
*data
,
283 struct wpa_ssid
*ssid
, int line
,
286 if (hwaddr_aton(value
, ssid
->bssid
)) {
287 wpa_printf(MSG_ERROR
, "Line %d: Invalid BSSID '%s'.",
292 wpa_hexdump(MSG_MSGDUMP
, "BSSID", ssid
->bssid
, ETH_ALEN
);
297 #ifndef NO_CONFIG_WRITE
298 static char * wpa_config_write_bssid(const struct parse_data
*data
,
299 struct wpa_ssid
*ssid
)
304 if (!ssid
->bssid_set
)
307 value
= os_malloc(20);
310 res
= os_snprintf(value
, 20, MACSTR
, MAC2STR(ssid
->bssid
));
311 if (res
< 0 || res
>= 20) {
315 value
[20 - 1] = '\0';
318 #endif /* NO_CONFIG_WRITE */
321 static int wpa_config_parse_psk(const struct parse_data
*data
,
322 struct wpa_ssid
*ssid
, int line
,
326 #ifndef CONFIG_NO_PBKDF2
331 pos
= os_strrchr(value
, '"');
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
);
342 wpa_hexdump_ascii_key(MSG_MSGDUMP
, "PSK (ASCII passphrase)",
344 if (ssid
->passphrase
&& os_strlen(ssid
->passphrase
) == len
&&
345 os_memcmp(ssid
->passphrase
, value
, len
) == 0)
348 os_free(ssid
->passphrase
);
349 ssid
->passphrase
= os_malloc(len
+ 1);
350 if (ssid
->passphrase
== NULL
)
352 os_memcpy(ssid
->passphrase
, value
, len
);
353 ssid
->passphrase
[len
] = '\0';
355 #else /* CONFIG_NO_PBKDF2 */
356 wpa_printf(MSG_ERROR
, "Line %d: ASCII passphrase not "
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'.",
369 os_free(ssid
->passphrase
);
370 ssid
->passphrase
= NULL
;
373 wpa_hexdump_key(MSG_MSGDUMP
, "PSK", ssid
->psk
, PMK_LEN
);
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
));
388 return wpa_config_write_string_hex(ssid
->psk
, PMK_LEN
);
392 #endif /* NO_CONFIG_WRITE */
395 static int wpa_config_parse_proto(const struct parse_data
*data
,
396 struct wpa_ssid
*ssid
, int line
,
399 int val
= 0, last
, errors
= 0;
400 char *start
, *end
, *buf
;
402 buf
= os_strdup(value
);
407 while (*start
!= '\0') {
408 while (*start
== ' ' || *start
== '\t')
413 while (*end
!= ' ' && *end
!= '\t' && *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
;
423 wpa_printf(MSG_ERROR
, "Line %d: invalid proto '%s'",
435 wpa_printf(MSG_ERROR
,
436 "Line %d: no proto values configured.", line
);
440 wpa_printf(MSG_MSGDUMP
, "proto: 0x%x", 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
)
451 char *buf
, *pos
, *end
;
453 pos
= buf
= os_zalloc(10);
458 if (ssid
->proto
& WPA_PROTO_WPA
) {
459 ret
= os_snprintf(pos
, end
- pos
, "%sWPA", first
? "" : " ");
460 if (ret
< 0 || ret
>= end
- pos
)
466 if (ssid
->proto
& WPA_PROTO_RSN
) {
467 ret
= os_snprintf(pos
, end
- pos
, "%sRSN", first
? "" : " ");
468 if (ret
< 0 || ret
>= end
- pos
)
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
,
483 int val
= 0, last
, errors
= 0;
484 char *start
, *end
, *buf
;
486 buf
= os_strdup(value
);
491 while (*start
!= '\0') {
492 while (*start
== ' ' || *start
== '\t')
497 while (*end
!= ' ' && *end
!= '\t' && *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 */
524 else if (os_strcmp(start
, "WPS") == 0)
525 val
|= WPA_KEY_MGMT_WPS
;
526 #endif /* CONFIG_WPS */
528 wpa_printf(MSG_ERROR
, "Line %d: invalid key_mgmt '%s'",
540 wpa_printf(MSG_ERROR
,
541 "Line %d: no key_mgmt values configured.", line
);
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
;
558 pos
= buf
= os_zalloc(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
) {
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
) {
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
) {
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
) {
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
) {
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 */
634 if (ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
)
635 pos
+= os_snprintf(pos
, end
- pos
, "%sWPS",
636 pos
== buf
? "" : " ");
637 #endif /* CONFIG_WPS */
641 #endif /* NO_CONFIG_WRITE */
644 static int wpa_config_parse_cipher(int line
, const char *value
)
647 char *start
, *end
, *buf
;
649 buf
= os_strdup(value
);
654 while (*start
!= '\0') {
655 while (*start
== ' ' || *start
== '\t')
660 while (*end
!= ' ' && *end
!= '\t' && *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
;
675 wpa_printf(MSG_ERROR
, "Line %d: invalid cipher '%s'.",
688 wpa_printf(MSG_ERROR
, "Line %d: no cipher values configured.",
696 #ifndef NO_CONFIG_WRITE
697 static char * wpa_config_write_cipher(int cipher
)
699 char *buf
, *pos
, *end
;
702 pos
= buf
= os_zalloc(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
) {
717 if (cipher
& WPA_CIPHER_TKIP
) {
718 ret
= os_snprintf(pos
, end
- pos
, "%sTKIP",
719 pos
== buf
? "" : " ");
720 if (ret
< 0 || ret
>= end
- pos
) {
727 if (cipher
& WPA_CIPHER_WEP104
) {
728 ret
= os_snprintf(pos
, end
- pos
, "%sWEP104",
729 pos
== buf
? "" : " ");
730 if (ret
< 0 || ret
>= end
- pos
) {
737 if (cipher
& WPA_CIPHER_WEP40
) {
738 ret
= os_snprintf(pos
, end
- pos
, "%sWEP40",
739 pos
== buf
? "" : " ");
740 if (ret
< 0 || ret
>= end
- pos
) {
747 if (cipher
& WPA_CIPHER_NONE
) {
748 ret
= os_snprintf(pos
, end
- pos
, "%sNONE",
749 pos
== buf
? "" : " ");
750 if (ret
< 0 || ret
>= end
- pos
) {
759 #endif /* NO_CONFIG_WRITE */
762 static int wpa_config_parse_pairwise(const struct parse_data
*data
,
763 struct wpa_ssid
*ssid
, int line
,
767 val
= wpa_config_parse_cipher(line
, value
);
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
);
776 wpa_printf(MSG_MSGDUMP
, "pairwise: 0x%x", val
);
777 ssid
->pairwise_cipher
= val
;
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
,
796 val
= wpa_config_parse_cipher(line
, value
);
799 if (val
& ~(WPA_CIPHER_CCMP
| WPA_CIPHER_TKIP
| WPA_CIPHER_WEP104
|
801 wpa_printf(MSG_ERROR
, "Line %d: not allowed group cipher "
802 "(0x%x).", line
, val
);
806 wpa_printf(MSG_MSGDUMP
, "group: 0x%x", val
);
807 ssid
->group_cipher
= val
;
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
,
825 int val
= 0, last
, errors
= 0;
826 char *start
, *end
, *buf
;
828 buf
= os_strdup(value
);
833 while (*start
!= '\0') {
834 while (*start
== ' ' || *start
== '\t')
839 while (*end
!= ' ' && *end
!= '\t' && *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
;
850 wpa_printf(MSG_ERROR
, "Line %d: invalid auth_alg '%s'",
862 wpa_printf(MSG_ERROR
,
863 "Line %d: no auth_alg values configured.", line
);
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
;
880 pos
= buf
= os_zalloc(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
) {
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
) {
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
) {
917 #endif /* NO_CONFIG_WRITE */
920 static int * wpa_config_parse_freqs(const struct parse_data
*data
,
921 struct wpa_ssid
*ssid
, int line
,
930 freqs
= os_zalloc((len
+ 1) * sizeof(int));
941 n
= os_realloc(freqs
, (len
* 2 + 1) * sizeof(int));
946 for (i
= len
; i
<= len
* 2; i
++)
952 freqs
[used
] = atoi(pos
);
953 if (freqs
[used
] == 0)
956 pos
= os_strchr(pos
+ 1, ' ');
963 static int wpa_config_parse_scan_freq(const struct parse_data
*data
,
964 struct wpa_ssid
*ssid
, int line
,
969 freqs
= wpa_config_parse_freqs(data
, ssid
, line
, value
);
972 os_free(ssid
->scan_freq
);
973 ssid
->scan_freq
= freqs
;
979 static int wpa_config_parse_freq_list(const struct parse_data
*data
,
980 struct wpa_ssid
*ssid
, int line
,
985 freqs
= wpa_config_parse_freqs(data
, ssid
, line
, value
);
988 os_free(ssid
->freq_list
);
989 ssid
->freq_list
= freqs
;
995 #ifndef NO_CONFIG_WRITE
996 static char * wpa_config_write_freqs(const struct parse_data
*data
,
999 char *buf
, *pos
, *end
;
1007 for (i
= 0; freqs
[i
]; i
++)
1010 pos
= buf
= os_zalloc(10 * count
+ 1);
1013 end
= buf
+ 10 * count
+ 1;
1015 for (i
= 0; freqs
[i
]; i
++) {
1016 ret
= os_snprintf(pos
, end
- pos
, "%s%u",
1017 i
== 0 ? "" : " ", freqs
[i
]);
1018 if (ret
< 0 || ret
>= end
- pos
) {
1029 static char * wpa_config_write_scan_freq(const struct parse_data
*data
,
1030 struct wpa_ssid
*ssid
)
1032 return wpa_config_write_freqs(data
, ssid
->scan_freq
);
1036 static char * wpa_config_write_freq_list(const struct parse_data
*data
,
1037 struct wpa_ssid
*ssid
)
1039 return wpa_config_write_freqs(data
, ssid
->freq_list
);
1041 #endif /* NO_CONFIG_WRITE */
1044 #ifdef IEEE8021X_EAPOL
1045 static int wpa_config_parse_eap(const struct parse_data
*data
,
1046 struct wpa_ssid
*ssid
, int line
,
1049 int last
, errors
= 0;
1050 char *start
, *end
, *buf
;
1051 struct eap_method_type
*methods
= NULL
, *tmp
;
1052 size_t num_methods
= 0;
1054 buf
= os_strdup(value
);
1059 while (*start
!= '\0') {
1060 while (*start
== ' ' || *start
== '\t')
1065 while (*end
!= ' ' && *end
!= '\t' && *end
!= '\0')
1067 last
= *end
== '\0';
1070 methods
= os_realloc(methods
,
1071 (num_methods
+ 1) * sizeof(*methods
));
1072 if (methods
== NULL
) {
1077 methods
[num_methods
].method
= eap_peer_get_type(
1078 start
, &methods
[num_methods
].vendor
);
1079 if (methods
[num_methods
].vendor
== EAP_VENDOR_IETF
&&
1080 methods
[num_methods
].method
== EAP_TYPE_NONE
) {
1081 wpa_printf(MSG_ERROR
, "Line %d: unknown EAP method "
1082 "'%s'", line
, start
);
1083 wpa_printf(MSG_ERROR
, "You may need to add support for"
1084 " this EAP method during wpa_supplicant\n"
1085 "build time configuration.\n"
1086 "See README for more information.");
1088 } else if (methods
[num_methods
].vendor
== EAP_VENDOR_IETF
&&
1089 methods
[num_methods
].method
== EAP_TYPE_LEAP
)
1101 methods
= os_realloc(methods
, (num_methods
+ 1) * sizeof(*methods
));
1102 if (methods
== NULL
) {
1106 methods
[num_methods
].vendor
= EAP_VENDOR_IETF
;
1107 methods
[num_methods
].method
= EAP_TYPE_NONE
;
1110 wpa_hexdump(MSG_MSGDUMP
, "eap methods",
1111 (u8
*) methods
, num_methods
* sizeof(*methods
));
1112 ssid
->eap
.eap_methods
= methods
;
1113 return errors
? -1 : 0;
1118 static char * wpa_config_write_eap(const struct parse_data
*data
,
1119 struct wpa_ssid
*ssid
)
1122 char *buf
, *pos
, *end
;
1123 const struct eap_method_type
*eap_methods
= ssid
->eap
.eap_methods
;
1126 if (eap_methods
== NULL
)
1129 pos
= buf
= os_zalloc(100);
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
);
1139 ret
= os_snprintf(pos
, end
- pos
, "%s%s",
1140 pos
== buf
? "" : " ", name
);
1141 if (ret
< 0 || ret
>= end
- pos
)
1154 static int wpa_config_parse_password(const struct parse_data
*data
,
1155 struct wpa_ssid
*ssid
, int line
,
1160 if (os_strcmp(value
, "NULL") == 0) {
1161 wpa_printf(MSG_DEBUG
, "Unset configuration string 'password'");
1162 os_free(ssid
->eap
.password
);
1163 ssid
->eap
.password
= NULL
;
1164 ssid
->eap
.password_len
= 0;
1168 if (os_strncmp(value
, "hash:", 5) != 0) {
1172 tmp
= wpa_config_parse_string(value
, &res_len
);
1174 wpa_printf(MSG_ERROR
, "Line %d: failed to parse "
1178 wpa_hexdump_ascii_key(MSG_MSGDUMP
, data
->name
,
1179 (u8
*) tmp
, res_len
);
1181 os_free(ssid
->eap
.password
);
1182 ssid
->eap
.password
= (u8
*) tmp
;
1183 ssid
->eap
.password_len
= res_len
;
1184 ssid
->eap
.flags
&= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH
;
1190 /* NtPasswordHash: hash:<32 hex digits> */
1191 if (os_strlen(value
+ 5) != 2 * 16) {
1192 wpa_printf(MSG_ERROR
, "Line %d: Invalid password hash length "
1193 "(expected 32 hex digits)", line
);
1197 hash
= os_malloc(16);
1201 if (hexstr2bin(value
+ 5, hash
, 16)) {
1203 wpa_printf(MSG_ERROR
, "Line %d: Invalid password hash", line
);
1207 wpa_hexdump_key(MSG_MSGDUMP
, data
->name
, hash
, 16);
1209 os_free(ssid
->eap
.password
);
1210 ssid
->eap
.password
= hash
;
1211 ssid
->eap
.password_len
= 16;
1212 ssid
->eap
.flags
|= EAP_CONFIG_FLAGS_PASSWORD_NTHASH
;
1218 #ifndef NO_CONFIG_WRITE
1219 static char * wpa_config_write_password(const struct parse_data
*data
,
1220 struct wpa_ssid
*ssid
)
1224 if (ssid
->eap
.password
== NULL
)
1227 if (!(ssid
->eap
.flags
& EAP_CONFIG_FLAGS_PASSWORD_NTHASH
)) {
1228 return wpa_config_write_string(
1229 ssid
->eap
.password
, ssid
->eap
.password_len
);
1232 buf
= os_malloc(5 + 32 + 1);
1236 os_memcpy(buf
, "hash:", 5);
1237 wpa_snprintf_hex(buf
+ 5, 32 + 1, ssid
->eap
.password
, 16);
1241 #endif /* NO_CONFIG_WRITE */
1242 #endif /* IEEE8021X_EAPOL */
1245 static int wpa_config_parse_wep_key(u8
*key
, size_t *len
, int line
,
1246 const char *value
, int idx
)
1248 char *buf
, title
[20];
1251 buf
= wpa_config_parse_string(value
, len
);
1253 wpa_printf(MSG_ERROR
, "Line %d: Invalid WEP key %d '%s'.",
1257 if (*len
> MAX_WEP_KEY_LEN
) {
1258 wpa_printf(MSG_ERROR
, "Line %d: Too long WEP key %d '%s'.",
1263 os_memcpy(key
, buf
, *len
);
1265 res
= os_snprintf(title
, sizeof(title
), "wep_key%d", idx
);
1266 if (res
>= 0 && (size_t) res
< sizeof(title
))
1267 wpa_hexdump_key(MSG_MSGDUMP
, title
, key
, *len
);
1272 static int wpa_config_parse_wep_key0(const struct parse_data
*data
,
1273 struct wpa_ssid
*ssid
, int line
,
1276 return wpa_config_parse_wep_key(ssid
->wep_key
[0],
1277 &ssid
->wep_key_len
[0], line
,
1282 static int wpa_config_parse_wep_key1(const struct parse_data
*data
,
1283 struct wpa_ssid
*ssid
, int line
,
1286 return wpa_config_parse_wep_key(ssid
->wep_key
[1],
1287 &ssid
->wep_key_len
[1], line
,
1292 static int wpa_config_parse_wep_key2(const struct parse_data
*data
,
1293 struct wpa_ssid
*ssid
, int line
,
1296 return wpa_config_parse_wep_key(ssid
->wep_key
[2],
1297 &ssid
->wep_key_len
[2], line
,
1302 static int wpa_config_parse_wep_key3(const struct parse_data
*data
,
1303 struct wpa_ssid
*ssid
, int line
,
1306 return wpa_config_parse_wep_key(ssid
->wep_key
[3],
1307 &ssid
->wep_key_len
[3], line
,
1312 #ifndef NO_CONFIG_WRITE
1313 static char * wpa_config_write_wep_key(struct wpa_ssid
*ssid
, int idx
)
1315 if (ssid
->wep_key_len
[idx
] == 0)
1317 return wpa_config_write_string(ssid
->wep_key
[idx
],
1318 ssid
->wep_key_len
[idx
]);
1322 static char * wpa_config_write_wep_key0(const struct parse_data
*data
,
1323 struct wpa_ssid
*ssid
)
1325 return wpa_config_write_wep_key(ssid
, 0);
1329 static char * wpa_config_write_wep_key1(const struct parse_data
*data
,
1330 struct wpa_ssid
*ssid
)
1332 return wpa_config_write_wep_key(ssid
, 1);
1336 static char * wpa_config_write_wep_key2(const struct parse_data
*data
,
1337 struct wpa_ssid
*ssid
)
1339 return wpa_config_write_wep_key(ssid
, 2);
1343 static char * wpa_config_write_wep_key3(const struct parse_data
*data
,
1344 struct wpa_ssid
*ssid
)
1346 return wpa_config_write_wep_key(ssid
, 3);
1348 #endif /* NO_CONFIG_WRITE */
1351 /* Helper macros for network block parser */
1356 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1357 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1359 /* STR: Define a string variable for an ASCII string; f = field name */
1360 #ifdef NO_CONFIG_WRITE
1361 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1362 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1363 #else /* NO_CONFIG_WRITE */
1364 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1365 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1366 #endif /* NO_CONFIG_WRITE */
1367 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1368 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1369 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1370 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1372 /* STR_LEN: Define a string variable with a separate variable for storing the
1373 * data length. Unlike STR(), this can be used to store arbitrary binary data
1374 * (i.e., even nul termination character). */
1375 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1376 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1377 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1378 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1379 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1381 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1382 * explicitly specified. */
1383 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1384 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1385 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1387 #ifdef NO_CONFIG_WRITE
1388 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1389 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1390 #else /* NO_CONFIG_WRITE */
1391 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1392 OFFSET(f), (void *) 0
1393 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1394 OFFSET(eap.f), (void *) 0
1395 #endif /* NO_CONFIG_WRITE */
1397 /* INT: Define an integer variable */
1398 #define INT(f) _INT(f), NULL, NULL, 0
1399 #define INTe(f) _INTe(f), NULL, NULL, 0
1401 /* INT_RANGE: Define an integer variable with allowed value range */
1402 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1404 /* FUNC: Define a configuration variable that uses a custom function for
1405 * parsing and writing the value. */
1406 #ifdef NO_CONFIG_WRITE
1407 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1408 #else /* NO_CONFIG_WRITE */
1409 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1410 NULL, NULL, NULL, NULL
1411 #endif /* NO_CONFIG_WRITE */
1412 #define FUNC(f) _FUNC(f), 0
1413 #define FUNC_KEY(f) _FUNC(f), 1
1416 * Table of network configuration variables. This table is used to parse each
1417 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1418 * that is inside a network block.
1420 * This table is generated using the helper macros defined above and with
1421 * generous help from the C pre-processor. The field name is stored as a string
1422 * into .name and for STR and INT types, the offset of the target buffer within
1423 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1424 * offset to the field containing the length of the configuration variable.
1425 * .param3 and .param4 can be used to mark the allowed range (length for STR
1426 * and value for INT).
1428 * For each configuration line in wpa_supplicant.conf, the parser goes through
1429 * this table and select the entry that matches with the field name. The parser
1430 * function (.parser) is then called to parse the actual value of the field.
1432 * This kind of mechanism makes it easy to add new configuration parameters,
1433 * since only one line needs to be added into this table and into the
1434 * struct wpa_ssid definition if the new variable is either a string or
1435 * integer. More complex types will need to use their own parser and writer
1438 static const struct parse_data ssid_fields
[] = {
1439 { STR_RANGE(ssid
, 0, MAX_SSID_LEN
) },
1440 { INT_RANGE(scan_ssid
, 0, 1) },
1448 { FUNC(scan_freq
) },
1449 { FUNC(freq_list
) },
1450 #ifdef IEEE8021X_EAPOL
1452 { STR_LENe(identity
) },
1453 { STR_LENe(anonymous_identity
) },
1454 { FUNC_KEY(password
) },
1457 { STRe(client_cert
) },
1458 { STRe(private_key
) },
1459 { STR_KEYe(private_key_passwd
) },
1461 { STRe(subject_match
) },
1462 { STRe(altsubject_match
) },
1465 { STRe(client_cert2
) },
1466 { STRe(private_key2
) },
1467 { STR_KEYe(private_key2_passwd
) },
1469 { STRe(subject_match2
) },
1470 { STRe(altsubject_match2
) },
1475 { STRe(engine_id
) },
1478 { STRe(ca_cert_id
) },
1480 { STRe(engine2_id
) },
1483 { STRe(ca_cert2_id
) },
1486 { INT(eapol_flags
) },
1487 #endif /* IEEE8021X_EAPOL */
1488 { FUNC_KEY(wep_key0
) },
1489 { FUNC_KEY(wep_key1
) },
1490 { FUNC_KEY(wep_key2
) },
1491 { FUNC_KEY(wep_key3
) },
1492 { INT(wep_tx_keyidx
) },
1494 #ifdef IEEE8021X_EAPOL
1495 { INT(eap_workaround
) },
1497 { INTe(fragment_size
) },
1498 #endif /* IEEE8021X_EAPOL */
1499 { INT_RANGE(mode
, 0, 2) },
1500 { INT_RANGE(proactive_key_caching
, 0, 1) },
1501 { INT_RANGE(disabled
, 0, 1) },
1503 #ifdef CONFIG_IEEE80211W
1504 { INT_RANGE(ieee80211w
, 0, 2) },
1505 #endif /* CONFIG_IEEE80211W */
1506 { INT_RANGE(peerkey
, 0, 1) },
1507 { INT_RANGE(mixed_cell
, 0, 1) },
1508 { INT_RANGE(frequency
, 0, 10000) },
1509 { INT(wpa_ptk_rekey
) },
1522 #undef STR_RANGE_KEY
1529 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1533 * wpa_config_add_prio_network - Add a network to priority lists
1534 * @config: Configuration data from wpa_config_read()
1535 * @ssid: Pointer to the network configuration to be added to the list
1536 * Returns: 0 on success, -1 on failure
1538 * This function is used to add a network block to the priority list of
1539 * networks. This must be called for each network when reading in the full
1540 * configuration. In addition, this can be used indirectly when updating
1541 * priorities by calling wpa_config_update_prio_list().
1543 int wpa_config_add_prio_network(struct wpa_config
*config
,
1544 struct wpa_ssid
*ssid
)
1547 struct wpa_ssid
*prev
, **nlist
;
1550 * Add to an existing priority list if one is available for the
1551 * configured priority level for this network.
1553 for (prio
= 0; prio
< config
->num_prio
; prio
++) {
1554 prev
= config
->pssid
[prio
];
1555 if (prev
->priority
== ssid
->priority
) {
1563 /* First network for this priority - add a new priority list */
1564 nlist
= os_realloc(config
->pssid
,
1565 (config
->num_prio
+ 1) * sizeof(struct wpa_ssid
*));
1569 for (prio
= 0; prio
< config
->num_prio
; prio
++) {
1570 if (nlist
[prio
]->priority
< ssid
->priority
)
1574 os_memmove(&nlist
[prio
+ 1], &nlist
[prio
],
1575 (config
->num_prio
- prio
) * sizeof(struct wpa_ssid
*));
1579 config
->pssid
= nlist
;
1586 * wpa_config_update_prio_list - Update network priority list
1587 * @config: Configuration data from wpa_config_read()
1588 * Returns: 0 on success, -1 on failure
1590 * This function is called to update the priority list of networks in the
1591 * configuration when a network is being added or removed. This is also called
1592 * if a priority for a network is changed.
1594 int wpa_config_update_prio_list(struct wpa_config
*config
)
1596 struct wpa_ssid
*ssid
;
1599 os_free(config
->pssid
);
1600 config
->pssid
= NULL
;
1601 config
->num_prio
= 0;
1603 ssid
= config
->ssid
;
1606 if (wpa_config_add_prio_network(config
, ssid
) < 0)
1615 #ifdef IEEE8021X_EAPOL
1616 static void eap_peer_config_free(struct eap_peer_config
*eap
)
1618 os_free(eap
->eap_methods
);
1619 os_free(eap
->identity
);
1620 os_free(eap
->anonymous_identity
);
1621 os_free(eap
->password
);
1622 os_free(eap
->ca_cert
);
1623 os_free(eap
->ca_path
);
1624 os_free(eap
->client_cert
);
1625 os_free(eap
->private_key
);
1626 os_free(eap
->private_key_passwd
);
1627 os_free(eap
->dh_file
);
1628 os_free(eap
->subject_match
);
1629 os_free(eap
->altsubject_match
);
1630 os_free(eap
->ca_cert2
);
1631 os_free(eap
->ca_path2
);
1632 os_free(eap
->client_cert2
);
1633 os_free(eap
->private_key2
);
1634 os_free(eap
->private_key2_passwd
);
1635 os_free(eap
->dh_file2
);
1636 os_free(eap
->subject_match2
);
1637 os_free(eap
->altsubject_match2
);
1638 os_free(eap
->phase1
);
1639 os_free(eap
->phase2
);
1642 os_free(eap
->engine_id
);
1643 os_free(eap
->key_id
);
1644 os_free(eap
->cert_id
);
1645 os_free(eap
->ca_cert_id
);
1646 os_free(eap
->key2_id
);
1647 os_free(eap
->cert2_id
);
1648 os_free(eap
->ca_cert2_id
);
1650 os_free(eap
->engine2_id
);
1652 os_free(eap
->pending_req_otp
);
1653 os_free(eap
->pac_file
);
1654 os_free(eap
->new_password
);
1656 #endif /* IEEE8021X_EAPOL */
1660 * wpa_config_free_ssid - Free network/ssid configuration data
1661 * @ssid: Configuration data for the network
1663 * This function frees all resources allocated for the network configuration
1666 void wpa_config_free_ssid(struct wpa_ssid
*ssid
)
1668 os_free(ssid
->ssid
);
1669 os_free(ssid
->passphrase
);
1670 #ifdef IEEE8021X_EAPOL
1671 eap_peer_config_free(&ssid
->eap
);
1672 #endif /* IEEE8021X_EAPOL */
1673 os_free(ssid
->id_str
);
1674 os_free(ssid
->scan_freq
);
1675 os_free(ssid
->freq_list
);
1676 os_free(ssid
->bgscan
);
1682 * wpa_config_free - Free configuration data
1683 * @config: Configuration data from wpa_config_read()
1685 * This function frees all resources allocated for the configuration data by
1686 * wpa_config_read().
1688 void wpa_config_free(struct wpa_config
*config
)
1690 #ifndef CONFIG_NO_CONFIG_BLOBS
1691 struct wpa_config_blob
*blob
, *prevblob
;
1692 #endif /* CONFIG_NO_CONFIG_BLOBS */
1693 struct wpa_ssid
*ssid
, *prev
= NULL
;
1694 ssid
= config
->ssid
;
1698 wpa_config_free_ssid(prev
);
1701 #ifndef CONFIG_NO_CONFIG_BLOBS
1702 blob
= config
->blobs
;
1707 wpa_config_free_blob(prevblob
);
1709 #endif /* CONFIG_NO_CONFIG_BLOBS */
1711 os_free(config
->ctrl_interface
);
1712 os_free(config
->ctrl_interface_group
);
1713 os_free(config
->opensc_engine_path
);
1714 os_free(config
->pkcs11_engine_path
);
1715 os_free(config
->pkcs11_module_path
);
1716 os_free(config
->driver_param
);
1717 os_free(config
->device_name
);
1718 os_free(config
->manufacturer
);
1719 os_free(config
->model_name
);
1720 os_free(config
->model_number
);
1721 os_free(config
->serial_number
);
1722 os_free(config
->device_type
);
1723 os_free(config
->config_methods
);
1724 os_free(config
->pssid
);
1730 * wpa_config_get_network - Get configured network based on id
1731 * @config: Configuration data from wpa_config_read()
1732 * @id: Unique network id to search for
1733 * Returns: Network configuration or %NULL if not found
1735 struct wpa_ssid
* wpa_config_get_network(struct wpa_config
*config
, int id
)
1737 struct wpa_ssid
*ssid
;
1739 ssid
= config
->ssid
;
1751 * wpa_config_add_network - Add a new network with empty configuration
1752 * @config: Configuration data from wpa_config_read()
1753 * Returns: The new network configuration or %NULL if operation failed
1755 struct wpa_ssid
* wpa_config_add_network(struct wpa_config
*config
)
1758 struct wpa_ssid
*ssid
, *last
= NULL
;
1761 ssid
= config
->ssid
;
1770 ssid
= os_zalloc(sizeof(*ssid
));
1777 config
->ssid
= ssid
;
1779 wpa_config_update_prio_list(config
);
1786 * wpa_config_remove_network - Remove a configured network based on id
1787 * @config: Configuration data from wpa_config_read()
1788 * @id: Unique network id to search for
1789 * Returns: 0 on success, or -1 if the network was not found
1791 int wpa_config_remove_network(struct wpa_config
*config
, int id
)
1793 struct wpa_ssid
*ssid
, *prev
= NULL
;
1795 ssid
= config
->ssid
;
1807 prev
->next
= ssid
->next
;
1809 config
->ssid
= ssid
->next
;
1811 wpa_config_update_prio_list(config
);
1812 wpa_config_free_ssid(ssid
);
1818 * wpa_config_set_network_defaults - Set network default values
1819 * @ssid: Pointer to network configuration data
1821 void wpa_config_set_network_defaults(struct wpa_ssid
*ssid
)
1823 ssid
->proto
= DEFAULT_PROTO
;
1824 ssid
->pairwise_cipher
= DEFAULT_PAIRWISE
;
1825 ssid
->group_cipher
= DEFAULT_GROUP
;
1826 ssid
->key_mgmt
= DEFAULT_KEY_MGMT
;
1827 #ifdef IEEE8021X_EAPOL
1828 ssid
->eapol_flags
= DEFAULT_EAPOL_FLAGS
;
1829 ssid
->eap_workaround
= DEFAULT_EAP_WORKAROUND
;
1830 ssid
->eap
.fragment_size
= DEFAULT_FRAGMENT_SIZE
;
1831 #endif /* IEEE8021X_EAPOL */
1836 * wpa_config_set - Set a variable in network configuration
1837 * @ssid: Pointer to network configuration data
1838 * @var: Variable name, e.g., "ssid"
1839 * @value: Variable value
1840 * @line: Line number in configuration file or 0 if not used
1841 * Returns: 0 on success, -1 on failure
1843 * This function can be used to set network configuration variables based on
1844 * both the configuration file and management interface input. The value
1845 * parameter must be in the same format as the text-based configuration file is
1846 * using. For example, strings are using double quotation marks.
1848 int wpa_config_set(struct wpa_ssid
*ssid
, const char *var
, const char *value
,
1854 if (ssid
== NULL
|| var
== NULL
|| value
== NULL
)
1857 for (i
= 0; i
< NUM_SSID_FIELDS
; i
++) {
1858 const struct parse_data
*field
= &ssid_fields
[i
];
1859 if (os_strcmp(var
, field
->name
) != 0)
1862 if (field
->parser(field
, ssid
, line
, value
)) {
1864 wpa_printf(MSG_ERROR
, "Line %d: failed to "
1865 "parse %s '%s'.", line
, var
, value
);
1871 if (i
== NUM_SSID_FIELDS
) {
1873 wpa_printf(MSG_ERROR
, "Line %d: unknown network field "
1874 "'%s'.", line
, var
);
1883 #ifndef NO_CONFIG_WRITE
1885 * wpa_config_get_all - Get all options from network configuration
1886 * @ssid: Pointer to network configuration data
1887 * @get_keys: Determines if keys/passwords will be included in returned list
1888 * Returns: %NULL terminated list of all set keys and their values in the form
1889 * of [key1, val1, key2, val2, ... , NULL]
1891 * This function can be used to get list of all configured network properties.
1892 * The caller is responsible for freeing the returned list and all its
1895 char ** wpa_config_get_all(struct wpa_ssid
*ssid
, int get_keys
)
1897 const struct parse_data
*field
;
1903 props
= os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS
) + 1));
1908 for (i
= 0; i
< NUM_SSID_FIELDS
; i
++) {
1909 field
= &ssid_fields
[i
];
1910 if (field
->key_data
&& !get_keys
)
1912 value
= field
->writer(field
, ssid
);
1915 if (os_strlen(value
) == 0) {
1920 key
= os_strdup(field
->name
);
1926 props
[fields_num
* 2] = key
;
1927 props
[fields_num
* 2 + 1] = value
;
1944 * wpa_config_get - Get a variable in network configuration
1945 * @ssid: Pointer to network configuration data
1946 * @var: Variable name, e.g., "ssid"
1947 * Returns: Value of the variable or %NULL on failure
1949 * This function can be used to get network configuration variables. The
1950 * returned value is a copy of the configuration variable in text format, i.e,.
1951 * the same format that the text-based configuration file and wpa_config_set()
1952 * are using for the value. The caller is responsible for freeing the returned
1955 char * wpa_config_get(struct wpa_ssid
*ssid
, const char *var
)
1959 if (ssid
== NULL
|| var
== NULL
)
1962 for (i
= 0; i
< NUM_SSID_FIELDS
; i
++) {
1963 const struct parse_data
*field
= &ssid_fields
[i
];
1964 if (os_strcmp(var
, field
->name
) == 0)
1965 return field
->writer(field
, ssid
);
1973 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
1974 * @ssid: Pointer to network configuration data
1975 * @var: Variable name, e.g., "ssid"
1976 * Returns: Value of the variable or %NULL on failure
1978 * This function can be used to get network configuration variable like
1979 * wpa_config_get(). The only difference is that this functions does not expose
1980 * key/password material from the configuration. In case a key/password field
1981 * is requested, the returned value is an empty string or %NULL if the variable
1982 * is not set or "*" if the variable is set (regardless of its value). The
1983 * returned value is a copy of the configuration variable in text format, i.e,.
1984 * the same format that the text-based configuration file and wpa_config_set()
1985 * are using for the value. The caller is responsible for freeing the returned
1988 char * wpa_config_get_no_key(struct wpa_ssid
*ssid
, const char *var
)
1992 if (ssid
== NULL
|| var
== NULL
)
1995 for (i
= 0; i
< NUM_SSID_FIELDS
; i
++) {
1996 const struct parse_data
*field
= &ssid_fields
[i
];
1997 if (os_strcmp(var
, field
->name
) == 0) {
1998 char *res
= field
->writer(field
, ssid
);
1999 if (field
->key_data
) {
2000 if (res
&& res
[0]) {
2001 wpa_printf(MSG_DEBUG
, "Do not allow "
2002 "key_data field to be "
2005 return os_strdup("*");
2017 #endif /* NO_CONFIG_WRITE */
2021 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2022 * @ssid: Pointer to network configuration data
2024 * This function must be called to update WPA PSK when either SSID or the
2025 * passphrase has changed for the network configuration.
2027 void wpa_config_update_psk(struct wpa_ssid
*ssid
)
2029 #ifndef CONFIG_NO_PBKDF2
2030 pbkdf2_sha1(ssid
->passphrase
,
2031 (char *) ssid
->ssid
, ssid
->ssid_len
, 4096,
2032 ssid
->psk
, PMK_LEN
);
2033 wpa_hexdump_key(MSG_MSGDUMP
, "PSK (from passphrase)",
2034 ssid
->psk
, PMK_LEN
);
2036 #endif /* CONFIG_NO_PBKDF2 */
2040 #ifndef CONFIG_NO_CONFIG_BLOBS
2042 * wpa_config_get_blob - Get a named configuration blob
2043 * @config: Configuration data from wpa_config_read()
2044 * @name: Name of the blob
2045 * Returns: Pointer to blob data or %NULL if not found
2047 const struct wpa_config_blob
* wpa_config_get_blob(struct wpa_config
*config
,
2050 struct wpa_config_blob
*blob
= config
->blobs
;
2053 if (os_strcmp(blob
->name
, name
) == 0)
2062 * wpa_config_set_blob - Set or add a named configuration blob
2063 * @config: Configuration data from wpa_config_read()
2064 * @blob: New value for the blob
2066 * Adds a new configuration blob or replaces the current value of an existing
2069 void wpa_config_set_blob(struct wpa_config
*config
,
2070 struct wpa_config_blob
*blob
)
2072 wpa_config_remove_blob(config
, blob
->name
);
2073 blob
->next
= config
->blobs
;
2074 config
->blobs
= blob
;
2079 * wpa_config_free_blob - Free blob data
2080 * @blob: Pointer to blob to be freed
2082 void wpa_config_free_blob(struct wpa_config_blob
*blob
)
2085 os_free(blob
->name
);
2086 os_free(blob
->data
);
2093 * wpa_config_remove_blob - Remove a named configuration blob
2094 * @config: Configuration data from wpa_config_read()
2095 * @name: Name of the blob to remove
2096 * Returns: 0 if blob was removed or -1 if blob was not found
2098 int wpa_config_remove_blob(struct wpa_config
*config
, const char *name
)
2100 struct wpa_config_blob
*pos
= config
->blobs
, *prev
= NULL
;
2103 if (os_strcmp(pos
->name
, name
) == 0) {
2105 prev
->next
= pos
->next
;
2107 config
->blobs
= pos
->next
;
2108 wpa_config_free_blob(pos
);
2117 #endif /* CONFIG_NO_CONFIG_BLOBS */
2121 * wpa_config_alloc_empty - Allocate an empty configuration
2122 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2124 * @driver_param: Driver parameters
2125 * Returns: Pointer to allocated configuration data or %NULL on failure
2127 struct wpa_config
* wpa_config_alloc_empty(const char *ctrl_interface
,
2128 const char *driver_param
)
2130 struct wpa_config
*config
;
2132 config
= os_zalloc(sizeof(*config
));
2135 config
->eapol_version
= DEFAULT_EAPOL_VERSION
;
2136 config
->ap_scan
= DEFAULT_AP_SCAN
;
2137 config
->fast_reauth
= DEFAULT_FAST_REAUTH
;
2138 config
->bss_max_count
= DEFAULT_BSS_MAX_COUNT
;
2141 config
->ctrl_interface
= os_strdup(ctrl_interface
);
2143 config
->driver_param
= os_strdup(driver_param
);
2149 #ifndef CONFIG_NO_STDOUT_DEBUG
2151 * wpa_config_debug_dump_networks - Debug dump of configured networks
2152 * @config: Configuration data from wpa_config_read()
2154 void wpa_config_debug_dump_networks(struct wpa_config
*config
)
2157 struct wpa_ssid
*ssid
;
2159 for (prio
= 0; prio
< config
->num_prio
; prio
++) {
2160 ssid
= config
->pssid
[prio
];
2161 wpa_printf(MSG_DEBUG
, "Priority group %d",
2164 wpa_printf(MSG_DEBUG
, " id=%d ssid='%s'",
2166 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
2171 #endif /* CONFIG_NO_STDOUT_DEBUG */