blob: e157845f73917de05dfc51bb2534e107118cc096 [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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056static int wpa_config_parse_str(const struct parse_data *data,
57 struct wpa_ssid *ssid,
58 int line, const char *value)
59{
60 size_t res_len, *dst_len;
61 char **dst, *tmp;
62
63 if (os_strcmp(value, "NULL") == 0) {
64 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
65 data->name);
66 tmp = NULL;
67 res_len = 0;
68 goto set;
69 }
70
71 tmp = wpa_config_parse_string(value, &res_len);
72 if (tmp == NULL) {
73 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
74 line, data->name,
75 data->key_data ? "[KEY DATA REMOVED]" : value);
76 return -1;
77 }
78
79 if (data->key_data) {
80 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
81 (u8 *) tmp, res_len);
82 } else {
83 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
84 (u8 *) tmp, res_len);
85 }
86
87 if (data->param3 && res_len < (size_t) data->param3) {
88 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
89 "min_len=%ld)", line, data->name,
90 (unsigned long) res_len, (long) data->param3);
91 os_free(tmp);
92 return -1;
93 }
94
95 if (data->param4 && res_len > (size_t) data->param4) {
96 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
97 "max_len=%ld)", line, data->name,
98 (unsigned long) res_len, (long) data->param4);
99 os_free(tmp);
100 return -1;
101 }
102
103set:
104 dst = (char **) (((u8 *) ssid) + (long) data->param1);
105 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
106 os_free(*dst);
107 *dst = tmp;
108 if (data->param2)
109 *dst_len = res_len;
110
111 return 0;
112}
113
114
115#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
117{
118 char *buf;
119
120 buf = os_malloc(len + 3);
121 if (buf == NULL)
122 return NULL;
123 buf[0] = '"';
124 os_memcpy(buf + 1, value, len);
125 buf[len + 1] = '"';
126 buf[len + 2] = '\0';
127
128 return buf;
129}
130
131
132static char * wpa_config_write_string_hex(const u8 *value, size_t len)
133{
134 char *buf;
135
136 buf = os_zalloc(2 * len + 1);
137 if (buf == NULL)
138 return NULL;
139 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
140
141 return buf;
142}
143
144
145static char * wpa_config_write_string(const u8 *value, size_t len)
146{
147 if (value == NULL)
148 return NULL;
149
150 if (is_hex(value, len))
151 return wpa_config_write_string_hex(value, len);
152 else
153 return wpa_config_write_string_ascii(value, len);
154}
155
156
157static char * wpa_config_write_str(const struct parse_data *data,
158 struct wpa_ssid *ssid)
159{
160 size_t len;
161 char **src;
162
163 src = (char **) (((u8 *) ssid) + (long) data->param1);
164 if (*src == NULL)
165 return NULL;
166
167 if (data->param2)
168 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
169 else
170 len = os_strlen(*src);
171
172 return wpa_config_write_string((const u8 *) *src, len);
173}
174#endif /* NO_CONFIG_WRITE */
175
176
177static int wpa_config_parse_int(const struct parse_data *data,
178 struct wpa_ssid *ssid,
179 int line, const char *value)
180{
181 int *dst;
182
183 dst = (int *) (((u8 *) ssid) + (long) data->param1);
184 *dst = atoi(value);
185 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
186
187 if (data->param3 && *dst < (long) data->param3) {
188 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
189 "min_value=%ld)", line, data->name, *dst,
190 (long) data->param3);
191 *dst = (long) data->param3;
192 return -1;
193 }
194
195 if (data->param4 && *dst > (long) data->param4) {
196 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
197 "max_value=%ld)", line, data->name, *dst,
198 (long) data->param4);
199 *dst = (long) data->param4;
200 return -1;
201 }
202
203 return 0;
204}
205
206
207#ifndef NO_CONFIG_WRITE
208static char * wpa_config_write_int(const struct parse_data *data,
209 struct wpa_ssid *ssid)
210{
211 int *src, res;
212 char *value;
213
214 src = (int *) (((u8 *) ssid) + (long) data->param1);
215
216 value = os_malloc(20);
217 if (value == NULL)
218 return NULL;
219 res = os_snprintf(value, 20, "%d", *src);
220 if (res < 0 || res >= 20) {
221 os_free(value);
222 return NULL;
223 }
224 value[20 - 1] = '\0';
225 return value;
226}
227#endif /* NO_CONFIG_WRITE */
228
229
230static int wpa_config_parse_bssid(const struct parse_data *data,
231 struct wpa_ssid *ssid, int line,
232 const char *value)
233{
234 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
235 os_strcmp(value, "any") == 0) {
236 ssid->bssid_set = 0;
237 wpa_printf(MSG_MSGDUMP, "BSSID any");
238 return 0;
239 }
240 if (hwaddr_aton(value, ssid->bssid)) {
241 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
242 line, value);
243 return -1;
244 }
245 ssid->bssid_set = 1;
246 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
247 return 0;
248}
249
250
251#ifndef NO_CONFIG_WRITE
252static char * wpa_config_write_bssid(const struct parse_data *data,
253 struct wpa_ssid *ssid)
254{
255 char *value;
256 int res;
257
258 if (!ssid->bssid_set)
259 return NULL;
260
261 value = os_malloc(20);
262 if (value == NULL)
263 return NULL;
264 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
265 if (res < 0 || res >= 20) {
266 os_free(value);
267 return NULL;
268 }
269 value[20 - 1] = '\0';
270 return value;
271}
272#endif /* NO_CONFIG_WRITE */
273
274
275static int wpa_config_parse_psk(const struct parse_data *data,
276 struct wpa_ssid *ssid, int line,
277 const char *value)
278{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700279#ifdef CONFIG_EXT_PASSWORD
280 if (os_strncmp(value, "ext:", 4) == 0) {
281 os_free(ssid->passphrase);
282 ssid->passphrase = NULL;
283 ssid->psk_set = 0;
284 os_free(ssid->ext_psk);
285 ssid->ext_psk = os_strdup(value + 4);
286 if (ssid->ext_psk == NULL)
287 return -1;
288 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
289 ssid->ext_psk);
290 return 0;
291 }
292#endif /* CONFIG_EXT_PASSWORD */
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 if (*value == '"') {
295#ifndef CONFIG_NO_PBKDF2
296 const char *pos;
297 size_t len;
298
299 value++;
300 pos = os_strrchr(value, '"');
301 if (pos)
302 len = pos - value;
303 else
304 len = os_strlen(value);
305 if (len < 8 || len > 63) {
306 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
307 "length %lu (expected: 8..63) '%s'.",
308 line, (unsigned long) len, value);
309 return -1;
310 }
311 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
312 (u8 *) value, len);
313 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
314 os_memcmp(ssid->passphrase, value, len) == 0)
315 return 0;
316 ssid->psk_set = 0;
317 os_free(ssid->passphrase);
318 ssid->passphrase = os_malloc(len + 1);
319 if (ssid->passphrase == NULL)
320 return -1;
321 os_memcpy(ssid->passphrase, value, len);
322 ssid->passphrase[len] = '\0';
323 return 0;
324#else /* CONFIG_NO_PBKDF2 */
325 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
326 "supported.", line);
327 return -1;
328#endif /* CONFIG_NO_PBKDF2 */
329 }
330
331 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
332 value[PMK_LEN * 2] != '\0') {
333 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
334 line, value);
335 return -1;
336 }
337
338 os_free(ssid->passphrase);
339 ssid->passphrase = NULL;
340
341 ssid->psk_set = 1;
342 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
343 return 0;
344}
345
346
347#ifndef NO_CONFIG_WRITE
348static char * wpa_config_write_psk(const struct parse_data *data,
349 struct wpa_ssid *ssid)
350{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700351#ifdef CONFIG_EXT_PASSWORD
352 if (ssid->ext_psk) {
353 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
354 char *buf = os_malloc(len);
355 if (buf == NULL)
356 return NULL;
357 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
358 return buf;
359 }
360#endif /* CONFIG_EXT_PASSWORD */
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 if (ssid->passphrase)
363 return wpa_config_write_string_ascii(
364 (const u8 *) ssid->passphrase,
365 os_strlen(ssid->passphrase));
366
367 if (ssid->psk_set)
368 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
369
370 return NULL;
371}
372#endif /* NO_CONFIG_WRITE */
373
374
375static int wpa_config_parse_proto(const struct parse_data *data,
376 struct wpa_ssid *ssid, int line,
377 const char *value)
378{
379 int val = 0, last, errors = 0;
380 char *start, *end, *buf;
381
382 buf = os_strdup(value);
383 if (buf == NULL)
384 return -1;
385 start = buf;
386
387 while (*start != '\0') {
388 while (*start == ' ' || *start == '\t')
389 start++;
390 if (*start == '\0')
391 break;
392 end = start;
393 while (*end != ' ' && *end != '\t' && *end != '\0')
394 end++;
395 last = *end == '\0';
396 *end = '\0';
397 if (os_strcmp(start, "WPA") == 0)
398 val |= WPA_PROTO_WPA;
399 else if (os_strcmp(start, "RSN") == 0 ||
400 os_strcmp(start, "WPA2") == 0)
401 val |= WPA_PROTO_RSN;
402 else {
403 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
404 line, start);
405 errors++;
406 }
407
408 if (last)
409 break;
410 start = end + 1;
411 }
412 os_free(buf);
413
414 if (val == 0) {
415 wpa_printf(MSG_ERROR,
416 "Line %d: no proto values configured.", line);
417 errors++;
418 }
419
420 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
421 ssid->proto = val;
422 return errors ? -1 : 0;
423}
424
425
426#ifndef NO_CONFIG_WRITE
427static char * wpa_config_write_proto(const struct parse_data *data,
428 struct wpa_ssid *ssid)
429{
430 int first = 1, ret;
431 char *buf, *pos, *end;
432
433 pos = buf = os_zalloc(10);
434 if (buf == NULL)
435 return NULL;
436 end = buf + 10;
437
438 if (ssid->proto & WPA_PROTO_WPA) {
439 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
440 if (ret < 0 || ret >= end - pos)
441 return buf;
442 pos += ret;
443 first = 0;
444 }
445
446 if (ssid->proto & WPA_PROTO_RSN) {
447 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
448 if (ret < 0 || ret >= end - pos)
449 return buf;
450 pos += ret;
451 first = 0;
452 }
453
454 return buf;
455}
456#endif /* NO_CONFIG_WRITE */
457
458
459static int wpa_config_parse_key_mgmt(const struct parse_data *data,
460 struct wpa_ssid *ssid, int line,
461 const char *value)
462{
463 int val = 0, last, errors = 0;
464 char *start, *end, *buf;
465
466 buf = os_strdup(value);
467 if (buf == NULL)
468 return -1;
469 start = buf;
470
471 while (*start != '\0') {
472 while (*start == ' ' || *start == '\t')
473 start++;
474 if (*start == '\0')
475 break;
476 end = start;
477 while (*end != ' ' && *end != '\t' && *end != '\0')
478 end++;
479 last = *end == '\0';
480 *end = '\0';
481 if (os_strcmp(start, "WPA-PSK") == 0)
482 val |= WPA_KEY_MGMT_PSK;
483 else if (os_strcmp(start, "WPA-EAP") == 0)
484 val |= WPA_KEY_MGMT_IEEE8021X;
485 else if (os_strcmp(start, "IEEE8021X") == 0)
486 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
487 else if (os_strcmp(start, "NONE") == 0)
488 val |= WPA_KEY_MGMT_NONE;
489 else if (os_strcmp(start, "WPA-NONE") == 0)
490 val |= WPA_KEY_MGMT_WPA_NONE;
491#ifdef CONFIG_IEEE80211R
492 else if (os_strcmp(start, "FT-PSK") == 0)
493 val |= WPA_KEY_MGMT_FT_PSK;
494 else if (os_strcmp(start, "FT-EAP") == 0)
495 val |= WPA_KEY_MGMT_FT_IEEE8021X;
496#endif /* CONFIG_IEEE80211R */
497#ifdef CONFIG_IEEE80211W
498 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
499 val |= WPA_KEY_MGMT_PSK_SHA256;
500 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
501 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
502#endif /* CONFIG_IEEE80211W */
503#ifdef CONFIG_WPS
504 else if (os_strcmp(start, "WPS") == 0)
505 val |= WPA_KEY_MGMT_WPS;
506#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800507#ifdef CONFIG_SAE
508 else if (os_strcmp(start, "SAE") == 0)
509 val |= WPA_KEY_MGMT_SAE;
510 else if (os_strcmp(start, "FT-SAE") == 0)
511 val |= WPA_KEY_MGMT_FT_SAE;
512#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 else {
514 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
515 line, start);
516 errors++;
517 }
518
519 if (last)
520 break;
521 start = end + 1;
522 }
523 os_free(buf);
524
525 if (val == 0) {
526 wpa_printf(MSG_ERROR,
527 "Line %d: no key_mgmt values configured.", line);
528 errors++;
529 }
530
531 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
532 ssid->key_mgmt = val;
533 return errors ? -1 : 0;
534}
535
536
537#ifndef NO_CONFIG_WRITE
538static char * wpa_config_write_key_mgmt(const struct parse_data *data,
539 struct wpa_ssid *ssid)
540{
541 char *buf, *pos, *end;
542 int ret;
543
544 pos = buf = os_zalloc(50);
545 if (buf == NULL)
546 return NULL;
547 end = buf + 50;
548
549 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
550 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
551 pos == buf ? "" : " ");
552 if (ret < 0 || ret >= end - pos) {
553 end[-1] = '\0';
554 return buf;
555 }
556 pos += ret;
557 }
558
559 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
560 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
561 pos == buf ? "" : " ");
562 if (ret < 0 || ret >= end - pos) {
563 end[-1] = '\0';
564 return buf;
565 }
566 pos += ret;
567 }
568
569 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
570 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
571 pos == buf ? "" : " ");
572 if (ret < 0 || ret >= end - pos) {
573 end[-1] = '\0';
574 return buf;
575 }
576 pos += ret;
577 }
578
579 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
580 ret = os_snprintf(pos, end - pos, "%sNONE",
581 pos == buf ? "" : " ");
582 if (ret < 0 || ret >= end - pos) {
583 end[-1] = '\0';
584 return buf;
585 }
586 pos += ret;
587 }
588
589 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
590 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
591 pos == buf ? "" : " ");
592 if (ret < 0 || ret >= end - pos) {
593 end[-1] = '\0';
594 return buf;
595 }
596 pos += ret;
597 }
598
599#ifdef CONFIG_IEEE80211R
600 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
601 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
602 pos == buf ? "" : " ");
603
604 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
605 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
606 pos == buf ? "" : " ");
607#endif /* CONFIG_IEEE80211R */
608
609#ifdef CONFIG_IEEE80211W
610 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
611 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
612 pos == buf ? "" : " ");
613
614 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
615 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
616 pos == buf ? "" : " ");
617#endif /* CONFIG_IEEE80211W */
618
619#ifdef CONFIG_WPS
620 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
621 pos += os_snprintf(pos, end - pos, "%sWPS",
622 pos == buf ? "" : " ");
623#endif /* CONFIG_WPS */
624
625 return buf;
626}
627#endif /* NO_CONFIG_WRITE */
628
629
630static int wpa_config_parse_cipher(int line, const char *value)
631{
632 int val = 0, last;
633 char *start, *end, *buf;
634
635 buf = os_strdup(value);
636 if (buf == NULL)
637 return -1;
638 start = buf;
639
640 while (*start != '\0') {
641 while (*start == ' ' || *start == '\t')
642 start++;
643 if (*start == '\0')
644 break;
645 end = start;
646 while (*end != ' ' && *end != '\t' && *end != '\0')
647 end++;
648 last = *end == '\0';
649 *end = '\0';
650 if (os_strcmp(start, "CCMP") == 0)
651 val |= WPA_CIPHER_CCMP;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652 else if (os_strcmp(start, "GCMP") == 0)
653 val |= WPA_CIPHER_GCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 else if (os_strcmp(start, "TKIP") == 0)
655 val |= WPA_CIPHER_TKIP;
656 else if (os_strcmp(start, "WEP104") == 0)
657 val |= WPA_CIPHER_WEP104;
658 else if (os_strcmp(start, "WEP40") == 0)
659 val |= WPA_CIPHER_WEP40;
660 else if (os_strcmp(start, "NONE") == 0)
661 val |= WPA_CIPHER_NONE;
662 else {
663 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
664 line, start);
665 os_free(buf);
666 return -1;
667 }
668
669 if (last)
670 break;
671 start = end + 1;
672 }
673 os_free(buf);
674
675 if (val == 0) {
676 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
677 line);
678 return -1;
679 }
680 return val;
681}
682
683
684#ifndef NO_CONFIG_WRITE
685static char * wpa_config_write_cipher(int cipher)
686{
687 char *buf, *pos, *end;
688 int ret;
689
690 pos = buf = os_zalloc(50);
691 if (buf == NULL)
692 return NULL;
693 end = buf + 50;
694
695 if (cipher & WPA_CIPHER_CCMP) {
696 ret = os_snprintf(pos, end - pos, "%sCCMP",
697 pos == buf ? "" : " ");
698 if (ret < 0 || ret >= end - pos) {
699 end[-1] = '\0';
700 return buf;
701 }
702 pos += ret;
703 }
704
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700705 if (cipher & WPA_CIPHER_GCMP) {
706 ret = os_snprintf(pos, end - pos, "%sGCMP",
707 pos == buf ? "" : " ");
708 if (ret < 0 || ret >= end - pos) {
709 end[-1] = '\0';
710 return buf;
711 }
712 pos += ret;
713 }
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 if (cipher & WPA_CIPHER_TKIP) {
716 ret = os_snprintf(pos, end - pos, "%sTKIP",
717 pos == buf ? "" : " ");
718 if (ret < 0 || ret >= end - pos) {
719 end[-1] = '\0';
720 return buf;
721 }
722 pos += ret;
723 }
724
725 if (cipher & WPA_CIPHER_WEP104) {
726 ret = os_snprintf(pos, end - pos, "%sWEP104",
727 pos == buf ? "" : " ");
728 if (ret < 0 || ret >= end - pos) {
729 end[-1] = '\0';
730 return buf;
731 }
732 pos += ret;
733 }
734
735 if (cipher & WPA_CIPHER_WEP40) {
736 ret = os_snprintf(pos, end - pos, "%sWEP40",
737 pos == buf ? "" : " ");
738 if (ret < 0 || ret >= end - pos) {
739 end[-1] = '\0';
740 return buf;
741 }
742 pos += ret;
743 }
744
745 if (cipher & WPA_CIPHER_NONE) {
746 ret = os_snprintf(pos, end - pos, "%sNONE",
747 pos == buf ? "" : " ");
748 if (ret < 0 || ret >= end - pos) {
749 end[-1] = '\0';
750 return buf;
751 }
752 pos += ret;
753 }
754
755 return buf;
756}
757#endif /* NO_CONFIG_WRITE */
758
759
760static int wpa_config_parse_pairwise(const struct parse_data *data,
761 struct wpa_ssid *ssid, int line,
762 const char *value)
763{
764 int val;
765 val = wpa_config_parse_cipher(line, value);
766 if (val == -1)
767 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700768 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_GCMP | WPA_CIPHER_TKIP |
769 WPA_CIPHER_NONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
771 "(0x%x).", line, val);
772 return -1;
773 }
774
775 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
776 ssid->pairwise_cipher = val;
777 return 0;
778}
779
780
781#ifndef NO_CONFIG_WRITE
782static char * wpa_config_write_pairwise(const struct parse_data *data,
783 struct wpa_ssid *ssid)
784{
785 return wpa_config_write_cipher(ssid->pairwise_cipher);
786}
787#endif /* NO_CONFIG_WRITE */
788
789
790static int wpa_config_parse_group(const struct parse_data *data,
791 struct wpa_ssid *ssid, int line,
792 const char *value)
793{
794 int val;
795 val = wpa_config_parse_cipher(line, value);
796 if (val == -1)
797 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700798 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_GCMP | WPA_CIPHER_TKIP |
799 WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
801 "(0x%x).", line, val);
802 return -1;
803 }
804
805 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
806 ssid->group_cipher = val;
807 return 0;
808}
809
810
811#ifndef NO_CONFIG_WRITE
812static char * wpa_config_write_group(const struct parse_data *data,
813 struct wpa_ssid *ssid)
814{
815 return wpa_config_write_cipher(ssid->group_cipher);
816}
817#endif /* NO_CONFIG_WRITE */
818
819
820static int wpa_config_parse_auth_alg(const struct parse_data *data,
821 struct wpa_ssid *ssid, int line,
822 const char *value)
823{
824 int val = 0, last, errors = 0;
825 char *start, *end, *buf;
826
827 buf = os_strdup(value);
828 if (buf == NULL)
829 return -1;
830 start = buf;
831
832 while (*start != '\0') {
833 while (*start == ' ' || *start == '\t')
834 start++;
835 if (*start == '\0')
836 break;
837 end = start;
838 while (*end != ' ' && *end != '\t' && *end != '\0')
839 end++;
840 last = *end == '\0';
841 *end = '\0';
842 if (os_strcmp(start, "OPEN") == 0)
843 val |= WPA_AUTH_ALG_OPEN;
844 else if (os_strcmp(start, "SHARED") == 0)
845 val |= WPA_AUTH_ALG_SHARED;
846 else if (os_strcmp(start, "LEAP") == 0)
847 val |= WPA_AUTH_ALG_LEAP;
848 else {
849 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
850 line, start);
851 errors++;
852 }
853
854 if (last)
855 break;
856 start = end + 1;
857 }
858 os_free(buf);
859
860 if (val == 0) {
861 wpa_printf(MSG_ERROR,
862 "Line %d: no auth_alg values configured.", line);
863 errors++;
864 }
865
866 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
867 ssid->auth_alg = val;
868 return errors ? -1 : 0;
869}
870
871
872#ifndef NO_CONFIG_WRITE
873static char * wpa_config_write_auth_alg(const struct parse_data *data,
874 struct wpa_ssid *ssid)
875{
876 char *buf, *pos, *end;
877 int ret;
878
879 pos = buf = os_zalloc(30);
880 if (buf == NULL)
881 return NULL;
882 end = buf + 30;
883
884 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
885 ret = os_snprintf(pos, end - pos, "%sOPEN",
886 pos == buf ? "" : " ");
887 if (ret < 0 || ret >= end - pos) {
888 end[-1] = '\0';
889 return buf;
890 }
891 pos += ret;
892 }
893
894 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
895 ret = os_snprintf(pos, end - pos, "%sSHARED",
896 pos == buf ? "" : " ");
897 if (ret < 0 || ret >= end - pos) {
898 end[-1] = '\0';
899 return buf;
900 }
901 pos += ret;
902 }
903
904 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
905 ret = os_snprintf(pos, end - pos, "%sLEAP",
906 pos == buf ? "" : " ");
907 if (ret < 0 || ret >= end - pos) {
908 end[-1] = '\0';
909 return buf;
910 }
911 pos += ret;
912 }
913
914 return buf;
915}
916#endif /* NO_CONFIG_WRITE */
917
918
919static int * wpa_config_parse_freqs(const struct parse_data *data,
920 struct wpa_ssid *ssid, int line,
921 const char *value)
922{
923 int *freqs;
924 size_t used, len;
925 const char *pos;
926
927 used = 0;
928 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700929 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 if (freqs == NULL)
931 return NULL;
932
933 pos = value;
934 while (pos) {
935 while (*pos == ' ')
936 pos++;
937 if (used == len) {
938 int *n;
939 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 if (n == NULL) {
942 os_free(freqs);
943 return NULL;
944 }
945 for (i = len; i <= len * 2; i++)
946 n[i] = 0;
947 freqs = n;
948 len *= 2;
949 }
950
951 freqs[used] = atoi(pos);
952 if (freqs[used] == 0)
953 break;
954 used++;
955 pos = os_strchr(pos + 1, ' ');
956 }
957
958 return freqs;
959}
960
961
962static int wpa_config_parse_scan_freq(const struct parse_data *data,
963 struct wpa_ssid *ssid, int line,
964 const char *value)
965{
966 int *freqs;
967
968 freqs = wpa_config_parse_freqs(data, ssid, line, value);
969 if (freqs == NULL)
970 return -1;
971 os_free(ssid->scan_freq);
972 ssid->scan_freq = freqs;
973
974 return 0;
975}
976
977
978static int wpa_config_parse_freq_list(const struct parse_data *data,
979 struct wpa_ssid *ssid, int line,
980 const char *value)
981{
982 int *freqs;
983
984 freqs = wpa_config_parse_freqs(data, ssid, line, value);
985 if (freqs == NULL)
986 return -1;
987 os_free(ssid->freq_list);
988 ssid->freq_list = freqs;
989
990 return 0;
991}
992
993
994#ifndef NO_CONFIG_WRITE
995static char * wpa_config_write_freqs(const struct parse_data *data,
996 const int *freqs)
997{
998 char *buf, *pos, *end;
999 int i, ret;
1000 size_t count;
1001
1002 if (freqs == NULL)
1003 return NULL;
1004
1005 count = 0;
1006 for (i = 0; freqs[i]; i++)
1007 count++;
1008
1009 pos = buf = os_zalloc(10 * count + 1);
1010 if (buf == NULL)
1011 return NULL;
1012 end = buf + 10 * count + 1;
1013
1014 for (i = 0; freqs[i]; i++) {
1015 ret = os_snprintf(pos, end - pos, "%s%u",
1016 i == 0 ? "" : " ", freqs[i]);
1017 if (ret < 0 || ret >= end - pos) {
1018 end[-1] = '\0';
1019 return buf;
1020 }
1021 pos += ret;
1022 }
1023
1024 return buf;
1025}
1026
1027
1028static char * wpa_config_write_scan_freq(const struct parse_data *data,
1029 struct wpa_ssid *ssid)
1030{
1031 return wpa_config_write_freqs(data, ssid->scan_freq);
1032}
1033
1034
1035static char * wpa_config_write_freq_list(const struct parse_data *data,
1036 struct wpa_ssid *ssid)
1037{
1038 return wpa_config_write_freqs(data, ssid->freq_list);
1039}
1040#endif /* NO_CONFIG_WRITE */
1041
1042
1043#ifdef IEEE8021X_EAPOL
1044static int wpa_config_parse_eap(const struct parse_data *data,
1045 struct wpa_ssid *ssid, int line,
1046 const char *value)
1047{
1048 int last, errors = 0;
1049 char *start, *end, *buf;
1050 struct eap_method_type *methods = NULL, *tmp;
1051 size_t num_methods = 0;
1052
1053 buf = os_strdup(value);
1054 if (buf == NULL)
1055 return -1;
1056 start = buf;
1057
1058 while (*start != '\0') {
1059 while (*start == ' ' || *start == '\t')
1060 start++;
1061 if (*start == '\0')
1062 break;
1063 end = start;
1064 while (*end != ' ' && *end != '\t' && *end != '\0')
1065 end++;
1066 last = *end == '\0';
1067 *end = '\0';
1068 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001069 methods = os_realloc_array(methods, num_methods + 1,
1070 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 if (methods == NULL) {
1072 os_free(tmp);
1073 os_free(buf);
1074 return -1;
1075 }
1076 methods[num_methods].method = eap_peer_get_type(
1077 start, &methods[num_methods].vendor);
1078 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1079 methods[num_methods].method == EAP_TYPE_NONE) {
1080 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1081 "'%s'", line, start);
1082 wpa_printf(MSG_ERROR, "You may need to add support for"
1083 " this EAP method during wpa_supplicant\n"
1084 "build time configuration.\n"
1085 "See README for more information.");
1086 errors++;
1087 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1088 methods[num_methods].method == EAP_TYPE_LEAP)
1089 ssid->leap++;
1090 else
1091 ssid->non_leap++;
1092 num_methods++;
1093 if (last)
1094 break;
1095 start = end + 1;
1096 }
1097 os_free(buf);
1098
1099 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001100 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 if (methods == NULL) {
1102 os_free(tmp);
1103 return -1;
1104 }
1105 methods[num_methods].vendor = EAP_VENDOR_IETF;
1106 methods[num_methods].method = EAP_TYPE_NONE;
1107 num_methods++;
1108
1109 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1110 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001111 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 ssid->eap.eap_methods = methods;
1113 return errors ? -1 : 0;
1114}
1115
1116
1117static char * wpa_config_write_eap(const struct parse_data *data,
1118 struct wpa_ssid *ssid)
1119{
1120 int i, ret;
1121 char *buf, *pos, *end;
1122 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1123 const char *name;
1124
1125 if (eap_methods == NULL)
1126 return NULL;
1127
1128 pos = buf = os_zalloc(100);
1129 if (buf == NULL)
1130 return NULL;
1131 end = buf + 100;
1132
1133 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1134 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1135 name = eap_get_name(eap_methods[i].vendor,
1136 eap_methods[i].method);
1137 if (name) {
1138 ret = os_snprintf(pos, end - pos, "%s%s",
1139 pos == buf ? "" : " ", name);
1140 if (ret < 0 || ret >= end - pos)
1141 break;
1142 pos += ret;
1143 }
1144 }
1145
1146 end[-1] = '\0';
1147
1148 return buf;
1149}
1150
1151
1152static int wpa_config_parse_password(const struct parse_data *data,
1153 struct wpa_ssid *ssid, int line,
1154 const char *value)
1155{
1156 u8 *hash;
1157
1158 if (os_strcmp(value, "NULL") == 0) {
1159 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1160 os_free(ssid->eap.password);
1161 ssid->eap.password = NULL;
1162 ssid->eap.password_len = 0;
1163 return 0;
1164 }
1165
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001166#ifdef CONFIG_EXT_PASSWORD
1167 if (os_strncmp(value, "ext:", 4) == 0) {
1168 char *name = os_strdup(value + 4);
1169 if (name == NULL)
1170 return -1;
1171 os_free(ssid->eap.password);
1172 ssid->eap.password = (u8 *) name;
1173 ssid->eap.password_len = os_strlen(name);
1174 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1175 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1176 return 0;
1177 }
1178#endif /* CONFIG_EXT_PASSWORD */
1179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 if (os_strncmp(value, "hash:", 5) != 0) {
1181 char *tmp;
1182 size_t res_len;
1183
1184 tmp = wpa_config_parse_string(value, &res_len);
1185 if (tmp == NULL) {
1186 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1187 "password.", line);
1188 return -1;
1189 }
1190 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1191 (u8 *) tmp, res_len);
1192
1193 os_free(ssid->eap.password);
1194 ssid->eap.password = (u8 *) tmp;
1195 ssid->eap.password_len = res_len;
1196 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001197 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198
1199 return 0;
1200 }
1201
1202
1203 /* NtPasswordHash: hash:<32 hex digits> */
1204 if (os_strlen(value + 5) != 2 * 16) {
1205 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1206 "(expected 32 hex digits)", line);
1207 return -1;
1208 }
1209
1210 hash = os_malloc(16);
1211 if (hash == NULL)
1212 return -1;
1213
1214 if (hexstr2bin(value + 5, hash, 16)) {
1215 os_free(hash);
1216 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1217 return -1;
1218 }
1219
1220 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1221
1222 os_free(ssid->eap.password);
1223 ssid->eap.password = hash;
1224 ssid->eap.password_len = 16;
1225 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
1228 return 0;
1229}
1230
1231
1232static char * wpa_config_write_password(const struct parse_data *data,
1233 struct wpa_ssid *ssid)
1234{
1235 char *buf;
1236
1237 if (ssid->eap.password == NULL)
1238 return NULL;
1239
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240#ifdef CONFIG_EXT_PASSWORD
1241 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1242 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1243 if (buf == NULL)
1244 return NULL;
1245 os_memcpy(buf, "ext:", 4);
1246 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1247 return buf;
1248 }
1249#endif /* CONFIG_EXT_PASSWORD */
1250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1252 return wpa_config_write_string(
1253 ssid->eap.password, ssid->eap.password_len);
1254 }
1255
1256 buf = os_malloc(5 + 32 + 1);
1257 if (buf == NULL)
1258 return NULL;
1259
1260 os_memcpy(buf, "hash:", 5);
1261 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1262
1263 return buf;
1264}
1265#endif /* IEEE8021X_EAPOL */
1266
1267
1268static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1269 const char *value, int idx)
1270{
1271 char *buf, title[20];
1272 int res;
1273
1274 buf = wpa_config_parse_string(value, len);
1275 if (buf == NULL) {
1276 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1277 line, idx, value);
1278 return -1;
1279 }
1280 if (*len > MAX_WEP_KEY_LEN) {
1281 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1282 line, idx, value);
1283 os_free(buf);
1284 return -1;
1285 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001286 if (*len && *len != 5 && *len != 13 && *len != 16) {
1287 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1288 "this network block will be ignored",
1289 line, (unsigned int) *len);
1290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 os_memcpy(key, buf, *len);
1292 os_free(buf);
1293 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1294 if (res >= 0 && (size_t) res < sizeof(title))
1295 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1296 return 0;
1297}
1298
1299
1300static int wpa_config_parse_wep_key0(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[0],
1305 &ssid->wep_key_len[0], line,
1306 value, 0);
1307}
1308
1309
1310static int wpa_config_parse_wep_key1(const struct parse_data *data,
1311 struct wpa_ssid *ssid, int line,
1312 const char *value)
1313{
1314 return wpa_config_parse_wep_key(ssid->wep_key[1],
1315 &ssid->wep_key_len[1], line,
1316 value, 1);
1317}
1318
1319
1320static int wpa_config_parse_wep_key2(const struct parse_data *data,
1321 struct wpa_ssid *ssid, int line,
1322 const char *value)
1323{
1324 return wpa_config_parse_wep_key(ssid->wep_key[2],
1325 &ssid->wep_key_len[2], line,
1326 value, 2);
1327}
1328
1329
1330static int wpa_config_parse_wep_key3(const struct parse_data *data,
1331 struct wpa_ssid *ssid, int line,
1332 const char *value)
1333{
1334 return wpa_config_parse_wep_key(ssid->wep_key[3],
1335 &ssid->wep_key_len[3], line,
1336 value, 3);
1337}
1338
1339
1340#ifndef NO_CONFIG_WRITE
1341static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1342{
1343 if (ssid->wep_key_len[idx] == 0)
1344 return NULL;
1345 return wpa_config_write_string(ssid->wep_key[idx],
1346 ssid->wep_key_len[idx]);
1347}
1348
1349
1350static char * wpa_config_write_wep_key0(const struct parse_data *data,
1351 struct wpa_ssid *ssid)
1352{
1353 return wpa_config_write_wep_key(ssid, 0);
1354}
1355
1356
1357static char * wpa_config_write_wep_key1(const struct parse_data *data,
1358 struct wpa_ssid *ssid)
1359{
1360 return wpa_config_write_wep_key(ssid, 1);
1361}
1362
1363
1364static char * wpa_config_write_wep_key2(const struct parse_data *data,
1365 struct wpa_ssid *ssid)
1366{
1367 return wpa_config_write_wep_key(ssid, 2);
1368}
1369
1370
1371static char * wpa_config_write_wep_key3(const struct parse_data *data,
1372 struct wpa_ssid *ssid)
1373{
1374 return wpa_config_write_wep_key(ssid, 3);
1375}
1376#endif /* NO_CONFIG_WRITE */
1377
1378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001379#ifdef CONFIG_P2P
1380
1381static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1382 struct wpa_ssid *ssid, int line,
1383 const char *value)
1384{
1385 const char *pos;
1386 u8 *buf, *n, addr[ETH_ALEN];
1387 size_t count;
1388
1389 buf = NULL;
1390 count = 0;
1391
1392 pos = value;
1393 while (pos && *pos) {
1394 while (*pos == ' ')
1395 pos++;
1396
1397 if (hwaddr_aton(pos, addr)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001398 if (count == 0) {
1399 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1400 "p2p_client_list address '%s'.",
1401 line, value);
1402 os_free(buf);
1403 return -1;
1404 }
1405 /* continue anyway since this could have been from a
1406 * truncated configuration file line */
1407 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1408 "truncated p2p_client_list address '%s'",
1409 line, pos);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001411 n = os_realloc_array(buf, count + 1, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 if (n == NULL) {
1413 os_free(buf);
1414 return -1;
1415 }
1416 buf = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001417 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1418 os_memcpy(buf, addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 count++;
1420 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1421 addr, ETH_ALEN);
1422 }
1423
1424 pos = os_strchr(pos, ' ');
1425 }
1426
1427 os_free(ssid->p2p_client_list);
1428 ssid->p2p_client_list = buf;
1429 ssid->num_p2p_clients = count;
1430
1431 return 0;
1432}
1433
1434
1435#ifndef NO_CONFIG_WRITE
1436static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1437 struct wpa_ssid *ssid)
1438{
1439 char *value, *end, *pos;
1440 int res;
1441 size_t i;
1442
1443 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1444 return NULL;
1445
1446 value = os_malloc(20 * ssid->num_p2p_clients);
1447 if (value == NULL)
1448 return NULL;
1449 pos = value;
1450 end = value + 20 * ssid->num_p2p_clients;
1451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001452 for (i = ssid->num_p2p_clients; i > 0; i--) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001453 res = os_snprintf(pos, end - pos, MACSTR " ",
1454 MAC2STR(ssid->p2p_client_list +
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001455 (i - 1) * ETH_ALEN));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 if (res < 0 || res >= end - pos) {
1457 os_free(value);
1458 return NULL;
1459 }
1460 pos += res;
1461 }
1462
1463 if (pos > value)
1464 pos[-1] = '\0';
1465
1466 return value;
1467}
1468#endif /* NO_CONFIG_WRITE */
1469
1470#endif /* CONFIG_P2P */
1471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472/* Helper macros for network block parser */
1473
1474#ifdef OFFSET
1475#undef OFFSET
1476#endif /* OFFSET */
1477/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1478#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1479
1480/* STR: Define a string variable for an ASCII string; f = field name */
1481#ifdef NO_CONFIG_WRITE
1482#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1483#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1484#else /* NO_CONFIG_WRITE */
1485#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1486#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1487#endif /* NO_CONFIG_WRITE */
1488#define STR(f) _STR(f), NULL, NULL, NULL, 0
1489#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1490#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1491#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1492
1493/* STR_LEN: Define a string variable with a separate variable for storing the
1494 * data length. Unlike STR(), this can be used to store arbitrary binary data
1495 * (i.e., even nul termination character). */
1496#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1497#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1498#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1499#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1500#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1501
1502/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1503 * explicitly specified. */
1504#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1505#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1506#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1507
1508#ifdef NO_CONFIG_WRITE
1509#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1510#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1511#else /* NO_CONFIG_WRITE */
1512#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1513 OFFSET(f), (void *) 0
1514#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1515 OFFSET(eap.f), (void *) 0
1516#endif /* NO_CONFIG_WRITE */
1517
1518/* INT: Define an integer variable */
1519#define INT(f) _INT(f), NULL, NULL, 0
1520#define INTe(f) _INTe(f), NULL, NULL, 0
1521
1522/* INT_RANGE: Define an integer variable with allowed value range */
1523#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1524
1525/* FUNC: Define a configuration variable that uses a custom function for
1526 * parsing and writing the value. */
1527#ifdef NO_CONFIG_WRITE
1528#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1529#else /* NO_CONFIG_WRITE */
1530#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1531 NULL, NULL, NULL, NULL
1532#endif /* NO_CONFIG_WRITE */
1533#define FUNC(f) _FUNC(f), 0
1534#define FUNC_KEY(f) _FUNC(f), 1
1535
1536/*
1537 * Table of network configuration variables. This table is used to parse each
1538 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1539 * that is inside a network block.
1540 *
1541 * This table is generated using the helper macros defined above and with
1542 * generous help from the C pre-processor. The field name is stored as a string
1543 * into .name and for STR and INT types, the offset of the target buffer within
1544 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1545 * offset to the field containing the length of the configuration variable.
1546 * .param3 and .param4 can be used to mark the allowed range (length for STR
1547 * and value for INT).
1548 *
1549 * For each configuration line in wpa_supplicant.conf, the parser goes through
1550 * this table and select the entry that matches with the field name. The parser
1551 * function (.parser) is then called to parse the actual value of the field.
1552 *
1553 * This kind of mechanism makes it easy to add new configuration parameters,
1554 * since only one line needs to be added into this table and into the
1555 * struct wpa_ssid definition if the new variable is either a string or
1556 * integer. More complex types will need to use their own parser and writer
1557 * functions.
1558 */
1559static const struct parse_data ssid_fields[] = {
1560 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1561 { 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 Shmidt8d520ff2011-05-09 14:06:53 -07001649#undef OFFSET
1650#undef _STR
1651#undef STR
1652#undef STR_KEY
1653#undef _STR_LEN
1654#undef STR_LEN
1655#undef STR_LEN_KEY
1656#undef _STR_RANGE
1657#undef STR_RANGE
1658#undef STR_RANGE_KEY
1659#undef _INT
1660#undef INT
1661#undef INT_RANGE
1662#undef _FUNC
1663#undef FUNC
1664#undef FUNC_KEY
1665#define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1666
1667
1668/**
1669 * wpa_config_add_prio_network - Add a network to priority lists
1670 * @config: Configuration data from wpa_config_read()
1671 * @ssid: Pointer to the network configuration to be added to the list
1672 * Returns: 0 on success, -1 on failure
1673 *
1674 * This function is used to add a network block to the priority list of
1675 * networks. This must be called for each network when reading in the full
1676 * configuration. In addition, this can be used indirectly when updating
1677 * priorities by calling wpa_config_update_prio_list().
1678 */
1679int wpa_config_add_prio_network(struct wpa_config *config,
1680 struct wpa_ssid *ssid)
1681{
1682 int prio;
1683 struct wpa_ssid *prev, **nlist;
1684
1685 /*
1686 * Add to an existing priority list if one is available for the
1687 * configured priority level for this network.
1688 */
1689 for (prio = 0; prio < config->num_prio; prio++) {
1690 prev = config->pssid[prio];
1691 if (prev->priority == ssid->priority) {
1692 while (prev->pnext)
1693 prev = prev->pnext;
1694 prev->pnext = ssid;
1695 return 0;
1696 }
1697 }
1698
1699 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001700 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1701 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 if (nlist == NULL)
1703 return -1;
1704
1705 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001706 if (nlist[prio]->priority < ssid->priority) {
1707 os_memmove(&nlist[prio + 1], &nlist[prio],
1708 (config->num_prio - prio) *
1709 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 }
1713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 nlist[prio] = ssid;
1715 config->num_prio++;
1716 config->pssid = nlist;
1717
1718 return 0;
1719}
1720
1721
1722/**
1723 * wpa_config_update_prio_list - Update network priority list
1724 * @config: Configuration data from wpa_config_read()
1725 * Returns: 0 on success, -1 on failure
1726 *
1727 * This function is called to update the priority list of networks in the
1728 * configuration when a network is being added or removed. This is also called
1729 * if a priority for a network is changed.
1730 */
1731int wpa_config_update_prio_list(struct wpa_config *config)
1732{
1733 struct wpa_ssid *ssid;
1734 int ret = 0;
1735
1736 os_free(config->pssid);
1737 config->pssid = NULL;
1738 config->num_prio = 0;
1739
1740 ssid = config->ssid;
1741 while (ssid) {
1742 ssid->pnext = NULL;
1743 if (wpa_config_add_prio_network(config, ssid) < 0)
1744 ret = -1;
1745 ssid = ssid->next;
1746 }
1747
1748 return ret;
1749}
1750
1751
1752#ifdef IEEE8021X_EAPOL
1753static void eap_peer_config_free(struct eap_peer_config *eap)
1754{
1755 os_free(eap->eap_methods);
1756 os_free(eap->identity);
1757 os_free(eap->anonymous_identity);
1758 os_free(eap->password);
1759 os_free(eap->ca_cert);
1760 os_free(eap->ca_path);
1761 os_free(eap->client_cert);
1762 os_free(eap->private_key);
1763 os_free(eap->private_key_passwd);
1764 os_free(eap->dh_file);
1765 os_free(eap->subject_match);
1766 os_free(eap->altsubject_match);
1767 os_free(eap->ca_cert2);
1768 os_free(eap->ca_path2);
1769 os_free(eap->client_cert2);
1770 os_free(eap->private_key2);
1771 os_free(eap->private_key2_passwd);
1772 os_free(eap->dh_file2);
1773 os_free(eap->subject_match2);
1774 os_free(eap->altsubject_match2);
1775 os_free(eap->phase1);
1776 os_free(eap->phase2);
1777 os_free(eap->pcsc);
1778 os_free(eap->pin);
1779 os_free(eap->engine_id);
1780 os_free(eap->key_id);
1781 os_free(eap->cert_id);
1782 os_free(eap->ca_cert_id);
1783 os_free(eap->key2_id);
1784 os_free(eap->cert2_id);
1785 os_free(eap->ca_cert2_id);
1786 os_free(eap->pin2);
1787 os_free(eap->engine2_id);
1788 os_free(eap->otp);
1789 os_free(eap->pending_req_otp);
1790 os_free(eap->pac_file);
1791 os_free(eap->new_password);
1792}
1793#endif /* IEEE8021X_EAPOL */
1794
1795
1796/**
1797 * wpa_config_free_ssid - Free network/ssid configuration data
1798 * @ssid: Configuration data for the network
1799 *
1800 * This function frees all resources allocated for the network configuration
1801 * data.
1802 */
1803void wpa_config_free_ssid(struct wpa_ssid *ssid)
1804{
1805 os_free(ssid->ssid);
1806 os_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001807 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808#ifdef IEEE8021X_EAPOL
1809 eap_peer_config_free(&ssid->eap);
1810#endif /* IEEE8021X_EAPOL */
1811 os_free(ssid->id_str);
1812 os_free(ssid->scan_freq);
1813 os_free(ssid->freq_list);
1814 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001815 os_free(ssid->p2p_client_list);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001816#ifdef CONFIG_HT_OVERRIDES
1817 os_free(ssid->ht_mcs);
1818#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 os_free(ssid);
1820}
1821
1822
Dmitry Shmidt04949592012-07-19 12:16:46 -07001823void wpa_config_free_cred(struct wpa_cred *cred)
1824{
1825 os_free(cred->realm);
1826 os_free(cred->username);
1827 os_free(cred->password);
1828 os_free(cred->ca_cert);
1829 os_free(cred->client_cert);
1830 os_free(cred->private_key);
1831 os_free(cred->private_key_passwd);
1832 os_free(cred->imsi);
1833 os_free(cred->milenage);
1834 os_free(cred->domain);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001835 os_free(cred->eap_method);
1836 os_free(cred->phase1);
1837 os_free(cred->phase2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001838 os_free(cred);
1839}
1840
1841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842/**
1843 * wpa_config_free - Free configuration data
1844 * @config: Configuration data from wpa_config_read()
1845 *
1846 * This function frees all resources allocated for the configuration data by
1847 * wpa_config_read().
1848 */
1849void wpa_config_free(struct wpa_config *config)
1850{
1851#ifndef CONFIG_NO_CONFIG_BLOBS
1852 struct wpa_config_blob *blob, *prevblob;
1853#endif /* CONFIG_NO_CONFIG_BLOBS */
1854 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001855 struct wpa_cred *cred, *cprev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856
1857 ssid = config->ssid;
1858 while (ssid) {
1859 prev = ssid;
1860 ssid = ssid->next;
1861 wpa_config_free_ssid(prev);
1862 }
1863
Dmitry Shmidt04949592012-07-19 12:16:46 -07001864 cred = config->cred;
1865 while (cred) {
1866 cprev = cred;
1867 cred = cred->next;
1868 wpa_config_free_cred(cprev);
1869 }
1870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871#ifndef CONFIG_NO_CONFIG_BLOBS
1872 blob = config->blobs;
1873 prevblob = NULL;
1874 while (blob) {
1875 prevblob = blob;
1876 blob = blob->next;
1877 wpa_config_free_blob(prevblob);
1878 }
1879#endif /* CONFIG_NO_CONFIG_BLOBS */
1880
Dmitry Shmidt04949592012-07-19 12:16:46 -07001881 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 os_free(config->ctrl_interface);
1883 os_free(config->ctrl_interface_group);
1884 os_free(config->opensc_engine_path);
1885 os_free(config->pkcs11_engine_path);
1886 os_free(config->pkcs11_module_path);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001887 os_free(config->pcsc_reader);
1888 os_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 os_free(config->driver_param);
1890 os_free(config->device_name);
1891 os_free(config->manufacturer);
1892 os_free(config->model_name);
1893 os_free(config->model_number);
1894 os_free(config->serial_number);
1895 os_free(config->config_methods);
1896 os_free(config->p2p_ssid_postfix);
1897 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001898 os_free(config->p2p_pref_chan);
1899 os_free(config->autoscan);
1900 wpabuf_free(config->wps_nfc_dh_pubkey);
1901 wpabuf_free(config->wps_nfc_dh_privkey);
1902 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001903 os_free(config->ext_password_backend);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 os_free(config);
1905}
1906
1907
1908/**
1909 * wpa_config_foreach_network - Iterate over each configured network
1910 * @config: Configuration data from wpa_config_read()
1911 * @func: Callback function to process each network
1912 * @arg: Opaque argument to pass to callback function
1913 *
1914 * Iterate over the set of configured networks calling the specified
1915 * function for each item. We guard against callbacks removing the
1916 * supplied network.
1917 */
1918void wpa_config_foreach_network(struct wpa_config *config,
1919 void (*func)(void *, struct wpa_ssid *),
1920 void *arg)
1921{
1922 struct wpa_ssid *ssid, *next;
1923
1924 ssid = config->ssid;
1925 while (ssid) {
1926 next = ssid->next;
1927 func(arg, ssid);
1928 ssid = next;
1929 }
1930}
1931
1932
1933/**
1934 * wpa_config_get_network - Get configured network based on id
1935 * @config: Configuration data from wpa_config_read()
1936 * @id: Unique network id to search for
1937 * Returns: Network configuration or %NULL if not found
1938 */
1939struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1940{
1941 struct wpa_ssid *ssid;
1942
1943 ssid = config->ssid;
1944 while (ssid) {
1945 if (id == ssid->id)
1946 break;
1947 ssid = ssid->next;
1948 }
1949
1950 return ssid;
1951}
1952
1953
1954/**
1955 * wpa_config_add_network - Add a new network with empty configuration
1956 * @config: Configuration data from wpa_config_read()
1957 * Returns: The new network configuration or %NULL if operation failed
1958 */
1959struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1960{
1961 int id;
1962 struct wpa_ssid *ssid, *last = NULL;
1963
1964 id = -1;
1965 ssid = config->ssid;
1966 while (ssid) {
1967 if (ssid->id > id)
1968 id = ssid->id;
1969 last = ssid;
1970 ssid = ssid->next;
1971 }
1972 id++;
1973
1974 ssid = os_zalloc(sizeof(*ssid));
1975 if (ssid == NULL)
1976 return NULL;
1977 ssid->id = id;
1978 if (last)
1979 last->next = ssid;
1980 else
1981 config->ssid = ssid;
1982
1983 wpa_config_update_prio_list(config);
1984
1985 return ssid;
1986}
1987
1988
1989/**
1990 * wpa_config_remove_network - Remove a configured network based on id
1991 * @config: Configuration data from wpa_config_read()
1992 * @id: Unique network id to search for
1993 * Returns: 0 on success, or -1 if the network was not found
1994 */
1995int wpa_config_remove_network(struct wpa_config *config, int id)
1996{
1997 struct wpa_ssid *ssid, *prev = NULL;
1998
1999 ssid = config->ssid;
2000 while (ssid) {
2001 if (id == ssid->id)
2002 break;
2003 prev = ssid;
2004 ssid = ssid->next;
2005 }
2006
2007 if (ssid == NULL)
2008 return -1;
2009
2010 if (prev)
2011 prev->next = ssid->next;
2012 else
2013 config->ssid = ssid->next;
2014
2015 wpa_config_update_prio_list(config);
2016 wpa_config_free_ssid(ssid);
2017 return 0;
2018}
2019
2020
2021/**
2022 * wpa_config_set_network_defaults - Set network default values
2023 * @ssid: Pointer to network configuration data
2024 */
2025void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2026{
2027 ssid->proto = DEFAULT_PROTO;
2028 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2029 ssid->group_cipher = DEFAULT_GROUP;
2030 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002031 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032#ifdef IEEE8021X_EAPOL
2033 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2034 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2035 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2036#endif /* IEEE8021X_EAPOL */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002037#ifdef CONFIG_HT_OVERRIDES
2038 ssid->disable_ht = DEFAULT_DISABLE_HT;
2039 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2040 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2041 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2042 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2043#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002044 ssid->proactive_key_caching = -1;
2045#ifdef CONFIG_IEEE80211W
2046 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2047#endif /* CONFIG_IEEE80211W */
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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002142 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002270 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 ssid->psk, PMK_LEN);
2272 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2273 ssid->psk, PMK_LEN);
2274 ssid->psk_set = 1;
2275#endif /* CONFIG_NO_PBKDF2 */
2276}
2277
2278
Dmitry Shmidt04949592012-07-19 12:16:46 -07002279int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2280 const char *value, int line)
2281{
2282 char *val;
2283 size_t len;
2284
2285 if (os_strcmp(var, "priority") == 0) {
2286 cred->priority = atoi(value);
2287 return 0;
2288 }
2289
2290 if (os_strcmp(var, "pcsc") == 0) {
2291 cred->pcsc = atoi(value);
2292 return 0;
2293 }
2294
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002295 if (os_strcmp(var, "eap") == 0) {
2296 struct eap_method_type method;
2297 method.method = eap_peer_get_type(value, &method.vendor);
2298 if (method.vendor == EAP_VENDOR_IETF &&
2299 method.method == EAP_TYPE_NONE) {
2300 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2301 "for a credential", line, value);
2302 return -1;
2303 }
2304 os_free(cred->eap_method);
2305 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2306 if (cred->eap_method == NULL)
2307 return -1;
2308 os_memcpy(cred->eap_method, &method, sizeof(method));
2309 return 0;
2310 }
2311
2312 if (os_strcmp(var, "password") == 0 &&
2313 os_strncmp(value, "ext:", 4) == 0) {
2314 os_free(cred->password);
2315 cred->password = os_strdup(value);
2316 cred->ext_password = 1;
2317 return 0;
2318 }
2319
Dmitry Shmidt04949592012-07-19 12:16:46 -07002320 val = wpa_config_parse_string(value, &len);
2321 if (val == NULL) {
2322 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2323 "value '%s'.", line, var, value);
2324 return -1;
2325 }
2326
2327 if (os_strcmp(var, "realm") == 0) {
2328 os_free(cred->realm);
2329 cred->realm = val;
2330 return 0;
2331 }
2332
2333 if (os_strcmp(var, "username") == 0) {
2334 os_free(cred->username);
2335 cred->username = val;
2336 return 0;
2337 }
2338
2339 if (os_strcmp(var, "password") == 0) {
2340 os_free(cred->password);
2341 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002342 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002343 return 0;
2344 }
2345
2346 if (os_strcmp(var, "ca_cert") == 0) {
2347 os_free(cred->ca_cert);
2348 cred->ca_cert = val;
2349 return 0;
2350 }
2351
2352 if (os_strcmp(var, "client_cert") == 0) {
2353 os_free(cred->client_cert);
2354 cred->client_cert = val;
2355 return 0;
2356 }
2357
2358 if (os_strcmp(var, "private_key") == 0) {
2359 os_free(cred->private_key);
2360 cred->private_key = val;
2361 return 0;
2362 }
2363
2364 if (os_strcmp(var, "private_key_passwd") == 0) {
2365 os_free(cred->private_key_passwd);
2366 cred->private_key_passwd = val;
2367 return 0;
2368 }
2369
2370 if (os_strcmp(var, "imsi") == 0) {
2371 os_free(cred->imsi);
2372 cred->imsi = val;
2373 return 0;
2374 }
2375
2376 if (os_strcmp(var, "milenage") == 0) {
2377 os_free(cred->milenage);
2378 cred->milenage = val;
2379 return 0;
2380 }
2381
2382 if (os_strcmp(var, "domain") == 0) {
2383 os_free(cred->domain);
2384 cred->domain = val;
2385 return 0;
2386 }
2387
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002388 if (os_strcmp(var, "phase1") == 0) {
2389 os_free(cred->phase1);
2390 cred->phase1 = val;
2391 return 0;
2392 }
2393
2394 if (os_strcmp(var, "phase2") == 0) {
2395 os_free(cred->phase2);
2396 cred->phase2 = val;
2397 return 0;
2398 }
2399
2400 if (os_strcmp(var, "roaming_consortium") == 0) {
2401 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2402 wpa_printf(MSG_ERROR, "Line %d: invalid "
2403 "roaming_consortium length %d (3..15 "
2404 "expected)", line, (int) len);
2405 os_free(val);
2406 return -1;
2407 }
2408 os_memcpy(cred->roaming_consortium, val, len);
2409 cred->roaming_consortium_len = len;
2410 os_free(val);
2411 return 0;
2412 }
2413
Dmitry Shmidt04949592012-07-19 12:16:46 -07002414 if (line) {
2415 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2416 line, var);
2417 }
2418
2419 os_free(val);
2420
2421 return -1;
2422}
2423
2424
2425struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2426{
2427 struct wpa_cred *cred;
2428
2429 cred = config->cred;
2430 while (cred) {
2431 if (id == cred->id)
2432 break;
2433 cred = cred->next;
2434 }
2435
2436 return cred;
2437}
2438
2439
2440struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2441{
2442 int id;
2443 struct wpa_cred *cred, *last = NULL;
2444
2445 id = -1;
2446 cred = config->cred;
2447 while (cred) {
2448 if (cred->id > id)
2449 id = cred->id;
2450 last = cred;
2451 cred = cred->next;
2452 }
2453 id++;
2454
2455 cred = os_zalloc(sizeof(*cred));
2456 if (cred == NULL)
2457 return NULL;
2458 cred->id = id;
2459 if (last)
2460 last->next = cred;
2461 else
2462 config->cred = cred;
2463
2464 return cred;
2465}
2466
2467
2468int wpa_config_remove_cred(struct wpa_config *config, int id)
2469{
2470 struct wpa_cred *cred, *prev = NULL;
2471
2472 cred = config->cred;
2473 while (cred) {
2474 if (id == cred->id)
2475 break;
2476 prev = cred;
2477 cred = cred->next;
2478 }
2479
2480 if (cred == NULL)
2481 return -1;
2482
2483 if (prev)
2484 prev->next = cred->next;
2485 else
2486 config->cred = cred->next;
2487
2488 wpa_config_free_cred(cred);
2489 return 0;
2490}
2491
2492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493#ifndef CONFIG_NO_CONFIG_BLOBS
2494/**
2495 * wpa_config_get_blob - Get a named configuration blob
2496 * @config: Configuration data from wpa_config_read()
2497 * @name: Name of the blob
2498 * Returns: Pointer to blob data or %NULL if not found
2499 */
2500const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2501 const char *name)
2502{
2503 struct wpa_config_blob *blob = config->blobs;
2504
2505 while (blob) {
2506 if (os_strcmp(blob->name, name) == 0)
2507 return blob;
2508 blob = blob->next;
2509 }
2510 return NULL;
2511}
2512
2513
2514/**
2515 * wpa_config_set_blob - Set or add a named configuration blob
2516 * @config: Configuration data from wpa_config_read()
2517 * @blob: New value for the blob
2518 *
2519 * Adds a new configuration blob or replaces the current value of an existing
2520 * blob.
2521 */
2522void wpa_config_set_blob(struct wpa_config *config,
2523 struct wpa_config_blob *blob)
2524{
2525 wpa_config_remove_blob(config, blob->name);
2526 blob->next = config->blobs;
2527 config->blobs = blob;
2528}
2529
2530
2531/**
2532 * wpa_config_free_blob - Free blob data
2533 * @blob: Pointer to blob to be freed
2534 */
2535void wpa_config_free_blob(struct wpa_config_blob *blob)
2536{
2537 if (blob) {
2538 os_free(blob->name);
2539 os_free(blob->data);
2540 os_free(blob);
2541 }
2542}
2543
2544
2545/**
2546 * wpa_config_remove_blob - Remove a named configuration blob
2547 * @config: Configuration data from wpa_config_read()
2548 * @name: Name of the blob to remove
2549 * Returns: 0 if blob was removed or -1 if blob was not found
2550 */
2551int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2552{
2553 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2554
2555 while (pos) {
2556 if (os_strcmp(pos->name, name) == 0) {
2557 if (prev)
2558 prev->next = pos->next;
2559 else
2560 config->blobs = pos->next;
2561 wpa_config_free_blob(pos);
2562 return 0;
2563 }
2564 prev = pos;
2565 pos = pos->next;
2566 }
2567
2568 return -1;
2569}
2570#endif /* CONFIG_NO_CONFIG_BLOBS */
2571
2572
2573/**
2574 * wpa_config_alloc_empty - Allocate an empty configuration
2575 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2576 * socket
2577 * @driver_param: Driver parameters
2578 * Returns: Pointer to allocated configuration data or %NULL on failure
2579 */
2580struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2581 const char *driver_param)
2582{
2583 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002584 const int aCWmin = 4, aCWmax = 10;
2585 const struct hostapd_wmm_ac_params ac_bk =
2586 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2587 const struct hostapd_wmm_ac_params ac_be =
2588 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2589 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2590 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2591 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2592 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593
2594 config = os_zalloc(sizeof(*config));
2595 if (config == NULL)
2596 return NULL;
2597 config->eapol_version = DEFAULT_EAPOL_VERSION;
2598 config->ap_scan = DEFAULT_AP_SCAN;
2599 config->fast_reauth = DEFAULT_FAST_REAUTH;
2600 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2601 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002602 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2604 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2605 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2606 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002607 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002608 config->wmm_ac_params[0] = ac_be;
2609 config->wmm_ac_params[1] = ac_bk;
2610 config->wmm_ac_params[2] = ac_vi;
2611 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612
2613 if (ctrl_interface)
2614 config->ctrl_interface = os_strdup(ctrl_interface);
2615 if (driver_param)
2616 config->driver_param = os_strdup(driver_param);
2617
2618 return config;
2619}
2620
2621
2622#ifndef CONFIG_NO_STDOUT_DEBUG
2623/**
2624 * wpa_config_debug_dump_networks - Debug dump of configured networks
2625 * @config: Configuration data from wpa_config_read()
2626 */
2627void wpa_config_debug_dump_networks(struct wpa_config *config)
2628{
2629 int prio;
2630 struct wpa_ssid *ssid;
2631
2632 for (prio = 0; prio < config->num_prio; prio++) {
2633 ssid = config->pssid[prio];
2634 wpa_printf(MSG_DEBUG, "Priority group %d",
2635 ssid->priority);
2636 while (ssid) {
2637 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2638 ssid->id,
2639 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2640 ssid = ssid->pnext;
2641 }
2642 }
2643}
2644#endif /* CONFIG_NO_STDOUT_DEBUG */
2645
2646
2647struct global_parse_data {
2648 char *name;
2649 int (*parser)(const struct global_parse_data *data,
2650 struct wpa_config *config, int line, const char *value);
2651 void *param1, *param2, *param3;
2652 unsigned int changed_flag;
2653};
2654
2655
2656static int wpa_global_config_parse_int(const struct global_parse_data *data,
2657 struct wpa_config *config, int line,
2658 const char *pos)
2659{
2660 int *dst;
2661 dst = (int *) (((u8 *) config) + (long) data->param1);
2662 *dst = atoi(pos);
2663 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2664
2665 if (data->param2 && *dst < (long) data->param2) {
2666 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2667 "min_value=%ld)", line, data->name, *dst,
2668 (long) data->param2);
2669 *dst = (long) data->param2;
2670 return -1;
2671 }
2672
2673 if (data->param3 && *dst > (long) data->param3) {
2674 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2675 "max_value=%ld)", line, data->name, *dst,
2676 (long) data->param3);
2677 *dst = (long) data->param3;
2678 return -1;
2679 }
2680
2681 return 0;
2682}
2683
2684
2685static int wpa_global_config_parse_str(const struct global_parse_data *data,
2686 struct wpa_config *config, int line,
2687 const char *pos)
2688{
2689 size_t len;
2690 char **dst, *tmp;
2691
2692 len = os_strlen(pos);
2693 if (data->param2 && len < (size_t) data->param2) {
2694 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2695 "min_len=%ld)", line, data->name,
2696 (unsigned long) len, (long) data->param2);
2697 return -1;
2698 }
2699
2700 if (data->param3 && len > (size_t) data->param3) {
2701 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2702 "max_len=%ld)", line, data->name,
2703 (unsigned long) len, (long) data->param3);
2704 return -1;
2705 }
2706
2707 tmp = os_strdup(pos);
2708 if (tmp == NULL)
2709 return -1;
2710
2711 dst = (char **) (((u8 *) config) + (long) data->param1);
2712 os_free(*dst);
2713 *dst = tmp;
2714 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2715
2716 return 0;
2717}
2718
2719
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2721 struct wpa_config *config, int line,
2722 const char *pos)
2723{
2724 size_t len;
2725 struct wpabuf **dst, *tmp;
2726
2727 len = os_strlen(pos);
2728 if (len & 0x01)
2729 return -1;
2730
2731 tmp = wpabuf_alloc(len / 2);
2732 if (tmp == NULL)
2733 return -1;
2734
2735 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2736 wpabuf_free(tmp);
2737 return -1;
2738 }
2739
2740 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2741 wpabuf_free(*dst);
2742 *dst = tmp;
2743 wpa_printf(MSG_DEBUG, "%s", data->name);
2744
2745 return 0;
2746}
2747
2748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749static int wpa_config_process_country(const struct global_parse_data *data,
2750 struct wpa_config *config, int line,
2751 const char *pos)
2752{
2753 if (!pos[0] || !pos[1]) {
2754 wpa_printf(MSG_DEBUG, "Invalid country set");
2755 return -1;
2756 }
2757 config->country[0] = pos[0];
2758 config->country[1] = pos[1];
2759 wpa_printf(MSG_DEBUG, "country='%c%c'",
2760 config->country[0], config->country[1]);
2761 return 0;
2762}
2763
2764
2765static int wpa_config_process_load_dynamic_eap(
2766 const struct global_parse_data *data, struct wpa_config *config,
2767 int line, const char *so)
2768{
2769 int ret;
2770 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2771 ret = eap_peer_method_load(so);
2772 if (ret == -2) {
2773 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2774 "reloading.");
2775 } else if (ret) {
2776 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2777 "method '%s'.", line, so);
2778 return -1;
2779 }
2780
2781 return 0;
2782}
2783
2784
2785#ifdef CONFIG_WPS
2786
2787static int wpa_config_process_uuid(const struct global_parse_data *data,
2788 struct wpa_config *config, int line,
2789 const char *pos)
2790{
2791 char buf[40];
2792 if (uuid_str2bin(pos, config->uuid)) {
2793 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2794 return -1;
2795 }
2796 uuid_bin2str(config->uuid, buf, sizeof(buf));
2797 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2798 return 0;
2799}
2800
2801
2802static int wpa_config_process_device_type(
2803 const struct global_parse_data *data,
2804 struct wpa_config *config, int line, const char *pos)
2805{
2806 return wps_dev_type_str2bin(pos, config->device_type);
2807}
2808
2809
2810static int wpa_config_process_os_version(const struct global_parse_data *data,
2811 struct wpa_config *config, int line,
2812 const char *pos)
2813{
2814 if (hexstr2bin(pos, config->os_version, 4)) {
2815 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2816 return -1;
2817 }
2818 wpa_printf(MSG_DEBUG, "os_version=%08x",
2819 WPA_GET_BE32(config->os_version));
2820 return 0;
2821}
2822
Dmitry Shmidt04949592012-07-19 12:16:46 -07002823
2824static int wpa_config_process_wps_vendor_ext_m1(
2825 const struct global_parse_data *data,
2826 struct wpa_config *config, int line, const char *pos)
2827{
2828 struct wpabuf *tmp;
2829 int len = os_strlen(pos) / 2;
2830 u8 *p;
2831
2832 if (!len) {
2833 wpa_printf(MSG_ERROR, "Line %d: "
2834 "invalid wps_vendor_ext_m1", line);
2835 return -1;
2836 }
2837
2838 tmp = wpabuf_alloc(len);
2839 if (tmp) {
2840 p = wpabuf_put(tmp, len);
2841
2842 if (hexstr2bin(pos, p, len)) {
2843 wpa_printf(MSG_ERROR, "Line %d: "
2844 "invalid wps_vendor_ext_m1", line);
2845 wpabuf_free(tmp);
2846 return -1;
2847 }
2848
2849 wpabuf_free(config->wps_vendor_ext_m1);
2850 config->wps_vendor_ext_m1 = tmp;
2851 } else {
2852 wpa_printf(MSG_ERROR, "Can not allocate "
2853 "memory for wps_vendor_ext_m1");
2854 return -1;
2855 }
2856
2857 return 0;
2858}
2859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860#endif /* CONFIG_WPS */
2861
2862#ifdef CONFIG_P2P
2863static int wpa_config_process_sec_device_type(
2864 const struct global_parse_data *data,
2865 struct wpa_config *config, int line, const char *pos)
2866{
2867 int idx;
2868
2869 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2870 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2871 "items", line);
2872 return -1;
2873 }
2874
2875 idx = config->num_sec_device_types;
2876
2877 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2878 return -1;
2879
2880 config->num_sec_device_types++;
2881 return 0;
2882}
Dmitry Shmidt04949592012-07-19 12:16:46 -07002883
2884
2885static int wpa_config_process_p2p_pref_chan(
2886 const struct global_parse_data *data,
2887 struct wpa_config *config, int line, const char *pos)
2888{
2889 struct p2p_channel *pref = NULL, *n;
2890 unsigned int num = 0;
2891 const char *pos2;
2892 u8 op_class, chan;
2893
2894 /* format: class:chan,class:chan,... */
2895
2896 while (*pos) {
2897 op_class = atoi(pos);
2898 pos2 = os_strchr(pos, ':');
2899 if (pos2 == NULL)
2900 goto fail;
2901 pos2++;
2902 chan = atoi(pos2);
2903
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002904 n = os_realloc_array(pref, num + 1,
2905 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002906 if (n == NULL)
2907 goto fail;
2908 pref = n;
2909 pref[num].op_class = op_class;
2910 pref[num].chan = chan;
2911 num++;
2912
2913 pos = os_strchr(pos2, ',');
2914 if (pos == NULL)
2915 break;
2916 pos++;
2917 }
2918
2919 os_free(config->p2p_pref_chan);
2920 config->p2p_pref_chan = pref;
2921 config->num_p2p_pref_chan = num;
2922 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2923 (u8 *) config->p2p_pref_chan,
2924 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2925
2926 return 0;
2927
2928fail:
2929 os_free(pref);
2930 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2931 return -1;
2932}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933#endif /* CONFIG_P2P */
2934
2935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002936static int wpa_config_process_hessid(
2937 const struct global_parse_data *data,
2938 struct wpa_config *config, int line, const char *pos)
2939{
2940 if (hwaddr_aton2(pos, config->hessid) < 0) {
2941 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2942 line, pos);
2943 return -1;
2944 }
2945
2946 return 0;
2947}
2948
2949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950#ifdef OFFSET
2951#undef OFFSET
2952#endif /* OFFSET */
2953/* OFFSET: Get offset of a variable within the wpa_config structure */
2954#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2955
2956#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2957#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2958#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2959#define INT(f) _INT(f), NULL, NULL
2960#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2961#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2962#define STR(f) _STR(f), NULL, NULL
2963#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07002964#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965
2966static const struct global_parse_data global_fields[] = {
2967#ifdef CONFIG_CTRL_IFACE
2968 { STR(ctrl_interface), 0 },
2969 { STR(ctrl_interface_group), 0 } /* deprecated */,
2970#endif /* CONFIG_CTRL_IFACE */
2971 { INT_RANGE(eapol_version, 1, 2), 0 },
2972 { INT(ap_scan), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 { INT(fast_reauth), 0 },
2975 { STR(opensc_engine_path), 0 },
2976 { STR(pkcs11_engine_path), 0 },
2977 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002978 { STR(pcsc_reader), 0 },
2979 { STR(pcsc_pin), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 { STR(driver_param), 0 },
2981 { INT(dot11RSNAConfigPMKLifetime), 0 },
2982 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2983 { INT(dot11RSNAConfigSATimeout), 0 },
2984#ifndef CONFIG_NO_CONFIG_WRITE
2985 { INT(update_config), 0 },
2986#endif /* CONFIG_NO_CONFIG_WRITE */
2987 { FUNC_NO_VAR(load_dynamic_eap), 0 },
2988#ifdef CONFIG_WPS
2989 { FUNC(uuid), CFG_CHANGED_UUID },
2990 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2991 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2992 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2993 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2994 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2995 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2996 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2997 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2998 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002999 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000#endif /* CONFIG_WPS */
3001#ifdef CONFIG_P2P
3002 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3003 { INT(p2p_listen_reg_class), 0 },
3004 { INT(p2p_listen_channel), 0 },
3005 { INT(p2p_oper_reg_class), 0 },
3006 { INT(p2p_oper_channel), 0 },
3007 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3008 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3009 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3010 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3011 { INT(p2p_group_idle), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003012 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003013 { INT(p2p_go_ht40), 0 },
3014 { INT(p2p_disabled), 0 },
3015 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016#endif /* CONFIG_P2P */
3017 { FUNC(country), CFG_CHANGED_COUNTRY },
3018 { INT(bss_max_count), 0 },
3019 { INT(bss_expiration_age), 0 },
3020 { INT(bss_expiration_scan_count), 0 },
3021 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003022 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003024 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003025#ifdef CONFIG_HS20
3026 { INT_RANGE(hs20, 0, 1), 0 },
3027#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028 { INT_RANGE(interworking, 0, 1), 0 },
3029 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003030 { INT_RANGE(access_network_type, 0, 15), 0 },
3031 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3032 { STR(autoscan), 0 },
3033 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff), 0 },
3034 { BIN(wps_nfc_dh_pubkey), 0 },
3035 { BIN(wps_nfc_dh_privkey), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003036 { BIN(wps_nfc_dev_pw), 0 },
3037 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3038 { INT(p2p_go_max_inactivity), 0 },
3039 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003040 { INT(okc), 0 },
3041 { INT(pmf), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042};
3043
3044#undef FUNC
3045#undef _INT
3046#undef INT
3047#undef INT_RANGE
3048#undef _STR
3049#undef STR
3050#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07003051#undef BIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052#define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
3053
3054
3055int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3056{
3057 size_t i;
3058 int ret = 0;
3059
3060 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3061 const struct global_parse_data *field = &global_fields[i];
3062 size_t flen = os_strlen(field->name);
3063 if (os_strncmp(pos, field->name, flen) != 0 ||
3064 pos[flen] != '=')
3065 continue;
3066
3067 if (field->parser(field, config, line, pos + flen + 1)) {
3068 wpa_printf(MSG_ERROR, "Line %d: failed to "
3069 "parse '%s'.", line, pos);
3070 ret = -1;
3071 }
3072 config->changed_parameters |= field->changed_flag;
3073 break;
3074 }
3075 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003076#ifdef CONFIG_AP
3077 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3078 char *tmp = os_strchr(pos, '=');
3079 if (tmp == NULL) {
3080 if (line < 0)
3081 return -1;
3082 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3083 "'%s'", line, pos);
3084 return -1;
3085 }
3086 *tmp++ = '\0';
3087 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3088 tmp)) {
3089 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3090 "AC item", line);
3091 return -1;
3092 }
3093 }
3094#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 if (line < 0)
3096 return -1;
3097 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3098 line, pos);
3099 ret = -1;
3100 }
3101
3102 return ret;
3103}