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