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