blob: 884acf6c89012ce0032d82e47c16e5ba1e7056be [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{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700181 int val, *dst;
182 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183
184 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700185 val = strtol(value, &end, 0);
186 if (*end) {
187 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
188 line, value);
189 return -1;
190 }
191 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
193
194 if (data->param3 && *dst < (long) data->param3) {
195 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
196 "min_value=%ld)", line, data->name, *dst,
197 (long) data->param3);
198 *dst = (long) data->param3;
199 return -1;
200 }
201
202 if (data->param4 && *dst > (long) data->param4) {
203 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
204 "max_value=%ld)", line, data->name, *dst,
205 (long) data->param4);
206 *dst = (long) data->param4;
207 return -1;
208 }
209
210 return 0;
211}
212
213
214#ifndef NO_CONFIG_WRITE
215static char * wpa_config_write_int(const struct parse_data *data,
216 struct wpa_ssid *ssid)
217{
218 int *src, res;
219 char *value;
220
221 src = (int *) (((u8 *) ssid) + (long) data->param1);
222
223 value = os_malloc(20);
224 if (value == NULL)
225 return NULL;
226 res = os_snprintf(value, 20, "%d", *src);
227 if (res < 0 || res >= 20) {
228 os_free(value);
229 return NULL;
230 }
231 value[20 - 1] = '\0';
232 return value;
233}
234#endif /* NO_CONFIG_WRITE */
235
236
237static int wpa_config_parse_bssid(const struct parse_data *data,
238 struct wpa_ssid *ssid, int line,
239 const char *value)
240{
241 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
242 os_strcmp(value, "any") == 0) {
243 ssid->bssid_set = 0;
244 wpa_printf(MSG_MSGDUMP, "BSSID any");
245 return 0;
246 }
247 if (hwaddr_aton(value, ssid->bssid)) {
248 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
249 line, value);
250 return -1;
251 }
252 ssid->bssid_set = 1;
253 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
254 return 0;
255}
256
257
258#ifndef NO_CONFIG_WRITE
259static char * wpa_config_write_bssid(const struct parse_data *data,
260 struct wpa_ssid *ssid)
261{
262 char *value;
263 int res;
264
265 if (!ssid->bssid_set)
266 return NULL;
267
268 value = os_malloc(20);
269 if (value == NULL)
270 return NULL;
271 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
272 if (res < 0 || res >= 20) {
273 os_free(value);
274 return NULL;
275 }
276 value[20 - 1] = '\0';
277 return value;
278}
279#endif /* NO_CONFIG_WRITE */
280
281
282static int wpa_config_parse_psk(const struct parse_data *data,
283 struct wpa_ssid *ssid, int line,
284 const char *value)
285{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700286#ifdef CONFIG_EXT_PASSWORD
287 if (os_strncmp(value, "ext:", 4) == 0) {
288 os_free(ssid->passphrase);
289 ssid->passphrase = NULL;
290 ssid->psk_set = 0;
291 os_free(ssid->ext_psk);
292 ssid->ext_psk = os_strdup(value + 4);
293 if (ssid->ext_psk == NULL)
294 return -1;
295 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
296 ssid->ext_psk);
297 return 0;
298 }
299#endif /* CONFIG_EXT_PASSWORD */
300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 if (*value == '"') {
302#ifndef CONFIG_NO_PBKDF2
303 const char *pos;
304 size_t len;
305
306 value++;
307 pos = os_strrchr(value, '"');
308 if (pos)
309 len = pos - value;
310 else
311 len = os_strlen(value);
312 if (len < 8 || len > 63) {
313 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
314 "length %lu (expected: 8..63) '%s'.",
315 line, (unsigned long) len, value);
316 return -1;
317 }
318 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
319 (u8 *) value, len);
320 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
321 os_memcmp(ssid->passphrase, value, len) == 0)
322 return 0;
323 ssid->psk_set = 0;
324 os_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700325 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 if (ssid->passphrase == NULL)
327 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 return 0;
329#else /* CONFIG_NO_PBKDF2 */
330 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
331 "supported.", line);
332 return -1;
333#endif /* CONFIG_NO_PBKDF2 */
334 }
335
336 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
337 value[PMK_LEN * 2] != '\0') {
338 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
339 line, value);
340 return -1;
341 }
342
343 os_free(ssid->passphrase);
344 ssid->passphrase = NULL;
345
346 ssid->psk_set = 1;
347 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
348 return 0;
349}
350
351
352#ifndef NO_CONFIG_WRITE
353static char * wpa_config_write_psk(const struct parse_data *data,
354 struct wpa_ssid *ssid)
355{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700356#ifdef CONFIG_EXT_PASSWORD
357 if (ssid->ext_psk) {
358 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
359 char *buf = os_malloc(len);
360 if (buf == NULL)
361 return NULL;
362 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
363 return buf;
364 }
365#endif /* CONFIG_EXT_PASSWORD */
366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 if (ssid->passphrase)
368 return wpa_config_write_string_ascii(
369 (const u8 *) ssid->passphrase,
370 os_strlen(ssid->passphrase));
371
372 if (ssid->psk_set)
373 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
374
375 return NULL;
376}
377#endif /* NO_CONFIG_WRITE */
378
379
380static int wpa_config_parse_proto(const struct parse_data *data,
381 struct wpa_ssid *ssid, int line,
382 const char *value)
383{
384 int val = 0, last, errors = 0;
385 char *start, *end, *buf;
386
387 buf = os_strdup(value);
388 if (buf == NULL)
389 return -1;
390 start = buf;
391
392 while (*start != '\0') {
393 while (*start == ' ' || *start == '\t')
394 start++;
395 if (*start == '\0')
396 break;
397 end = start;
398 while (*end != ' ' && *end != '\t' && *end != '\0')
399 end++;
400 last = *end == '\0';
401 *end = '\0';
402 if (os_strcmp(start, "WPA") == 0)
403 val |= WPA_PROTO_WPA;
404 else if (os_strcmp(start, "RSN") == 0 ||
405 os_strcmp(start, "WPA2") == 0)
406 val |= WPA_PROTO_RSN;
407 else {
408 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
409 line, start);
410 errors++;
411 }
412
413 if (last)
414 break;
415 start = end + 1;
416 }
417 os_free(buf);
418
419 if (val == 0) {
420 wpa_printf(MSG_ERROR,
421 "Line %d: no proto values configured.", line);
422 errors++;
423 }
424
425 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
426 ssid->proto = val;
427 return errors ? -1 : 0;
428}
429
430
431#ifndef NO_CONFIG_WRITE
432static char * wpa_config_write_proto(const struct parse_data *data,
433 struct wpa_ssid *ssid)
434{
435 int first = 1, ret;
436 char *buf, *pos, *end;
437
438 pos = buf = os_zalloc(10);
439 if (buf == NULL)
440 return NULL;
441 end = buf + 10;
442
443 if (ssid->proto & WPA_PROTO_WPA) {
444 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
445 if (ret < 0 || ret >= end - pos)
446 return buf;
447 pos += ret;
448 first = 0;
449 }
450
451 if (ssid->proto & WPA_PROTO_RSN) {
452 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
453 if (ret < 0 || ret >= end - pos)
454 return buf;
455 pos += ret;
456 first = 0;
457 }
458
459 return buf;
460}
461#endif /* NO_CONFIG_WRITE */
462
463
464static int wpa_config_parse_key_mgmt(const struct parse_data *data,
465 struct wpa_ssid *ssid, int line,
466 const char *value)
467{
468 int val = 0, last, errors = 0;
469 char *start, *end, *buf;
470
471 buf = os_strdup(value);
472 if (buf == NULL)
473 return -1;
474 start = buf;
475
476 while (*start != '\0') {
477 while (*start == ' ' || *start == '\t')
478 start++;
479 if (*start == '\0')
480 break;
481 end = start;
482 while (*end != ' ' && *end != '\t' && *end != '\0')
483 end++;
484 last = *end == '\0';
485 *end = '\0';
486 if (os_strcmp(start, "WPA-PSK") == 0)
487 val |= WPA_KEY_MGMT_PSK;
488 else if (os_strcmp(start, "WPA-EAP") == 0)
489 val |= WPA_KEY_MGMT_IEEE8021X;
490 else if (os_strcmp(start, "IEEE8021X") == 0)
491 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
492 else if (os_strcmp(start, "NONE") == 0)
493 val |= WPA_KEY_MGMT_NONE;
494 else if (os_strcmp(start, "WPA-NONE") == 0)
495 val |= WPA_KEY_MGMT_WPA_NONE;
496#ifdef CONFIG_IEEE80211R
497 else if (os_strcmp(start, "FT-PSK") == 0)
498 val |= WPA_KEY_MGMT_FT_PSK;
499 else if (os_strcmp(start, "FT-EAP") == 0)
500 val |= WPA_KEY_MGMT_FT_IEEE8021X;
501#endif /* CONFIG_IEEE80211R */
502#ifdef CONFIG_IEEE80211W
503 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
504 val |= WPA_KEY_MGMT_PSK_SHA256;
505 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
506 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
507#endif /* CONFIG_IEEE80211W */
508#ifdef CONFIG_WPS
509 else if (os_strcmp(start, "WPS") == 0)
510 val |= WPA_KEY_MGMT_WPS;
511#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800512#ifdef CONFIG_SAE
513 else if (os_strcmp(start, "SAE") == 0)
514 val |= WPA_KEY_MGMT_SAE;
515 else if (os_strcmp(start, "FT-SAE") == 0)
516 val |= WPA_KEY_MGMT_FT_SAE;
517#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 else {
519 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
520 line, start);
521 errors++;
522 }
523
524 if (last)
525 break;
526 start = end + 1;
527 }
528 os_free(buf);
529
530 if (val == 0) {
531 wpa_printf(MSG_ERROR,
532 "Line %d: no key_mgmt values configured.", line);
533 errors++;
534 }
535
536 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
537 ssid->key_mgmt = val;
538 return errors ? -1 : 0;
539}
540
541
542#ifndef NO_CONFIG_WRITE
543static char * wpa_config_write_key_mgmt(const struct parse_data *data,
544 struct wpa_ssid *ssid)
545{
546 char *buf, *pos, *end;
547 int ret;
548
Dmitry Shmidt96571392013-10-14 12:54:46 -0700549 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 if (buf == NULL)
551 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700552 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553
554 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
555 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
556 pos == buf ? "" : " ");
557 if (ret < 0 || ret >= end - pos) {
558 end[-1] = '\0';
559 return buf;
560 }
561 pos += ret;
562 }
563
564 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
565 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
566 pos == buf ? "" : " ");
567 if (ret < 0 || ret >= end - pos) {
568 end[-1] = '\0';
569 return buf;
570 }
571 pos += ret;
572 }
573
574 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
575 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
576 pos == buf ? "" : " ");
577 if (ret < 0 || ret >= end - pos) {
578 end[-1] = '\0';
579 return buf;
580 }
581 pos += ret;
582 }
583
584 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
585 ret = os_snprintf(pos, end - pos, "%sNONE",
586 pos == buf ? "" : " ");
587 if (ret < 0 || ret >= end - pos) {
588 end[-1] = '\0';
589 return buf;
590 }
591 pos += ret;
592 }
593
594 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
595 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
596 pos == buf ? "" : " ");
597 if (ret < 0 || ret >= end - pos) {
598 end[-1] = '\0';
599 return buf;
600 }
601 pos += ret;
602 }
603
604#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700605 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
606 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
607 pos == buf ? "" : " ");
608 if (ret < 0 || ret >= end - pos) {
609 end[-1] = '\0';
610 return buf;
611 }
612 pos += ret;
613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614
Dmitry Shmidt96571392013-10-14 12:54:46 -0700615 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
616 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
617 pos == buf ? "" : " ");
618 if (ret < 0 || ret >= end - pos) {
619 end[-1] = '\0';
620 return buf;
621 }
622 pos += ret;
623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624#endif /* CONFIG_IEEE80211R */
625
626#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700627 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
628 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
629 pos == buf ? "" : " ");
630 if (ret < 0 || ret >= end - pos) {
631 end[-1] = '\0';
632 return buf;
633 }
634 pos += ret;
635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636
Dmitry Shmidt96571392013-10-14 12:54:46 -0700637 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
638 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
639 pos == buf ? "" : " ");
640 if (ret < 0 || ret >= end - pos) {
641 end[-1] = '\0';
642 return buf;
643 }
644 pos += ret;
645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646#endif /* CONFIG_IEEE80211W */
647
648#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700649 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
650 ret = os_snprintf(pos, end - pos, "%sWPS",
651 pos == buf ? "" : " ");
652 if (ret < 0 || ret >= end - pos) {
653 end[-1] = '\0';
654 return buf;
655 }
656 pos += ret;
657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658#endif /* CONFIG_WPS */
659
660 return buf;
661}
662#endif /* NO_CONFIG_WRITE */
663
664
665static int wpa_config_parse_cipher(int line, const char *value)
666{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800667 int val = wpa_parse_cipher(value);
668 if (val < 0) {
669 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
670 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 if (val == 0) {
674 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
675 line);
676 return -1;
677 }
678 return val;
679}
680
681
682#ifndef NO_CONFIG_WRITE
683static char * wpa_config_write_cipher(int cipher)
684{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800685 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 if (buf == NULL)
687 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800689 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
690 os_free(buf);
691 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 }
693
694 return buf;
695}
696#endif /* NO_CONFIG_WRITE */
697
698
699static int wpa_config_parse_pairwise(const struct parse_data *data,
700 struct wpa_ssid *ssid, int line,
701 const char *value)
702{
703 int val;
704 val = wpa_config_parse_cipher(line, value);
705 if (val == -1)
706 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800707 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
709 "(0x%x).", line, val);
710 return -1;
711 }
712
713 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
714 ssid->pairwise_cipher = val;
715 return 0;
716}
717
718
719#ifndef NO_CONFIG_WRITE
720static char * wpa_config_write_pairwise(const struct parse_data *data,
721 struct wpa_ssid *ssid)
722{
723 return wpa_config_write_cipher(ssid->pairwise_cipher);
724}
725#endif /* NO_CONFIG_WRITE */
726
727
728static int wpa_config_parse_group(const struct parse_data *data,
729 struct wpa_ssid *ssid, int line,
730 const char *value)
731{
732 int val;
733 val = wpa_config_parse_cipher(line, value);
734 if (val == -1)
735 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800736 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
738 "(0x%x).", line, val);
739 return -1;
740 }
741
742 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
743 ssid->group_cipher = val;
744 return 0;
745}
746
747
748#ifndef NO_CONFIG_WRITE
749static char * wpa_config_write_group(const struct parse_data *data,
750 struct wpa_ssid *ssid)
751{
752 return wpa_config_write_cipher(ssid->group_cipher);
753}
754#endif /* NO_CONFIG_WRITE */
755
756
757static int wpa_config_parse_auth_alg(const struct parse_data *data,
758 struct wpa_ssid *ssid, int line,
759 const char *value)
760{
761 int val = 0, last, errors = 0;
762 char *start, *end, *buf;
763
764 buf = os_strdup(value);
765 if (buf == NULL)
766 return -1;
767 start = buf;
768
769 while (*start != '\0') {
770 while (*start == ' ' || *start == '\t')
771 start++;
772 if (*start == '\0')
773 break;
774 end = start;
775 while (*end != ' ' && *end != '\t' && *end != '\0')
776 end++;
777 last = *end == '\0';
778 *end = '\0';
779 if (os_strcmp(start, "OPEN") == 0)
780 val |= WPA_AUTH_ALG_OPEN;
781 else if (os_strcmp(start, "SHARED") == 0)
782 val |= WPA_AUTH_ALG_SHARED;
783 else if (os_strcmp(start, "LEAP") == 0)
784 val |= WPA_AUTH_ALG_LEAP;
785 else {
786 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
787 line, start);
788 errors++;
789 }
790
791 if (last)
792 break;
793 start = end + 1;
794 }
795 os_free(buf);
796
797 if (val == 0) {
798 wpa_printf(MSG_ERROR,
799 "Line %d: no auth_alg values configured.", line);
800 errors++;
801 }
802
803 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
804 ssid->auth_alg = val;
805 return errors ? -1 : 0;
806}
807
808
809#ifndef NO_CONFIG_WRITE
810static char * wpa_config_write_auth_alg(const struct parse_data *data,
811 struct wpa_ssid *ssid)
812{
813 char *buf, *pos, *end;
814 int ret;
815
816 pos = buf = os_zalloc(30);
817 if (buf == NULL)
818 return NULL;
819 end = buf + 30;
820
821 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
822 ret = os_snprintf(pos, end - pos, "%sOPEN",
823 pos == buf ? "" : " ");
824 if (ret < 0 || ret >= end - pos) {
825 end[-1] = '\0';
826 return buf;
827 }
828 pos += ret;
829 }
830
831 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
832 ret = os_snprintf(pos, end - pos, "%sSHARED",
833 pos == buf ? "" : " ");
834 if (ret < 0 || ret >= end - pos) {
835 end[-1] = '\0';
836 return buf;
837 }
838 pos += ret;
839 }
840
841 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
842 ret = os_snprintf(pos, end - pos, "%sLEAP",
843 pos == buf ? "" : " ");
844 if (ret < 0 || ret >= end - pos) {
845 end[-1] = '\0';
846 return buf;
847 }
848 pos += ret;
849 }
850
851 return buf;
852}
853#endif /* NO_CONFIG_WRITE */
854
855
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800856static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857{
858 int *freqs;
859 size_t used, len;
860 const char *pos;
861
862 used = 0;
863 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700864 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 if (freqs == NULL)
866 return NULL;
867
868 pos = value;
869 while (pos) {
870 while (*pos == ' ')
871 pos++;
872 if (used == len) {
873 int *n;
874 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700875 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 if (n == NULL) {
877 os_free(freqs);
878 return NULL;
879 }
880 for (i = len; i <= len * 2; i++)
881 n[i] = 0;
882 freqs = n;
883 len *= 2;
884 }
885
886 freqs[used] = atoi(pos);
887 if (freqs[used] == 0)
888 break;
889 used++;
890 pos = os_strchr(pos + 1, ' ');
891 }
892
893 return freqs;
894}
895
896
897static int wpa_config_parse_scan_freq(const struct parse_data *data,
898 struct wpa_ssid *ssid, int line,
899 const char *value)
900{
901 int *freqs;
902
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800903 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 if (freqs == NULL)
905 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700906 if (freqs[0] == 0) {
907 os_free(freqs);
908 freqs = NULL;
909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 os_free(ssid->scan_freq);
911 ssid->scan_freq = freqs;
912
913 return 0;
914}
915
916
917static int wpa_config_parse_freq_list(const struct parse_data *data,
918 struct wpa_ssid *ssid, int line,
919 const char *value)
920{
921 int *freqs;
922
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 if (freqs == NULL)
925 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700926 if (freqs[0] == 0) {
927 os_free(freqs);
928 freqs = NULL;
929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 os_free(ssid->freq_list);
931 ssid->freq_list = freqs;
932
933 return 0;
934}
935
936
937#ifndef NO_CONFIG_WRITE
938static char * wpa_config_write_freqs(const struct parse_data *data,
939 const int *freqs)
940{
941 char *buf, *pos, *end;
942 int i, ret;
943 size_t count;
944
945 if (freqs == NULL)
946 return NULL;
947
948 count = 0;
949 for (i = 0; freqs[i]; i++)
950 count++;
951
952 pos = buf = os_zalloc(10 * count + 1);
953 if (buf == NULL)
954 return NULL;
955 end = buf + 10 * count + 1;
956
957 for (i = 0; freqs[i]; i++) {
958 ret = os_snprintf(pos, end - pos, "%s%u",
959 i == 0 ? "" : " ", freqs[i]);
960 if (ret < 0 || ret >= end - pos) {
961 end[-1] = '\0';
962 return buf;
963 }
964 pos += ret;
965 }
966
967 return buf;
968}
969
970
971static char * wpa_config_write_scan_freq(const struct parse_data *data,
972 struct wpa_ssid *ssid)
973{
974 return wpa_config_write_freqs(data, ssid->scan_freq);
975}
976
977
978static char * wpa_config_write_freq_list(const struct parse_data *data,
979 struct wpa_ssid *ssid)
980{
981 return wpa_config_write_freqs(data, ssid->freq_list);
982}
983#endif /* NO_CONFIG_WRITE */
984
985
986#ifdef IEEE8021X_EAPOL
987static int wpa_config_parse_eap(const struct parse_data *data,
988 struct wpa_ssid *ssid, int line,
989 const char *value)
990{
991 int last, errors = 0;
992 char *start, *end, *buf;
993 struct eap_method_type *methods = NULL, *tmp;
994 size_t num_methods = 0;
995
996 buf = os_strdup(value);
997 if (buf == NULL)
998 return -1;
999 start = buf;
1000
1001 while (*start != '\0') {
1002 while (*start == ' ' || *start == '\t')
1003 start++;
1004 if (*start == '\0')
1005 break;
1006 end = start;
1007 while (*end != ' ' && *end != '\t' && *end != '\0')
1008 end++;
1009 last = *end == '\0';
1010 *end = '\0';
1011 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 methods = os_realloc_array(methods, num_methods + 1,
1013 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 if (methods == NULL) {
1015 os_free(tmp);
1016 os_free(buf);
1017 return -1;
1018 }
1019 methods[num_methods].method = eap_peer_get_type(
1020 start, &methods[num_methods].vendor);
1021 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1022 methods[num_methods].method == EAP_TYPE_NONE) {
1023 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1024 "'%s'", line, start);
1025 wpa_printf(MSG_ERROR, "You may need to add support for"
1026 " this EAP method during wpa_supplicant\n"
1027 "build time configuration.\n"
1028 "See README for more information.");
1029 errors++;
1030 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1031 methods[num_methods].method == EAP_TYPE_LEAP)
1032 ssid->leap++;
1033 else
1034 ssid->non_leap++;
1035 num_methods++;
1036 if (last)
1037 break;
1038 start = end + 1;
1039 }
1040 os_free(buf);
1041
1042 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001043 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 if (methods == NULL) {
1045 os_free(tmp);
1046 return -1;
1047 }
1048 methods[num_methods].vendor = EAP_VENDOR_IETF;
1049 methods[num_methods].method = EAP_TYPE_NONE;
1050 num_methods++;
1051
1052 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1053 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001054 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 ssid->eap.eap_methods = methods;
1056 return errors ? -1 : 0;
1057}
1058
1059
1060static char * wpa_config_write_eap(const struct parse_data *data,
1061 struct wpa_ssid *ssid)
1062{
1063 int i, ret;
1064 char *buf, *pos, *end;
1065 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1066 const char *name;
1067
1068 if (eap_methods == NULL)
1069 return NULL;
1070
1071 pos = buf = os_zalloc(100);
1072 if (buf == NULL)
1073 return NULL;
1074 end = buf + 100;
1075
1076 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1077 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1078 name = eap_get_name(eap_methods[i].vendor,
1079 eap_methods[i].method);
1080 if (name) {
1081 ret = os_snprintf(pos, end - pos, "%s%s",
1082 pos == buf ? "" : " ", name);
1083 if (ret < 0 || ret >= end - pos)
1084 break;
1085 pos += ret;
1086 }
1087 }
1088
1089 end[-1] = '\0';
1090
1091 return buf;
1092}
1093
1094
1095static int wpa_config_parse_password(const struct parse_data *data,
1096 struct wpa_ssid *ssid, int line,
1097 const char *value)
1098{
1099 u8 *hash;
1100
1101 if (os_strcmp(value, "NULL") == 0) {
1102 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1103 os_free(ssid->eap.password);
1104 ssid->eap.password = NULL;
1105 ssid->eap.password_len = 0;
1106 return 0;
1107 }
1108
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001109#ifdef CONFIG_EXT_PASSWORD
1110 if (os_strncmp(value, "ext:", 4) == 0) {
1111 char *name = os_strdup(value + 4);
1112 if (name == NULL)
1113 return -1;
1114 os_free(ssid->eap.password);
1115 ssid->eap.password = (u8 *) name;
1116 ssid->eap.password_len = os_strlen(name);
1117 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1118 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1119 return 0;
1120 }
1121#endif /* CONFIG_EXT_PASSWORD */
1122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (os_strncmp(value, "hash:", 5) != 0) {
1124 char *tmp;
1125 size_t res_len;
1126
1127 tmp = wpa_config_parse_string(value, &res_len);
1128 if (tmp == NULL) {
1129 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1130 "password.", line);
1131 return -1;
1132 }
1133 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1134 (u8 *) tmp, res_len);
1135
1136 os_free(ssid->eap.password);
1137 ssid->eap.password = (u8 *) tmp;
1138 ssid->eap.password_len = res_len;
1139 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141
1142 return 0;
1143 }
1144
1145
1146 /* NtPasswordHash: hash:<32 hex digits> */
1147 if (os_strlen(value + 5) != 2 * 16) {
1148 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1149 "(expected 32 hex digits)", line);
1150 return -1;
1151 }
1152
1153 hash = os_malloc(16);
1154 if (hash == NULL)
1155 return -1;
1156
1157 if (hexstr2bin(value + 5, hash, 16)) {
1158 os_free(hash);
1159 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1160 return -1;
1161 }
1162
1163 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1164
1165 os_free(ssid->eap.password);
1166 ssid->eap.password = hash;
1167 ssid->eap.password_len = 16;
1168 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001169 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170
1171 return 0;
1172}
1173
1174
1175static char * wpa_config_write_password(const struct parse_data *data,
1176 struct wpa_ssid *ssid)
1177{
1178 char *buf;
1179
1180 if (ssid->eap.password == NULL)
1181 return NULL;
1182
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001183#ifdef CONFIG_EXT_PASSWORD
1184 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1185 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1186 if (buf == NULL)
1187 return NULL;
1188 os_memcpy(buf, "ext:", 4);
1189 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1190 return buf;
1191 }
1192#endif /* CONFIG_EXT_PASSWORD */
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1195 return wpa_config_write_string(
1196 ssid->eap.password, ssid->eap.password_len);
1197 }
1198
1199 buf = os_malloc(5 + 32 + 1);
1200 if (buf == NULL)
1201 return NULL;
1202
1203 os_memcpy(buf, "hash:", 5);
1204 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1205
1206 return buf;
1207}
1208#endif /* IEEE8021X_EAPOL */
1209
1210
1211static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1212 const char *value, int idx)
1213{
1214 char *buf, title[20];
1215 int res;
1216
1217 buf = wpa_config_parse_string(value, len);
1218 if (buf == NULL) {
1219 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1220 line, idx, value);
1221 return -1;
1222 }
1223 if (*len > MAX_WEP_KEY_LEN) {
1224 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1225 line, idx, value);
1226 os_free(buf);
1227 return -1;
1228 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001229 if (*len && *len != 5 && *len != 13 && *len != 16) {
1230 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1231 "this network block will be ignored",
1232 line, (unsigned int) *len);
1233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 os_memcpy(key, buf, *len);
1235 os_free(buf);
1236 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1237 if (res >= 0 && (size_t) res < sizeof(title))
1238 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1239 return 0;
1240}
1241
1242
1243static int wpa_config_parse_wep_key0(const struct parse_data *data,
1244 struct wpa_ssid *ssid, int line,
1245 const char *value)
1246{
1247 return wpa_config_parse_wep_key(ssid->wep_key[0],
1248 &ssid->wep_key_len[0], line,
1249 value, 0);
1250}
1251
1252
1253static int wpa_config_parse_wep_key1(const struct parse_data *data,
1254 struct wpa_ssid *ssid, int line,
1255 const char *value)
1256{
1257 return wpa_config_parse_wep_key(ssid->wep_key[1],
1258 &ssid->wep_key_len[1], line,
1259 value, 1);
1260}
1261
1262
1263static int wpa_config_parse_wep_key2(const struct parse_data *data,
1264 struct wpa_ssid *ssid, int line,
1265 const char *value)
1266{
1267 return wpa_config_parse_wep_key(ssid->wep_key[2],
1268 &ssid->wep_key_len[2], line,
1269 value, 2);
1270}
1271
1272
1273static int wpa_config_parse_wep_key3(const struct parse_data *data,
1274 struct wpa_ssid *ssid, int line,
1275 const char *value)
1276{
1277 return wpa_config_parse_wep_key(ssid->wep_key[3],
1278 &ssid->wep_key_len[3], line,
1279 value, 3);
1280}
1281
1282
1283#ifndef NO_CONFIG_WRITE
1284static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1285{
1286 if (ssid->wep_key_len[idx] == 0)
1287 return NULL;
1288 return wpa_config_write_string(ssid->wep_key[idx],
1289 ssid->wep_key_len[idx]);
1290}
1291
1292
1293static char * wpa_config_write_wep_key0(const struct parse_data *data,
1294 struct wpa_ssid *ssid)
1295{
1296 return wpa_config_write_wep_key(ssid, 0);
1297}
1298
1299
1300static char * wpa_config_write_wep_key1(const struct parse_data *data,
1301 struct wpa_ssid *ssid)
1302{
1303 return wpa_config_write_wep_key(ssid, 1);
1304}
1305
1306
1307static char * wpa_config_write_wep_key2(const struct parse_data *data,
1308 struct wpa_ssid *ssid)
1309{
1310 return wpa_config_write_wep_key(ssid, 2);
1311}
1312
1313
1314static char * wpa_config_write_wep_key3(const struct parse_data *data,
1315 struct wpa_ssid *ssid)
1316{
1317 return wpa_config_write_wep_key(ssid, 3);
1318}
1319#endif /* NO_CONFIG_WRITE */
1320
1321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322#ifdef CONFIG_P2P
1323
Dmitry Shmidt54605472013-11-08 11:10:19 -08001324static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1325 struct wpa_ssid *ssid, int line,
1326 const char *value)
1327{
1328 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1329 os_strcmp(value, "any") == 0) {
1330 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1331 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1332 return 0;
1333 }
1334 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1335 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1336 line, value);
1337 return -1;
1338 }
1339 ssid->bssid_set = 1;
1340 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1341 MAC2STR(ssid->go_p2p_dev_addr));
1342 return 0;
1343}
1344
1345
1346#ifndef NO_CONFIG_WRITE
1347static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1348 struct wpa_ssid *ssid)
1349{
1350 char *value;
1351 int res;
1352
1353 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1354 return NULL;
1355
1356 value = os_malloc(20);
1357 if (value == NULL)
1358 return NULL;
1359 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1360 if (res < 0 || res >= 20) {
1361 os_free(value);
1362 return NULL;
1363 }
1364 value[20 - 1] = '\0';
1365 return value;
1366}
1367#endif /* NO_CONFIG_WRITE */
1368
1369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001370static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1371 struct wpa_ssid *ssid, int line,
1372 const char *value)
1373{
1374 const char *pos;
1375 u8 *buf, *n, addr[ETH_ALEN];
1376 size_t count;
1377
1378 buf = NULL;
1379 count = 0;
1380
1381 pos = value;
1382 while (pos && *pos) {
1383 while (*pos == ' ')
1384 pos++;
1385
1386 if (hwaddr_aton(pos, addr)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001387 if (count == 0) {
1388 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1389 "p2p_client_list address '%s'.",
1390 line, value);
1391 os_free(buf);
1392 return -1;
1393 }
1394 /* continue anyway since this could have been from a
1395 * truncated configuration file line */
1396 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1397 "truncated p2p_client_list address '%s'",
1398 line, pos);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001400 n = os_realloc_array(buf, count + 1, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 if (n == NULL) {
1402 os_free(buf);
1403 return -1;
1404 }
1405 buf = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001406 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1407 os_memcpy(buf, addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 count++;
1409 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1410 addr, ETH_ALEN);
1411 }
1412
1413 pos = os_strchr(pos, ' ');
1414 }
1415
1416 os_free(ssid->p2p_client_list);
1417 ssid->p2p_client_list = buf;
1418 ssid->num_p2p_clients = count;
1419
1420 return 0;
1421}
1422
1423
1424#ifndef NO_CONFIG_WRITE
1425static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1426 struct wpa_ssid *ssid)
1427{
1428 char *value, *end, *pos;
1429 int res;
1430 size_t i;
1431
1432 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1433 return NULL;
1434
1435 value = os_malloc(20 * ssid->num_p2p_clients);
1436 if (value == NULL)
1437 return NULL;
1438 pos = value;
1439 end = value + 20 * ssid->num_p2p_clients;
1440
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001441 for (i = ssid->num_p2p_clients; i > 0; i--) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001442 res = os_snprintf(pos, end - pos, MACSTR " ",
1443 MAC2STR(ssid->p2p_client_list +
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001444 (i - 1) * ETH_ALEN));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001445 if (res < 0 || res >= end - pos) {
1446 os_free(value);
1447 return NULL;
1448 }
1449 pos += res;
1450 }
1451
1452 if (pos > value)
1453 pos[-1] = '\0';
1454
1455 return value;
1456}
1457#endif /* NO_CONFIG_WRITE */
1458
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001459
1460static int wpa_config_parse_psk_list(const struct parse_data *data,
1461 struct wpa_ssid *ssid, int line,
1462 const char *value)
1463{
1464 struct psk_list_entry *p;
1465 const char *pos;
1466
1467 p = os_zalloc(sizeof(*p));
1468 if (p == NULL)
1469 return -1;
1470
1471 pos = value;
1472 if (os_strncmp(pos, "P2P-", 4) == 0) {
1473 p->p2p = 1;
1474 pos += 4;
1475 }
1476
1477 if (hwaddr_aton(pos, p->addr)) {
1478 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1479 line, pos);
1480 os_free(p);
1481 return -1;
1482 }
1483 pos += 17;
1484 if (*pos != '-') {
1485 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1486 line, pos);
1487 os_free(p);
1488 return -1;
1489 }
1490 pos++;
1491
1492 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1493 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1494 line, pos);
1495 os_free(p);
1496 return -1;
1497 }
1498
1499 dl_list_add(&ssid->psk_list, &p->list);
1500
1501 return 0;
1502}
1503
1504
1505#ifndef NO_CONFIG_WRITE
1506static char * wpa_config_write_psk_list(const struct parse_data *data,
1507 struct wpa_ssid *ssid)
1508{
1509 return NULL;
1510}
1511#endif /* NO_CONFIG_WRITE */
1512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513#endif /* CONFIG_P2P */
1514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515/* Helper macros for network block parser */
1516
1517#ifdef OFFSET
1518#undef OFFSET
1519#endif /* OFFSET */
1520/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1521#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1522
1523/* STR: Define a string variable for an ASCII string; f = field name */
1524#ifdef NO_CONFIG_WRITE
1525#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1526#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1527#else /* NO_CONFIG_WRITE */
1528#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1529#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1530#endif /* NO_CONFIG_WRITE */
1531#define STR(f) _STR(f), NULL, NULL, NULL, 0
1532#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1533#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1534#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1535
1536/* STR_LEN: Define a string variable with a separate variable for storing the
1537 * data length. Unlike STR(), this can be used to store arbitrary binary data
1538 * (i.e., even nul termination character). */
1539#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1540#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1541#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1542#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1543#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1544
1545/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1546 * explicitly specified. */
1547#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1548#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1549#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1550
1551#ifdef NO_CONFIG_WRITE
1552#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1553#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1554#else /* NO_CONFIG_WRITE */
1555#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1556 OFFSET(f), (void *) 0
1557#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1558 OFFSET(eap.f), (void *) 0
1559#endif /* NO_CONFIG_WRITE */
1560
1561/* INT: Define an integer variable */
1562#define INT(f) _INT(f), NULL, NULL, 0
1563#define INTe(f) _INTe(f), NULL, NULL, 0
1564
1565/* INT_RANGE: Define an integer variable with allowed value range */
1566#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1567
1568/* FUNC: Define a configuration variable that uses a custom function for
1569 * parsing and writing the value. */
1570#ifdef NO_CONFIG_WRITE
1571#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1572#else /* NO_CONFIG_WRITE */
1573#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1574 NULL, NULL, NULL, NULL
1575#endif /* NO_CONFIG_WRITE */
1576#define FUNC(f) _FUNC(f), 0
1577#define FUNC_KEY(f) _FUNC(f), 1
1578
1579/*
1580 * Table of network configuration variables. This table is used to parse each
1581 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1582 * that is inside a network block.
1583 *
1584 * This table is generated using the helper macros defined above and with
1585 * generous help from the C pre-processor. The field name is stored as a string
1586 * into .name and for STR and INT types, the offset of the target buffer within
1587 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1588 * offset to the field containing the length of the configuration variable.
1589 * .param3 and .param4 can be used to mark the allowed range (length for STR
1590 * and value for INT).
1591 *
1592 * For each configuration line in wpa_supplicant.conf, the parser goes through
1593 * this table and select the entry that matches with the field name. The parser
1594 * function (.parser) is then called to parse the actual value of the field.
1595 *
1596 * This kind of mechanism makes it easy to add new configuration parameters,
1597 * since only one line needs to be added into this table and into the
1598 * struct wpa_ssid definition if the new variable is either a string or
1599 * integer. More complex types will need to use their own parser and writer
1600 * functions.
1601 */
1602static const struct parse_data ssid_fields[] = {
1603 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1604 { INT_RANGE(scan_ssid, 0, 1) },
1605 { FUNC(bssid) },
1606 { FUNC_KEY(psk) },
1607 { FUNC(proto) },
1608 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001609 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 { FUNC(pairwise) },
1611 { FUNC(group) },
1612 { FUNC(auth_alg) },
1613 { FUNC(scan_freq) },
1614 { FUNC(freq_list) },
1615#ifdef IEEE8021X_EAPOL
1616 { FUNC(eap) },
1617 { STR_LENe(identity) },
1618 { STR_LENe(anonymous_identity) },
1619 { FUNC_KEY(password) },
1620 { STRe(ca_cert) },
1621 { STRe(ca_path) },
1622 { STRe(client_cert) },
1623 { STRe(private_key) },
1624 { STR_KEYe(private_key_passwd) },
1625 { STRe(dh_file) },
1626 { STRe(subject_match) },
1627 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001628 { STRe(domain_suffix_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 { STRe(ca_cert2) },
1630 { STRe(ca_path2) },
1631 { STRe(client_cert2) },
1632 { STRe(private_key2) },
1633 { STR_KEYe(private_key2_passwd) },
1634 { STRe(dh_file2) },
1635 { STRe(subject_match2) },
1636 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001637 { STRe(domain_suffix_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 { STRe(phase1) },
1639 { STRe(phase2) },
1640 { STRe(pcsc) },
1641 { STR_KEYe(pin) },
1642 { STRe(engine_id) },
1643 { STRe(key_id) },
1644 { STRe(cert_id) },
1645 { STRe(ca_cert_id) },
1646 { STR_KEYe(pin2) },
1647 { STRe(engine2_id) },
1648 { STRe(key2_id) },
1649 { STRe(cert2_id) },
1650 { STRe(ca_cert2_id) },
1651 { INTe(engine) },
1652 { INTe(engine2) },
1653 { INT(eapol_flags) },
1654#endif /* IEEE8021X_EAPOL */
1655 { FUNC_KEY(wep_key0) },
1656 { FUNC_KEY(wep_key1) },
1657 { FUNC_KEY(wep_key2) },
1658 { FUNC_KEY(wep_key3) },
1659 { INT(wep_tx_keyidx) },
1660 { INT(priority) },
1661#ifdef IEEE8021X_EAPOL
1662 { INT(eap_workaround) },
1663 { STRe(pac_file) },
1664 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001665 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666#endif /* IEEE8021X_EAPOL */
1667 { INT_RANGE(mode, 0, 4) },
1668 { INT_RANGE(proactive_key_caching, 0, 1) },
1669 { INT_RANGE(disabled, 0, 2) },
1670 { STR(id_str) },
1671#ifdef CONFIG_IEEE80211W
1672 { INT_RANGE(ieee80211w, 0, 2) },
1673#endif /* CONFIG_IEEE80211W */
1674 { INT_RANGE(peerkey, 0, 1) },
1675 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001676 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 { INT(wpa_ptk_rekey) },
1678 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001679 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001680#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001681 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001683 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001685#ifdef CONFIG_HT_OVERRIDES
1686 { INT_RANGE(disable_ht, 0, 1) },
1687 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001688 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001689 { INT_RANGE(disable_max_amsdu, -1, 1) },
1690 { INT_RANGE(ampdu_factor, -1, 3) },
1691 { INT_RANGE(ampdu_density, -1, 7) },
1692 { STR(ht_mcs) },
1693#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001694#ifdef CONFIG_VHT_OVERRIDES
1695 { INT_RANGE(disable_vht, 0, 1) },
1696 { INT(vht_capa) },
1697 { INT(vht_capa_mask) },
1698 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1699 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1700 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1701 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1702 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1703 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1704 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1705 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1706 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1707 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1708 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1709 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1710 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1711 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1712 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1713 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1714#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001715 { INT(ap_max_inactivity) },
1716 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001717 { INT(beacon_int) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718};
1719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720#undef OFFSET
1721#undef _STR
1722#undef STR
1723#undef STR_KEY
1724#undef _STR_LEN
1725#undef STR_LEN
1726#undef STR_LEN_KEY
1727#undef _STR_RANGE
1728#undef STR_RANGE
1729#undef STR_RANGE_KEY
1730#undef _INT
1731#undef INT
1732#undef INT_RANGE
1733#undef _FUNC
1734#undef FUNC
1735#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001736#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737
1738
1739/**
1740 * wpa_config_add_prio_network - Add a network to priority lists
1741 * @config: Configuration data from wpa_config_read()
1742 * @ssid: Pointer to the network configuration to be added to the list
1743 * Returns: 0 on success, -1 on failure
1744 *
1745 * This function is used to add a network block to the priority list of
1746 * networks. This must be called for each network when reading in the full
1747 * configuration. In addition, this can be used indirectly when updating
1748 * priorities by calling wpa_config_update_prio_list().
1749 */
1750int wpa_config_add_prio_network(struct wpa_config *config,
1751 struct wpa_ssid *ssid)
1752{
1753 int prio;
1754 struct wpa_ssid *prev, **nlist;
1755
1756 /*
1757 * Add to an existing priority list if one is available for the
1758 * configured priority level for this network.
1759 */
1760 for (prio = 0; prio < config->num_prio; prio++) {
1761 prev = config->pssid[prio];
1762 if (prev->priority == ssid->priority) {
1763 while (prev->pnext)
1764 prev = prev->pnext;
1765 prev->pnext = ssid;
1766 return 0;
1767 }
1768 }
1769
1770 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001771 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1772 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 if (nlist == NULL)
1774 return -1;
1775
1776 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001777 if (nlist[prio]->priority < ssid->priority) {
1778 os_memmove(&nlist[prio + 1], &nlist[prio],
1779 (config->num_prio - prio) *
1780 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 }
1784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 nlist[prio] = ssid;
1786 config->num_prio++;
1787 config->pssid = nlist;
1788
1789 return 0;
1790}
1791
1792
1793/**
1794 * wpa_config_update_prio_list - Update network priority list
1795 * @config: Configuration data from wpa_config_read()
1796 * Returns: 0 on success, -1 on failure
1797 *
1798 * This function is called to update the priority list of networks in the
1799 * configuration when a network is being added or removed. This is also called
1800 * if a priority for a network is changed.
1801 */
1802int wpa_config_update_prio_list(struct wpa_config *config)
1803{
1804 struct wpa_ssid *ssid;
1805 int ret = 0;
1806
1807 os_free(config->pssid);
1808 config->pssid = NULL;
1809 config->num_prio = 0;
1810
1811 ssid = config->ssid;
1812 while (ssid) {
1813 ssid->pnext = NULL;
1814 if (wpa_config_add_prio_network(config, ssid) < 0)
1815 ret = -1;
1816 ssid = ssid->next;
1817 }
1818
1819 return ret;
1820}
1821
1822
1823#ifdef IEEE8021X_EAPOL
1824static void eap_peer_config_free(struct eap_peer_config *eap)
1825{
1826 os_free(eap->eap_methods);
1827 os_free(eap->identity);
1828 os_free(eap->anonymous_identity);
1829 os_free(eap->password);
1830 os_free(eap->ca_cert);
1831 os_free(eap->ca_path);
1832 os_free(eap->client_cert);
1833 os_free(eap->private_key);
1834 os_free(eap->private_key_passwd);
1835 os_free(eap->dh_file);
1836 os_free(eap->subject_match);
1837 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001838 os_free(eap->domain_suffix_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 os_free(eap->ca_cert2);
1840 os_free(eap->ca_path2);
1841 os_free(eap->client_cert2);
1842 os_free(eap->private_key2);
1843 os_free(eap->private_key2_passwd);
1844 os_free(eap->dh_file2);
1845 os_free(eap->subject_match2);
1846 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001847 os_free(eap->domain_suffix_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 os_free(eap->phase1);
1849 os_free(eap->phase2);
1850 os_free(eap->pcsc);
1851 os_free(eap->pin);
1852 os_free(eap->engine_id);
1853 os_free(eap->key_id);
1854 os_free(eap->cert_id);
1855 os_free(eap->ca_cert_id);
1856 os_free(eap->key2_id);
1857 os_free(eap->cert2_id);
1858 os_free(eap->ca_cert2_id);
1859 os_free(eap->pin2);
1860 os_free(eap->engine2_id);
1861 os_free(eap->otp);
1862 os_free(eap->pending_req_otp);
1863 os_free(eap->pac_file);
1864 os_free(eap->new_password);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001865 os_free(eap->external_sim_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866}
1867#endif /* IEEE8021X_EAPOL */
1868
1869
1870/**
1871 * wpa_config_free_ssid - Free network/ssid configuration data
1872 * @ssid: Configuration data for the network
1873 *
1874 * This function frees all resources allocated for the network configuration
1875 * data.
1876 */
1877void wpa_config_free_ssid(struct wpa_ssid *ssid)
1878{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001879 struct psk_list_entry *psk;
1880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 os_free(ssid->ssid);
1882 os_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001883 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884#ifdef IEEE8021X_EAPOL
1885 eap_peer_config_free(&ssid->eap);
1886#endif /* IEEE8021X_EAPOL */
1887 os_free(ssid->id_str);
1888 os_free(ssid->scan_freq);
1889 os_free(ssid->freq_list);
1890 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001891 os_free(ssid->p2p_client_list);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001892#ifdef CONFIG_HT_OVERRIDES
1893 os_free(ssid->ht_mcs);
1894#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001895 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1896 list))) {
1897 dl_list_del(&psk->list);
1898 os_free(psk);
1899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 os_free(ssid);
1901}
1902
1903
Dmitry Shmidt04949592012-07-19 12:16:46 -07001904void wpa_config_free_cred(struct wpa_cred *cred)
1905{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001906 size_t i;
1907
Dmitry Shmidt04949592012-07-19 12:16:46 -07001908 os_free(cred->realm);
1909 os_free(cred->username);
1910 os_free(cred->password);
1911 os_free(cred->ca_cert);
1912 os_free(cred->client_cert);
1913 os_free(cred->private_key);
1914 os_free(cred->private_key_passwd);
1915 os_free(cred->imsi);
1916 os_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001917 for (i = 0; i < cred->num_domain; i++)
1918 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001919 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001920 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001921 os_free(cred->eap_method);
1922 os_free(cred->phase1);
1923 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001924 os_free(cred->excluded_ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001925 os_free(cred);
1926}
1927
1928
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001929void wpa_config_flush_blobs(struct wpa_config *config)
1930{
1931#ifndef CONFIG_NO_CONFIG_BLOBS
1932 struct wpa_config_blob *blob, *prev;
1933
1934 blob = config->blobs;
1935 config->blobs = NULL;
1936 while (blob) {
1937 prev = blob;
1938 blob = blob->next;
1939 wpa_config_free_blob(prev);
1940 }
1941#endif /* CONFIG_NO_CONFIG_BLOBS */
1942}
1943
1944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945/**
1946 * wpa_config_free - Free configuration data
1947 * @config: Configuration data from wpa_config_read()
1948 *
1949 * This function frees all resources allocated for the configuration data by
1950 * wpa_config_read().
1951 */
1952void wpa_config_free(struct wpa_config *config)
1953{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001955 struct wpa_cred *cred, *cprev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956
1957 ssid = config->ssid;
1958 while (ssid) {
1959 prev = ssid;
1960 ssid = ssid->next;
1961 wpa_config_free_ssid(prev);
1962 }
1963
Dmitry Shmidt04949592012-07-19 12:16:46 -07001964 cred = config->cred;
1965 while (cred) {
1966 cprev = cred;
1967 cred = cred->next;
1968 wpa_config_free_cred(cprev);
1969 }
1970
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001971 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972
Dmitry Shmidt04949592012-07-19 12:16:46 -07001973 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 os_free(config->ctrl_interface);
1975 os_free(config->ctrl_interface_group);
1976 os_free(config->opensc_engine_path);
1977 os_free(config->pkcs11_engine_path);
1978 os_free(config->pkcs11_module_path);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001979 os_free(config->pcsc_reader);
1980 os_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 os_free(config->driver_param);
1982 os_free(config->device_name);
1983 os_free(config->manufacturer);
1984 os_free(config->model_name);
1985 os_free(config->model_number);
1986 os_free(config->serial_number);
1987 os_free(config->config_methods);
1988 os_free(config->p2p_ssid_postfix);
1989 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001990 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001991 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001992 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001993 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001994 wpabuf_free(config->wps_nfc_dh_pubkey);
1995 wpabuf_free(config->wps_nfc_dh_privkey);
1996 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001997 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001998 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001999 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 os_free(config);
2001}
2002
2003
2004/**
2005 * wpa_config_foreach_network - Iterate over each configured network
2006 * @config: Configuration data from wpa_config_read()
2007 * @func: Callback function to process each network
2008 * @arg: Opaque argument to pass to callback function
2009 *
2010 * Iterate over the set of configured networks calling the specified
2011 * function for each item. We guard against callbacks removing the
2012 * supplied network.
2013 */
2014void wpa_config_foreach_network(struct wpa_config *config,
2015 void (*func)(void *, struct wpa_ssid *),
2016 void *arg)
2017{
2018 struct wpa_ssid *ssid, *next;
2019
2020 ssid = config->ssid;
2021 while (ssid) {
2022 next = ssid->next;
2023 func(arg, ssid);
2024 ssid = next;
2025 }
2026}
2027
2028
2029/**
2030 * wpa_config_get_network - Get configured network based on id
2031 * @config: Configuration data from wpa_config_read()
2032 * @id: Unique network id to search for
2033 * Returns: Network configuration or %NULL if not found
2034 */
2035struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2036{
2037 struct wpa_ssid *ssid;
2038
2039 ssid = config->ssid;
2040 while (ssid) {
2041 if (id == ssid->id)
2042 break;
2043 ssid = ssid->next;
2044 }
2045
2046 return ssid;
2047}
2048
2049
2050/**
2051 * wpa_config_add_network - Add a new network with empty configuration
2052 * @config: Configuration data from wpa_config_read()
2053 * Returns: The new network configuration or %NULL if operation failed
2054 */
2055struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2056{
2057 int id;
2058 struct wpa_ssid *ssid, *last = NULL;
2059
2060 id = -1;
2061 ssid = config->ssid;
2062 while (ssid) {
2063 if (ssid->id > id)
2064 id = ssid->id;
2065 last = ssid;
2066 ssid = ssid->next;
2067 }
2068 id++;
2069
2070 ssid = os_zalloc(sizeof(*ssid));
2071 if (ssid == NULL)
2072 return NULL;
2073 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002074 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 if (last)
2076 last->next = ssid;
2077 else
2078 config->ssid = ssid;
2079
2080 wpa_config_update_prio_list(config);
2081
2082 return ssid;
2083}
2084
2085
2086/**
2087 * wpa_config_remove_network - Remove a configured network based on id
2088 * @config: Configuration data from wpa_config_read()
2089 * @id: Unique network id to search for
2090 * Returns: 0 on success, or -1 if the network was not found
2091 */
2092int wpa_config_remove_network(struct wpa_config *config, int id)
2093{
2094 struct wpa_ssid *ssid, *prev = NULL;
2095
2096 ssid = config->ssid;
2097 while (ssid) {
2098 if (id == ssid->id)
2099 break;
2100 prev = ssid;
2101 ssid = ssid->next;
2102 }
2103
2104 if (ssid == NULL)
2105 return -1;
2106
2107 if (prev)
2108 prev->next = ssid->next;
2109 else
2110 config->ssid = ssid->next;
2111
2112 wpa_config_update_prio_list(config);
2113 wpa_config_free_ssid(ssid);
2114 return 0;
2115}
2116
2117
2118/**
2119 * wpa_config_set_network_defaults - Set network default values
2120 * @ssid: Pointer to network configuration data
2121 */
2122void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2123{
2124 ssid->proto = DEFAULT_PROTO;
2125 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2126 ssid->group_cipher = DEFAULT_GROUP;
2127 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002128 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129#ifdef IEEE8021X_EAPOL
2130 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2131 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2132 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2133#endif /* IEEE8021X_EAPOL */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002134#ifdef CONFIG_HT_OVERRIDES
2135 ssid->disable_ht = DEFAULT_DISABLE_HT;
2136 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002137 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002138 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2139 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2140 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2141#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002142#ifdef CONFIG_VHT_OVERRIDES
2143 ssid->vht_rx_mcs_nss_1 = -1;
2144 ssid->vht_rx_mcs_nss_2 = -1;
2145 ssid->vht_rx_mcs_nss_3 = -1;
2146 ssid->vht_rx_mcs_nss_4 = -1;
2147 ssid->vht_rx_mcs_nss_5 = -1;
2148 ssid->vht_rx_mcs_nss_6 = -1;
2149 ssid->vht_rx_mcs_nss_7 = -1;
2150 ssid->vht_rx_mcs_nss_8 = -1;
2151 ssid->vht_tx_mcs_nss_1 = -1;
2152 ssid->vht_tx_mcs_nss_2 = -1;
2153 ssid->vht_tx_mcs_nss_3 = -1;
2154 ssid->vht_tx_mcs_nss_4 = -1;
2155 ssid->vht_tx_mcs_nss_5 = -1;
2156 ssid->vht_tx_mcs_nss_6 = -1;
2157 ssid->vht_tx_mcs_nss_7 = -1;
2158 ssid->vht_tx_mcs_nss_8 = -1;
2159#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002160 ssid->proactive_key_caching = -1;
2161#ifdef CONFIG_IEEE80211W
2162 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2163#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164}
2165
2166
2167/**
2168 * wpa_config_set - Set a variable in network configuration
2169 * @ssid: Pointer to network configuration data
2170 * @var: Variable name, e.g., "ssid"
2171 * @value: Variable value
2172 * @line: Line number in configuration file or 0 if not used
2173 * Returns: 0 on success, -1 on failure
2174 *
2175 * This function can be used to set network configuration variables based on
2176 * both the configuration file and management interface input. The value
2177 * parameter must be in the same format as the text-based configuration file is
2178 * using. For example, strings are using double quotation marks.
2179 */
2180int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2181 int line)
2182{
2183 size_t i;
2184 int ret = 0;
2185
2186 if (ssid == NULL || var == NULL || value == NULL)
2187 return -1;
2188
2189 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2190 const struct parse_data *field = &ssid_fields[i];
2191 if (os_strcmp(var, field->name) != 0)
2192 continue;
2193
2194 if (field->parser(field, ssid, line, value)) {
2195 if (line) {
2196 wpa_printf(MSG_ERROR, "Line %d: failed to "
2197 "parse %s '%s'.", line, var, value);
2198 }
2199 ret = -1;
2200 }
2201 break;
2202 }
2203 if (i == NUM_SSID_FIELDS) {
2204 if (line) {
2205 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2206 "'%s'.", line, var);
2207 }
2208 ret = -1;
2209 }
2210
2211 return ret;
2212}
2213
2214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002215int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2216 const char *value)
2217{
2218 size_t len;
2219 char *buf;
2220 int ret;
2221
2222 len = os_strlen(value);
2223 buf = os_malloc(len + 3);
2224 if (buf == NULL)
2225 return -1;
2226 buf[0] = '"';
2227 os_memcpy(buf + 1, value, len);
2228 buf[len + 1] = '"';
2229 buf[len + 2] = '\0';
2230 ret = wpa_config_set(ssid, var, buf, 0);
2231 os_free(buf);
2232 return ret;
2233}
2234
2235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236/**
2237 * wpa_config_get_all - Get all options from network configuration
2238 * @ssid: Pointer to network configuration data
2239 * @get_keys: Determines if keys/passwords will be included in returned list
2240 * (if they may be exported)
2241 * Returns: %NULL terminated list of all set keys and their values in the form
2242 * of [key1, val1, key2, val2, ... , NULL]
2243 *
2244 * This function can be used to get list of all configured network properties.
2245 * The caller is responsible for freeing the returned list and all its
2246 * elements.
2247 */
2248char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2249{
2250 const struct parse_data *field;
2251 char *key, *value;
2252 size_t i;
2253 char **props;
2254 int fields_num;
2255
2256 get_keys = get_keys && ssid->export_keys;
2257
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002258 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 if (!props)
2260 return NULL;
2261
2262 fields_num = 0;
2263 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2264 field = &ssid_fields[i];
2265 if (field->key_data && !get_keys)
2266 continue;
2267 value = field->writer(field, ssid);
2268 if (value == NULL)
2269 continue;
2270 if (os_strlen(value) == 0) {
2271 os_free(value);
2272 continue;
2273 }
2274
2275 key = os_strdup(field->name);
2276 if (key == NULL) {
2277 os_free(value);
2278 goto err;
2279 }
2280
2281 props[fields_num * 2] = key;
2282 props[fields_num * 2 + 1] = value;
2283
2284 fields_num++;
2285 }
2286
2287 return props;
2288
2289err:
2290 value = *props;
2291 while (value)
2292 os_free(value++);
2293 os_free(props);
2294 return NULL;
2295}
2296
2297
2298#ifndef NO_CONFIG_WRITE
2299/**
2300 * wpa_config_get - Get a variable in network configuration
2301 * @ssid: Pointer to network configuration data
2302 * @var: Variable name, e.g., "ssid"
2303 * Returns: Value of the variable or %NULL on failure
2304 *
2305 * This function can be used to get network configuration variables. The
2306 * returned value is a copy of the configuration variable in text format, i.e,.
2307 * the same format that the text-based configuration file and wpa_config_set()
2308 * are using for the value. The caller is responsible for freeing the returned
2309 * value.
2310 */
2311char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2312{
2313 size_t i;
2314
2315 if (ssid == NULL || var == NULL)
2316 return NULL;
2317
2318 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2319 const struct parse_data *field = &ssid_fields[i];
2320 if (os_strcmp(var, field->name) == 0)
2321 return field->writer(field, ssid);
2322 }
2323
2324 return NULL;
2325}
2326
2327
2328/**
2329 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2330 * @ssid: Pointer to network configuration data
2331 * @var: Variable name, e.g., "ssid"
2332 * Returns: Value of the variable or %NULL on failure
2333 *
2334 * This function can be used to get network configuration variable like
2335 * wpa_config_get(). The only difference is that this functions does not expose
2336 * key/password material from the configuration. In case a key/password field
2337 * is requested, the returned value is an empty string or %NULL if the variable
2338 * is not set or "*" if the variable is set (regardless of its value). The
2339 * returned value is a copy of the configuration variable in text format, i.e,.
2340 * the same format that the text-based configuration file and wpa_config_set()
2341 * are using for the value. The caller is responsible for freeing the returned
2342 * value.
2343 */
2344char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2345{
2346 size_t i;
2347
2348 if (ssid == NULL || var == NULL)
2349 return NULL;
2350
2351 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2352 const struct parse_data *field = &ssid_fields[i];
2353 if (os_strcmp(var, field->name) == 0) {
2354 char *res = field->writer(field, ssid);
2355 if (field->key_data) {
2356 if (res && res[0]) {
2357 wpa_printf(MSG_DEBUG, "Do not allow "
2358 "key_data field to be "
2359 "exposed");
2360 os_free(res);
2361 return os_strdup("*");
2362 }
2363
2364 os_free(res);
2365 return NULL;
2366 }
2367 return res;
2368 }
2369 }
2370
2371 return NULL;
2372}
2373#endif /* NO_CONFIG_WRITE */
2374
2375
2376/**
2377 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2378 * @ssid: Pointer to network configuration data
2379 *
2380 * This function must be called to update WPA PSK when either SSID or the
2381 * passphrase has changed for the network configuration.
2382 */
2383void wpa_config_update_psk(struct wpa_ssid *ssid)
2384{
2385#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002386 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 ssid->psk, PMK_LEN);
2388 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2389 ssid->psk, PMK_LEN);
2390 ssid->psk_set = 1;
2391#endif /* CONFIG_NO_PBKDF2 */
2392}
2393
2394
Dmitry Shmidt04949592012-07-19 12:16:46 -07002395int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2396 const char *value, int line)
2397{
2398 char *val;
2399 size_t len;
2400
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002401 if (os_strcmp(var, "temporary") == 0) {
2402 cred->temporary = atoi(value);
2403 return 0;
2404 }
2405
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406 if (os_strcmp(var, "priority") == 0) {
2407 cred->priority = atoi(value);
2408 return 0;
2409 }
2410
2411 if (os_strcmp(var, "pcsc") == 0) {
2412 cred->pcsc = atoi(value);
2413 return 0;
2414 }
2415
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002416 if (os_strcmp(var, "eap") == 0) {
2417 struct eap_method_type method;
2418 method.method = eap_peer_get_type(value, &method.vendor);
2419 if (method.vendor == EAP_VENDOR_IETF &&
2420 method.method == EAP_TYPE_NONE) {
2421 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2422 "for a credential", line, value);
2423 return -1;
2424 }
2425 os_free(cred->eap_method);
2426 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2427 if (cred->eap_method == NULL)
2428 return -1;
2429 os_memcpy(cred->eap_method, &method, sizeof(method));
2430 return 0;
2431 }
2432
2433 if (os_strcmp(var, "password") == 0 &&
2434 os_strncmp(value, "ext:", 4) == 0) {
2435 os_free(cred->password);
2436 cred->password = os_strdup(value);
2437 cred->ext_password = 1;
2438 return 0;
2439 }
2440
Dmitry Shmidt04949592012-07-19 12:16:46 -07002441 val = wpa_config_parse_string(value, &len);
2442 if (val == NULL) {
2443 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2444 "value '%s'.", line, var, value);
2445 return -1;
2446 }
2447
2448 if (os_strcmp(var, "realm") == 0) {
2449 os_free(cred->realm);
2450 cred->realm = val;
2451 return 0;
2452 }
2453
2454 if (os_strcmp(var, "username") == 0) {
2455 os_free(cred->username);
2456 cred->username = val;
2457 return 0;
2458 }
2459
2460 if (os_strcmp(var, "password") == 0) {
2461 os_free(cred->password);
2462 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002463 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002464 return 0;
2465 }
2466
2467 if (os_strcmp(var, "ca_cert") == 0) {
2468 os_free(cred->ca_cert);
2469 cred->ca_cert = val;
2470 return 0;
2471 }
2472
2473 if (os_strcmp(var, "client_cert") == 0) {
2474 os_free(cred->client_cert);
2475 cred->client_cert = val;
2476 return 0;
2477 }
2478
2479 if (os_strcmp(var, "private_key") == 0) {
2480 os_free(cred->private_key);
2481 cred->private_key = val;
2482 return 0;
2483 }
2484
2485 if (os_strcmp(var, "private_key_passwd") == 0) {
2486 os_free(cred->private_key_passwd);
2487 cred->private_key_passwd = val;
2488 return 0;
2489 }
2490
2491 if (os_strcmp(var, "imsi") == 0) {
2492 os_free(cred->imsi);
2493 cred->imsi = val;
2494 return 0;
2495 }
2496
2497 if (os_strcmp(var, "milenage") == 0) {
2498 os_free(cred->milenage);
2499 cred->milenage = val;
2500 return 0;
2501 }
2502
Dmitry Shmidt051af732013-10-22 13:52:46 -07002503 if (os_strcmp(var, "domain_suffix_match") == 0) {
2504 os_free(cred->domain_suffix_match);
2505 cred->domain_suffix_match = val;
2506 return 0;
2507 }
2508
Dmitry Shmidt04949592012-07-19 12:16:46 -07002509 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002510 char **new_domain;
2511 new_domain = os_realloc_array(cred->domain,
2512 cred->num_domain + 1,
2513 sizeof(char *));
2514 if (new_domain == NULL) {
2515 os_free(val);
2516 return -1;
2517 }
2518 new_domain[cred->num_domain++] = val;
2519 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002520 return 0;
2521 }
2522
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002523 if (os_strcmp(var, "phase1") == 0) {
2524 os_free(cred->phase1);
2525 cred->phase1 = val;
2526 return 0;
2527 }
2528
2529 if (os_strcmp(var, "phase2") == 0) {
2530 os_free(cred->phase2);
2531 cred->phase2 = val;
2532 return 0;
2533 }
2534
2535 if (os_strcmp(var, "roaming_consortium") == 0) {
2536 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2537 wpa_printf(MSG_ERROR, "Line %d: invalid "
2538 "roaming_consortium length %d (3..15 "
2539 "expected)", line, (int) len);
2540 os_free(val);
2541 return -1;
2542 }
2543 os_memcpy(cred->roaming_consortium, val, len);
2544 cred->roaming_consortium_len = len;
2545 os_free(val);
2546 return 0;
2547 }
2548
Dmitry Shmidt051af732013-10-22 13:52:46 -07002549 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2550 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2551 {
2552 wpa_printf(MSG_ERROR, "Line %d: invalid "
2553 "required_roaming_consortium length %d "
2554 "(3..15 expected)", line, (int) len);
2555 os_free(val);
2556 return -1;
2557 }
2558 os_memcpy(cred->required_roaming_consortium, val, len);
2559 cred->required_roaming_consortium_len = len;
2560 os_free(val);
2561 return 0;
2562 }
2563
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002564 if (os_strcmp(var, "excluded_ssid") == 0) {
2565 struct excluded_ssid *e;
2566
2567 if (len > MAX_SSID_LEN) {
2568 wpa_printf(MSG_ERROR, "Line %d: invalid "
2569 "excluded_ssid length %d", line, (int) len);
2570 os_free(val);
2571 return -1;
2572 }
2573
2574 e = os_realloc_array(cred->excluded_ssid,
2575 cred->num_excluded_ssid + 1,
2576 sizeof(struct excluded_ssid));
2577 if (e == NULL) {
2578 os_free(val);
2579 return -1;
2580 }
2581 cred->excluded_ssid = e;
2582
2583 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2584 os_memcpy(e->ssid, val, len);
2585 e->ssid_len = len;
2586
2587 os_free(val);
2588
2589 return 0;
2590 }
2591
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592 if (line) {
2593 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2594 line, var);
2595 }
2596
2597 os_free(val);
2598
2599 return -1;
2600}
2601
2602
2603struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2604{
2605 struct wpa_cred *cred;
2606
2607 cred = config->cred;
2608 while (cred) {
2609 if (id == cred->id)
2610 break;
2611 cred = cred->next;
2612 }
2613
2614 return cred;
2615}
2616
2617
2618struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2619{
2620 int id;
2621 struct wpa_cred *cred, *last = NULL;
2622
2623 id = -1;
2624 cred = config->cred;
2625 while (cred) {
2626 if (cred->id > id)
2627 id = cred->id;
2628 last = cred;
2629 cred = cred->next;
2630 }
2631 id++;
2632
2633 cred = os_zalloc(sizeof(*cred));
2634 if (cred == NULL)
2635 return NULL;
2636 cred->id = id;
2637 if (last)
2638 last->next = cred;
2639 else
2640 config->cred = cred;
2641
2642 return cred;
2643}
2644
2645
2646int wpa_config_remove_cred(struct wpa_config *config, int id)
2647{
2648 struct wpa_cred *cred, *prev = NULL;
2649
2650 cred = config->cred;
2651 while (cred) {
2652 if (id == cred->id)
2653 break;
2654 prev = cred;
2655 cred = cred->next;
2656 }
2657
2658 if (cred == NULL)
2659 return -1;
2660
2661 if (prev)
2662 prev->next = cred->next;
2663 else
2664 config->cred = cred->next;
2665
2666 wpa_config_free_cred(cred);
2667 return 0;
2668}
2669
2670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671#ifndef CONFIG_NO_CONFIG_BLOBS
2672/**
2673 * wpa_config_get_blob - Get a named configuration blob
2674 * @config: Configuration data from wpa_config_read()
2675 * @name: Name of the blob
2676 * Returns: Pointer to blob data or %NULL if not found
2677 */
2678const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2679 const char *name)
2680{
2681 struct wpa_config_blob *blob = config->blobs;
2682
2683 while (blob) {
2684 if (os_strcmp(blob->name, name) == 0)
2685 return blob;
2686 blob = blob->next;
2687 }
2688 return NULL;
2689}
2690
2691
2692/**
2693 * wpa_config_set_blob - Set or add a named configuration blob
2694 * @config: Configuration data from wpa_config_read()
2695 * @blob: New value for the blob
2696 *
2697 * Adds a new configuration blob or replaces the current value of an existing
2698 * blob.
2699 */
2700void wpa_config_set_blob(struct wpa_config *config,
2701 struct wpa_config_blob *blob)
2702{
2703 wpa_config_remove_blob(config, blob->name);
2704 blob->next = config->blobs;
2705 config->blobs = blob;
2706}
2707
2708
2709/**
2710 * wpa_config_free_blob - Free blob data
2711 * @blob: Pointer to blob to be freed
2712 */
2713void wpa_config_free_blob(struct wpa_config_blob *blob)
2714{
2715 if (blob) {
2716 os_free(blob->name);
2717 os_free(blob->data);
2718 os_free(blob);
2719 }
2720}
2721
2722
2723/**
2724 * wpa_config_remove_blob - Remove a named configuration blob
2725 * @config: Configuration data from wpa_config_read()
2726 * @name: Name of the blob to remove
2727 * Returns: 0 if blob was removed or -1 if blob was not found
2728 */
2729int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2730{
2731 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2732
2733 while (pos) {
2734 if (os_strcmp(pos->name, name) == 0) {
2735 if (prev)
2736 prev->next = pos->next;
2737 else
2738 config->blobs = pos->next;
2739 wpa_config_free_blob(pos);
2740 return 0;
2741 }
2742 prev = pos;
2743 pos = pos->next;
2744 }
2745
2746 return -1;
2747}
2748#endif /* CONFIG_NO_CONFIG_BLOBS */
2749
2750
2751/**
2752 * wpa_config_alloc_empty - Allocate an empty configuration
2753 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2754 * socket
2755 * @driver_param: Driver parameters
2756 * Returns: Pointer to allocated configuration data or %NULL on failure
2757 */
2758struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2759 const char *driver_param)
2760{
2761 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002762 const int aCWmin = 4, aCWmax = 10;
2763 const struct hostapd_wmm_ac_params ac_bk =
2764 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2765 const struct hostapd_wmm_ac_params ac_be =
2766 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2767 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2768 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2769 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2770 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771
2772 config = os_zalloc(sizeof(*config));
2773 if (config == NULL)
2774 return NULL;
2775 config->eapol_version = DEFAULT_EAPOL_VERSION;
2776 config->ap_scan = DEFAULT_AP_SCAN;
2777 config->fast_reauth = DEFAULT_FAST_REAUTH;
2778 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2779 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002780 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2782 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2783 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2784 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002785 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002786 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002787 config->wmm_ac_params[0] = ac_be;
2788 config->wmm_ac_params[1] = ac_bk;
2789 config->wmm_ac_params[2] = ac_vi;
2790 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791
2792 if (ctrl_interface)
2793 config->ctrl_interface = os_strdup(ctrl_interface);
2794 if (driver_param)
2795 config->driver_param = os_strdup(driver_param);
2796
2797 return config;
2798}
2799
2800
2801#ifndef CONFIG_NO_STDOUT_DEBUG
2802/**
2803 * wpa_config_debug_dump_networks - Debug dump of configured networks
2804 * @config: Configuration data from wpa_config_read()
2805 */
2806void wpa_config_debug_dump_networks(struct wpa_config *config)
2807{
2808 int prio;
2809 struct wpa_ssid *ssid;
2810
2811 for (prio = 0; prio < config->num_prio; prio++) {
2812 ssid = config->pssid[prio];
2813 wpa_printf(MSG_DEBUG, "Priority group %d",
2814 ssid->priority);
2815 while (ssid) {
2816 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2817 ssid->id,
2818 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2819 ssid = ssid->pnext;
2820 }
2821 }
2822}
2823#endif /* CONFIG_NO_STDOUT_DEBUG */
2824
2825
2826struct global_parse_data {
2827 char *name;
2828 int (*parser)(const struct global_parse_data *data,
2829 struct wpa_config *config, int line, const char *value);
2830 void *param1, *param2, *param3;
2831 unsigned int changed_flag;
2832};
2833
2834
2835static int wpa_global_config_parse_int(const struct global_parse_data *data,
2836 struct wpa_config *config, int line,
2837 const char *pos)
2838{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002839 int val, *dst;
2840 char *end;
2841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002843 val = strtol(pos, &end, 0);
2844 if (*end) {
2845 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
2846 line, pos);
2847 return -1;
2848 }
2849 *dst = val;
2850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2852
2853 if (data->param2 && *dst < (long) data->param2) {
2854 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2855 "min_value=%ld)", line, data->name, *dst,
2856 (long) data->param2);
2857 *dst = (long) data->param2;
2858 return -1;
2859 }
2860
2861 if (data->param3 && *dst > (long) data->param3) {
2862 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2863 "max_value=%ld)", line, data->name, *dst,
2864 (long) data->param3);
2865 *dst = (long) data->param3;
2866 return -1;
2867 }
2868
2869 return 0;
2870}
2871
2872
2873static int wpa_global_config_parse_str(const struct global_parse_data *data,
2874 struct wpa_config *config, int line,
2875 const char *pos)
2876{
2877 size_t len;
2878 char **dst, *tmp;
2879
2880 len = os_strlen(pos);
2881 if (data->param2 && len < (size_t) data->param2) {
2882 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2883 "min_len=%ld)", line, data->name,
2884 (unsigned long) len, (long) data->param2);
2885 return -1;
2886 }
2887
2888 if (data->param3 && len > (size_t) data->param3) {
2889 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2890 "max_len=%ld)", line, data->name,
2891 (unsigned long) len, (long) data->param3);
2892 return -1;
2893 }
2894
2895 tmp = os_strdup(pos);
2896 if (tmp == NULL)
2897 return -1;
2898
2899 dst = (char **) (((u8 *) config) + (long) data->param1);
2900 os_free(*dst);
2901 *dst = tmp;
2902 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2903
2904 return 0;
2905}
2906
2907
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08002908static int wpa_config_process_bgscan(const struct global_parse_data *data,
2909 struct wpa_config *config, int line,
2910 const char *pos)
2911{
2912 size_t len;
2913 char *tmp;
2914
2915 tmp = wpa_config_parse_string(pos, &len);
2916 if (tmp == NULL) {
2917 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
2918 line, data->name);
2919 return -1;
2920 }
2921
2922 return wpa_global_config_parse_str(data, config, line, tmp);
2923}
2924
2925
Dmitry Shmidt04949592012-07-19 12:16:46 -07002926static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2927 struct wpa_config *config, int line,
2928 const char *pos)
2929{
2930 size_t len;
2931 struct wpabuf **dst, *tmp;
2932
2933 len = os_strlen(pos);
2934 if (len & 0x01)
2935 return -1;
2936
2937 tmp = wpabuf_alloc(len / 2);
2938 if (tmp == NULL)
2939 return -1;
2940
2941 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2942 wpabuf_free(tmp);
2943 return -1;
2944 }
2945
2946 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2947 wpabuf_free(*dst);
2948 *dst = tmp;
2949 wpa_printf(MSG_DEBUG, "%s", data->name);
2950
2951 return 0;
2952}
2953
2954
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002955static int wpa_config_process_freq_list(const struct global_parse_data *data,
2956 struct wpa_config *config, int line,
2957 const char *value)
2958{
2959 int *freqs;
2960
2961 freqs = wpa_config_parse_int_array(value);
2962 if (freqs == NULL)
2963 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07002964 if (freqs[0] == 0) {
2965 os_free(freqs);
2966 freqs = NULL;
2967 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002968 os_free(config->freq_list);
2969 config->freq_list = freqs;
2970 return 0;
2971}
2972
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974static int wpa_config_process_country(const struct global_parse_data *data,
2975 struct wpa_config *config, int line,
2976 const char *pos)
2977{
2978 if (!pos[0] || !pos[1]) {
2979 wpa_printf(MSG_DEBUG, "Invalid country set");
2980 return -1;
2981 }
2982 config->country[0] = pos[0];
2983 config->country[1] = pos[1];
2984 wpa_printf(MSG_DEBUG, "country='%c%c'",
2985 config->country[0], config->country[1]);
2986 return 0;
2987}
2988
2989
2990static int wpa_config_process_load_dynamic_eap(
2991 const struct global_parse_data *data, struct wpa_config *config,
2992 int line, const char *so)
2993{
2994 int ret;
2995 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2996 ret = eap_peer_method_load(so);
2997 if (ret == -2) {
2998 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2999 "reloading.");
3000 } else if (ret) {
3001 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3002 "method '%s'.", line, so);
3003 return -1;
3004 }
3005
3006 return 0;
3007}
3008
3009
3010#ifdef CONFIG_WPS
3011
3012static int wpa_config_process_uuid(const struct global_parse_data *data,
3013 struct wpa_config *config, int line,
3014 const char *pos)
3015{
3016 char buf[40];
3017 if (uuid_str2bin(pos, config->uuid)) {
3018 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3019 return -1;
3020 }
3021 uuid_bin2str(config->uuid, buf, sizeof(buf));
3022 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3023 return 0;
3024}
3025
3026
3027static int wpa_config_process_device_type(
3028 const struct global_parse_data *data,
3029 struct wpa_config *config, int line, const char *pos)
3030{
3031 return wps_dev_type_str2bin(pos, config->device_type);
3032}
3033
3034
3035static int wpa_config_process_os_version(const struct global_parse_data *data,
3036 struct wpa_config *config, int line,
3037 const char *pos)
3038{
3039 if (hexstr2bin(pos, config->os_version, 4)) {
3040 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3041 return -1;
3042 }
3043 wpa_printf(MSG_DEBUG, "os_version=%08x",
3044 WPA_GET_BE32(config->os_version));
3045 return 0;
3046}
3047
Dmitry Shmidt04949592012-07-19 12:16:46 -07003048
3049static int wpa_config_process_wps_vendor_ext_m1(
3050 const struct global_parse_data *data,
3051 struct wpa_config *config, int line, const char *pos)
3052{
3053 struct wpabuf *tmp;
3054 int len = os_strlen(pos) / 2;
3055 u8 *p;
3056
3057 if (!len) {
3058 wpa_printf(MSG_ERROR, "Line %d: "
3059 "invalid wps_vendor_ext_m1", line);
3060 return -1;
3061 }
3062
3063 tmp = wpabuf_alloc(len);
3064 if (tmp) {
3065 p = wpabuf_put(tmp, len);
3066
3067 if (hexstr2bin(pos, p, len)) {
3068 wpa_printf(MSG_ERROR, "Line %d: "
3069 "invalid wps_vendor_ext_m1", line);
3070 wpabuf_free(tmp);
3071 return -1;
3072 }
3073
3074 wpabuf_free(config->wps_vendor_ext_m1);
3075 config->wps_vendor_ext_m1 = tmp;
3076 } else {
3077 wpa_printf(MSG_ERROR, "Can not allocate "
3078 "memory for wps_vendor_ext_m1");
3079 return -1;
3080 }
3081
3082 return 0;
3083}
3084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085#endif /* CONFIG_WPS */
3086
3087#ifdef CONFIG_P2P
3088static int wpa_config_process_sec_device_type(
3089 const struct global_parse_data *data,
3090 struct wpa_config *config, int line, const char *pos)
3091{
3092 int idx;
3093
3094 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3095 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3096 "items", line);
3097 return -1;
3098 }
3099
3100 idx = config->num_sec_device_types;
3101
3102 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3103 return -1;
3104
3105 config->num_sec_device_types++;
3106 return 0;
3107}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003108
3109
3110static int wpa_config_process_p2p_pref_chan(
3111 const struct global_parse_data *data,
3112 struct wpa_config *config, int line, const char *pos)
3113{
3114 struct p2p_channel *pref = NULL, *n;
3115 unsigned int num = 0;
3116 const char *pos2;
3117 u8 op_class, chan;
3118
3119 /* format: class:chan,class:chan,... */
3120
3121 while (*pos) {
3122 op_class = atoi(pos);
3123 pos2 = os_strchr(pos, ':');
3124 if (pos2 == NULL)
3125 goto fail;
3126 pos2++;
3127 chan = atoi(pos2);
3128
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003129 n = os_realloc_array(pref, num + 1,
3130 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003131 if (n == NULL)
3132 goto fail;
3133 pref = n;
3134 pref[num].op_class = op_class;
3135 pref[num].chan = chan;
3136 num++;
3137
3138 pos = os_strchr(pos2, ',');
3139 if (pos == NULL)
3140 break;
3141 pos++;
3142 }
3143
3144 os_free(config->p2p_pref_chan);
3145 config->p2p_pref_chan = pref;
3146 config->num_p2p_pref_chan = num;
3147 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3148 (u8 *) config->p2p_pref_chan,
3149 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3150
3151 return 0;
3152
3153fail:
3154 os_free(pref);
3155 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3156 return -1;
3157}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003158
3159
3160static int wpa_config_process_p2p_no_go_freq(
3161 const struct global_parse_data *data,
3162 struct wpa_config *config, int line, const char *pos)
3163{
3164 int ret;
3165
3166 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3167 if (ret < 0) {
3168 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3169 return -1;
3170 }
3171
3172 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3173 config->p2p_no_go_freq.num);
3174
3175 return 0;
3176}
3177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178#endif /* CONFIG_P2P */
3179
3180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003181static int wpa_config_process_hessid(
3182 const struct global_parse_data *data,
3183 struct wpa_config *config, int line, const char *pos)
3184{
3185 if (hwaddr_aton2(pos, config->hessid) < 0) {
3186 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3187 line, pos);
3188 return -1;
3189 }
3190
3191 return 0;
3192}
3193
3194
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003195static int wpa_config_process_sae_groups(
3196 const struct global_parse_data *data,
3197 struct wpa_config *config, int line, const char *pos)
3198{
3199 int *groups = wpa_config_parse_int_array(pos);
3200 if (groups == NULL) {
3201 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3202 line, pos);
3203 return -1;
3204 }
3205
3206 os_free(config->sae_groups);
3207 config->sae_groups = groups;
3208
3209 return 0;
3210}
3211
3212
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003213static int wpa_config_process_ap_vendor_elements(
3214 const struct global_parse_data *data,
3215 struct wpa_config *config, int line, const char *pos)
3216{
3217 struct wpabuf *tmp;
3218 int len = os_strlen(pos) / 2;
3219 u8 *p;
3220
3221 if (!len) {
3222 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3223 line);
3224 return -1;
3225 }
3226
3227 tmp = wpabuf_alloc(len);
3228 if (tmp) {
3229 p = wpabuf_put(tmp, len);
3230
3231 if (hexstr2bin(pos, p, len)) {
3232 wpa_printf(MSG_ERROR, "Line %d: invalid "
3233 "ap_vendor_elements", line);
3234 wpabuf_free(tmp);
3235 return -1;
3236 }
3237
3238 wpabuf_free(config->ap_vendor_elements);
3239 config->ap_vendor_elements = tmp;
3240 } else {
3241 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3242 "ap_vendor_elements");
3243 return -1;
3244 }
3245
3246 return 0;
3247}
3248
3249
Dmitry Shmidt56052862013-10-04 10:23:25 -07003250#ifdef CONFIG_CTRL_IFACE
3251static int wpa_config_process_no_ctrl_interface(
3252 const struct global_parse_data *data,
3253 struct wpa_config *config, int line, const char *pos)
3254{
3255 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3256 os_free(config->ctrl_interface);
3257 config->ctrl_interface = NULL;
3258 return 0;
3259}
3260#endif /* CONFIG_CTRL_IFACE */
3261
3262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263#ifdef OFFSET
3264#undef OFFSET
3265#endif /* OFFSET */
3266/* OFFSET: Get offset of a variable within the wpa_config structure */
3267#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3268
3269#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3270#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3271#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3272#define INT(f) _INT(f), NULL, NULL
3273#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3274#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3275#define STR(f) _STR(f), NULL, NULL
3276#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07003277#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278
3279static const struct global_parse_data global_fields[] = {
3280#ifdef CONFIG_CTRL_IFACE
3281 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003282 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 { STR(ctrl_interface_group), 0 } /* deprecated */,
3284#endif /* CONFIG_CTRL_IFACE */
3285 { INT_RANGE(eapol_version, 1, 2), 0 },
3286 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003287 { FUNC(bgscan), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003288 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 { INT(fast_reauth), 0 },
3290 { STR(opensc_engine_path), 0 },
3291 { STR(pkcs11_engine_path), 0 },
3292 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003293 { STR(pcsc_reader), 0 },
3294 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07003295 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 { STR(driver_param), 0 },
3297 { INT(dot11RSNAConfigPMKLifetime), 0 },
3298 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3299 { INT(dot11RSNAConfigSATimeout), 0 },
3300#ifndef CONFIG_NO_CONFIG_WRITE
3301 { INT(update_config), 0 },
3302#endif /* CONFIG_NO_CONFIG_WRITE */
3303 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3304#ifdef CONFIG_WPS
3305 { FUNC(uuid), CFG_CHANGED_UUID },
3306 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3307 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3308 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3309 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3310 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3311 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3312 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3313 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3314 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003315 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316#endif /* CONFIG_WPS */
3317#ifdef CONFIG_P2P
3318 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3319 { INT(p2p_listen_reg_class), 0 },
3320 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003321 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3322 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3324 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3325 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3326 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3327 { INT(p2p_group_idle), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003329 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
3330 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003331 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003332 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003333 { INT(p2p_disabled), 0 },
3334 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003335 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003336#endif /* CONFIG_P2P */
3337 { FUNC(country), CFG_CHANGED_COUNTRY },
3338 { INT(bss_max_count), 0 },
3339 { INT(bss_expiration_age), 0 },
3340 { INT(bss_expiration_scan_count), 0 },
3341 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003342 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003345#ifdef CONFIG_HS20
3346 { INT_RANGE(hs20, 0, 1), 0 },
3347#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 { INT_RANGE(interworking, 0, 1), 0 },
3349 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003350 { INT_RANGE(access_network_type, 0, 15), 0 },
3351 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3352 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003353 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3354 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3355 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3356 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3357 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003358 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3359 { INT(p2p_go_max_inactivity), 0 },
3360 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003361 { INT(okc), 0 },
3362 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003363 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003364 { INT(dtim_period), 0 },
3365 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003366 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003367 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003368 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003369 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003370 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003371 { INT(tdls_external_control), 0},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372};
3373
3374#undef FUNC
3375#undef _INT
3376#undef INT
3377#undef INT_RANGE
3378#undef _STR
3379#undef STR
3380#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07003381#undef BIN
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003382#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383
3384
3385int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3386{
3387 size_t i;
3388 int ret = 0;
3389
3390 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3391 const struct global_parse_data *field = &global_fields[i];
3392 size_t flen = os_strlen(field->name);
3393 if (os_strncmp(pos, field->name, flen) != 0 ||
3394 pos[flen] != '=')
3395 continue;
3396
3397 if (field->parser(field, config, line, pos + flen + 1)) {
3398 wpa_printf(MSG_ERROR, "Line %d: failed to "
3399 "parse '%s'.", line, pos);
3400 ret = -1;
3401 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003402 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3403 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404 config->changed_parameters |= field->changed_flag;
3405 break;
3406 }
3407 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003408#ifdef CONFIG_AP
3409 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3410 char *tmp = os_strchr(pos, '=');
3411 if (tmp == NULL) {
3412 if (line < 0)
3413 return -1;
3414 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3415 "'%s'", line, pos);
3416 return -1;
3417 }
3418 *tmp++ = '\0';
3419 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3420 tmp)) {
3421 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3422 "AC item", line);
3423 return -1;
3424 }
3425 }
3426#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 if (line < 0)
3428 return -1;
3429 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3430 line, pos);
3431 ret = -1;
3432 }
3433
3434 return ret;
3435}