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