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