blob: 5d7a063cd3031676a3bdb8544544e0815ab7f839 [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"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/sha1.h"
15#include "rsn_supp/wpa.h"
16#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "config.h"
19
20
21#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
22#define NO_CONFIG_WRITE
23#endif
24
25/*
26 * Structure for network configuration parsing. This data is used to implement
27 * a generic parser for each network block variable. The table of configuration
28 * variables is defined below in this file (ssid_fields[]).
29 */
30struct parse_data {
31 /* Configuration variable name */
32 char *name;
33
34 /* Parser function for this variable */
35 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
36 int line, const char *value);
37
38#ifndef NO_CONFIG_WRITE
39 /* Writer function (i.e., to get the variable in text format from
40 * internal presentation). */
41 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
42#endif /* NO_CONFIG_WRITE */
43
44 /* Variable specific parameters for the parser. */
45 void *param1, *param2, *param3, *param4;
46
47 /* 0 = this variable can be included in debug output and ctrl_iface
48 * 1 = this variable contains key/private data and it must not be
49 * included in debug output unless explicitly requested. In
50 * addition, this variable will not be readable through the
51 * ctrl_iface.
52 */
53 int key_data;
54};
55
56
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057static int wpa_config_parse_str(const struct parse_data *data,
58 struct wpa_ssid *ssid,
59 int line, const char *value)
60{
61 size_t res_len, *dst_len;
62 char **dst, *tmp;
63
64 if (os_strcmp(value, "NULL") == 0) {
65 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
66 data->name);
67 tmp = NULL;
68 res_len = 0;
69 goto set;
70 }
71
72 tmp = wpa_config_parse_string(value, &res_len);
73 if (tmp == NULL) {
74 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
75 line, data->name,
76 data->key_data ? "[KEY DATA REMOVED]" : value);
77 return -1;
78 }
79
80 if (data->key_data) {
81 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
82 (u8 *) tmp, res_len);
83 } else {
84 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
85 (u8 *) tmp, res_len);
86 }
87
88 if (data->param3 && res_len < (size_t) data->param3) {
89 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
90 "min_len=%ld)", line, data->name,
91 (unsigned long) res_len, (long) data->param3);
92 os_free(tmp);
93 return -1;
94 }
95
96 if (data->param4 && res_len > (size_t) data->param4) {
97 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
98 "max_len=%ld)", line, data->name,
99 (unsigned long) res_len, (long) data->param4);
100 os_free(tmp);
101 return -1;
102 }
103
104set:
105 dst = (char **) (((u8 *) ssid) + (long) data->param1);
106 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
107 os_free(*dst);
108 *dst = tmp;
109 if (data->param2)
110 *dst_len = res_len;
111
112 return 0;
113}
114
115
116#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
118{
119 char *buf;
120
121 buf = os_malloc(len + 3);
122 if (buf == NULL)
123 return NULL;
124 buf[0] = '"';
125 os_memcpy(buf + 1, value, len);
126 buf[len + 1] = '"';
127 buf[len + 2] = '\0';
128
129 return buf;
130}
131
132
133static char * wpa_config_write_string_hex(const u8 *value, size_t len)
134{
135 char *buf;
136
137 buf = os_zalloc(2 * len + 1);
138 if (buf == NULL)
139 return NULL;
140 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
141
142 return buf;
143}
144
145
146static char * wpa_config_write_string(const u8 *value, size_t len)
147{
148 if (value == NULL)
149 return NULL;
150
151 if (is_hex(value, len))
152 return wpa_config_write_string_hex(value, len);
153 else
154 return wpa_config_write_string_ascii(value, len);
155}
156
157
158static char * wpa_config_write_str(const struct parse_data *data,
159 struct wpa_ssid *ssid)
160{
161 size_t len;
162 char **src;
163
164 src = (char **) (((u8 *) ssid) + (long) data->param1);
165 if (*src == NULL)
166 return NULL;
167
168 if (data->param2)
169 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
170 else
171 len = os_strlen(*src);
172
173 return wpa_config_write_string((const u8 *) *src, len);
174}
175#endif /* NO_CONFIG_WRITE */
176
177
178static int wpa_config_parse_int(const struct parse_data *data,
179 struct wpa_ssid *ssid,
180 int line, const char *value)
181{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700182 int val, *dst;
183 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
185 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700186 val = strtol(value, &end, 0);
187 if (*end) {
188 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
189 line, value);
190 return -1;
191 }
192 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
194
195 if (data->param3 && *dst < (long) data->param3) {
196 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
197 "min_value=%ld)", line, data->name, *dst,
198 (long) data->param3);
199 *dst = (long) data->param3;
200 return -1;
201 }
202
203 if (data->param4 && *dst > (long) data->param4) {
204 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
205 "max_value=%ld)", line, data->name, *dst,
206 (long) data->param4);
207 *dst = (long) data->param4;
208 return -1;
209 }
210
211 return 0;
212}
213
214
215#ifndef NO_CONFIG_WRITE
216static char * wpa_config_write_int(const struct parse_data *data,
217 struct wpa_ssid *ssid)
218{
219 int *src, res;
220 char *value;
221
222 src = (int *) (((u8 *) ssid) + (long) data->param1);
223
224 value = os_malloc(20);
225 if (value == NULL)
226 return NULL;
227 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 os_free(value);
230 return NULL;
231 }
232 value[20 - 1] = '\0';
233 return value;
234}
235#endif /* NO_CONFIG_WRITE */
236
237
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800238static int wpa_config_parse_addr_list(const struct parse_data *data,
239 int line, const char *value,
240 u8 **list, size_t *num, char *name,
241 u8 abort_on_error, u8 masked)
242{
243 const char *pos;
244 u8 *buf, *n, addr[2 * ETH_ALEN];
245 size_t count;
246
247 buf = NULL;
248 count = 0;
249
250 pos = value;
251 while (pos && *pos) {
252 while (*pos == ' ')
253 pos++;
254
255 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
256 if (abort_on_error || count == 0) {
257 wpa_printf(MSG_ERROR,
258 "Line %d: Invalid %s address '%s'",
259 line, name, value);
260 os_free(buf);
261 return -1;
262 }
263 /* continue anyway since this could have been from a
264 * truncated configuration file line */
265 wpa_printf(MSG_INFO,
266 "Line %d: Ignore likely truncated %s address '%s'",
267 line, name, pos);
268 } else {
269 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
270 if (n == NULL) {
271 os_free(buf);
272 return -1;
273 }
274 buf = n;
275 os_memmove(buf + 2 * ETH_ALEN, buf,
276 count * 2 * ETH_ALEN);
277 os_memcpy(buf, addr, 2 * ETH_ALEN);
278 count++;
279 wpa_printf(MSG_MSGDUMP,
280 "%s: addr=" MACSTR " mask=" MACSTR,
281 name, MAC2STR(addr),
282 MAC2STR(&addr[ETH_ALEN]));
283 }
284
285 pos = os_strchr(pos, ' ');
286 }
287
288 os_free(*list);
289 *list = buf;
290 *num = count;
291
292 return 0;
293}
294
295
296#ifndef NO_CONFIG_WRITE
297static char * wpa_config_write_addr_list(const struct parse_data *data,
298 const u8 *list, size_t num, char *name)
299{
300 char *value, *end, *pos;
301 int res;
302 size_t i;
303
304 if (list == NULL || num == 0)
305 return NULL;
306
307 value = os_malloc(2 * 20 * num);
308 if (value == NULL)
309 return NULL;
310 pos = value;
311 end = value + 2 * 20 * num;
312
313 for (i = num; i > 0; i--) {
314 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
315 const u8 *m = a + ETH_ALEN;
316
317 if (i < num)
318 *pos++ = ' ';
319 res = hwaddr_mask_txt(pos, end - pos, a, m);
320 if (res < 0) {
321 os_free(value);
322 return NULL;
323 }
324 pos += res;
325 }
326
327 return value;
328}
329#endif /* NO_CONFIG_WRITE */
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331static int wpa_config_parse_bssid(const struct parse_data *data,
332 struct wpa_ssid *ssid, int line,
333 const char *value)
334{
335 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
336 os_strcmp(value, "any") == 0) {
337 ssid->bssid_set = 0;
338 wpa_printf(MSG_MSGDUMP, "BSSID any");
339 return 0;
340 }
341 if (hwaddr_aton(value, ssid->bssid)) {
342 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
343 line, value);
344 return -1;
345 }
346 ssid->bssid_set = 1;
347 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
348 return 0;
349}
350
351
352#ifndef NO_CONFIG_WRITE
353static char * wpa_config_write_bssid(const struct parse_data *data,
354 struct wpa_ssid *ssid)
355{
356 char *value;
357 int res;
358
359 if (!ssid->bssid_set)
360 return NULL;
361
362 value = os_malloc(20);
363 if (value == NULL)
364 return NULL;
365 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800366 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 os_free(value);
368 return NULL;
369 }
370 value[20 - 1] = '\0';
371 return value;
372}
373#endif /* NO_CONFIG_WRITE */
374
375
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800376static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
377 struct wpa_ssid *ssid, int line,
378 const char *value)
379{
380 return wpa_config_parse_addr_list(data, line, value,
381 &ssid->bssid_blacklist,
382 &ssid->num_bssid_blacklist,
383 "bssid_blacklist", 1, 1);
384}
385
386
387#ifndef NO_CONFIG_WRITE
388static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
389 struct wpa_ssid *ssid)
390{
391 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
392 ssid->num_bssid_blacklist,
393 "bssid_blacklist");
394}
395#endif /* NO_CONFIG_WRITE */
396
397
398static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
399 struct wpa_ssid *ssid, int line,
400 const char *value)
401{
402 return wpa_config_parse_addr_list(data, line, value,
403 &ssid->bssid_whitelist,
404 &ssid->num_bssid_whitelist,
405 "bssid_whitelist", 1, 1);
406}
407
408
409#ifndef NO_CONFIG_WRITE
410static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
411 struct wpa_ssid *ssid)
412{
413 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
414 ssid->num_bssid_whitelist,
415 "bssid_whitelist");
416}
417#endif /* NO_CONFIG_WRITE */
418
419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420static int wpa_config_parse_psk(const struct parse_data *data,
421 struct wpa_ssid *ssid, int line,
422 const char *value)
423{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424#ifdef CONFIG_EXT_PASSWORD
425 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700426 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427 ssid->passphrase = NULL;
428 ssid->psk_set = 0;
429 os_free(ssid->ext_psk);
430 ssid->ext_psk = os_strdup(value + 4);
431 if (ssid->ext_psk == NULL)
432 return -1;
433 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
434 ssid->ext_psk);
435 return 0;
436 }
437#endif /* CONFIG_EXT_PASSWORD */
438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (*value == '"') {
440#ifndef CONFIG_NO_PBKDF2
441 const char *pos;
442 size_t len;
443
444 value++;
445 pos = os_strrchr(value, '"');
446 if (pos)
447 len = pos - value;
448 else
449 len = os_strlen(value);
450 if (len < 8 || len > 63) {
451 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
452 "length %lu (expected: 8..63) '%s'.",
453 line, (unsigned long) len, value);
454 return -1;
455 }
456 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
457 (u8 *) value, len);
458 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
459 os_memcmp(ssid->passphrase, value, len) == 0)
460 return 0;
461 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700462 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700463 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 if (ssid->passphrase == NULL)
465 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 return 0;
467#else /* CONFIG_NO_PBKDF2 */
468 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
469 "supported.", line);
470 return -1;
471#endif /* CONFIG_NO_PBKDF2 */
472 }
473
474 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
475 value[PMK_LEN * 2] != '\0') {
476 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
477 line, value);
478 return -1;
479 }
480
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700481 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 ssid->passphrase = NULL;
483
484 ssid->psk_set = 1;
485 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
486 return 0;
487}
488
489
490#ifndef NO_CONFIG_WRITE
491static char * wpa_config_write_psk(const struct parse_data *data,
492 struct wpa_ssid *ssid)
493{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700494#ifdef CONFIG_EXT_PASSWORD
495 if (ssid->ext_psk) {
496 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
497 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800498 int res;
499
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700500 if (buf == NULL)
501 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800502 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
503 if (os_snprintf_error(len, res)) {
504 os_free(buf);
505 buf = NULL;
506 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700507 return buf;
508 }
509#endif /* CONFIG_EXT_PASSWORD */
510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 if (ssid->passphrase)
512 return wpa_config_write_string_ascii(
513 (const u8 *) ssid->passphrase,
514 os_strlen(ssid->passphrase));
515
516 if (ssid->psk_set)
517 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
518
519 return NULL;
520}
521#endif /* NO_CONFIG_WRITE */
522
523
524static int wpa_config_parse_proto(const struct parse_data *data,
525 struct wpa_ssid *ssid, int line,
526 const char *value)
527{
528 int val = 0, last, errors = 0;
529 char *start, *end, *buf;
530
531 buf = os_strdup(value);
532 if (buf == NULL)
533 return -1;
534 start = buf;
535
536 while (*start != '\0') {
537 while (*start == ' ' || *start == '\t')
538 start++;
539 if (*start == '\0')
540 break;
541 end = start;
542 while (*end != ' ' && *end != '\t' && *end != '\0')
543 end++;
544 last = *end == '\0';
545 *end = '\0';
546 if (os_strcmp(start, "WPA") == 0)
547 val |= WPA_PROTO_WPA;
548 else if (os_strcmp(start, "RSN") == 0 ||
549 os_strcmp(start, "WPA2") == 0)
550 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800551 else if (os_strcmp(start, "OSEN") == 0)
552 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 else {
554 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
555 line, start);
556 errors++;
557 }
558
559 if (last)
560 break;
561 start = end + 1;
562 }
563 os_free(buf);
564
565 if (val == 0) {
566 wpa_printf(MSG_ERROR,
567 "Line %d: no proto values configured.", line);
568 errors++;
569 }
570
571 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
572 ssid->proto = val;
573 return errors ? -1 : 0;
574}
575
576
577#ifndef NO_CONFIG_WRITE
578static char * wpa_config_write_proto(const struct parse_data *data,
579 struct wpa_ssid *ssid)
580{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700581 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 char *buf, *pos, *end;
583
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800584 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 if (buf == NULL)
586 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800587 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588
589 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700590 ret = os_snprintf(pos, end - pos, "%sWPA",
591 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800592 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 return buf;
594 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 }
596
597 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700598 ret = os_snprintf(pos, end - pos, "%sRSN",
599 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800600 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 return buf;
602 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 }
604
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800605 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700606 ret = os_snprintf(pos, end - pos, "%sOSEN",
607 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800609 return buf;
610 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700611 }
612
613 if (pos == buf) {
614 os_free(buf);
615 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800616 }
617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 return buf;
619}
620#endif /* NO_CONFIG_WRITE */
621
622
623static int wpa_config_parse_key_mgmt(const struct parse_data *data,
624 struct wpa_ssid *ssid, int line,
625 const char *value)
626{
627 int val = 0, last, errors = 0;
628 char *start, *end, *buf;
629
630 buf = os_strdup(value);
631 if (buf == NULL)
632 return -1;
633 start = buf;
634
635 while (*start != '\0') {
636 while (*start == ' ' || *start == '\t')
637 start++;
638 if (*start == '\0')
639 break;
640 end = start;
641 while (*end != ' ' && *end != '\t' && *end != '\0')
642 end++;
643 last = *end == '\0';
644 *end = '\0';
645 if (os_strcmp(start, "WPA-PSK") == 0)
646 val |= WPA_KEY_MGMT_PSK;
647 else if (os_strcmp(start, "WPA-EAP") == 0)
648 val |= WPA_KEY_MGMT_IEEE8021X;
649 else if (os_strcmp(start, "IEEE8021X") == 0)
650 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
651 else if (os_strcmp(start, "NONE") == 0)
652 val |= WPA_KEY_MGMT_NONE;
653 else if (os_strcmp(start, "WPA-NONE") == 0)
654 val |= WPA_KEY_MGMT_WPA_NONE;
655#ifdef CONFIG_IEEE80211R
656 else if (os_strcmp(start, "FT-PSK") == 0)
657 val |= WPA_KEY_MGMT_FT_PSK;
658 else if (os_strcmp(start, "FT-EAP") == 0)
659 val |= WPA_KEY_MGMT_FT_IEEE8021X;
660#endif /* CONFIG_IEEE80211R */
661#ifdef CONFIG_IEEE80211W
662 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
663 val |= WPA_KEY_MGMT_PSK_SHA256;
664 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
665 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
666#endif /* CONFIG_IEEE80211W */
667#ifdef CONFIG_WPS
668 else if (os_strcmp(start, "WPS") == 0)
669 val |= WPA_KEY_MGMT_WPS;
670#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800671#ifdef CONFIG_SAE
672 else if (os_strcmp(start, "SAE") == 0)
673 val |= WPA_KEY_MGMT_SAE;
674 else if (os_strcmp(start, "FT-SAE") == 0)
675 val |= WPA_KEY_MGMT_FT_SAE;
676#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800677#ifdef CONFIG_HS20
678 else if (os_strcmp(start, "OSEN") == 0)
679 val |= WPA_KEY_MGMT_OSEN;
680#endif /* CONFIG_HS20 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800681 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
682 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 else {
684 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
685 line, start);
686 errors++;
687 }
688
689 if (last)
690 break;
691 start = end + 1;
692 }
693 os_free(buf);
694
695 if (val == 0) {
696 wpa_printf(MSG_ERROR,
697 "Line %d: no key_mgmt values configured.", line);
698 errors++;
699 }
700
701 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
702 ssid->key_mgmt = val;
703 return errors ? -1 : 0;
704}
705
706
707#ifndef NO_CONFIG_WRITE
708static char * wpa_config_write_key_mgmt(const struct parse_data *data,
709 struct wpa_ssid *ssid)
710{
711 char *buf, *pos, *end;
712 int ret;
713
Dmitry Shmidt96571392013-10-14 12:54:46 -0700714 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 if (buf == NULL)
716 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700717 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718
719 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
720 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
721 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800722 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 end[-1] = '\0';
724 return buf;
725 }
726 pos += ret;
727 }
728
729 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
730 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
731 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800732 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 end[-1] = '\0';
734 return buf;
735 }
736 pos += ret;
737 }
738
739 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
740 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
741 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800742 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 end[-1] = '\0';
744 return buf;
745 }
746 pos += ret;
747 }
748
749 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
750 ret = os_snprintf(pos, end - pos, "%sNONE",
751 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800752 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 end[-1] = '\0';
754 return buf;
755 }
756 pos += ret;
757 }
758
759 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
760 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
761 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800762 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 end[-1] = '\0';
764 return buf;
765 }
766 pos += ret;
767 }
768
769#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700770 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
771 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
772 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800773 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700774 end[-1] = '\0';
775 return buf;
776 }
777 pos += ret;
778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779
Dmitry Shmidt96571392013-10-14 12:54:46 -0700780 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
781 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
782 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800783 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700784 end[-1] = '\0';
785 return buf;
786 }
787 pos += ret;
788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789#endif /* CONFIG_IEEE80211R */
790
791#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700792 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
793 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
794 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800795 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700796 end[-1] = '\0';
797 return buf;
798 }
799 pos += ret;
800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
Dmitry Shmidt96571392013-10-14 12:54:46 -0700802 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
803 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
804 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800805 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700806 end[-1] = '\0';
807 return buf;
808 }
809 pos += ret;
810 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811#endif /* CONFIG_IEEE80211W */
812
813#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700814 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
815 ret = os_snprintf(pos, end - pos, "%sWPS",
816 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800817 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700818 end[-1] = '\0';
819 return buf;
820 }
821 pos += ret;
822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823#endif /* CONFIG_WPS */
824
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800825#ifdef CONFIG_SAE
826 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
827 ret = os_snprintf(pos, end - pos, "%sSAE",
828 pos == buf ? "" : " ");
829 if (os_snprintf_error(end - pos, ret)) {
830 end[-1] = '\0';
831 return buf;
832 }
833 pos += ret;
834 }
835
836 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
837 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
838 pos == buf ? "" : " ");
839 if (os_snprintf_error(end - pos, ret)) {
840 end[-1] = '\0';
841 return buf;
842 }
843 pos += ret;
844 }
845#endif /* CONFIG_SAE */
846
847#ifdef CONFIG_HS20
848 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
849 ret = os_snprintf(pos, end - pos, "%sOSEN",
850 pos == buf ? "" : " ");
851 if (os_snprintf_error(end - pos, ret)) {
852 end[-1] = '\0';
853 return buf;
854 }
855 pos += ret;
856 }
857#endif /* CONFIG_HS20 */
858
859 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
860 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
861 pos == buf ? "" : " ");
862 if (os_snprintf_error(end - pos, ret)) {
863 end[-1] = '\0';
864 return buf;
865 }
866 pos += ret;
867 }
868
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700869 if (pos == buf) {
870 os_free(buf);
871 buf = NULL;
872 }
873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 return buf;
875}
876#endif /* NO_CONFIG_WRITE */
877
878
879static int wpa_config_parse_cipher(int line, const char *value)
880{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800881 int val = wpa_parse_cipher(value);
882 if (val < 0) {
883 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
884 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (val == 0) {
888 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
889 line);
890 return -1;
891 }
892 return val;
893}
894
895
896#ifndef NO_CONFIG_WRITE
897static char * wpa_config_write_cipher(int cipher)
898{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800899 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 if (buf == NULL)
901 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800903 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
904 os_free(buf);
905 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
907
908 return buf;
909}
910#endif /* NO_CONFIG_WRITE */
911
912
913static int wpa_config_parse_pairwise(const struct parse_data *data,
914 struct wpa_ssid *ssid, int line,
915 const char *value)
916{
917 int val;
918 val = wpa_config_parse_cipher(line, value);
919 if (val == -1)
920 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800921 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
923 "(0x%x).", line, val);
924 return -1;
925 }
926
927 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
928 ssid->pairwise_cipher = val;
929 return 0;
930}
931
932
933#ifndef NO_CONFIG_WRITE
934static char * wpa_config_write_pairwise(const struct parse_data *data,
935 struct wpa_ssid *ssid)
936{
937 return wpa_config_write_cipher(ssid->pairwise_cipher);
938}
939#endif /* NO_CONFIG_WRITE */
940
941
942static int wpa_config_parse_group(const struct parse_data *data,
943 struct wpa_ssid *ssid, int line,
944 const char *value)
945{
946 int val;
947 val = wpa_config_parse_cipher(line, value);
948 if (val == -1)
949 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800950 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
952 "(0x%x).", line, val);
953 return -1;
954 }
955
956 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
957 ssid->group_cipher = val;
958 return 0;
959}
960
961
962#ifndef NO_CONFIG_WRITE
963static char * wpa_config_write_group(const struct parse_data *data,
964 struct wpa_ssid *ssid)
965{
966 return wpa_config_write_cipher(ssid->group_cipher);
967}
968#endif /* NO_CONFIG_WRITE */
969
970
971static int wpa_config_parse_auth_alg(const struct parse_data *data,
972 struct wpa_ssid *ssid, int line,
973 const char *value)
974{
975 int val = 0, last, errors = 0;
976 char *start, *end, *buf;
977
978 buf = os_strdup(value);
979 if (buf == NULL)
980 return -1;
981 start = buf;
982
983 while (*start != '\0') {
984 while (*start == ' ' || *start == '\t')
985 start++;
986 if (*start == '\0')
987 break;
988 end = start;
989 while (*end != ' ' && *end != '\t' && *end != '\0')
990 end++;
991 last = *end == '\0';
992 *end = '\0';
993 if (os_strcmp(start, "OPEN") == 0)
994 val |= WPA_AUTH_ALG_OPEN;
995 else if (os_strcmp(start, "SHARED") == 0)
996 val |= WPA_AUTH_ALG_SHARED;
997 else if (os_strcmp(start, "LEAP") == 0)
998 val |= WPA_AUTH_ALG_LEAP;
999 else {
1000 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1001 line, start);
1002 errors++;
1003 }
1004
1005 if (last)
1006 break;
1007 start = end + 1;
1008 }
1009 os_free(buf);
1010
1011 if (val == 0) {
1012 wpa_printf(MSG_ERROR,
1013 "Line %d: no auth_alg values configured.", line);
1014 errors++;
1015 }
1016
1017 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1018 ssid->auth_alg = val;
1019 return errors ? -1 : 0;
1020}
1021
1022
1023#ifndef NO_CONFIG_WRITE
1024static char * wpa_config_write_auth_alg(const struct parse_data *data,
1025 struct wpa_ssid *ssid)
1026{
1027 char *buf, *pos, *end;
1028 int ret;
1029
1030 pos = buf = os_zalloc(30);
1031 if (buf == NULL)
1032 return NULL;
1033 end = buf + 30;
1034
1035 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1036 ret = os_snprintf(pos, end - pos, "%sOPEN",
1037 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001038 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 end[-1] = '\0';
1040 return buf;
1041 }
1042 pos += ret;
1043 }
1044
1045 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1046 ret = os_snprintf(pos, end - pos, "%sSHARED",
1047 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001048 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 end[-1] = '\0';
1050 return buf;
1051 }
1052 pos += ret;
1053 }
1054
1055 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1056 ret = os_snprintf(pos, end - pos, "%sLEAP",
1057 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001058 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 end[-1] = '\0';
1060 return buf;
1061 }
1062 pos += ret;
1063 }
1064
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001065 if (pos == buf) {
1066 os_free(buf);
1067 buf = NULL;
1068 }
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return buf;
1071}
1072#endif /* NO_CONFIG_WRITE */
1073
1074
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001075static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076{
1077 int *freqs;
1078 size_t used, len;
1079 const char *pos;
1080
1081 used = 0;
1082 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001083 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 if (freqs == NULL)
1085 return NULL;
1086
1087 pos = value;
1088 while (pos) {
1089 while (*pos == ' ')
1090 pos++;
1091 if (used == len) {
1092 int *n;
1093 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001094 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 if (n == NULL) {
1096 os_free(freqs);
1097 return NULL;
1098 }
1099 for (i = len; i <= len * 2; i++)
1100 n[i] = 0;
1101 freqs = n;
1102 len *= 2;
1103 }
1104
1105 freqs[used] = atoi(pos);
1106 if (freqs[used] == 0)
1107 break;
1108 used++;
1109 pos = os_strchr(pos + 1, ' ');
1110 }
1111
1112 return freqs;
1113}
1114
1115
1116static int wpa_config_parse_scan_freq(const struct parse_data *data,
1117 struct wpa_ssid *ssid, int line,
1118 const char *value)
1119{
1120 int *freqs;
1121
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001122 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (freqs == NULL)
1124 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001125 if (freqs[0] == 0) {
1126 os_free(freqs);
1127 freqs = NULL;
1128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 os_free(ssid->scan_freq);
1130 ssid->scan_freq = freqs;
1131
1132 return 0;
1133}
1134
1135
1136static int wpa_config_parse_freq_list(const struct parse_data *data,
1137 struct wpa_ssid *ssid, int line,
1138 const char *value)
1139{
1140 int *freqs;
1141
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001142 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 if (freqs == NULL)
1144 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001145 if (freqs[0] == 0) {
1146 os_free(freqs);
1147 freqs = NULL;
1148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 os_free(ssid->freq_list);
1150 ssid->freq_list = freqs;
1151
1152 return 0;
1153}
1154
1155
1156#ifndef NO_CONFIG_WRITE
1157static char * wpa_config_write_freqs(const struct parse_data *data,
1158 const int *freqs)
1159{
1160 char *buf, *pos, *end;
1161 int i, ret;
1162 size_t count;
1163
1164 if (freqs == NULL)
1165 return NULL;
1166
1167 count = 0;
1168 for (i = 0; freqs[i]; i++)
1169 count++;
1170
1171 pos = buf = os_zalloc(10 * count + 1);
1172 if (buf == NULL)
1173 return NULL;
1174 end = buf + 10 * count + 1;
1175
1176 for (i = 0; freqs[i]; i++) {
1177 ret = os_snprintf(pos, end - pos, "%s%u",
1178 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001179 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 end[-1] = '\0';
1181 return buf;
1182 }
1183 pos += ret;
1184 }
1185
1186 return buf;
1187}
1188
1189
1190static char * wpa_config_write_scan_freq(const struct parse_data *data,
1191 struct wpa_ssid *ssid)
1192{
1193 return wpa_config_write_freqs(data, ssid->scan_freq);
1194}
1195
1196
1197static char * wpa_config_write_freq_list(const struct parse_data *data,
1198 struct wpa_ssid *ssid)
1199{
1200 return wpa_config_write_freqs(data, ssid->freq_list);
1201}
1202#endif /* NO_CONFIG_WRITE */
1203
1204
1205#ifdef IEEE8021X_EAPOL
1206static int wpa_config_parse_eap(const struct parse_data *data,
1207 struct wpa_ssid *ssid, int line,
1208 const char *value)
1209{
1210 int last, errors = 0;
1211 char *start, *end, *buf;
1212 struct eap_method_type *methods = NULL, *tmp;
1213 size_t num_methods = 0;
1214
1215 buf = os_strdup(value);
1216 if (buf == NULL)
1217 return -1;
1218 start = buf;
1219
1220 while (*start != '\0') {
1221 while (*start == ' ' || *start == '\t')
1222 start++;
1223 if (*start == '\0')
1224 break;
1225 end = start;
1226 while (*end != ' ' && *end != '\t' && *end != '\0')
1227 end++;
1228 last = *end == '\0';
1229 *end = '\0';
1230 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001231 methods = os_realloc_array(methods, num_methods + 1,
1232 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 if (methods == NULL) {
1234 os_free(tmp);
1235 os_free(buf);
1236 return -1;
1237 }
1238 methods[num_methods].method = eap_peer_get_type(
1239 start, &methods[num_methods].vendor);
1240 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1241 methods[num_methods].method == EAP_TYPE_NONE) {
1242 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1243 "'%s'", line, start);
1244 wpa_printf(MSG_ERROR, "You may need to add support for"
1245 " this EAP method during wpa_supplicant\n"
1246 "build time configuration.\n"
1247 "See README for more information.");
1248 errors++;
1249 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1250 methods[num_methods].method == EAP_TYPE_LEAP)
1251 ssid->leap++;
1252 else
1253 ssid->non_leap++;
1254 num_methods++;
1255 if (last)
1256 break;
1257 start = end + 1;
1258 }
1259 os_free(buf);
1260
1261 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001262 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 if (methods == NULL) {
1264 os_free(tmp);
1265 return -1;
1266 }
1267 methods[num_methods].vendor = EAP_VENDOR_IETF;
1268 methods[num_methods].method = EAP_TYPE_NONE;
1269 num_methods++;
1270
1271 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1272 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001273 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 ssid->eap.eap_methods = methods;
1275 return errors ? -1 : 0;
1276}
1277
1278
1279static char * wpa_config_write_eap(const struct parse_data *data,
1280 struct wpa_ssid *ssid)
1281{
1282 int i, ret;
1283 char *buf, *pos, *end;
1284 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1285 const char *name;
1286
1287 if (eap_methods == NULL)
1288 return NULL;
1289
1290 pos = buf = os_zalloc(100);
1291 if (buf == NULL)
1292 return NULL;
1293 end = buf + 100;
1294
1295 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1296 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1297 name = eap_get_name(eap_methods[i].vendor,
1298 eap_methods[i].method);
1299 if (name) {
1300 ret = os_snprintf(pos, end - pos, "%s%s",
1301 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001302 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 break;
1304 pos += ret;
1305 }
1306 }
1307
1308 end[-1] = '\0';
1309
1310 return buf;
1311}
1312
1313
1314static int wpa_config_parse_password(const struct parse_data *data,
1315 struct wpa_ssid *ssid, int line,
1316 const char *value)
1317{
1318 u8 *hash;
1319
1320 if (os_strcmp(value, "NULL") == 0) {
1321 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001322 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 ssid->eap.password = NULL;
1324 ssid->eap.password_len = 0;
1325 return 0;
1326 }
1327
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328#ifdef CONFIG_EXT_PASSWORD
1329 if (os_strncmp(value, "ext:", 4) == 0) {
1330 char *name = os_strdup(value + 4);
1331 if (name == NULL)
1332 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001333 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001334 ssid->eap.password = (u8 *) name;
1335 ssid->eap.password_len = os_strlen(name);
1336 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1337 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1338 return 0;
1339 }
1340#endif /* CONFIG_EXT_PASSWORD */
1341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 if (os_strncmp(value, "hash:", 5) != 0) {
1343 char *tmp;
1344 size_t res_len;
1345
1346 tmp = wpa_config_parse_string(value, &res_len);
1347 if (tmp == NULL) {
1348 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1349 "password.", line);
1350 return -1;
1351 }
1352 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1353 (u8 *) tmp, res_len);
1354
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001355 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 ssid->eap.password = (u8 *) tmp;
1357 ssid->eap.password_len = res_len;
1358 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001359 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360
1361 return 0;
1362 }
1363
1364
1365 /* NtPasswordHash: hash:<32 hex digits> */
1366 if (os_strlen(value + 5) != 2 * 16) {
1367 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1368 "(expected 32 hex digits)", line);
1369 return -1;
1370 }
1371
1372 hash = os_malloc(16);
1373 if (hash == NULL)
1374 return -1;
1375
1376 if (hexstr2bin(value + 5, hash, 16)) {
1377 os_free(hash);
1378 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1379 return -1;
1380 }
1381
1382 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1383
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001384 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 ssid->eap.password = hash;
1386 ssid->eap.password_len = 16;
1387 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001388 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389
1390 return 0;
1391}
1392
1393
1394static char * wpa_config_write_password(const struct parse_data *data,
1395 struct wpa_ssid *ssid)
1396{
1397 char *buf;
1398
1399 if (ssid->eap.password == NULL)
1400 return NULL;
1401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402#ifdef CONFIG_EXT_PASSWORD
1403 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1404 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1405 if (buf == NULL)
1406 return NULL;
1407 os_memcpy(buf, "ext:", 4);
1408 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1409 return buf;
1410 }
1411#endif /* CONFIG_EXT_PASSWORD */
1412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1414 return wpa_config_write_string(
1415 ssid->eap.password, ssid->eap.password_len);
1416 }
1417
1418 buf = os_malloc(5 + 32 + 1);
1419 if (buf == NULL)
1420 return NULL;
1421
1422 os_memcpy(buf, "hash:", 5);
1423 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1424
1425 return buf;
1426}
1427#endif /* IEEE8021X_EAPOL */
1428
1429
1430static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1431 const char *value, int idx)
1432{
1433 char *buf, title[20];
1434 int res;
1435
1436 buf = wpa_config_parse_string(value, len);
1437 if (buf == NULL) {
1438 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1439 line, idx, value);
1440 return -1;
1441 }
1442 if (*len > MAX_WEP_KEY_LEN) {
1443 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1444 line, idx, value);
1445 os_free(buf);
1446 return -1;
1447 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001448 if (*len && *len != 5 && *len != 13 && *len != 16) {
1449 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1450 "this network block will be ignored",
1451 line, (unsigned int) *len);
1452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001454 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001456 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1458 return 0;
1459}
1460
1461
1462static int wpa_config_parse_wep_key0(const struct parse_data *data,
1463 struct wpa_ssid *ssid, int line,
1464 const char *value)
1465{
1466 return wpa_config_parse_wep_key(ssid->wep_key[0],
1467 &ssid->wep_key_len[0], line,
1468 value, 0);
1469}
1470
1471
1472static int wpa_config_parse_wep_key1(const struct parse_data *data,
1473 struct wpa_ssid *ssid, int line,
1474 const char *value)
1475{
1476 return wpa_config_parse_wep_key(ssid->wep_key[1],
1477 &ssid->wep_key_len[1], line,
1478 value, 1);
1479}
1480
1481
1482static int wpa_config_parse_wep_key2(const struct parse_data *data,
1483 struct wpa_ssid *ssid, int line,
1484 const char *value)
1485{
1486 return wpa_config_parse_wep_key(ssid->wep_key[2],
1487 &ssid->wep_key_len[2], line,
1488 value, 2);
1489}
1490
1491
1492static int wpa_config_parse_wep_key3(const struct parse_data *data,
1493 struct wpa_ssid *ssid, int line,
1494 const char *value)
1495{
1496 return wpa_config_parse_wep_key(ssid->wep_key[3],
1497 &ssid->wep_key_len[3], line,
1498 value, 3);
1499}
1500
1501
1502#ifndef NO_CONFIG_WRITE
1503static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1504{
1505 if (ssid->wep_key_len[idx] == 0)
1506 return NULL;
1507 return wpa_config_write_string(ssid->wep_key[idx],
1508 ssid->wep_key_len[idx]);
1509}
1510
1511
1512static char * wpa_config_write_wep_key0(const struct parse_data *data,
1513 struct wpa_ssid *ssid)
1514{
1515 return wpa_config_write_wep_key(ssid, 0);
1516}
1517
1518
1519static char * wpa_config_write_wep_key1(const struct parse_data *data,
1520 struct wpa_ssid *ssid)
1521{
1522 return wpa_config_write_wep_key(ssid, 1);
1523}
1524
1525
1526static char * wpa_config_write_wep_key2(const struct parse_data *data,
1527 struct wpa_ssid *ssid)
1528{
1529 return wpa_config_write_wep_key(ssid, 2);
1530}
1531
1532
1533static char * wpa_config_write_wep_key3(const struct parse_data *data,
1534 struct wpa_ssid *ssid)
1535{
1536 return wpa_config_write_wep_key(ssid, 3);
1537}
1538#endif /* NO_CONFIG_WRITE */
1539
1540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001541#ifdef CONFIG_P2P
1542
Dmitry Shmidt54605472013-11-08 11:10:19 -08001543static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1544 struct wpa_ssid *ssid, int line,
1545 const char *value)
1546{
1547 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1548 os_strcmp(value, "any") == 0) {
1549 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1550 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1551 return 0;
1552 }
1553 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1554 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1555 line, value);
1556 return -1;
1557 }
1558 ssid->bssid_set = 1;
1559 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1560 MAC2STR(ssid->go_p2p_dev_addr));
1561 return 0;
1562}
1563
1564
1565#ifndef NO_CONFIG_WRITE
1566static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1567 struct wpa_ssid *ssid)
1568{
1569 char *value;
1570 int res;
1571
1572 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1573 return NULL;
1574
1575 value = os_malloc(20);
1576 if (value == NULL)
1577 return NULL;
1578 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001579 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001580 os_free(value);
1581 return NULL;
1582 }
1583 value[20 - 1] = '\0';
1584 return value;
1585}
1586#endif /* NO_CONFIG_WRITE */
1587
1588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1590 struct wpa_ssid *ssid, int line,
1591 const char *value)
1592{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001593 return wpa_config_parse_addr_list(data, line, value,
1594 &ssid->p2p_client_list,
1595 &ssid->num_p2p_clients,
1596 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597}
1598
1599
1600#ifndef NO_CONFIG_WRITE
1601static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1602 struct wpa_ssid *ssid)
1603{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001604 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1605 ssid->num_p2p_clients,
1606 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001607}
1608#endif /* NO_CONFIG_WRITE */
1609
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001610
1611static int wpa_config_parse_psk_list(const struct parse_data *data,
1612 struct wpa_ssid *ssid, int line,
1613 const char *value)
1614{
1615 struct psk_list_entry *p;
1616 const char *pos;
1617
1618 p = os_zalloc(sizeof(*p));
1619 if (p == NULL)
1620 return -1;
1621
1622 pos = value;
1623 if (os_strncmp(pos, "P2P-", 4) == 0) {
1624 p->p2p = 1;
1625 pos += 4;
1626 }
1627
1628 if (hwaddr_aton(pos, p->addr)) {
1629 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1630 line, pos);
1631 os_free(p);
1632 return -1;
1633 }
1634 pos += 17;
1635 if (*pos != '-') {
1636 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1637 line, pos);
1638 os_free(p);
1639 return -1;
1640 }
1641 pos++;
1642
1643 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1644 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1645 line, pos);
1646 os_free(p);
1647 return -1;
1648 }
1649
1650 dl_list_add(&ssid->psk_list, &p->list);
1651
1652 return 0;
1653}
1654
1655
1656#ifndef NO_CONFIG_WRITE
1657static char * wpa_config_write_psk_list(const struct parse_data *data,
1658 struct wpa_ssid *ssid)
1659{
1660 return NULL;
1661}
1662#endif /* NO_CONFIG_WRITE */
1663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001664#endif /* CONFIG_P2P */
1665
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001666
1667#ifdef CONFIG_MESH
1668
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001669static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1670 struct wpa_ssid *ssid, int line,
1671 const char *value)
1672{
1673 int *rates = wpa_config_parse_int_array(value);
1674
1675 if (rates == NULL) {
1676 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1677 line, value);
1678 return -1;
1679 }
1680 if (rates[0] == 0) {
1681 os_free(rates);
1682 rates = NULL;
1683 }
1684
1685 os_free(ssid->mesh_basic_rates);
1686 ssid->mesh_basic_rates = rates;
1687
1688 return 0;
1689}
1690
1691
1692#ifndef NO_CONFIG_WRITE
1693
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001694static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1695 struct wpa_ssid *ssid)
1696{
1697 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1698}
1699
1700#endif /* NO_CONFIG_WRITE */
1701
1702#endif /* CONFIG_MESH */
1703
1704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705/* Helper macros for network block parser */
1706
1707#ifdef OFFSET
1708#undef OFFSET
1709#endif /* OFFSET */
1710/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1711#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1712
1713/* STR: Define a string variable for an ASCII string; f = field name */
1714#ifdef NO_CONFIG_WRITE
1715#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1716#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1717#else /* NO_CONFIG_WRITE */
1718#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1719#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1720#endif /* NO_CONFIG_WRITE */
1721#define STR(f) _STR(f), NULL, NULL, NULL, 0
1722#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1723#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1724#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1725
1726/* STR_LEN: Define a string variable with a separate variable for storing the
1727 * data length. Unlike STR(), this can be used to store arbitrary binary data
1728 * (i.e., even nul termination character). */
1729#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1730#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1731#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1732#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1733#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1734
1735/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1736 * explicitly specified. */
1737#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1738#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1739#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1740
1741#ifdef NO_CONFIG_WRITE
1742#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1743#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1744#else /* NO_CONFIG_WRITE */
1745#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1746 OFFSET(f), (void *) 0
1747#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1748 OFFSET(eap.f), (void *) 0
1749#endif /* NO_CONFIG_WRITE */
1750
1751/* INT: Define an integer variable */
1752#define INT(f) _INT(f), NULL, NULL, 0
1753#define INTe(f) _INTe(f), NULL, NULL, 0
1754
1755/* INT_RANGE: Define an integer variable with allowed value range */
1756#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1757
1758/* FUNC: Define a configuration variable that uses a custom function for
1759 * parsing and writing the value. */
1760#ifdef NO_CONFIG_WRITE
1761#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1762#else /* NO_CONFIG_WRITE */
1763#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1764 NULL, NULL, NULL, NULL
1765#endif /* NO_CONFIG_WRITE */
1766#define FUNC(f) _FUNC(f), 0
1767#define FUNC_KEY(f) _FUNC(f), 1
1768
1769/*
1770 * Table of network configuration variables. This table is used to parse each
1771 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1772 * that is inside a network block.
1773 *
1774 * This table is generated using the helper macros defined above and with
1775 * generous help from the C pre-processor. The field name is stored as a string
1776 * into .name and for STR and INT types, the offset of the target buffer within
1777 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1778 * offset to the field containing the length of the configuration variable.
1779 * .param3 and .param4 can be used to mark the allowed range (length for STR
1780 * and value for INT).
1781 *
1782 * For each configuration line in wpa_supplicant.conf, the parser goes through
1783 * this table and select the entry that matches with the field name. The parser
1784 * function (.parser) is then called to parse the actual value of the field.
1785 *
1786 * This kind of mechanism makes it easy to add new configuration parameters,
1787 * since only one line needs to be added into this table and into the
1788 * struct wpa_ssid definition if the new variable is either a string or
1789 * integer. More complex types will need to use their own parser and writer
1790 * functions.
1791 */
1792static const struct parse_data ssid_fields[] = {
1793 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1794 { INT_RANGE(scan_ssid, 0, 1) },
1795 { FUNC(bssid) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001796 { FUNC(bssid_blacklist) },
1797 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 { FUNC_KEY(psk) },
1799 { FUNC(proto) },
1800 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001801 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 { FUNC(pairwise) },
1803 { FUNC(group) },
1804 { FUNC(auth_alg) },
1805 { FUNC(scan_freq) },
1806 { FUNC(freq_list) },
1807#ifdef IEEE8021X_EAPOL
1808 { FUNC(eap) },
1809 { STR_LENe(identity) },
1810 { STR_LENe(anonymous_identity) },
1811 { FUNC_KEY(password) },
1812 { STRe(ca_cert) },
1813 { STRe(ca_path) },
1814 { STRe(client_cert) },
1815 { STRe(private_key) },
1816 { STR_KEYe(private_key_passwd) },
1817 { STRe(dh_file) },
1818 { STRe(subject_match) },
1819 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001820 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001821 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 { STRe(ca_cert2) },
1823 { STRe(ca_path2) },
1824 { STRe(client_cert2) },
1825 { STRe(private_key2) },
1826 { STR_KEYe(private_key2_passwd) },
1827 { STRe(dh_file2) },
1828 { STRe(subject_match2) },
1829 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001830 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001831 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 { STRe(phase1) },
1833 { STRe(phase2) },
1834 { STRe(pcsc) },
1835 { STR_KEYe(pin) },
1836 { STRe(engine_id) },
1837 { STRe(key_id) },
1838 { STRe(cert_id) },
1839 { STRe(ca_cert_id) },
1840 { STR_KEYe(pin2) },
1841 { STRe(engine2_id) },
1842 { STRe(key2_id) },
1843 { STRe(cert2_id) },
1844 { STRe(ca_cert2_id) },
1845 { INTe(engine) },
1846 { INTe(engine2) },
1847 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001848 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001849 { STRe(openssl_ciphers) },
1850 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851#endif /* IEEE8021X_EAPOL */
1852 { FUNC_KEY(wep_key0) },
1853 { FUNC_KEY(wep_key1) },
1854 { FUNC_KEY(wep_key2) },
1855 { FUNC_KEY(wep_key3) },
1856 { INT(wep_tx_keyidx) },
1857 { INT(priority) },
1858#ifdef IEEE8021X_EAPOL
1859 { INT(eap_workaround) },
1860 { STRe(pac_file) },
1861 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001862 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001864#ifdef CONFIG_MESH
1865 { INT_RANGE(mode, 0, 5) },
1866 { INT_RANGE(no_auto_peer, 0, 1) },
1867#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 { INT_RANGE(proactive_key_caching, 0, 1) },
1871 { INT_RANGE(disabled, 0, 2) },
1872 { STR(id_str) },
1873#ifdef CONFIG_IEEE80211W
1874 { INT_RANGE(ieee80211w, 0, 2) },
1875#endif /* CONFIG_IEEE80211W */
1876 { INT_RANGE(peerkey, 0, 1) },
1877 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001878 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 { FUNC(mesh_basic_rates) },
1881 { INT(dot11MeshMaxRetries) },
1882 { INT(dot11MeshRetryTimeout) },
1883 { INT(dot11MeshConfirmTimeout) },
1884 { INT(dot11MeshHoldingTimeout) },
1885#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 { INT(wpa_ptk_rekey) },
1887 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001888 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001890 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001891 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001892 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001893#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001894#ifdef CONFIG_HT_OVERRIDES
1895 { INT_RANGE(disable_ht, 0, 1) },
1896 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001897 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001898 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001899 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001900 { INT_RANGE(disable_max_amsdu, -1, 1) },
1901 { INT_RANGE(ampdu_factor, -1, 3) },
1902 { INT_RANGE(ampdu_density, -1, 7) },
1903 { STR(ht_mcs) },
1904#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001905#ifdef CONFIG_VHT_OVERRIDES
1906 { INT_RANGE(disable_vht, 0, 1) },
1907 { INT(vht_capa) },
1908 { INT(vht_capa_mask) },
1909 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1910 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1911 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1912 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1913 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1914 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1915 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1916 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1917 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1918 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1919 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1920 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1921 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1922 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1923 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1924 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1925#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001926 { INT(ap_max_inactivity) },
1927 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001928 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001929#ifdef CONFIG_MACSEC
1930 { INT_RANGE(macsec_policy, 0, 1) },
1931#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001932#ifdef CONFIG_HS20
1933 { INT(update_identifier) },
1934#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001935 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936};
1937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938#undef OFFSET
1939#undef _STR
1940#undef STR
1941#undef STR_KEY
1942#undef _STR_LEN
1943#undef STR_LEN
1944#undef STR_LEN_KEY
1945#undef _STR_RANGE
1946#undef STR_RANGE
1947#undef STR_RANGE_KEY
1948#undef _INT
1949#undef INT
1950#undef INT_RANGE
1951#undef _FUNC
1952#undef FUNC
1953#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001954#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955
1956
1957/**
1958 * wpa_config_add_prio_network - Add a network to priority lists
1959 * @config: Configuration data from wpa_config_read()
1960 * @ssid: Pointer to the network configuration to be added to the list
1961 * Returns: 0 on success, -1 on failure
1962 *
1963 * This function is used to add a network block to the priority list of
1964 * networks. This must be called for each network when reading in the full
1965 * configuration. In addition, this can be used indirectly when updating
1966 * priorities by calling wpa_config_update_prio_list().
1967 */
1968int wpa_config_add_prio_network(struct wpa_config *config,
1969 struct wpa_ssid *ssid)
1970{
1971 int prio;
1972 struct wpa_ssid *prev, **nlist;
1973
1974 /*
1975 * Add to an existing priority list if one is available for the
1976 * configured priority level for this network.
1977 */
1978 for (prio = 0; prio < config->num_prio; prio++) {
1979 prev = config->pssid[prio];
1980 if (prev->priority == ssid->priority) {
1981 while (prev->pnext)
1982 prev = prev->pnext;
1983 prev->pnext = ssid;
1984 return 0;
1985 }
1986 }
1987
1988 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001989 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1990 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 if (nlist == NULL)
1992 return -1;
1993
1994 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001995 if (nlist[prio]->priority < ssid->priority) {
1996 os_memmove(&nlist[prio + 1], &nlist[prio],
1997 (config->num_prio - prio) *
1998 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 }
2002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 nlist[prio] = ssid;
2004 config->num_prio++;
2005 config->pssid = nlist;
2006
2007 return 0;
2008}
2009
2010
2011/**
2012 * wpa_config_update_prio_list - Update network priority list
2013 * @config: Configuration data from wpa_config_read()
2014 * Returns: 0 on success, -1 on failure
2015 *
2016 * This function is called to update the priority list of networks in the
2017 * configuration when a network is being added or removed. This is also called
2018 * if a priority for a network is changed.
2019 */
2020int wpa_config_update_prio_list(struct wpa_config *config)
2021{
2022 struct wpa_ssid *ssid;
2023 int ret = 0;
2024
2025 os_free(config->pssid);
2026 config->pssid = NULL;
2027 config->num_prio = 0;
2028
2029 ssid = config->ssid;
2030 while (ssid) {
2031 ssid->pnext = NULL;
2032 if (wpa_config_add_prio_network(config, ssid) < 0)
2033 ret = -1;
2034 ssid = ssid->next;
2035 }
2036
2037 return ret;
2038}
2039
2040
2041#ifdef IEEE8021X_EAPOL
2042static void eap_peer_config_free(struct eap_peer_config *eap)
2043{
2044 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002045 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002047 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 os_free(eap->ca_cert);
2049 os_free(eap->ca_path);
2050 os_free(eap->client_cert);
2051 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002052 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 os_free(eap->dh_file);
2054 os_free(eap->subject_match);
2055 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002056 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002057 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 os_free(eap->ca_cert2);
2059 os_free(eap->ca_path2);
2060 os_free(eap->client_cert2);
2061 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002062 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 os_free(eap->dh_file2);
2064 os_free(eap->subject_match2);
2065 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002066 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002067 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 os_free(eap->phase1);
2069 os_free(eap->phase2);
2070 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002071 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 os_free(eap->engine_id);
2073 os_free(eap->key_id);
2074 os_free(eap->cert_id);
2075 os_free(eap->ca_cert_id);
2076 os_free(eap->key2_id);
2077 os_free(eap->cert2_id);
2078 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002079 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 os_free(eap->engine2_id);
2081 os_free(eap->otp);
2082 os_free(eap->pending_req_otp);
2083 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002084 bin_clear_free(eap->new_password, eap->new_password_len);
2085 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002086 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087}
2088#endif /* IEEE8021X_EAPOL */
2089
2090
2091/**
2092 * wpa_config_free_ssid - Free network/ssid configuration data
2093 * @ssid: Configuration data for the network
2094 *
2095 * This function frees all resources allocated for the network configuration
2096 * data.
2097 */
2098void wpa_config_free_ssid(struct wpa_ssid *ssid)
2099{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002100 struct psk_list_entry *psk;
2101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002103 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002104 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105#ifdef IEEE8021X_EAPOL
2106 eap_peer_config_free(&ssid->eap);
2107#endif /* IEEE8021X_EAPOL */
2108 os_free(ssid->id_str);
2109 os_free(ssid->scan_freq);
2110 os_free(ssid->freq_list);
2111 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002113 os_free(ssid->bssid_blacklist);
2114 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002115#ifdef CONFIG_HT_OVERRIDES
2116 os_free(ssid->ht_mcs);
2117#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002118#ifdef CONFIG_MESH
2119 os_free(ssid->mesh_basic_rates);
2120#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002121 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2122 list))) {
2123 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002124 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002125 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002126 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127}
2128
2129
Dmitry Shmidt04949592012-07-19 12:16:46 -07002130void wpa_config_free_cred(struct wpa_cred *cred)
2131{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002132 size_t i;
2133
Dmitry Shmidt04949592012-07-19 12:16:46 -07002134 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002135 str_clear_free(cred->username);
2136 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002137 os_free(cred->ca_cert);
2138 os_free(cred->client_cert);
2139 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002140 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002142 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002143 for (i = 0; i < cred->num_domain; i++)
2144 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002145 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002146 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002147 os_free(cred->eap_method);
2148 os_free(cred->phase1);
2149 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002150 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002151 os_free(cred->roaming_partner);
2152 os_free(cred->provisioning_sp);
2153 for (i = 0; i < cred->num_req_conn_capab; i++)
2154 os_free(cred->req_conn_capab_port[i]);
2155 os_free(cred->req_conn_capab_port);
2156 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002157 os_free(cred);
2158}
2159
2160
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002161void wpa_config_flush_blobs(struct wpa_config *config)
2162{
2163#ifndef CONFIG_NO_CONFIG_BLOBS
2164 struct wpa_config_blob *blob, *prev;
2165
2166 blob = config->blobs;
2167 config->blobs = NULL;
2168 while (blob) {
2169 prev = blob;
2170 blob = blob->next;
2171 wpa_config_free_blob(prev);
2172 }
2173#endif /* CONFIG_NO_CONFIG_BLOBS */
2174}
2175
2176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177/**
2178 * wpa_config_free - Free configuration data
2179 * @config: Configuration data from wpa_config_read()
2180 *
2181 * This function frees all resources allocated for the configuration data by
2182 * wpa_config_read().
2183 */
2184void wpa_config_free(struct wpa_config *config)
2185{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002187 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002188 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189
2190 ssid = config->ssid;
2191 while (ssid) {
2192 prev = ssid;
2193 ssid = ssid->next;
2194 wpa_config_free_ssid(prev);
2195 }
2196
Dmitry Shmidt04949592012-07-19 12:16:46 -07002197 cred = config->cred;
2198 while (cred) {
2199 cprev = cred;
2200 cred = cred->next;
2201 wpa_config_free_cred(cprev);
2202 }
2203
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002204 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002207 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2208 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 os_free(config->ctrl_interface);
2210 os_free(config->ctrl_interface_group);
2211 os_free(config->opensc_engine_path);
2212 os_free(config->pkcs11_engine_path);
2213 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002214 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002215 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002216 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 os_free(config->driver_param);
2218 os_free(config->device_name);
2219 os_free(config->manufacturer);
2220 os_free(config->model_name);
2221 os_free(config->model_number);
2222 os_free(config->serial_number);
2223 os_free(config->config_methods);
2224 os_free(config->p2p_ssid_postfix);
2225 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002226 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002227 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002229 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002230 wpabuf_free(config->wps_nfc_dh_pubkey);
2231 wpabuf_free(config->wps_nfc_dh_privkey);
2232 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002233 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002234 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002235 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002236 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002237 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002238 os_free(config->wowlan_triggers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 os_free(config);
2240}
2241
2242
2243/**
2244 * wpa_config_foreach_network - Iterate over each configured network
2245 * @config: Configuration data from wpa_config_read()
2246 * @func: Callback function to process each network
2247 * @arg: Opaque argument to pass to callback function
2248 *
2249 * Iterate over the set of configured networks calling the specified
2250 * function for each item. We guard against callbacks removing the
2251 * supplied network.
2252 */
2253void wpa_config_foreach_network(struct wpa_config *config,
2254 void (*func)(void *, struct wpa_ssid *),
2255 void *arg)
2256{
2257 struct wpa_ssid *ssid, *next;
2258
2259 ssid = config->ssid;
2260 while (ssid) {
2261 next = ssid->next;
2262 func(arg, ssid);
2263 ssid = next;
2264 }
2265}
2266
2267
2268/**
2269 * wpa_config_get_network - Get configured network based on id
2270 * @config: Configuration data from wpa_config_read()
2271 * @id: Unique network id to search for
2272 * Returns: Network configuration or %NULL if not found
2273 */
2274struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2275{
2276 struct wpa_ssid *ssid;
2277
2278 ssid = config->ssid;
2279 while (ssid) {
2280 if (id == ssid->id)
2281 break;
2282 ssid = ssid->next;
2283 }
2284
2285 return ssid;
2286}
2287
2288
2289/**
2290 * wpa_config_add_network - Add a new network with empty configuration
2291 * @config: Configuration data from wpa_config_read()
2292 * Returns: The new network configuration or %NULL if operation failed
2293 */
2294struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2295{
2296 int id;
2297 struct wpa_ssid *ssid, *last = NULL;
2298
2299 id = -1;
2300 ssid = config->ssid;
2301 while (ssid) {
2302 if (ssid->id > id)
2303 id = ssid->id;
2304 last = ssid;
2305 ssid = ssid->next;
2306 }
2307 id++;
2308
2309 ssid = os_zalloc(sizeof(*ssid));
2310 if (ssid == NULL)
2311 return NULL;
2312 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002313 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 if (last)
2315 last->next = ssid;
2316 else
2317 config->ssid = ssid;
2318
2319 wpa_config_update_prio_list(config);
2320
2321 return ssid;
2322}
2323
2324
2325/**
2326 * wpa_config_remove_network - Remove a configured network based on id
2327 * @config: Configuration data from wpa_config_read()
2328 * @id: Unique network id to search for
2329 * Returns: 0 on success, or -1 if the network was not found
2330 */
2331int wpa_config_remove_network(struct wpa_config *config, int id)
2332{
2333 struct wpa_ssid *ssid, *prev = NULL;
2334
2335 ssid = config->ssid;
2336 while (ssid) {
2337 if (id == ssid->id)
2338 break;
2339 prev = ssid;
2340 ssid = ssid->next;
2341 }
2342
2343 if (ssid == NULL)
2344 return -1;
2345
2346 if (prev)
2347 prev->next = ssid->next;
2348 else
2349 config->ssid = ssid->next;
2350
2351 wpa_config_update_prio_list(config);
2352 wpa_config_free_ssid(ssid);
2353 return 0;
2354}
2355
2356
2357/**
2358 * wpa_config_set_network_defaults - Set network default values
2359 * @ssid: Pointer to network configuration data
2360 */
2361void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2362{
2363 ssid->proto = DEFAULT_PROTO;
2364 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2365 ssid->group_cipher = DEFAULT_GROUP;
2366 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002367 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368#ifdef IEEE8021X_EAPOL
2369 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2370 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2371 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002372 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002374#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002375 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2376 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2377 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2378 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2379#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002380#ifdef CONFIG_HT_OVERRIDES
2381 ssid->disable_ht = DEFAULT_DISABLE_HT;
2382 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002383 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002384 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002385 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2386 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2387 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2388#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002389#ifdef CONFIG_VHT_OVERRIDES
2390 ssid->vht_rx_mcs_nss_1 = -1;
2391 ssid->vht_rx_mcs_nss_2 = -1;
2392 ssid->vht_rx_mcs_nss_3 = -1;
2393 ssid->vht_rx_mcs_nss_4 = -1;
2394 ssid->vht_rx_mcs_nss_5 = -1;
2395 ssid->vht_rx_mcs_nss_6 = -1;
2396 ssid->vht_rx_mcs_nss_7 = -1;
2397 ssid->vht_rx_mcs_nss_8 = -1;
2398 ssid->vht_tx_mcs_nss_1 = -1;
2399 ssid->vht_tx_mcs_nss_2 = -1;
2400 ssid->vht_tx_mcs_nss_3 = -1;
2401 ssid->vht_tx_mcs_nss_4 = -1;
2402 ssid->vht_tx_mcs_nss_5 = -1;
2403 ssid->vht_tx_mcs_nss_6 = -1;
2404 ssid->vht_tx_mcs_nss_7 = -1;
2405 ssid->vht_tx_mcs_nss_8 = -1;
2406#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002407 ssid->proactive_key_caching = -1;
2408#ifdef CONFIG_IEEE80211W
2409 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2410#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002411 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412}
2413
2414
2415/**
2416 * wpa_config_set - Set a variable in network configuration
2417 * @ssid: Pointer to network configuration data
2418 * @var: Variable name, e.g., "ssid"
2419 * @value: Variable value
2420 * @line: Line number in configuration file or 0 if not used
2421 * Returns: 0 on success, -1 on failure
2422 *
2423 * This function can be used to set network configuration variables based on
2424 * both the configuration file and management interface input. The value
2425 * parameter must be in the same format as the text-based configuration file is
2426 * using. For example, strings are using double quotation marks.
2427 */
2428int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2429 int line)
2430{
2431 size_t i;
2432 int ret = 0;
2433
2434 if (ssid == NULL || var == NULL || value == NULL)
2435 return -1;
2436
2437 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2438 const struct parse_data *field = &ssid_fields[i];
2439 if (os_strcmp(var, field->name) != 0)
2440 continue;
2441
2442 if (field->parser(field, ssid, line, value)) {
2443 if (line) {
2444 wpa_printf(MSG_ERROR, "Line %d: failed to "
2445 "parse %s '%s'.", line, var, value);
2446 }
2447 ret = -1;
2448 }
2449 break;
2450 }
2451 if (i == NUM_SSID_FIELDS) {
2452 if (line) {
2453 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2454 "'%s'.", line, var);
2455 }
2456 ret = -1;
2457 }
2458
2459 return ret;
2460}
2461
2462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002463int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2464 const char *value)
2465{
2466 size_t len;
2467 char *buf;
2468 int ret;
2469
2470 len = os_strlen(value);
2471 buf = os_malloc(len + 3);
2472 if (buf == NULL)
2473 return -1;
2474 buf[0] = '"';
2475 os_memcpy(buf + 1, value, len);
2476 buf[len + 1] = '"';
2477 buf[len + 2] = '\0';
2478 ret = wpa_config_set(ssid, var, buf, 0);
2479 os_free(buf);
2480 return ret;
2481}
2482
2483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484/**
2485 * wpa_config_get_all - Get all options from network configuration
2486 * @ssid: Pointer to network configuration data
2487 * @get_keys: Determines if keys/passwords will be included in returned list
2488 * (if they may be exported)
2489 * Returns: %NULL terminated list of all set keys and their values in the form
2490 * of [key1, val1, key2, val2, ... , NULL]
2491 *
2492 * This function can be used to get list of all configured network properties.
2493 * The caller is responsible for freeing the returned list and all its
2494 * elements.
2495 */
2496char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2497{
2498 const struct parse_data *field;
2499 char *key, *value;
2500 size_t i;
2501 char **props;
2502 int fields_num;
2503
2504 get_keys = get_keys && ssid->export_keys;
2505
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002506 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 if (!props)
2508 return NULL;
2509
2510 fields_num = 0;
2511 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2512 field = &ssid_fields[i];
2513 if (field->key_data && !get_keys)
2514 continue;
2515 value = field->writer(field, ssid);
2516 if (value == NULL)
2517 continue;
2518 if (os_strlen(value) == 0) {
2519 os_free(value);
2520 continue;
2521 }
2522
2523 key = os_strdup(field->name);
2524 if (key == NULL) {
2525 os_free(value);
2526 goto err;
2527 }
2528
2529 props[fields_num * 2] = key;
2530 props[fields_num * 2 + 1] = value;
2531
2532 fields_num++;
2533 }
2534
2535 return props;
2536
2537err:
2538 value = *props;
2539 while (value)
2540 os_free(value++);
2541 os_free(props);
2542 return NULL;
2543}
2544
2545
2546#ifndef NO_CONFIG_WRITE
2547/**
2548 * wpa_config_get - Get a variable in network configuration
2549 * @ssid: Pointer to network configuration data
2550 * @var: Variable name, e.g., "ssid"
2551 * Returns: Value of the variable or %NULL on failure
2552 *
2553 * This function can be used to get network configuration variables. The
2554 * returned value is a copy of the configuration variable in text format, i.e,.
2555 * the same format that the text-based configuration file and wpa_config_set()
2556 * are using for the value. The caller is responsible for freeing the returned
2557 * value.
2558 */
2559char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2560{
2561 size_t i;
2562
2563 if (ssid == NULL || var == NULL)
2564 return NULL;
2565
2566 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2567 const struct parse_data *field = &ssid_fields[i];
2568 if (os_strcmp(var, field->name) == 0)
2569 return field->writer(field, ssid);
2570 }
2571
2572 return NULL;
2573}
2574
2575
2576/**
2577 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2578 * @ssid: Pointer to network configuration data
2579 * @var: Variable name, e.g., "ssid"
2580 * Returns: Value of the variable or %NULL on failure
2581 *
2582 * This function can be used to get network configuration variable like
2583 * wpa_config_get(). The only difference is that this functions does not expose
2584 * key/password material from the configuration. In case a key/password field
2585 * is requested, the returned value is an empty string or %NULL if the variable
2586 * is not set or "*" if the variable is set (regardless of its value). The
2587 * returned value is a copy of the configuration variable in text format, i.e,.
2588 * the same format that the text-based configuration file and wpa_config_set()
2589 * are using for the value. The caller is responsible for freeing the returned
2590 * value.
2591 */
2592char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2593{
2594 size_t i;
2595
2596 if (ssid == NULL || var == NULL)
2597 return NULL;
2598
2599 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2600 const struct parse_data *field = &ssid_fields[i];
2601 if (os_strcmp(var, field->name) == 0) {
2602 char *res = field->writer(field, ssid);
2603 if (field->key_data) {
2604 if (res && res[0]) {
2605 wpa_printf(MSG_DEBUG, "Do not allow "
2606 "key_data field to be "
2607 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002608 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 return os_strdup("*");
2610 }
2611
2612 os_free(res);
2613 return NULL;
2614 }
2615 return res;
2616 }
2617 }
2618
2619 return NULL;
2620}
2621#endif /* NO_CONFIG_WRITE */
2622
2623
2624/**
2625 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2626 * @ssid: Pointer to network configuration data
2627 *
2628 * This function must be called to update WPA PSK when either SSID or the
2629 * passphrase has changed for the network configuration.
2630 */
2631void wpa_config_update_psk(struct wpa_ssid *ssid)
2632{
2633#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002634 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 ssid->psk, PMK_LEN);
2636 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2637 ssid->psk, PMK_LEN);
2638 ssid->psk_set = 1;
2639#endif /* CONFIG_NO_PBKDF2 */
2640}
2641
2642
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002643static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2644 const char *value)
2645{
2646 u8 *proto;
2647 int **port;
2648 int *ports, *nports;
2649 const char *pos;
2650 unsigned int num_ports;
2651
2652 proto = os_realloc_array(cred->req_conn_capab_proto,
2653 cred->num_req_conn_capab + 1, sizeof(u8));
2654 if (proto == NULL)
2655 return -1;
2656 cred->req_conn_capab_proto = proto;
2657
2658 port = os_realloc_array(cred->req_conn_capab_port,
2659 cred->num_req_conn_capab + 1, sizeof(int *));
2660 if (port == NULL)
2661 return -1;
2662 cred->req_conn_capab_port = port;
2663
2664 proto[cred->num_req_conn_capab] = atoi(value);
2665
2666 pos = os_strchr(value, ':');
2667 if (pos == NULL) {
2668 port[cred->num_req_conn_capab] = NULL;
2669 cred->num_req_conn_capab++;
2670 return 0;
2671 }
2672 pos++;
2673
2674 ports = NULL;
2675 num_ports = 0;
2676
2677 while (*pos) {
2678 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2679 if (nports == NULL) {
2680 os_free(ports);
2681 return -1;
2682 }
2683 ports = nports;
2684 ports[num_ports++] = atoi(pos);
2685
2686 pos = os_strchr(pos, ',');
2687 if (pos == NULL)
2688 break;
2689 pos++;
2690 }
2691
2692 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2693 if (nports == NULL) {
2694 os_free(ports);
2695 return -1;
2696 }
2697 ports = nports;
2698 ports[num_ports] = -1;
2699
2700 port[cred->num_req_conn_capab] = ports;
2701 cred->num_req_conn_capab++;
2702 return 0;
2703}
2704
2705
Dmitry Shmidt04949592012-07-19 12:16:46 -07002706int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2707 const char *value, int line)
2708{
2709 char *val;
2710 size_t len;
2711
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002712 if (os_strcmp(var, "temporary") == 0) {
2713 cred->temporary = atoi(value);
2714 return 0;
2715 }
2716
Dmitry Shmidt04949592012-07-19 12:16:46 -07002717 if (os_strcmp(var, "priority") == 0) {
2718 cred->priority = atoi(value);
2719 return 0;
2720 }
2721
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002722 if (os_strcmp(var, "sp_priority") == 0) {
2723 int prio = atoi(value);
2724 if (prio < 0 || prio > 255)
2725 return -1;
2726 cred->sp_priority = prio;
2727 return 0;
2728 }
2729
Dmitry Shmidt04949592012-07-19 12:16:46 -07002730 if (os_strcmp(var, "pcsc") == 0) {
2731 cred->pcsc = atoi(value);
2732 return 0;
2733 }
2734
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002735 if (os_strcmp(var, "eap") == 0) {
2736 struct eap_method_type method;
2737 method.method = eap_peer_get_type(value, &method.vendor);
2738 if (method.vendor == EAP_VENDOR_IETF &&
2739 method.method == EAP_TYPE_NONE) {
2740 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2741 "for a credential", line, value);
2742 return -1;
2743 }
2744 os_free(cred->eap_method);
2745 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2746 if (cred->eap_method == NULL)
2747 return -1;
2748 os_memcpy(cred->eap_method, &method, sizeof(method));
2749 return 0;
2750 }
2751
2752 if (os_strcmp(var, "password") == 0 &&
2753 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002754 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002755 cred->password = os_strdup(value);
2756 cred->ext_password = 1;
2757 return 0;
2758 }
2759
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002760 if (os_strcmp(var, "update_identifier") == 0) {
2761 cred->update_identifier = atoi(value);
2762 return 0;
2763 }
2764
2765 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2766 cred->min_dl_bandwidth_home = atoi(value);
2767 return 0;
2768 }
2769
2770 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2771 cred->min_ul_bandwidth_home = atoi(value);
2772 return 0;
2773 }
2774
2775 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2776 cred->min_dl_bandwidth_roaming = atoi(value);
2777 return 0;
2778 }
2779
2780 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2781 cred->min_ul_bandwidth_roaming = atoi(value);
2782 return 0;
2783 }
2784
2785 if (os_strcmp(var, "max_bss_load") == 0) {
2786 cred->max_bss_load = atoi(value);
2787 return 0;
2788 }
2789
2790 if (os_strcmp(var, "req_conn_capab") == 0)
2791 return wpa_config_set_cred_req_conn_capab(cred, value);
2792
2793 if (os_strcmp(var, "ocsp") == 0) {
2794 cred->ocsp = atoi(value);
2795 return 0;
2796 }
2797
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002798 if (os_strcmp(var, "sim_num") == 0) {
2799 cred->sim_num = atoi(value);
2800 return 0;
2801 }
2802
Dmitry Shmidt04949592012-07-19 12:16:46 -07002803 val = wpa_config_parse_string(value, &len);
2804 if (val == NULL) {
2805 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2806 "value '%s'.", line, var, value);
2807 return -1;
2808 }
2809
2810 if (os_strcmp(var, "realm") == 0) {
2811 os_free(cred->realm);
2812 cred->realm = val;
2813 return 0;
2814 }
2815
2816 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002817 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002818 cred->username = val;
2819 return 0;
2820 }
2821
2822 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002823 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002824 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002825 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002826 return 0;
2827 }
2828
2829 if (os_strcmp(var, "ca_cert") == 0) {
2830 os_free(cred->ca_cert);
2831 cred->ca_cert = val;
2832 return 0;
2833 }
2834
2835 if (os_strcmp(var, "client_cert") == 0) {
2836 os_free(cred->client_cert);
2837 cred->client_cert = val;
2838 return 0;
2839 }
2840
2841 if (os_strcmp(var, "private_key") == 0) {
2842 os_free(cred->private_key);
2843 cred->private_key = val;
2844 return 0;
2845 }
2846
2847 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002848 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002849 cred->private_key_passwd = val;
2850 return 0;
2851 }
2852
2853 if (os_strcmp(var, "imsi") == 0) {
2854 os_free(cred->imsi);
2855 cred->imsi = val;
2856 return 0;
2857 }
2858
2859 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002860 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861 cred->milenage = val;
2862 return 0;
2863 }
2864
Dmitry Shmidt051af732013-10-22 13:52:46 -07002865 if (os_strcmp(var, "domain_suffix_match") == 0) {
2866 os_free(cred->domain_suffix_match);
2867 cred->domain_suffix_match = val;
2868 return 0;
2869 }
2870
Dmitry Shmidt04949592012-07-19 12:16:46 -07002871 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002872 char **new_domain;
2873 new_domain = os_realloc_array(cred->domain,
2874 cred->num_domain + 1,
2875 sizeof(char *));
2876 if (new_domain == NULL) {
2877 os_free(val);
2878 return -1;
2879 }
2880 new_domain[cred->num_domain++] = val;
2881 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002882 return 0;
2883 }
2884
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002885 if (os_strcmp(var, "phase1") == 0) {
2886 os_free(cred->phase1);
2887 cred->phase1 = val;
2888 return 0;
2889 }
2890
2891 if (os_strcmp(var, "phase2") == 0) {
2892 os_free(cred->phase2);
2893 cred->phase2 = val;
2894 return 0;
2895 }
2896
2897 if (os_strcmp(var, "roaming_consortium") == 0) {
2898 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2899 wpa_printf(MSG_ERROR, "Line %d: invalid "
2900 "roaming_consortium length %d (3..15 "
2901 "expected)", line, (int) len);
2902 os_free(val);
2903 return -1;
2904 }
2905 os_memcpy(cred->roaming_consortium, val, len);
2906 cred->roaming_consortium_len = len;
2907 os_free(val);
2908 return 0;
2909 }
2910
Dmitry Shmidt051af732013-10-22 13:52:46 -07002911 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2912 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2913 {
2914 wpa_printf(MSG_ERROR, "Line %d: invalid "
2915 "required_roaming_consortium length %d "
2916 "(3..15 expected)", line, (int) len);
2917 os_free(val);
2918 return -1;
2919 }
2920 os_memcpy(cred->required_roaming_consortium, val, len);
2921 cred->required_roaming_consortium_len = len;
2922 os_free(val);
2923 return 0;
2924 }
2925
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002926 if (os_strcmp(var, "excluded_ssid") == 0) {
2927 struct excluded_ssid *e;
2928
2929 if (len > MAX_SSID_LEN) {
2930 wpa_printf(MSG_ERROR, "Line %d: invalid "
2931 "excluded_ssid length %d", line, (int) len);
2932 os_free(val);
2933 return -1;
2934 }
2935
2936 e = os_realloc_array(cred->excluded_ssid,
2937 cred->num_excluded_ssid + 1,
2938 sizeof(struct excluded_ssid));
2939 if (e == NULL) {
2940 os_free(val);
2941 return -1;
2942 }
2943 cred->excluded_ssid = e;
2944
2945 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2946 os_memcpy(e->ssid, val, len);
2947 e->ssid_len = len;
2948
2949 os_free(val);
2950
2951 return 0;
2952 }
2953
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002954 if (os_strcmp(var, "roaming_partner") == 0) {
2955 struct roaming_partner *p;
2956 char *pos;
2957
2958 p = os_realloc_array(cred->roaming_partner,
2959 cred->num_roaming_partner + 1,
2960 sizeof(struct roaming_partner));
2961 if (p == NULL) {
2962 os_free(val);
2963 return -1;
2964 }
2965 cred->roaming_partner = p;
2966
2967 p = &cred->roaming_partner[cred->num_roaming_partner];
2968
2969 pos = os_strchr(val, ',');
2970 if (pos == NULL) {
2971 os_free(val);
2972 return -1;
2973 }
2974 *pos++ = '\0';
2975 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2976 os_free(val);
2977 return -1;
2978 }
2979 os_memcpy(p->fqdn, val, pos - val);
2980
2981 p->exact_match = atoi(pos);
2982
2983 pos = os_strchr(pos, ',');
2984 if (pos == NULL) {
2985 os_free(val);
2986 return -1;
2987 }
2988 *pos++ = '\0';
2989
2990 p->priority = atoi(pos);
2991
2992 pos = os_strchr(pos, ',');
2993 if (pos == NULL) {
2994 os_free(val);
2995 return -1;
2996 }
2997 *pos++ = '\0';
2998
2999 if (os_strlen(pos) >= sizeof(p->country)) {
3000 os_free(val);
3001 return -1;
3002 }
3003 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3004
3005 cred->num_roaming_partner++;
3006 os_free(val);
3007
3008 return 0;
3009 }
3010
3011 if (os_strcmp(var, "provisioning_sp") == 0) {
3012 os_free(cred->provisioning_sp);
3013 cred->provisioning_sp = val;
3014 return 0;
3015 }
3016
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017 if (line) {
3018 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3019 line, var);
3020 }
3021
3022 os_free(val);
3023
3024 return -1;
3025}
3026
3027
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003028static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003029{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003031 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003032 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003033
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003034 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003035 if (buf == NULL)
3036 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003037 res = os_snprintf(buf, bufsize, "%d", val);
3038 if (os_snprintf_error(bufsize, res)) {
3039 os_free(buf);
3040 buf = NULL;
3041 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003042 return buf;
3043}
3044
3045
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003046static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003047{
3048 if (str == NULL)
3049 return NULL;
3050 return os_strdup(str);
3051}
3052
3053
3054char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3055{
3056 if (os_strcmp(var, "temporary") == 0)
3057 return alloc_int_str(cred->temporary);
3058
3059 if (os_strcmp(var, "priority") == 0)
3060 return alloc_int_str(cred->priority);
3061
3062 if (os_strcmp(var, "sp_priority") == 0)
3063 return alloc_int_str(cred->sp_priority);
3064
3065 if (os_strcmp(var, "pcsc") == 0)
3066 return alloc_int_str(cred->pcsc);
3067
3068 if (os_strcmp(var, "eap") == 0) {
3069 if (!cred->eap_method)
3070 return NULL;
3071 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3072 cred->eap_method[0].method));
3073 }
3074
3075 if (os_strcmp(var, "update_identifier") == 0)
3076 return alloc_int_str(cred->update_identifier);
3077
3078 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3079 return alloc_int_str(cred->min_dl_bandwidth_home);
3080
3081 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3082 return alloc_int_str(cred->min_ul_bandwidth_home);
3083
3084 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3085 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3086
3087 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3088 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3089
3090 if (os_strcmp(var, "max_bss_load") == 0)
3091 return alloc_int_str(cred->max_bss_load);
3092
3093 if (os_strcmp(var, "req_conn_capab") == 0) {
3094 unsigned int i;
3095 char *buf, *end, *pos;
3096 int ret;
3097
3098 if (!cred->num_req_conn_capab)
3099 return NULL;
3100
3101 buf = os_malloc(4000);
3102 if (buf == NULL)
3103 return NULL;
3104 pos = buf;
3105 end = pos + 4000;
3106 for (i = 0; i < cred->num_req_conn_capab; i++) {
3107 int *ports;
3108
3109 ret = os_snprintf(pos, end - pos, "%s%u",
3110 i > 0 ? "\n" : "",
3111 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003112 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003113 return buf;
3114 pos += ret;
3115
3116 ports = cred->req_conn_capab_port[i];
3117 if (ports) {
3118 int j;
3119 for (j = 0; ports[j] != -1; j++) {
3120 ret = os_snprintf(pos, end - pos,
3121 "%s%d",
3122 j > 0 ? "," : ":",
3123 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003124 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003125 return buf;
3126 pos += ret;
3127 }
3128 }
3129 }
3130
3131 return buf;
3132 }
3133
3134 if (os_strcmp(var, "ocsp") == 0)
3135 return alloc_int_str(cred->ocsp);
3136
3137 if (os_strcmp(var, "realm") == 0)
3138 return alloc_strdup(cred->realm);
3139
3140 if (os_strcmp(var, "username") == 0)
3141 return alloc_strdup(cred->username);
3142
3143 if (os_strcmp(var, "password") == 0) {
3144 if (!cred->password)
3145 return NULL;
3146 return alloc_strdup("*");
3147 }
3148
3149 if (os_strcmp(var, "ca_cert") == 0)
3150 return alloc_strdup(cred->ca_cert);
3151
3152 if (os_strcmp(var, "client_cert") == 0)
3153 return alloc_strdup(cred->client_cert);
3154
3155 if (os_strcmp(var, "private_key") == 0)
3156 return alloc_strdup(cred->private_key);
3157
3158 if (os_strcmp(var, "private_key_passwd") == 0) {
3159 if (!cred->private_key_passwd)
3160 return NULL;
3161 return alloc_strdup("*");
3162 }
3163
3164 if (os_strcmp(var, "imsi") == 0)
3165 return alloc_strdup(cred->imsi);
3166
3167 if (os_strcmp(var, "milenage") == 0) {
3168 if (!(cred->milenage))
3169 return NULL;
3170 return alloc_strdup("*");
3171 }
3172
3173 if (os_strcmp(var, "domain_suffix_match") == 0)
3174 return alloc_strdup(cred->domain_suffix_match);
3175
3176 if (os_strcmp(var, "domain") == 0) {
3177 unsigned int i;
3178 char *buf, *end, *pos;
3179 int ret;
3180
3181 if (!cred->num_domain)
3182 return NULL;
3183
3184 buf = os_malloc(4000);
3185 if (buf == NULL)
3186 return NULL;
3187 pos = buf;
3188 end = pos + 4000;
3189
3190 for (i = 0; i < cred->num_domain; i++) {
3191 ret = os_snprintf(pos, end - pos, "%s%s",
3192 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003193 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003194 return buf;
3195 pos += ret;
3196 }
3197
3198 return buf;
3199 }
3200
3201 if (os_strcmp(var, "phase1") == 0)
3202 return alloc_strdup(cred->phase1);
3203
3204 if (os_strcmp(var, "phase2") == 0)
3205 return alloc_strdup(cred->phase2);
3206
3207 if (os_strcmp(var, "roaming_consortium") == 0) {
3208 size_t buflen;
3209 char *buf;
3210
3211 if (!cred->roaming_consortium_len)
3212 return NULL;
3213 buflen = cred->roaming_consortium_len * 2 + 1;
3214 buf = os_malloc(buflen);
3215 if (buf == NULL)
3216 return NULL;
3217 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3218 cred->roaming_consortium_len);
3219 return buf;
3220 }
3221
3222 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3223 size_t buflen;
3224 char *buf;
3225
3226 if (!cred->required_roaming_consortium_len)
3227 return NULL;
3228 buflen = cred->required_roaming_consortium_len * 2 + 1;
3229 buf = os_malloc(buflen);
3230 if (buf == NULL)
3231 return NULL;
3232 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3233 cred->required_roaming_consortium_len);
3234 return buf;
3235 }
3236
3237 if (os_strcmp(var, "excluded_ssid") == 0) {
3238 unsigned int i;
3239 char *buf, *end, *pos;
3240
3241 if (!cred->num_excluded_ssid)
3242 return NULL;
3243
3244 buf = os_malloc(4000);
3245 if (buf == NULL)
3246 return NULL;
3247 pos = buf;
3248 end = pos + 4000;
3249
3250 for (i = 0; i < cred->num_excluded_ssid; i++) {
3251 struct excluded_ssid *e;
3252 int ret;
3253
3254 e = &cred->excluded_ssid[i];
3255 ret = os_snprintf(pos, end - pos, "%s%s",
3256 i > 0 ? "\n" : "",
3257 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003258 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003259 return buf;
3260 pos += ret;
3261 }
3262
3263 return buf;
3264 }
3265
3266 if (os_strcmp(var, "roaming_partner") == 0) {
3267 unsigned int i;
3268 char *buf, *end, *pos;
3269
3270 if (!cred->num_roaming_partner)
3271 return NULL;
3272
3273 buf = os_malloc(4000);
3274 if (buf == NULL)
3275 return NULL;
3276 pos = buf;
3277 end = pos + 4000;
3278
3279 for (i = 0; i < cred->num_roaming_partner; i++) {
3280 struct roaming_partner *p;
3281 int ret;
3282
3283 p = &cred->roaming_partner[i];
3284 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3285 i > 0 ? "\n" : "",
3286 p->fqdn, p->exact_match, p->priority,
3287 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003288 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003289 return buf;
3290 pos += ret;
3291 }
3292
3293 return buf;
3294 }
3295
3296 if (os_strcmp(var, "provisioning_sp") == 0)
3297 return alloc_strdup(cred->provisioning_sp);
3298
3299 return NULL;
3300}
3301
3302
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3304{
3305 struct wpa_cred *cred;
3306
3307 cred = config->cred;
3308 while (cred) {
3309 if (id == cred->id)
3310 break;
3311 cred = cred->next;
3312 }
3313
3314 return cred;
3315}
3316
3317
3318struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3319{
3320 int id;
3321 struct wpa_cred *cred, *last = NULL;
3322
3323 id = -1;
3324 cred = config->cred;
3325 while (cred) {
3326 if (cred->id > id)
3327 id = cred->id;
3328 last = cred;
3329 cred = cred->next;
3330 }
3331 id++;
3332
3333 cred = os_zalloc(sizeof(*cred));
3334 if (cred == NULL)
3335 return NULL;
3336 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003337 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 if (last)
3339 last->next = cred;
3340 else
3341 config->cred = cred;
3342
3343 return cred;
3344}
3345
3346
3347int wpa_config_remove_cred(struct wpa_config *config, int id)
3348{
3349 struct wpa_cred *cred, *prev = NULL;
3350
3351 cred = config->cred;
3352 while (cred) {
3353 if (id == cred->id)
3354 break;
3355 prev = cred;
3356 cred = cred->next;
3357 }
3358
3359 if (cred == NULL)
3360 return -1;
3361
3362 if (prev)
3363 prev->next = cred->next;
3364 else
3365 config->cred = cred->next;
3366
3367 wpa_config_free_cred(cred);
3368 return 0;
3369}
3370
3371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372#ifndef CONFIG_NO_CONFIG_BLOBS
3373/**
3374 * wpa_config_get_blob - Get a named configuration blob
3375 * @config: Configuration data from wpa_config_read()
3376 * @name: Name of the blob
3377 * Returns: Pointer to blob data or %NULL if not found
3378 */
3379const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3380 const char *name)
3381{
3382 struct wpa_config_blob *blob = config->blobs;
3383
3384 while (blob) {
3385 if (os_strcmp(blob->name, name) == 0)
3386 return blob;
3387 blob = blob->next;
3388 }
3389 return NULL;
3390}
3391
3392
3393/**
3394 * wpa_config_set_blob - Set or add a named configuration blob
3395 * @config: Configuration data from wpa_config_read()
3396 * @blob: New value for the blob
3397 *
3398 * Adds a new configuration blob or replaces the current value of an existing
3399 * blob.
3400 */
3401void wpa_config_set_blob(struct wpa_config *config,
3402 struct wpa_config_blob *blob)
3403{
3404 wpa_config_remove_blob(config, blob->name);
3405 blob->next = config->blobs;
3406 config->blobs = blob;
3407}
3408
3409
3410/**
3411 * wpa_config_free_blob - Free blob data
3412 * @blob: Pointer to blob to be freed
3413 */
3414void wpa_config_free_blob(struct wpa_config_blob *blob)
3415{
3416 if (blob) {
3417 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003418 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 os_free(blob);
3420 }
3421}
3422
3423
3424/**
3425 * wpa_config_remove_blob - Remove a named configuration blob
3426 * @config: Configuration data from wpa_config_read()
3427 * @name: Name of the blob to remove
3428 * Returns: 0 if blob was removed or -1 if blob was not found
3429 */
3430int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3431{
3432 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3433
3434 while (pos) {
3435 if (os_strcmp(pos->name, name) == 0) {
3436 if (prev)
3437 prev->next = pos->next;
3438 else
3439 config->blobs = pos->next;
3440 wpa_config_free_blob(pos);
3441 return 0;
3442 }
3443 prev = pos;
3444 pos = pos->next;
3445 }
3446
3447 return -1;
3448}
3449#endif /* CONFIG_NO_CONFIG_BLOBS */
3450
3451
3452/**
3453 * wpa_config_alloc_empty - Allocate an empty configuration
3454 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3455 * socket
3456 * @driver_param: Driver parameters
3457 * Returns: Pointer to allocated configuration data or %NULL on failure
3458 */
3459struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3460 const char *driver_param)
3461{
3462 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003463 const int aCWmin = 4, aCWmax = 10;
3464 const struct hostapd_wmm_ac_params ac_bk =
3465 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3466 const struct hostapd_wmm_ac_params ac_be =
3467 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3468 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3469 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3470 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3471 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472
3473 config = os_zalloc(sizeof(*config));
3474 if (config == NULL)
3475 return NULL;
3476 config->eapol_version = DEFAULT_EAPOL_VERSION;
3477 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003478 config->user_mpm = DEFAULT_USER_MPM;
3479 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003480 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481 config->fast_reauth = DEFAULT_FAST_REAUTH;
3482 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3483 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003484 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003485 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3487 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3488 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3489 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003490 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003491 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003492 config->wmm_ac_params[0] = ac_be;
3493 config->wmm_ac_params[1] = ac_bk;
3494 config->wmm_ac_params[2] = ac_vi;
3495 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003496 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003497 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003498 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003499 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500
3501 if (ctrl_interface)
3502 config->ctrl_interface = os_strdup(ctrl_interface);
3503 if (driver_param)
3504 config->driver_param = os_strdup(driver_param);
3505
3506 return config;
3507}
3508
3509
3510#ifndef CONFIG_NO_STDOUT_DEBUG
3511/**
3512 * wpa_config_debug_dump_networks - Debug dump of configured networks
3513 * @config: Configuration data from wpa_config_read()
3514 */
3515void wpa_config_debug_dump_networks(struct wpa_config *config)
3516{
3517 int prio;
3518 struct wpa_ssid *ssid;
3519
3520 for (prio = 0; prio < config->num_prio; prio++) {
3521 ssid = config->pssid[prio];
3522 wpa_printf(MSG_DEBUG, "Priority group %d",
3523 ssid->priority);
3524 while (ssid) {
3525 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3526 ssid->id,
3527 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3528 ssid = ssid->pnext;
3529 }
3530 }
3531}
3532#endif /* CONFIG_NO_STDOUT_DEBUG */
3533
3534
3535struct global_parse_data {
3536 char *name;
3537 int (*parser)(const struct global_parse_data *data,
3538 struct wpa_config *config, int line, const char *value);
3539 void *param1, *param2, *param3;
3540 unsigned int changed_flag;
3541};
3542
3543
3544static int wpa_global_config_parse_int(const struct global_parse_data *data,
3545 struct wpa_config *config, int line,
3546 const char *pos)
3547{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003548 int val, *dst;
3549 char *end;
3550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003552 val = strtol(pos, &end, 0);
3553 if (*end) {
3554 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3555 line, pos);
3556 return -1;
3557 }
3558 *dst = val;
3559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3561
3562 if (data->param2 && *dst < (long) data->param2) {
3563 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3564 "min_value=%ld)", line, data->name, *dst,
3565 (long) data->param2);
3566 *dst = (long) data->param2;
3567 return -1;
3568 }
3569
3570 if (data->param3 && *dst > (long) data->param3) {
3571 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3572 "max_value=%ld)", line, data->name, *dst,
3573 (long) data->param3);
3574 *dst = (long) data->param3;
3575 return -1;
3576 }
3577
3578 return 0;
3579}
3580
3581
3582static int wpa_global_config_parse_str(const struct global_parse_data *data,
3583 struct wpa_config *config, int line,
3584 const char *pos)
3585{
3586 size_t len;
3587 char **dst, *tmp;
3588
3589 len = os_strlen(pos);
3590 if (data->param2 && len < (size_t) data->param2) {
3591 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3592 "min_len=%ld)", line, data->name,
3593 (unsigned long) len, (long) data->param2);
3594 return -1;
3595 }
3596
3597 if (data->param3 && len > (size_t) data->param3) {
3598 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3599 "max_len=%ld)", line, data->name,
3600 (unsigned long) len, (long) data->param3);
3601 return -1;
3602 }
3603
3604 tmp = os_strdup(pos);
3605 if (tmp == NULL)
3606 return -1;
3607
3608 dst = (char **) (((u8 *) config) + (long) data->param1);
3609 os_free(*dst);
3610 *dst = tmp;
3611 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3612
3613 return 0;
3614}
3615
3616
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003617static int wpa_config_process_bgscan(const struct global_parse_data *data,
3618 struct wpa_config *config, int line,
3619 const char *pos)
3620{
3621 size_t len;
3622 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003623 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003624
3625 tmp = wpa_config_parse_string(pos, &len);
3626 if (tmp == NULL) {
3627 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3628 line, data->name);
3629 return -1;
3630 }
3631
Dmitry Shmidt97672262014-02-03 13:02:54 -08003632 res = wpa_global_config_parse_str(data, config, line, tmp);
3633 os_free(tmp);
3634 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003635}
3636
3637
Dmitry Shmidt04949592012-07-19 12:16:46 -07003638static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3639 struct wpa_config *config, int line,
3640 const char *pos)
3641{
3642 size_t len;
3643 struct wpabuf **dst, *tmp;
3644
3645 len = os_strlen(pos);
3646 if (len & 0x01)
3647 return -1;
3648
3649 tmp = wpabuf_alloc(len / 2);
3650 if (tmp == NULL)
3651 return -1;
3652
3653 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3654 wpabuf_free(tmp);
3655 return -1;
3656 }
3657
3658 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3659 wpabuf_free(*dst);
3660 *dst = tmp;
3661 wpa_printf(MSG_DEBUG, "%s", data->name);
3662
3663 return 0;
3664}
3665
3666
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003667static int wpa_config_process_freq_list(const struct global_parse_data *data,
3668 struct wpa_config *config, int line,
3669 const char *value)
3670{
3671 int *freqs;
3672
3673 freqs = wpa_config_parse_int_array(value);
3674 if (freqs == NULL)
3675 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003676 if (freqs[0] == 0) {
3677 os_free(freqs);
3678 freqs = NULL;
3679 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003680 os_free(config->freq_list);
3681 config->freq_list = freqs;
3682 return 0;
3683}
3684
3685
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003686#ifdef CONFIG_P2P
3687static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3688 struct wpa_config *config, int line,
3689 const char *pos)
3690{
3691 u32 *dst;
3692 struct hostapd_ip_addr addr;
3693
3694 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3695 return -1;
3696 if (addr.af != AF_INET)
3697 return -1;
3698
3699 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3700 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3701 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3702 WPA_GET_BE32((u8 *) dst));
3703
3704 return 0;
3705}
3706#endif /* CONFIG_P2P */
3707
3708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709static int wpa_config_process_country(const struct global_parse_data *data,
3710 struct wpa_config *config, int line,
3711 const char *pos)
3712{
3713 if (!pos[0] || !pos[1]) {
3714 wpa_printf(MSG_DEBUG, "Invalid country set");
3715 return -1;
3716 }
3717 config->country[0] = pos[0];
3718 config->country[1] = pos[1];
3719 wpa_printf(MSG_DEBUG, "country='%c%c'",
3720 config->country[0], config->country[1]);
3721 return 0;
3722}
3723
3724
3725static int wpa_config_process_load_dynamic_eap(
3726 const struct global_parse_data *data, struct wpa_config *config,
3727 int line, const char *so)
3728{
3729 int ret;
3730 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3731 ret = eap_peer_method_load(so);
3732 if (ret == -2) {
3733 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3734 "reloading.");
3735 } else if (ret) {
3736 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3737 "method '%s'.", line, so);
3738 return -1;
3739 }
3740
3741 return 0;
3742}
3743
3744
3745#ifdef CONFIG_WPS
3746
3747static int wpa_config_process_uuid(const struct global_parse_data *data,
3748 struct wpa_config *config, int line,
3749 const char *pos)
3750{
3751 char buf[40];
3752 if (uuid_str2bin(pos, config->uuid)) {
3753 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3754 return -1;
3755 }
3756 uuid_bin2str(config->uuid, buf, sizeof(buf));
3757 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3758 return 0;
3759}
3760
3761
3762static int wpa_config_process_device_type(
3763 const struct global_parse_data *data,
3764 struct wpa_config *config, int line, const char *pos)
3765{
3766 return wps_dev_type_str2bin(pos, config->device_type);
3767}
3768
3769
3770static int wpa_config_process_os_version(const struct global_parse_data *data,
3771 struct wpa_config *config, int line,
3772 const char *pos)
3773{
3774 if (hexstr2bin(pos, config->os_version, 4)) {
3775 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3776 return -1;
3777 }
3778 wpa_printf(MSG_DEBUG, "os_version=%08x",
3779 WPA_GET_BE32(config->os_version));
3780 return 0;
3781}
3782
Dmitry Shmidt04949592012-07-19 12:16:46 -07003783
3784static int wpa_config_process_wps_vendor_ext_m1(
3785 const struct global_parse_data *data,
3786 struct wpa_config *config, int line, const char *pos)
3787{
3788 struct wpabuf *tmp;
3789 int len = os_strlen(pos) / 2;
3790 u8 *p;
3791
3792 if (!len) {
3793 wpa_printf(MSG_ERROR, "Line %d: "
3794 "invalid wps_vendor_ext_m1", line);
3795 return -1;
3796 }
3797
3798 tmp = wpabuf_alloc(len);
3799 if (tmp) {
3800 p = wpabuf_put(tmp, len);
3801
3802 if (hexstr2bin(pos, p, len)) {
3803 wpa_printf(MSG_ERROR, "Line %d: "
3804 "invalid wps_vendor_ext_m1", line);
3805 wpabuf_free(tmp);
3806 return -1;
3807 }
3808
3809 wpabuf_free(config->wps_vendor_ext_m1);
3810 config->wps_vendor_ext_m1 = tmp;
3811 } else {
3812 wpa_printf(MSG_ERROR, "Can not allocate "
3813 "memory for wps_vendor_ext_m1");
3814 return -1;
3815 }
3816
3817 return 0;
3818}
3819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820#endif /* CONFIG_WPS */
3821
3822#ifdef CONFIG_P2P
3823static int wpa_config_process_sec_device_type(
3824 const struct global_parse_data *data,
3825 struct wpa_config *config, int line, const char *pos)
3826{
3827 int idx;
3828
3829 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3830 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3831 "items", line);
3832 return -1;
3833 }
3834
3835 idx = config->num_sec_device_types;
3836
3837 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3838 return -1;
3839
3840 config->num_sec_device_types++;
3841 return 0;
3842}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003843
3844
3845static int wpa_config_process_p2p_pref_chan(
3846 const struct global_parse_data *data,
3847 struct wpa_config *config, int line, const char *pos)
3848{
3849 struct p2p_channel *pref = NULL, *n;
3850 unsigned int num = 0;
3851 const char *pos2;
3852 u8 op_class, chan;
3853
3854 /* format: class:chan,class:chan,... */
3855
3856 while (*pos) {
3857 op_class = atoi(pos);
3858 pos2 = os_strchr(pos, ':');
3859 if (pos2 == NULL)
3860 goto fail;
3861 pos2++;
3862 chan = atoi(pos2);
3863
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003864 n = os_realloc_array(pref, num + 1,
3865 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003866 if (n == NULL)
3867 goto fail;
3868 pref = n;
3869 pref[num].op_class = op_class;
3870 pref[num].chan = chan;
3871 num++;
3872
3873 pos = os_strchr(pos2, ',');
3874 if (pos == NULL)
3875 break;
3876 pos++;
3877 }
3878
3879 os_free(config->p2p_pref_chan);
3880 config->p2p_pref_chan = pref;
3881 config->num_p2p_pref_chan = num;
3882 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3883 (u8 *) config->p2p_pref_chan,
3884 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3885
3886 return 0;
3887
3888fail:
3889 os_free(pref);
3890 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3891 return -1;
3892}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003893
3894
3895static int wpa_config_process_p2p_no_go_freq(
3896 const struct global_parse_data *data,
3897 struct wpa_config *config, int line, const char *pos)
3898{
3899 int ret;
3900
3901 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3902 if (ret < 0) {
3903 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3904 return -1;
3905 }
3906
3907 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3908 config->p2p_no_go_freq.num);
3909
3910 return 0;
3911}
3912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913#endif /* CONFIG_P2P */
3914
3915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003916static int wpa_config_process_hessid(
3917 const struct global_parse_data *data,
3918 struct wpa_config *config, int line, const char *pos)
3919{
3920 if (hwaddr_aton2(pos, config->hessid) < 0) {
3921 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3922 line, pos);
3923 return -1;
3924 }
3925
3926 return 0;
3927}
3928
3929
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003930static int wpa_config_process_sae_groups(
3931 const struct global_parse_data *data,
3932 struct wpa_config *config, int line, const char *pos)
3933{
3934 int *groups = wpa_config_parse_int_array(pos);
3935 if (groups == NULL) {
3936 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3937 line, pos);
3938 return -1;
3939 }
3940
3941 os_free(config->sae_groups);
3942 config->sae_groups = groups;
3943
3944 return 0;
3945}
3946
3947
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003948static int wpa_config_process_ap_vendor_elements(
3949 const struct global_parse_data *data,
3950 struct wpa_config *config, int line, const char *pos)
3951{
3952 struct wpabuf *tmp;
3953 int len = os_strlen(pos) / 2;
3954 u8 *p;
3955
3956 if (!len) {
3957 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3958 line);
3959 return -1;
3960 }
3961
3962 tmp = wpabuf_alloc(len);
3963 if (tmp) {
3964 p = wpabuf_put(tmp, len);
3965
3966 if (hexstr2bin(pos, p, len)) {
3967 wpa_printf(MSG_ERROR, "Line %d: invalid "
3968 "ap_vendor_elements", line);
3969 wpabuf_free(tmp);
3970 return -1;
3971 }
3972
3973 wpabuf_free(config->ap_vendor_elements);
3974 config->ap_vendor_elements = tmp;
3975 } else {
3976 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3977 "ap_vendor_elements");
3978 return -1;
3979 }
3980
3981 return 0;
3982}
3983
3984
Dmitry Shmidt56052862013-10-04 10:23:25 -07003985#ifdef CONFIG_CTRL_IFACE
3986static int wpa_config_process_no_ctrl_interface(
3987 const struct global_parse_data *data,
3988 struct wpa_config *config, int line, const char *pos)
3989{
3990 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3991 os_free(config->ctrl_interface);
3992 config->ctrl_interface = NULL;
3993 return 0;
3994}
3995#endif /* CONFIG_CTRL_IFACE */
3996
3997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998#ifdef OFFSET
3999#undef OFFSET
4000#endif /* OFFSET */
4001/* OFFSET: Get offset of a variable within the wpa_config structure */
4002#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4003
4004#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
4005#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
4006#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
4007#define INT(f) _INT(f), NULL, NULL
4008#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
4009#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
4010#define STR(f) _STR(f), NULL, NULL
4011#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07004012#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004013#define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014
4015static const struct global_parse_data global_fields[] = {
4016#ifdef CONFIG_CTRL_IFACE
4017 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004018 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 { STR(ctrl_interface_group), 0 } /* deprecated */,
4020#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004021#ifdef CONFIG_MACSEC
4022 { INT_RANGE(eapol_version, 1, 3), 0 },
4023#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004025#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004027 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004028#ifdef CONFIG_MESH
4029 { INT(user_mpm), 0 },
4030 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004031 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004032#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004033 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 { INT(fast_reauth), 0 },
4035 { STR(opensc_engine_path), 0 },
4036 { STR(pkcs11_engine_path), 0 },
4037 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004038 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004039 { STR(pcsc_reader), 0 },
4040 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004041 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 { STR(driver_param), 0 },
4043 { INT(dot11RSNAConfigPMKLifetime), 0 },
4044 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4045 { INT(dot11RSNAConfigSATimeout), 0 },
4046#ifndef CONFIG_NO_CONFIG_WRITE
4047 { INT(update_config), 0 },
4048#endif /* CONFIG_NO_CONFIG_WRITE */
4049 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4050#ifdef CONFIG_WPS
4051 { FUNC(uuid), CFG_CHANGED_UUID },
4052 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
4053 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4054 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4055 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4056 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4057 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4058 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4059 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4060 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004061 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062#endif /* CONFIG_WPS */
4063#ifdef CONFIG_P2P
4064 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
4065 { INT(p2p_listen_reg_class), 0 },
4066 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004067 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4068 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4070 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4071 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4072 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4073 { INT(p2p_group_idle), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004074 { INT_RANGE(p2p_passphrase_len, 8, 63),
4075 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004077 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4078 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004079 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004080 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004081 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004082 { INT(p2p_disabled), 0 },
4083 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004084 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004085 { IPV4(ip_addr_go), 0 },
4086 { IPV4(ip_addr_mask), 0 },
4087 { IPV4(ip_addr_start), 0 },
4088 { IPV4(ip_addr_end), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089#endif /* CONFIG_P2P */
4090 { FUNC(country), CFG_CHANGED_COUNTRY },
4091 { INT(bss_max_count), 0 },
4092 { INT(bss_expiration_age), 0 },
4093 { INT(bss_expiration_scan_count), 0 },
4094 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004095 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004097 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004098#ifdef CONFIG_HS20
4099 { INT_RANGE(hs20, 0, 1), 0 },
4100#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004101 { INT_RANGE(interworking, 0, 1), 0 },
4102 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004103 { INT_RANGE(access_network_type, 0, 15), 0 },
4104 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4105 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004106 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4107 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4108 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4109 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4110 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004111 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4112 { INT(p2p_go_max_inactivity), 0 },
4113 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004114 { INT(okc), 0 },
4115 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004116 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004117 { INT(dtim_period), 0 },
4118 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004119 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004120 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004121 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004122 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004123 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004124 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004125 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004126 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004127 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004128 { INT(mac_addr), 0 },
4129 { INT(rand_addr_lifetime), 0 },
4130 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004131 { INT(key_mgmt_offload), 0},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132};
4133
4134#undef FUNC
4135#undef _INT
4136#undef INT
4137#undef INT_RANGE
4138#undef _STR
4139#undef STR
4140#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004141#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004142#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004143#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144
4145
4146int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4147{
4148 size_t i;
4149 int ret = 0;
4150
4151 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4152 const struct global_parse_data *field = &global_fields[i];
4153 size_t flen = os_strlen(field->name);
4154 if (os_strncmp(pos, field->name, flen) != 0 ||
4155 pos[flen] != '=')
4156 continue;
4157
4158 if (field->parser(field, config, line, pos + flen + 1)) {
4159 wpa_printf(MSG_ERROR, "Line %d: failed to "
4160 "parse '%s'.", line, pos);
4161 ret = -1;
4162 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004163 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4164 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165 config->changed_parameters |= field->changed_flag;
4166 break;
4167 }
4168 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004169#ifdef CONFIG_AP
4170 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4171 char *tmp = os_strchr(pos, '=');
4172 if (tmp == NULL) {
4173 if (line < 0)
4174 return -1;
4175 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4176 "'%s'", line, pos);
4177 return -1;
4178 }
4179 *tmp++ = '\0';
4180 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4181 tmp)) {
4182 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4183 "AC item", line);
4184 return -1;
4185 }
4186 }
4187#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 if (line < 0)
4189 return -1;
4190 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4191 line, pos);
4192 ret = -1;
4193 }
4194
4195 return ret;
4196}