blob: 4ebf684341ee301e39c95fd6a26a87839fea437d [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
238static int wpa_config_parse_bssid(const struct parse_data *data,
239 struct wpa_ssid *ssid, int line,
240 const char *value)
241{
242 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
243 os_strcmp(value, "any") == 0) {
244 ssid->bssid_set = 0;
245 wpa_printf(MSG_MSGDUMP, "BSSID any");
246 return 0;
247 }
248 if (hwaddr_aton(value, ssid->bssid)) {
249 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
250 line, value);
251 return -1;
252 }
253 ssid->bssid_set = 1;
254 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
255 return 0;
256}
257
258
259#ifndef NO_CONFIG_WRITE
260static char * wpa_config_write_bssid(const struct parse_data *data,
261 struct wpa_ssid *ssid)
262{
263 char *value;
264 int res;
265
266 if (!ssid->bssid_set)
267 return NULL;
268
269 value = os_malloc(20);
270 if (value == NULL)
271 return NULL;
272 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800273 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 os_free(value);
275 return NULL;
276 }
277 value[20 - 1] = '\0';
278 return value;
279}
280#endif /* NO_CONFIG_WRITE */
281
282
283static int wpa_config_parse_psk(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
285 const char *value)
286{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287#ifdef CONFIG_EXT_PASSWORD
288 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700289 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700290 ssid->passphrase = NULL;
291 ssid->psk_set = 0;
292 os_free(ssid->ext_psk);
293 ssid->ext_psk = os_strdup(value + 4);
294 if (ssid->ext_psk == NULL)
295 return -1;
296 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
297 ssid->ext_psk);
298 return 0;
299 }
300#endif /* CONFIG_EXT_PASSWORD */
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 if (*value == '"') {
303#ifndef CONFIG_NO_PBKDF2
304 const char *pos;
305 size_t len;
306
307 value++;
308 pos = os_strrchr(value, '"');
309 if (pos)
310 len = pos - value;
311 else
312 len = os_strlen(value);
313 if (len < 8 || len > 63) {
314 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
315 "length %lu (expected: 8..63) '%s'.",
316 line, (unsigned long) len, value);
317 return -1;
318 }
319 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
320 (u8 *) value, len);
321 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
322 os_memcmp(ssid->passphrase, value, len) == 0)
323 return 0;
324 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700325 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700326 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 if (ssid->passphrase == NULL)
328 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 return 0;
330#else /* CONFIG_NO_PBKDF2 */
331 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
332 "supported.", line);
333 return -1;
334#endif /* CONFIG_NO_PBKDF2 */
335 }
336
337 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
338 value[PMK_LEN * 2] != '\0') {
339 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
340 line, value);
341 return -1;
342 }
343
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700344 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 ssid->passphrase = NULL;
346
347 ssid->psk_set = 1;
348 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
349 return 0;
350}
351
352
353#ifndef NO_CONFIG_WRITE
354static char * wpa_config_write_psk(const struct parse_data *data,
355 struct wpa_ssid *ssid)
356{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700357#ifdef CONFIG_EXT_PASSWORD
358 if (ssid->ext_psk) {
359 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
360 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800361 int res;
362
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700363 if (buf == NULL)
364 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800365 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
366 if (os_snprintf_error(len, res)) {
367 os_free(buf);
368 buf = NULL;
369 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700370 return buf;
371 }
372#endif /* CONFIG_EXT_PASSWORD */
373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 if (ssid->passphrase)
375 return wpa_config_write_string_ascii(
376 (const u8 *) ssid->passphrase,
377 os_strlen(ssid->passphrase));
378
379 if (ssid->psk_set)
380 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
381
382 return NULL;
383}
384#endif /* NO_CONFIG_WRITE */
385
386
387static int wpa_config_parse_proto(const struct parse_data *data,
388 struct wpa_ssid *ssid, int line,
389 const char *value)
390{
391 int val = 0, last, errors = 0;
392 char *start, *end, *buf;
393
394 buf = os_strdup(value);
395 if (buf == NULL)
396 return -1;
397 start = buf;
398
399 while (*start != '\0') {
400 while (*start == ' ' || *start == '\t')
401 start++;
402 if (*start == '\0')
403 break;
404 end = start;
405 while (*end != ' ' && *end != '\t' && *end != '\0')
406 end++;
407 last = *end == '\0';
408 *end = '\0';
409 if (os_strcmp(start, "WPA") == 0)
410 val |= WPA_PROTO_WPA;
411 else if (os_strcmp(start, "RSN") == 0 ||
412 os_strcmp(start, "WPA2") == 0)
413 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800414 else if (os_strcmp(start, "OSEN") == 0)
415 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 else {
417 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
418 line, start);
419 errors++;
420 }
421
422 if (last)
423 break;
424 start = end + 1;
425 }
426 os_free(buf);
427
428 if (val == 0) {
429 wpa_printf(MSG_ERROR,
430 "Line %d: no proto values configured.", line);
431 errors++;
432 }
433
434 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
435 ssid->proto = val;
436 return errors ? -1 : 0;
437}
438
439
440#ifndef NO_CONFIG_WRITE
441static char * wpa_config_write_proto(const struct parse_data *data,
442 struct wpa_ssid *ssid)
443{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700444 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 char *buf, *pos, *end;
446
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800447 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 if (buf == NULL)
449 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800450 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451
452 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700453 ret = os_snprintf(pos, end - pos, "%sWPA",
454 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800455 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 return buf;
457 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 }
459
460 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700461 ret = os_snprintf(pos, end - pos, "%sRSN",
462 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800463 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 return buf;
465 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 }
467
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800468 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700469 ret = os_snprintf(pos, end - pos, "%sOSEN",
470 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800471 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800472 return buf;
473 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700474 }
475
476 if (pos == buf) {
477 os_free(buf);
478 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800479 }
480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 return buf;
482}
483#endif /* NO_CONFIG_WRITE */
484
485
486static int wpa_config_parse_key_mgmt(const struct parse_data *data,
487 struct wpa_ssid *ssid, int line,
488 const char *value)
489{
490 int val = 0, last, errors = 0;
491 char *start, *end, *buf;
492
493 buf = os_strdup(value);
494 if (buf == NULL)
495 return -1;
496 start = buf;
497
498 while (*start != '\0') {
499 while (*start == ' ' || *start == '\t')
500 start++;
501 if (*start == '\0')
502 break;
503 end = start;
504 while (*end != ' ' && *end != '\t' && *end != '\0')
505 end++;
506 last = *end == '\0';
507 *end = '\0';
508 if (os_strcmp(start, "WPA-PSK") == 0)
509 val |= WPA_KEY_MGMT_PSK;
510 else if (os_strcmp(start, "WPA-EAP") == 0)
511 val |= WPA_KEY_MGMT_IEEE8021X;
512 else if (os_strcmp(start, "IEEE8021X") == 0)
513 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
514 else if (os_strcmp(start, "NONE") == 0)
515 val |= WPA_KEY_MGMT_NONE;
516 else if (os_strcmp(start, "WPA-NONE") == 0)
517 val |= WPA_KEY_MGMT_WPA_NONE;
518#ifdef CONFIG_IEEE80211R
519 else if (os_strcmp(start, "FT-PSK") == 0)
520 val |= WPA_KEY_MGMT_FT_PSK;
521 else if (os_strcmp(start, "FT-EAP") == 0)
522 val |= WPA_KEY_MGMT_FT_IEEE8021X;
523#endif /* CONFIG_IEEE80211R */
524#ifdef CONFIG_IEEE80211W
525 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
526 val |= WPA_KEY_MGMT_PSK_SHA256;
527 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
528 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
529#endif /* CONFIG_IEEE80211W */
530#ifdef CONFIG_WPS
531 else if (os_strcmp(start, "WPS") == 0)
532 val |= WPA_KEY_MGMT_WPS;
533#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800534#ifdef CONFIG_SAE
535 else if (os_strcmp(start, "SAE") == 0)
536 val |= WPA_KEY_MGMT_SAE;
537 else if (os_strcmp(start, "FT-SAE") == 0)
538 val |= WPA_KEY_MGMT_FT_SAE;
539#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800540#ifdef CONFIG_HS20
541 else if (os_strcmp(start, "OSEN") == 0)
542 val |= WPA_KEY_MGMT_OSEN;
543#endif /* CONFIG_HS20 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
545 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 else {
547 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
548 line, start);
549 errors++;
550 }
551
552 if (last)
553 break;
554 start = end + 1;
555 }
556 os_free(buf);
557
558 if (val == 0) {
559 wpa_printf(MSG_ERROR,
560 "Line %d: no key_mgmt values configured.", line);
561 errors++;
562 }
563
564 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
565 ssid->key_mgmt = val;
566 return errors ? -1 : 0;
567}
568
569
570#ifndef NO_CONFIG_WRITE
571static char * wpa_config_write_key_mgmt(const struct parse_data *data,
572 struct wpa_ssid *ssid)
573{
574 char *buf, *pos, *end;
575 int ret;
576
Dmitry Shmidt96571392013-10-14 12:54:46 -0700577 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 if (buf == NULL)
579 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700580 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581
582 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
583 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
584 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800585 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 end[-1] = '\0';
587 return buf;
588 }
589 pos += ret;
590 }
591
592 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
593 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
594 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800595 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 end[-1] = '\0';
597 return buf;
598 }
599 pos += ret;
600 }
601
602 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
603 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
604 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800605 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 end[-1] = '\0';
607 return buf;
608 }
609 pos += ret;
610 }
611
612 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
613 ret = os_snprintf(pos, end - pos, "%sNONE",
614 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800615 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 end[-1] = '\0';
617 return buf;
618 }
619 pos += ret;
620 }
621
622 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
623 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
624 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800625 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 end[-1] = '\0';
627 return buf;
628 }
629 pos += ret;
630 }
631
632#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700633 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
634 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
635 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800636 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700637 end[-1] = '\0';
638 return buf;
639 }
640 pos += ret;
641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642
Dmitry Shmidt96571392013-10-14 12:54:46 -0700643 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
644 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
645 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800646 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700647 end[-1] = '\0';
648 return buf;
649 }
650 pos += ret;
651 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652#endif /* CONFIG_IEEE80211R */
653
654#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700655 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
656 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
657 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800658 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700659 end[-1] = '\0';
660 return buf;
661 }
662 pos += ret;
663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664
Dmitry Shmidt96571392013-10-14 12:54:46 -0700665 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
666 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
667 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800668 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700669 end[-1] = '\0';
670 return buf;
671 }
672 pos += ret;
673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674#endif /* CONFIG_IEEE80211W */
675
676#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700677 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
678 ret = os_snprintf(pos, end - pos, "%sWPS",
679 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800680 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700681 end[-1] = '\0';
682 return buf;
683 }
684 pos += ret;
685 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686#endif /* CONFIG_WPS */
687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800688#ifdef CONFIG_SAE
689 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
690 ret = os_snprintf(pos, end - pos, "%sSAE",
691 pos == buf ? "" : " ");
692 if (os_snprintf_error(end - pos, ret)) {
693 end[-1] = '\0';
694 return buf;
695 }
696 pos += ret;
697 }
698
699 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
700 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
701 pos == buf ? "" : " ");
702 if (os_snprintf_error(end - pos, ret)) {
703 end[-1] = '\0';
704 return buf;
705 }
706 pos += ret;
707 }
708#endif /* CONFIG_SAE */
709
710#ifdef CONFIG_HS20
711 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
712 ret = os_snprintf(pos, end - pos, "%sOSEN",
713 pos == buf ? "" : " ");
714 if (os_snprintf_error(end - pos, ret)) {
715 end[-1] = '\0';
716 return buf;
717 }
718 pos += ret;
719 }
720#endif /* CONFIG_HS20 */
721
722 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
723 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
724 pos == buf ? "" : " ");
725 if (os_snprintf_error(end - pos, ret)) {
726 end[-1] = '\0';
727 return buf;
728 }
729 pos += ret;
730 }
731
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700732 if (pos == buf) {
733 os_free(buf);
734 buf = NULL;
735 }
736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 return buf;
738}
739#endif /* NO_CONFIG_WRITE */
740
741
742static int wpa_config_parse_cipher(int line, const char *value)
743{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800744 int val = wpa_parse_cipher(value);
745 if (val < 0) {
746 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
747 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 if (val == 0) {
751 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
752 line);
753 return -1;
754 }
755 return val;
756}
757
758
759#ifndef NO_CONFIG_WRITE
760static char * wpa_config_write_cipher(int cipher)
761{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800762 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 if (buf == NULL)
764 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800766 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
767 os_free(buf);
768 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 }
770
771 return buf;
772}
773#endif /* NO_CONFIG_WRITE */
774
775
776static int wpa_config_parse_pairwise(const struct parse_data *data,
777 struct wpa_ssid *ssid, int line,
778 const char *value)
779{
780 int val;
781 val = wpa_config_parse_cipher(line, value);
782 if (val == -1)
783 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800784 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
786 "(0x%x).", line, val);
787 return -1;
788 }
789
790 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
791 ssid->pairwise_cipher = val;
792 return 0;
793}
794
795
796#ifndef NO_CONFIG_WRITE
797static char * wpa_config_write_pairwise(const struct parse_data *data,
798 struct wpa_ssid *ssid)
799{
800 return wpa_config_write_cipher(ssid->pairwise_cipher);
801}
802#endif /* NO_CONFIG_WRITE */
803
804
805static int wpa_config_parse_group(const struct parse_data *data,
806 struct wpa_ssid *ssid, int line,
807 const char *value)
808{
809 int val;
810 val = wpa_config_parse_cipher(line, value);
811 if (val == -1)
812 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800813 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
815 "(0x%x).", line, val);
816 return -1;
817 }
818
819 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
820 ssid->group_cipher = val;
821 return 0;
822}
823
824
825#ifndef NO_CONFIG_WRITE
826static char * wpa_config_write_group(const struct parse_data *data,
827 struct wpa_ssid *ssid)
828{
829 return wpa_config_write_cipher(ssid->group_cipher);
830}
831#endif /* NO_CONFIG_WRITE */
832
833
834static int wpa_config_parse_auth_alg(const struct parse_data *data,
835 struct wpa_ssid *ssid, int line,
836 const char *value)
837{
838 int val = 0, last, errors = 0;
839 char *start, *end, *buf;
840
841 buf = os_strdup(value);
842 if (buf == NULL)
843 return -1;
844 start = buf;
845
846 while (*start != '\0') {
847 while (*start == ' ' || *start == '\t')
848 start++;
849 if (*start == '\0')
850 break;
851 end = start;
852 while (*end != ' ' && *end != '\t' && *end != '\0')
853 end++;
854 last = *end == '\0';
855 *end = '\0';
856 if (os_strcmp(start, "OPEN") == 0)
857 val |= WPA_AUTH_ALG_OPEN;
858 else if (os_strcmp(start, "SHARED") == 0)
859 val |= WPA_AUTH_ALG_SHARED;
860 else if (os_strcmp(start, "LEAP") == 0)
861 val |= WPA_AUTH_ALG_LEAP;
862 else {
863 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
864 line, start);
865 errors++;
866 }
867
868 if (last)
869 break;
870 start = end + 1;
871 }
872 os_free(buf);
873
874 if (val == 0) {
875 wpa_printf(MSG_ERROR,
876 "Line %d: no auth_alg values configured.", line);
877 errors++;
878 }
879
880 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
881 ssid->auth_alg = val;
882 return errors ? -1 : 0;
883}
884
885
886#ifndef NO_CONFIG_WRITE
887static char * wpa_config_write_auth_alg(const struct parse_data *data,
888 struct wpa_ssid *ssid)
889{
890 char *buf, *pos, *end;
891 int ret;
892
893 pos = buf = os_zalloc(30);
894 if (buf == NULL)
895 return NULL;
896 end = buf + 30;
897
898 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
899 ret = os_snprintf(pos, end - pos, "%sOPEN",
900 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800901 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 end[-1] = '\0';
903 return buf;
904 }
905 pos += ret;
906 }
907
908 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
909 ret = os_snprintf(pos, end - pos, "%sSHARED",
910 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800911 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 end[-1] = '\0';
913 return buf;
914 }
915 pos += ret;
916 }
917
918 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
919 ret = os_snprintf(pos, end - pos, "%sLEAP",
920 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 end[-1] = '\0';
923 return buf;
924 }
925 pos += ret;
926 }
927
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700928 if (pos == buf) {
929 os_free(buf);
930 buf = NULL;
931 }
932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return buf;
934}
935#endif /* NO_CONFIG_WRITE */
936
937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800938static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939{
940 int *freqs;
941 size_t used, len;
942 const char *pos;
943
944 used = 0;
945 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (freqs == NULL)
948 return NULL;
949
950 pos = value;
951 while (pos) {
952 while (*pos == ' ')
953 pos++;
954 if (used == len) {
955 int *n;
956 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700957 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 if (n == NULL) {
959 os_free(freqs);
960 return NULL;
961 }
962 for (i = len; i <= len * 2; i++)
963 n[i] = 0;
964 freqs = n;
965 len *= 2;
966 }
967
968 freqs[used] = atoi(pos);
969 if (freqs[used] == 0)
970 break;
971 used++;
972 pos = os_strchr(pos + 1, ' ');
973 }
974
975 return freqs;
976}
977
978
979static int wpa_config_parse_scan_freq(const struct parse_data *data,
980 struct wpa_ssid *ssid, int line,
981 const char *value)
982{
983 int *freqs;
984
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800985 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 if (freqs == NULL)
987 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700988 if (freqs[0] == 0) {
989 os_free(freqs);
990 freqs = NULL;
991 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 os_free(ssid->scan_freq);
993 ssid->scan_freq = freqs;
994
995 return 0;
996}
997
998
999static int wpa_config_parse_freq_list(const struct parse_data *data,
1000 struct wpa_ssid *ssid, int line,
1001 const char *value)
1002{
1003 int *freqs;
1004
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001005 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 if (freqs == NULL)
1007 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001008 if (freqs[0] == 0) {
1009 os_free(freqs);
1010 freqs = NULL;
1011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 os_free(ssid->freq_list);
1013 ssid->freq_list = freqs;
1014
1015 return 0;
1016}
1017
1018
1019#ifndef NO_CONFIG_WRITE
1020static char * wpa_config_write_freqs(const struct parse_data *data,
1021 const int *freqs)
1022{
1023 char *buf, *pos, *end;
1024 int i, ret;
1025 size_t count;
1026
1027 if (freqs == NULL)
1028 return NULL;
1029
1030 count = 0;
1031 for (i = 0; freqs[i]; i++)
1032 count++;
1033
1034 pos = buf = os_zalloc(10 * count + 1);
1035 if (buf == NULL)
1036 return NULL;
1037 end = buf + 10 * count + 1;
1038
1039 for (i = 0; freqs[i]; i++) {
1040 ret = os_snprintf(pos, end - pos, "%s%u",
1041 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 end[-1] = '\0';
1044 return buf;
1045 }
1046 pos += ret;
1047 }
1048
1049 return buf;
1050}
1051
1052
1053static char * wpa_config_write_scan_freq(const struct parse_data *data,
1054 struct wpa_ssid *ssid)
1055{
1056 return wpa_config_write_freqs(data, ssid->scan_freq);
1057}
1058
1059
1060static char * wpa_config_write_freq_list(const struct parse_data *data,
1061 struct wpa_ssid *ssid)
1062{
1063 return wpa_config_write_freqs(data, ssid->freq_list);
1064}
1065#endif /* NO_CONFIG_WRITE */
1066
1067
1068#ifdef IEEE8021X_EAPOL
1069static int wpa_config_parse_eap(const struct parse_data *data,
1070 struct wpa_ssid *ssid, int line,
1071 const char *value)
1072{
1073 int last, errors = 0;
1074 char *start, *end, *buf;
1075 struct eap_method_type *methods = NULL, *tmp;
1076 size_t num_methods = 0;
1077
1078 buf = os_strdup(value);
1079 if (buf == NULL)
1080 return -1;
1081 start = buf;
1082
1083 while (*start != '\0') {
1084 while (*start == ' ' || *start == '\t')
1085 start++;
1086 if (*start == '\0')
1087 break;
1088 end = start;
1089 while (*end != ' ' && *end != '\t' && *end != '\0')
1090 end++;
1091 last = *end == '\0';
1092 *end = '\0';
1093 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001094 methods = os_realloc_array(methods, num_methods + 1,
1095 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 if (methods == NULL) {
1097 os_free(tmp);
1098 os_free(buf);
1099 return -1;
1100 }
1101 methods[num_methods].method = eap_peer_get_type(
1102 start, &methods[num_methods].vendor);
1103 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1104 methods[num_methods].method == EAP_TYPE_NONE) {
1105 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1106 "'%s'", line, start);
1107 wpa_printf(MSG_ERROR, "You may need to add support for"
1108 " this EAP method during wpa_supplicant\n"
1109 "build time configuration.\n"
1110 "See README for more information.");
1111 errors++;
1112 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1113 methods[num_methods].method == EAP_TYPE_LEAP)
1114 ssid->leap++;
1115 else
1116 ssid->non_leap++;
1117 num_methods++;
1118 if (last)
1119 break;
1120 start = end + 1;
1121 }
1122 os_free(buf);
1123
1124 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001125 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 if (methods == NULL) {
1127 os_free(tmp);
1128 return -1;
1129 }
1130 methods[num_methods].vendor = EAP_VENDOR_IETF;
1131 methods[num_methods].method = EAP_TYPE_NONE;
1132 num_methods++;
1133
1134 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1135 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001136 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 ssid->eap.eap_methods = methods;
1138 return errors ? -1 : 0;
1139}
1140
1141
1142static char * wpa_config_write_eap(const struct parse_data *data,
1143 struct wpa_ssid *ssid)
1144{
1145 int i, ret;
1146 char *buf, *pos, *end;
1147 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1148 const char *name;
1149
1150 if (eap_methods == NULL)
1151 return NULL;
1152
1153 pos = buf = os_zalloc(100);
1154 if (buf == NULL)
1155 return NULL;
1156 end = buf + 100;
1157
1158 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1159 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1160 name = eap_get_name(eap_methods[i].vendor,
1161 eap_methods[i].method);
1162 if (name) {
1163 ret = os_snprintf(pos, end - pos, "%s%s",
1164 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001165 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 break;
1167 pos += ret;
1168 }
1169 }
1170
1171 end[-1] = '\0';
1172
1173 return buf;
1174}
1175
1176
1177static int wpa_config_parse_password(const struct parse_data *data,
1178 struct wpa_ssid *ssid, int line,
1179 const char *value)
1180{
1181 u8 *hash;
1182
1183 if (os_strcmp(value, "NULL") == 0) {
1184 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001185 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 ssid->eap.password = NULL;
1187 ssid->eap.password_len = 0;
1188 return 0;
1189 }
1190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001191#ifdef CONFIG_EXT_PASSWORD
1192 if (os_strncmp(value, "ext:", 4) == 0) {
1193 char *name = os_strdup(value + 4);
1194 if (name == NULL)
1195 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001196 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001197 ssid->eap.password = (u8 *) name;
1198 ssid->eap.password_len = os_strlen(name);
1199 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1200 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1201 return 0;
1202 }
1203#endif /* CONFIG_EXT_PASSWORD */
1204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 if (os_strncmp(value, "hash:", 5) != 0) {
1206 char *tmp;
1207 size_t res_len;
1208
1209 tmp = wpa_config_parse_string(value, &res_len);
1210 if (tmp == NULL) {
1211 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1212 "password.", line);
1213 return -1;
1214 }
1215 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1216 (u8 *) tmp, res_len);
1217
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001218 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 ssid->eap.password = (u8 *) tmp;
1220 ssid->eap.password_len = res_len;
1221 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001222 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223
1224 return 0;
1225 }
1226
1227
1228 /* NtPasswordHash: hash:<32 hex digits> */
1229 if (os_strlen(value + 5) != 2 * 16) {
1230 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1231 "(expected 32 hex digits)", line);
1232 return -1;
1233 }
1234
1235 hash = os_malloc(16);
1236 if (hash == NULL)
1237 return -1;
1238
1239 if (hexstr2bin(value + 5, hash, 16)) {
1240 os_free(hash);
1241 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1242 return -1;
1243 }
1244
1245 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1246
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001247 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 ssid->eap.password = hash;
1249 ssid->eap.password_len = 16;
1250 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001251 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252
1253 return 0;
1254}
1255
1256
1257static char * wpa_config_write_password(const struct parse_data *data,
1258 struct wpa_ssid *ssid)
1259{
1260 char *buf;
1261
1262 if (ssid->eap.password == NULL)
1263 return NULL;
1264
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001265#ifdef CONFIG_EXT_PASSWORD
1266 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1267 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1268 if (buf == NULL)
1269 return NULL;
1270 os_memcpy(buf, "ext:", 4);
1271 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1272 return buf;
1273 }
1274#endif /* CONFIG_EXT_PASSWORD */
1275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1277 return wpa_config_write_string(
1278 ssid->eap.password, ssid->eap.password_len);
1279 }
1280
1281 buf = os_malloc(5 + 32 + 1);
1282 if (buf == NULL)
1283 return NULL;
1284
1285 os_memcpy(buf, "hash:", 5);
1286 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1287
1288 return buf;
1289}
1290#endif /* IEEE8021X_EAPOL */
1291
1292
1293static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1294 const char *value, int idx)
1295{
1296 char *buf, title[20];
1297 int res;
1298
1299 buf = wpa_config_parse_string(value, len);
1300 if (buf == NULL) {
1301 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1302 line, idx, value);
1303 return -1;
1304 }
1305 if (*len > MAX_WEP_KEY_LEN) {
1306 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1307 line, idx, value);
1308 os_free(buf);
1309 return -1;
1310 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001311 if (*len && *len != 5 && *len != 13 && *len != 16) {
1312 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1313 "this network block will be ignored",
1314 line, (unsigned int) *len);
1315 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001317 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1321 return 0;
1322}
1323
1324
1325static int wpa_config_parse_wep_key0(const struct parse_data *data,
1326 struct wpa_ssid *ssid, int line,
1327 const char *value)
1328{
1329 return wpa_config_parse_wep_key(ssid->wep_key[0],
1330 &ssid->wep_key_len[0], line,
1331 value, 0);
1332}
1333
1334
1335static int wpa_config_parse_wep_key1(const struct parse_data *data,
1336 struct wpa_ssid *ssid, int line,
1337 const char *value)
1338{
1339 return wpa_config_parse_wep_key(ssid->wep_key[1],
1340 &ssid->wep_key_len[1], line,
1341 value, 1);
1342}
1343
1344
1345static int wpa_config_parse_wep_key2(const struct parse_data *data,
1346 struct wpa_ssid *ssid, int line,
1347 const char *value)
1348{
1349 return wpa_config_parse_wep_key(ssid->wep_key[2],
1350 &ssid->wep_key_len[2], line,
1351 value, 2);
1352}
1353
1354
1355static int wpa_config_parse_wep_key3(const struct parse_data *data,
1356 struct wpa_ssid *ssid, int line,
1357 const char *value)
1358{
1359 return wpa_config_parse_wep_key(ssid->wep_key[3],
1360 &ssid->wep_key_len[3], line,
1361 value, 3);
1362}
1363
1364
1365#ifndef NO_CONFIG_WRITE
1366static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1367{
1368 if (ssid->wep_key_len[idx] == 0)
1369 return NULL;
1370 return wpa_config_write_string(ssid->wep_key[idx],
1371 ssid->wep_key_len[idx]);
1372}
1373
1374
1375static char * wpa_config_write_wep_key0(const struct parse_data *data,
1376 struct wpa_ssid *ssid)
1377{
1378 return wpa_config_write_wep_key(ssid, 0);
1379}
1380
1381
1382static char * wpa_config_write_wep_key1(const struct parse_data *data,
1383 struct wpa_ssid *ssid)
1384{
1385 return wpa_config_write_wep_key(ssid, 1);
1386}
1387
1388
1389static char * wpa_config_write_wep_key2(const struct parse_data *data,
1390 struct wpa_ssid *ssid)
1391{
1392 return wpa_config_write_wep_key(ssid, 2);
1393}
1394
1395
1396static char * wpa_config_write_wep_key3(const struct parse_data *data,
1397 struct wpa_ssid *ssid)
1398{
1399 return wpa_config_write_wep_key(ssid, 3);
1400}
1401#endif /* NO_CONFIG_WRITE */
1402
1403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404#ifdef CONFIG_P2P
1405
Dmitry Shmidt54605472013-11-08 11:10:19 -08001406static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1407 struct wpa_ssid *ssid, int line,
1408 const char *value)
1409{
1410 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1411 os_strcmp(value, "any") == 0) {
1412 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1413 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1414 return 0;
1415 }
1416 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1417 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1418 line, value);
1419 return -1;
1420 }
1421 ssid->bssid_set = 1;
1422 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1423 MAC2STR(ssid->go_p2p_dev_addr));
1424 return 0;
1425}
1426
1427
1428#ifndef NO_CONFIG_WRITE
1429static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1430 struct wpa_ssid *ssid)
1431{
1432 char *value;
1433 int res;
1434
1435 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1436 return NULL;
1437
1438 value = os_malloc(20);
1439 if (value == NULL)
1440 return NULL;
1441 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001442 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001443 os_free(value);
1444 return NULL;
1445 }
1446 value[20 - 1] = '\0';
1447 return value;
1448}
1449#endif /* NO_CONFIG_WRITE */
1450
1451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001452static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1453 struct wpa_ssid *ssid, int line,
1454 const char *value)
1455{
1456 const char *pos;
1457 u8 *buf, *n, addr[ETH_ALEN];
1458 size_t count;
1459
1460 buf = NULL;
1461 count = 0;
1462
1463 pos = value;
1464 while (pos && *pos) {
1465 while (*pos == ' ')
1466 pos++;
1467
1468 if (hwaddr_aton(pos, addr)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001469 if (count == 0) {
1470 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1471 "p2p_client_list address '%s'.",
1472 line, value);
1473 os_free(buf);
1474 return -1;
1475 }
1476 /* continue anyway since this could have been from a
1477 * truncated configuration file line */
1478 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1479 "truncated p2p_client_list address '%s'",
1480 line, pos);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001481 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001482 n = os_realloc_array(buf, count + 1, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001483 if (n == NULL) {
1484 os_free(buf);
1485 return -1;
1486 }
1487 buf = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001488 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1489 os_memcpy(buf, addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001490 count++;
1491 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1492 addr, ETH_ALEN);
1493 }
1494
1495 pos = os_strchr(pos, ' ');
1496 }
1497
1498 os_free(ssid->p2p_client_list);
1499 ssid->p2p_client_list = buf;
1500 ssid->num_p2p_clients = count;
1501
1502 return 0;
1503}
1504
1505
1506#ifndef NO_CONFIG_WRITE
1507static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1508 struct wpa_ssid *ssid)
1509{
1510 char *value, *end, *pos;
1511 int res;
1512 size_t i;
1513
1514 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1515 return NULL;
1516
1517 value = os_malloc(20 * ssid->num_p2p_clients);
1518 if (value == NULL)
1519 return NULL;
1520 pos = value;
1521 end = value + 20 * ssid->num_p2p_clients;
1522
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001523 for (i = ssid->num_p2p_clients; i > 0; i--) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001524 res = os_snprintf(pos, end - pos, MACSTR " ",
1525 MAC2STR(ssid->p2p_client_list +
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001526 (i - 1) * ETH_ALEN));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001527 if (os_snprintf_error(end - pos, res)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001528 os_free(value);
1529 return NULL;
1530 }
1531 pos += res;
1532 }
1533
1534 if (pos > value)
1535 pos[-1] = '\0';
1536
1537 return value;
1538}
1539#endif /* NO_CONFIG_WRITE */
1540
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001541
1542static int wpa_config_parse_psk_list(const struct parse_data *data,
1543 struct wpa_ssid *ssid, int line,
1544 const char *value)
1545{
1546 struct psk_list_entry *p;
1547 const char *pos;
1548
1549 p = os_zalloc(sizeof(*p));
1550 if (p == NULL)
1551 return -1;
1552
1553 pos = value;
1554 if (os_strncmp(pos, "P2P-", 4) == 0) {
1555 p->p2p = 1;
1556 pos += 4;
1557 }
1558
1559 if (hwaddr_aton(pos, p->addr)) {
1560 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1561 line, pos);
1562 os_free(p);
1563 return -1;
1564 }
1565 pos += 17;
1566 if (*pos != '-') {
1567 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1568 line, pos);
1569 os_free(p);
1570 return -1;
1571 }
1572 pos++;
1573
1574 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1575 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1576 line, pos);
1577 os_free(p);
1578 return -1;
1579 }
1580
1581 dl_list_add(&ssid->psk_list, &p->list);
1582
1583 return 0;
1584}
1585
1586
1587#ifndef NO_CONFIG_WRITE
1588static char * wpa_config_write_psk_list(const struct parse_data *data,
1589 struct wpa_ssid *ssid)
1590{
1591 return NULL;
1592}
1593#endif /* NO_CONFIG_WRITE */
1594
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001595#endif /* CONFIG_P2P */
1596
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001597
1598#ifdef CONFIG_MESH
1599
1600static int wpa_config_parse_mesh_ht_mode(const struct parse_data *data,
1601 struct wpa_ssid *ssid, int line,
1602 const char *value)
1603{
1604 int htval = 0;
1605
1606 if (os_strcmp(value, "NOHT") == 0)
1607 htval = CHAN_NO_HT;
1608 else if (os_strcmp(value, "HT20") == 0)
1609 htval = CHAN_HT20;
1610 else if (os_strcmp(value, "HT40-") == 0)
1611 htval = CHAN_HT40MINUS;
1612 else if (os_strcmp(value, "HT40+") == 0)
1613 htval = CHAN_HT40PLUS;
1614 else {
1615 wpa_printf(MSG_ERROR,
1616 "Line %d: no ht_mode configured.", line);
1617 return -1;
1618 }
1619
1620 wpa_printf(MSG_MSGDUMP, "mesh_ht_mode: 0x%x", htval);
1621 ssid->mesh_ht_mode = htval;
1622 return 0;
1623}
1624
1625
1626static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1627 struct wpa_ssid *ssid, int line,
1628 const char *value)
1629{
1630 int *rates = wpa_config_parse_int_array(value);
1631
1632 if (rates == NULL) {
1633 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1634 line, value);
1635 return -1;
1636 }
1637 if (rates[0] == 0) {
1638 os_free(rates);
1639 rates = NULL;
1640 }
1641
1642 os_free(ssid->mesh_basic_rates);
1643 ssid->mesh_basic_rates = rates;
1644
1645 return 0;
1646}
1647
1648
1649#ifndef NO_CONFIG_WRITE
1650
1651static char * wpa_config_write_mesh_ht_mode(const struct parse_data *data,
1652 struct wpa_ssid *ssid)
1653{
1654 char *val;
1655
1656 switch (ssid->mesh_ht_mode) {
1657 default:
1658 val = NULL;
1659 break;
1660 case CHAN_NO_HT:
1661 val = "NOHT";
1662 break;
1663 case CHAN_HT20:
1664 val = "HT20";
1665 break;
1666 case CHAN_HT40MINUS:
1667 val = "HT40-";
1668 break;
1669 case CHAN_HT40PLUS:
1670 val = "HT40+";
1671 break;
1672 }
1673 return val ? os_strdup(val) : NULL;
1674}
1675
1676
1677static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1678 struct wpa_ssid *ssid)
1679{
1680 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1681}
1682
1683#endif /* NO_CONFIG_WRITE */
1684
1685#endif /* CONFIG_MESH */
1686
1687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688/* Helper macros for network block parser */
1689
1690#ifdef OFFSET
1691#undef OFFSET
1692#endif /* OFFSET */
1693/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1694#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1695
1696/* STR: Define a string variable for an ASCII string; f = field name */
1697#ifdef NO_CONFIG_WRITE
1698#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1699#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1700#else /* NO_CONFIG_WRITE */
1701#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1702#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1703#endif /* NO_CONFIG_WRITE */
1704#define STR(f) _STR(f), NULL, NULL, NULL, 0
1705#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1706#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1707#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1708
1709/* STR_LEN: Define a string variable with a separate variable for storing the
1710 * data length. Unlike STR(), this can be used to store arbitrary binary data
1711 * (i.e., even nul termination character). */
1712#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1713#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1714#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1715#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1716#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1717
1718/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1719 * explicitly specified. */
1720#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1721#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1722#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1723
1724#ifdef NO_CONFIG_WRITE
1725#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1726#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1727#else /* NO_CONFIG_WRITE */
1728#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1729 OFFSET(f), (void *) 0
1730#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1731 OFFSET(eap.f), (void *) 0
1732#endif /* NO_CONFIG_WRITE */
1733
1734/* INT: Define an integer variable */
1735#define INT(f) _INT(f), NULL, NULL, 0
1736#define INTe(f) _INTe(f), NULL, NULL, 0
1737
1738/* INT_RANGE: Define an integer variable with allowed value range */
1739#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1740
1741/* FUNC: Define a configuration variable that uses a custom function for
1742 * parsing and writing the value. */
1743#ifdef NO_CONFIG_WRITE
1744#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1745#else /* NO_CONFIG_WRITE */
1746#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1747 NULL, NULL, NULL, NULL
1748#endif /* NO_CONFIG_WRITE */
1749#define FUNC(f) _FUNC(f), 0
1750#define FUNC_KEY(f) _FUNC(f), 1
1751
1752/*
1753 * Table of network configuration variables. This table is used to parse each
1754 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1755 * that is inside a network block.
1756 *
1757 * This table is generated using the helper macros defined above and with
1758 * generous help from the C pre-processor. The field name is stored as a string
1759 * into .name and for STR and INT types, the offset of the target buffer within
1760 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1761 * offset to the field containing the length of the configuration variable.
1762 * .param3 and .param4 can be used to mark the allowed range (length for STR
1763 * and value for INT).
1764 *
1765 * For each configuration line in wpa_supplicant.conf, the parser goes through
1766 * this table and select the entry that matches with the field name. The parser
1767 * function (.parser) is then called to parse the actual value of the field.
1768 *
1769 * This kind of mechanism makes it easy to add new configuration parameters,
1770 * since only one line needs to be added into this table and into the
1771 * struct wpa_ssid definition if the new variable is either a string or
1772 * integer. More complex types will need to use their own parser and writer
1773 * functions.
1774 */
1775static const struct parse_data ssid_fields[] = {
1776 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1777 { INT_RANGE(scan_ssid, 0, 1) },
1778 { FUNC(bssid) },
1779 { FUNC_KEY(psk) },
1780 { FUNC(proto) },
1781 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001782 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 { FUNC(pairwise) },
1784 { FUNC(group) },
1785 { FUNC(auth_alg) },
1786 { FUNC(scan_freq) },
1787 { FUNC(freq_list) },
1788#ifdef IEEE8021X_EAPOL
1789 { FUNC(eap) },
1790 { STR_LENe(identity) },
1791 { STR_LENe(anonymous_identity) },
1792 { FUNC_KEY(password) },
1793 { STRe(ca_cert) },
1794 { STRe(ca_path) },
1795 { STRe(client_cert) },
1796 { STRe(private_key) },
1797 { STR_KEYe(private_key_passwd) },
1798 { STRe(dh_file) },
1799 { STRe(subject_match) },
1800 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001801 { STRe(domain_suffix_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 { STRe(ca_cert2) },
1803 { STRe(ca_path2) },
1804 { STRe(client_cert2) },
1805 { STRe(private_key2) },
1806 { STR_KEYe(private_key2_passwd) },
1807 { STRe(dh_file2) },
1808 { STRe(subject_match2) },
1809 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001810 { STRe(domain_suffix_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 { STRe(phase1) },
1812 { STRe(phase2) },
1813 { STRe(pcsc) },
1814 { STR_KEYe(pin) },
1815 { STRe(engine_id) },
1816 { STRe(key_id) },
1817 { STRe(cert_id) },
1818 { STRe(ca_cert_id) },
1819 { STR_KEYe(pin2) },
1820 { STRe(engine2_id) },
1821 { STRe(key2_id) },
1822 { STRe(cert2_id) },
1823 { STRe(ca_cert2_id) },
1824 { INTe(engine) },
1825 { INTe(engine2) },
1826 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001827 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001828 { STRe(openssl_ciphers) },
1829 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830#endif /* IEEE8021X_EAPOL */
1831 { FUNC_KEY(wep_key0) },
1832 { FUNC_KEY(wep_key1) },
1833 { FUNC_KEY(wep_key2) },
1834 { FUNC_KEY(wep_key3) },
1835 { INT(wep_tx_keyidx) },
1836 { INT(priority) },
1837#ifdef IEEE8021X_EAPOL
1838 { INT(eap_workaround) },
1839 { STRe(pac_file) },
1840 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001841 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001843#ifdef CONFIG_MESH
1844 { INT_RANGE(mode, 0, 5) },
1845 { INT_RANGE(no_auto_peer, 0, 1) },
1846#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001848#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 { INT_RANGE(proactive_key_caching, 0, 1) },
1850 { INT_RANGE(disabled, 0, 2) },
1851 { STR(id_str) },
1852#ifdef CONFIG_IEEE80211W
1853 { INT_RANGE(ieee80211w, 0, 2) },
1854#endif /* CONFIG_IEEE80211W */
1855 { INT_RANGE(peerkey, 0, 1) },
1856 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001857 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858#ifdef CONFIG_MESH
1859 { FUNC(mesh_ht_mode) },
1860 { FUNC(mesh_basic_rates) },
1861 { INT(dot11MeshMaxRetries) },
1862 { INT(dot11MeshRetryTimeout) },
1863 { INT(dot11MeshConfirmTimeout) },
1864 { INT(dot11MeshHoldingTimeout) },
1865#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 { INT(wpa_ptk_rekey) },
1867 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001868 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001869#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001870 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001872 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001874#ifdef CONFIG_HT_OVERRIDES
1875 { INT_RANGE(disable_ht, 0, 1) },
1876 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001877 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001878 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001879 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001880 { INT_RANGE(disable_max_amsdu, -1, 1) },
1881 { INT_RANGE(ampdu_factor, -1, 3) },
1882 { INT_RANGE(ampdu_density, -1, 7) },
1883 { STR(ht_mcs) },
1884#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001885#ifdef CONFIG_VHT_OVERRIDES
1886 { INT_RANGE(disable_vht, 0, 1) },
1887 { INT(vht_capa) },
1888 { INT(vht_capa_mask) },
1889 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1890 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1891 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1892 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1893 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1894 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1895 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1896 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1897 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1898 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1899 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1900 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1901 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1902 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1903 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1904 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1905#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001906 { INT(ap_max_inactivity) },
1907 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001908 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001909#ifdef CONFIG_MACSEC
1910 { INT_RANGE(macsec_policy, 0, 1) },
1911#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001912#ifdef CONFIG_HS20
1913 { INT(update_identifier) },
1914#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001915 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916};
1917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918#undef OFFSET
1919#undef _STR
1920#undef STR
1921#undef STR_KEY
1922#undef _STR_LEN
1923#undef STR_LEN
1924#undef STR_LEN_KEY
1925#undef _STR_RANGE
1926#undef STR_RANGE
1927#undef STR_RANGE_KEY
1928#undef _INT
1929#undef INT
1930#undef INT_RANGE
1931#undef _FUNC
1932#undef FUNC
1933#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001934#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935
1936
1937/**
1938 * wpa_config_add_prio_network - Add a network to priority lists
1939 * @config: Configuration data from wpa_config_read()
1940 * @ssid: Pointer to the network configuration to be added to the list
1941 * Returns: 0 on success, -1 on failure
1942 *
1943 * This function is used to add a network block to the priority list of
1944 * networks. This must be called for each network when reading in the full
1945 * configuration. In addition, this can be used indirectly when updating
1946 * priorities by calling wpa_config_update_prio_list().
1947 */
1948int wpa_config_add_prio_network(struct wpa_config *config,
1949 struct wpa_ssid *ssid)
1950{
1951 int prio;
1952 struct wpa_ssid *prev, **nlist;
1953
1954 /*
1955 * Add to an existing priority list if one is available for the
1956 * configured priority level for this network.
1957 */
1958 for (prio = 0; prio < config->num_prio; prio++) {
1959 prev = config->pssid[prio];
1960 if (prev->priority == ssid->priority) {
1961 while (prev->pnext)
1962 prev = prev->pnext;
1963 prev->pnext = ssid;
1964 return 0;
1965 }
1966 }
1967
1968 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001969 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1970 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 if (nlist == NULL)
1972 return -1;
1973
1974 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001975 if (nlist[prio]->priority < ssid->priority) {
1976 os_memmove(&nlist[prio + 1], &nlist[prio],
1977 (config->num_prio - prio) *
1978 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 }
1982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 nlist[prio] = ssid;
1984 config->num_prio++;
1985 config->pssid = nlist;
1986
1987 return 0;
1988}
1989
1990
1991/**
1992 * wpa_config_update_prio_list - Update network priority list
1993 * @config: Configuration data from wpa_config_read()
1994 * Returns: 0 on success, -1 on failure
1995 *
1996 * This function is called to update the priority list of networks in the
1997 * configuration when a network is being added or removed. This is also called
1998 * if a priority for a network is changed.
1999 */
2000int wpa_config_update_prio_list(struct wpa_config *config)
2001{
2002 struct wpa_ssid *ssid;
2003 int ret = 0;
2004
2005 os_free(config->pssid);
2006 config->pssid = NULL;
2007 config->num_prio = 0;
2008
2009 ssid = config->ssid;
2010 while (ssid) {
2011 ssid->pnext = NULL;
2012 if (wpa_config_add_prio_network(config, ssid) < 0)
2013 ret = -1;
2014 ssid = ssid->next;
2015 }
2016
2017 return ret;
2018}
2019
2020
2021#ifdef IEEE8021X_EAPOL
2022static void eap_peer_config_free(struct eap_peer_config *eap)
2023{
2024 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002025 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002027 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 os_free(eap->ca_cert);
2029 os_free(eap->ca_path);
2030 os_free(eap->client_cert);
2031 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002032 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 os_free(eap->dh_file);
2034 os_free(eap->subject_match);
2035 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002036 os_free(eap->domain_suffix_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 os_free(eap->ca_cert2);
2038 os_free(eap->ca_path2);
2039 os_free(eap->client_cert2);
2040 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002041 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 os_free(eap->dh_file2);
2043 os_free(eap->subject_match2);
2044 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002045 os_free(eap->domain_suffix_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 os_free(eap->phase1);
2047 os_free(eap->phase2);
2048 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002049 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 os_free(eap->engine_id);
2051 os_free(eap->key_id);
2052 os_free(eap->cert_id);
2053 os_free(eap->ca_cert_id);
2054 os_free(eap->key2_id);
2055 os_free(eap->cert2_id);
2056 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002057 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 os_free(eap->engine2_id);
2059 os_free(eap->otp);
2060 os_free(eap->pending_req_otp);
2061 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002062 bin_clear_free(eap->new_password, eap->new_password_len);
2063 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002064 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065}
2066#endif /* IEEE8021X_EAPOL */
2067
2068
2069/**
2070 * wpa_config_free_ssid - Free network/ssid configuration data
2071 * @ssid: Configuration data for the network
2072 *
2073 * This function frees all resources allocated for the network configuration
2074 * data.
2075 */
2076void wpa_config_free_ssid(struct wpa_ssid *ssid)
2077{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002078 struct psk_list_entry *psk;
2079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002081 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002082 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083#ifdef IEEE8021X_EAPOL
2084 eap_peer_config_free(&ssid->eap);
2085#endif /* IEEE8021X_EAPOL */
2086 os_free(ssid->id_str);
2087 os_free(ssid->scan_freq);
2088 os_free(ssid->freq_list);
2089 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 os_free(ssid->p2p_client_list);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002091#ifdef CONFIG_HT_OVERRIDES
2092 os_free(ssid->ht_mcs);
2093#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002094#ifdef CONFIG_MESH
2095 os_free(ssid->mesh_basic_rates);
2096#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002097 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2098 list))) {
2099 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002100 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002101 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002102 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103}
2104
2105
Dmitry Shmidt04949592012-07-19 12:16:46 -07002106void wpa_config_free_cred(struct wpa_cred *cred)
2107{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002108 size_t i;
2109
Dmitry Shmidt04949592012-07-19 12:16:46 -07002110 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002111 str_clear_free(cred->username);
2112 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002113 os_free(cred->ca_cert);
2114 os_free(cred->client_cert);
2115 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002116 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002117 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002118 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002119 for (i = 0; i < cred->num_domain; i++)
2120 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002121 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002122 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002123 os_free(cred->eap_method);
2124 os_free(cred->phase1);
2125 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002126 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002127 os_free(cred->roaming_partner);
2128 os_free(cred->provisioning_sp);
2129 for (i = 0; i < cred->num_req_conn_capab; i++)
2130 os_free(cred->req_conn_capab_port[i]);
2131 os_free(cred->req_conn_capab_port);
2132 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002133 os_free(cred);
2134}
2135
2136
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002137void wpa_config_flush_blobs(struct wpa_config *config)
2138{
2139#ifndef CONFIG_NO_CONFIG_BLOBS
2140 struct wpa_config_blob *blob, *prev;
2141
2142 blob = config->blobs;
2143 config->blobs = NULL;
2144 while (blob) {
2145 prev = blob;
2146 blob = blob->next;
2147 wpa_config_free_blob(prev);
2148 }
2149#endif /* CONFIG_NO_CONFIG_BLOBS */
2150}
2151
2152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153/**
2154 * wpa_config_free - Free configuration data
2155 * @config: Configuration data from wpa_config_read()
2156 *
2157 * This function frees all resources allocated for the configuration data by
2158 * wpa_config_read().
2159 */
2160void wpa_config_free(struct wpa_config *config)
2161{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002163 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002164 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165
2166 ssid = config->ssid;
2167 while (ssid) {
2168 prev = ssid;
2169 ssid = ssid->next;
2170 wpa_config_free_ssid(prev);
2171 }
2172
Dmitry Shmidt04949592012-07-19 12:16:46 -07002173 cred = config->cred;
2174 while (cred) {
2175 cprev = cred;
2176 cred = cred->next;
2177 wpa_config_free_cred(cprev);
2178 }
2179
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002180 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181
Dmitry Shmidt04949592012-07-19 12:16:46 -07002182 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002183 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2184 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 os_free(config->ctrl_interface);
2186 os_free(config->ctrl_interface_group);
2187 os_free(config->opensc_engine_path);
2188 os_free(config->pkcs11_engine_path);
2189 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002190 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002191 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002192 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 os_free(config->driver_param);
2194 os_free(config->device_name);
2195 os_free(config->manufacturer);
2196 os_free(config->model_name);
2197 os_free(config->model_number);
2198 os_free(config->serial_number);
2199 os_free(config->config_methods);
2200 os_free(config->p2p_ssid_postfix);
2201 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002202 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002203 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002204 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002205 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206 wpabuf_free(config->wps_nfc_dh_pubkey);
2207 wpabuf_free(config->wps_nfc_dh_privkey);
2208 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002210 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002211 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002212 os_free(config->osu_dir);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002213 os_free(config->wowlan_triggers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 os_free(config);
2215}
2216
2217
2218/**
2219 * wpa_config_foreach_network - Iterate over each configured network
2220 * @config: Configuration data from wpa_config_read()
2221 * @func: Callback function to process each network
2222 * @arg: Opaque argument to pass to callback function
2223 *
2224 * Iterate over the set of configured networks calling the specified
2225 * function for each item. We guard against callbacks removing the
2226 * supplied network.
2227 */
2228void wpa_config_foreach_network(struct wpa_config *config,
2229 void (*func)(void *, struct wpa_ssid *),
2230 void *arg)
2231{
2232 struct wpa_ssid *ssid, *next;
2233
2234 ssid = config->ssid;
2235 while (ssid) {
2236 next = ssid->next;
2237 func(arg, ssid);
2238 ssid = next;
2239 }
2240}
2241
2242
2243/**
2244 * wpa_config_get_network - Get configured network based on id
2245 * @config: Configuration data from wpa_config_read()
2246 * @id: Unique network id to search for
2247 * Returns: Network configuration or %NULL if not found
2248 */
2249struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2250{
2251 struct wpa_ssid *ssid;
2252
2253 ssid = config->ssid;
2254 while (ssid) {
2255 if (id == ssid->id)
2256 break;
2257 ssid = ssid->next;
2258 }
2259
2260 return ssid;
2261}
2262
2263
2264/**
2265 * wpa_config_add_network - Add a new network with empty configuration
2266 * @config: Configuration data from wpa_config_read()
2267 * Returns: The new network configuration or %NULL if operation failed
2268 */
2269struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2270{
2271 int id;
2272 struct wpa_ssid *ssid, *last = NULL;
2273
2274 id = -1;
2275 ssid = config->ssid;
2276 while (ssid) {
2277 if (ssid->id > id)
2278 id = ssid->id;
2279 last = ssid;
2280 ssid = ssid->next;
2281 }
2282 id++;
2283
2284 ssid = os_zalloc(sizeof(*ssid));
2285 if (ssid == NULL)
2286 return NULL;
2287 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002288 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 if (last)
2290 last->next = ssid;
2291 else
2292 config->ssid = ssid;
2293
2294 wpa_config_update_prio_list(config);
2295
2296 return ssid;
2297}
2298
2299
2300/**
2301 * wpa_config_remove_network - Remove a configured network based on id
2302 * @config: Configuration data from wpa_config_read()
2303 * @id: Unique network id to search for
2304 * Returns: 0 on success, or -1 if the network was not found
2305 */
2306int wpa_config_remove_network(struct wpa_config *config, int id)
2307{
2308 struct wpa_ssid *ssid, *prev = NULL;
2309
2310 ssid = config->ssid;
2311 while (ssid) {
2312 if (id == ssid->id)
2313 break;
2314 prev = ssid;
2315 ssid = ssid->next;
2316 }
2317
2318 if (ssid == NULL)
2319 return -1;
2320
2321 if (prev)
2322 prev->next = ssid->next;
2323 else
2324 config->ssid = ssid->next;
2325
2326 wpa_config_update_prio_list(config);
2327 wpa_config_free_ssid(ssid);
2328 return 0;
2329}
2330
2331
2332/**
2333 * wpa_config_set_network_defaults - Set network default values
2334 * @ssid: Pointer to network configuration data
2335 */
2336void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2337{
2338 ssid->proto = DEFAULT_PROTO;
2339 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2340 ssid->group_cipher = DEFAULT_GROUP;
2341 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002342 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343#ifdef IEEE8021X_EAPOL
2344 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2345 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2346 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002347 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002349#ifdef CONFIG_MESH
2350 ssid->mesh_ht_mode = DEFAULT_MESH_HT_MODE;
2351 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2352 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2353 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2354 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2355#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002356#ifdef CONFIG_HT_OVERRIDES
2357 ssid->disable_ht = DEFAULT_DISABLE_HT;
2358 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002359 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002360 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002361 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2362 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2363 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2364#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002365#ifdef CONFIG_VHT_OVERRIDES
2366 ssid->vht_rx_mcs_nss_1 = -1;
2367 ssid->vht_rx_mcs_nss_2 = -1;
2368 ssid->vht_rx_mcs_nss_3 = -1;
2369 ssid->vht_rx_mcs_nss_4 = -1;
2370 ssid->vht_rx_mcs_nss_5 = -1;
2371 ssid->vht_rx_mcs_nss_6 = -1;
2372 ssid->vht_rx_mcs_nss_7 = -1;
2373 ssid->vht_rx_mcs_nss_8 = -1;
2374 ssid->vht_tx_mcs_nss_1 = -1;
2375 ssid->vht_tx_mcs_nss_2 = -1;
2376 ssid->vht_tx_mcs_nss_3 = -1;
2377 ssid->vht_tx_mcs_nss_4 = -1;
2378 ssid->vht_tx_mcs_nss_5 = -1;
2379 ssid->vht_tx_mcs_nss_6 = -1;
2380 ssid->vht_tx_mcs_nss_7 = -1;
2381 ssid->vht_tx_mcs_nss_8 = -1;
2382#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002383 ssid->proactive_key_caching = -1;
2384#ifdef CONFIG_IEEE80211W
2385 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2386#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002387 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388}
2389
2390
2391/**
2392 * wpa_config_set - Set a variable in network configuration
2393 * @ssid: Pointer to network configuration data
2394 * @var: Variable name, e.g., "ssid"
2395 * @value: Variable value
2396 * @line: Line number in configuration file or 0 if not used
2397 * Returns: 0 on success, -1 on failure
2398 *
2399 * This function can be used to set network configuration variables based on
2400 * both the configuration file and management interface input. The value
2401 * parameter must be in the same format as the text-based configuration file is
2402 * using. For example, strings are using double quotation marks.
2403 */
2404int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2405 int line)
2406{
2407 size_t i;
2408 int ret = 0;
2409
2410 if (ssid == NULL || var == NULL || value == NULL)
2411 return -1;
2412
2413 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2414 const struct parse_data *field = &ssid_fields[i];
2415 if (os_strcmp(var, field->name) != 0)
2416 continue;
2417
2418 if (field->parser(field, ssid, line, value)) {
2419 if (line) {
2420 wpa_printf(MSG_ERROR, "Line %d: failed to "
2421 "parse %s '%s'.", line, var, value);
2422 }
2423 ret = -1;
2424 }
2425 break;
2426 }
2427 if (i == NUM_SSID_FIELDS) {
2428 if (line) {
2429 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2430 "'%s'.", line, var);
2431 }
2432 ret = -1;
2433 }
2434
2435 return ret;
2436}
2437
2438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2440 const char *value)
2441{
2442 size_t len;
2443 char *buf;
2444 int ret;
2445
2446 len = os_strlen(value);
2447 buf = os_malloc(len + 3);
2448 if (buf == NULL)
2449 return -1;
2450 buf[0] = '"';
2451 os_memcpy(buf + 1, value, len);
2452 buf[len + 1] = '"';
2453 buf[len + 2] = '\0';
2454 ret = wpa_config_set(ssid, var, buf, 0);
2455 os_free(buf);
2456 return ret;
2457}
2458
2459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460/**
2461 * wpa_config_get_all - Get all options from network configuration
2462 * @ssid: Pointer to network configuration data
2463 * @get_keys: Determines if keys/passwords will be included in returned list
2464 * (if they may be exported)
2465 * Returns: %NULL terminated list of all set keys and their values in the form
2466 * of [key1, val1, key2, val2, ... , NULL]
2467 *
2468 * This function can be used to get list of all configured network properties.
2469 * The caller is responsible for freeing the returned list and all its
2470 * elements.
2471 */
2472char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2473{
2474 const struct parse_data *field;
2475 char *key, *value;
2476 size_t i;
2477 char **props;
2478 int fields_num;
2479
2480 get_keys = get_keys && ssid->export_keys;
2481
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002482 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 if (!props)
2484 return NULL;
2485
2486 fields_num = 0;
2487 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2488 field = &ssid_fields[i];
2489 if (field->key_data && !get_keys)
2490 continue;
2491 value = field->writer(field, ssid);
2492 if (value == NULL)
2493 continue;
2494 if (os_strlen(value) == 0) {
2495 os_free(value);
2496 continue;
2497 }
2498
2499 key = os_strdup(field->name);
2500 if (key == NULL) {
2501 os_free(value);
2502 goto err;
2503 }
2504
2505 props[fields_num * 2] = key;
2506 props[fields_num * 2 + 1] = value;
2507
2508 fields_num++;
2509 }
2510
2511 return props;
2512
2513err:
2514 value = *props;
2515 while (value)
2516 os_free(value++);
2517 os_free(props);
2518 return NULL;
2519}
2520
2521
2522#ifndef NO_CONFIG_WRITE
2523/**
2524 * wpa_config_get - Get a variable in network configuration
2525 * @ssid: Pointer to network configuration data
2526 * @var: Variable name, e.g., "ssid"
2527 * Returns: Value of the variable or %NULL on failure
2528 *
2529 * This function can be used to get network configuration variables. The
2530 * returned value is a copy of the configuration variable in text format, i.e,.
2531 * the same format that the text-based configuration file and wpa_config_set()
2532 * are using for the value. The caller is responsible for freeing the returned
2533 * value.
2534 */
2535char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2536{
2537 size_t i;
2538
2539 if (ssid == NULL || var == NULL)
2540 return NULL;
2541
2542 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2543 const struct parse_data *field = &ssid_fields[i];
2544 if (os_strcmp(var, field->name) == 0)
2545 return field->writer(field, ssid);
2546 }
2547
2548 return NULL;
2549}
2550
2551
2552/**
2553 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2554 * @ssid: Pointer to network configuration data
2555 * @var: Variable name, e.g., "ssid"
2556 * Returns: Value of the variable or %NULL on failure
2557 *
2558 * This function can be used to get network configuration variable like
2559 * wpa_config_get(). The only difference is that this functions does not expose
2560 * key/password material from the configuration. In case a key/password field
2561 * is requested, the returned value is an empty string or %NULL if the variable
2562 * is not set or "*" if the variable is set (regardless of its value). The
2563 * returned value is a copy of the configuration variable in text format, i.e,.
2564 * the same format that the text-based configuration file and wpa_config_set()
2565 * are using for the value. The caller is responsible for freeing the returned
2566 * value.
2567 */
2568char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2569{
2570 size_t i;
2571
2572 if (ssid == NULL || var == NULL)
2573 return NULL;
2574
2575 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2576 const struct parse_data *field = &ssid_fields[i];
2577 if (os_strcmp(var, field->name) == 0) {
2578 char *res = field->writer(field, ssid);
2579 if (field->key_data) {
2580 if (res && res[0]) {
2581 wpa_printf(MSG_DEBUG, "Do not allow "
2582 "key_data field to be "
2583 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002584 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 return os_strdup("*");
2586 }
2587
2588 os_free(res);
2589 return NULL;
2590 }
2591 return res;
2592 }
2593 }
2594
2595 return NULL;
2596}
2597#endif /* NO_CONFIG_WRITE */
2598
2599
2600/**
2601 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2602 * @ssid: Pointer to network configuration data
2603 *
2604 * This function must be called to update WPA PSK when either SSID or the
2605 * passphrase has changed for the network configuration.
2606 */
2607void wpa_config_update_psk(struct wpa_ssid *ssid)
2608{
2609#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002610 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 ssid->psk, PMK_LEN);
2612 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2613 ssid->psk, PMK_LEN);
2614 ssid->psk_set = 1;
2615#endif /* CONFIG_NO_PBKDF2 */
2616}
2617
2618
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002619static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2620 const char *value)
2621{
2622 u8 *proto;
2623 int **port;
2624 int *ports, *nports;
2625 const char *pos;
2626 unsigned int num_ports;
2627
2628 proto = os_realloc_array(cred->req_conn_capab_proto,
2629 cred->num_req_conn_capab + 1, sizeof(u8));
2630 if (proto == NULL)
2631 return -1;
2632 cred->req_conn_capab_proto = proto;
2633
2634 port = os_realloc_array(cred->req_conn_capab_port,
2635 cred->num_req_conn_capab + 1, sizeof(int *));
2636 if (port == NULL)
2637 return -1;
2638 cred->req_conn_capab_port = port;
2639
2640 proto[cred->num_req_conn_capab] = atoi(value);
2641
2642 pos = os_strchr(value, ':');
2643 if (pos == NULL) {
2644 port[cred->num_req_conn_capab] = NULL;
2645 cred->num_req_conn_capab++;
2646 return 0;
2647 }
2648 pos++;
2649
2650 ports = NULL;
2651 num_ports = 0;
2652
2653 while (*pos) {
2654 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2655 if (nports == NULL) {
2656 os_free(ports);
2657 return -1;
2658 }
2659 ports = nports;
2660 ports[num_ports++] = atoi(pos);
2661
2662 pos = os_strchr(pos, ',');
2663 if (pos == NULL)
2664 break;
2665 pos++;
2666 }
2667
2668 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2669 if (nports == NULL) {
2670 os_free(ports);
2671 return -1;
2672 }
2673 ports = nports;
2674 ports[num_ports] = -1;
2675
2676 port[cred->num_req_conn_capab] = ports;
2677 cred->num_req_conn_capab++;
2678 return 0;
2679}
2680
2681
Dmitry Shmidt04949592012-07-19 12:16:46 -07002682int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2683 const char *value, int line)
2684{
2685 char *val;
2686 size_t len;
2687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002688 if (os_strcmp(var, "temporary") == 0) {
2689 cred->temporary = atoi(value);
2690 return 0;
2691 }
2692
Dmitry Shmidt04949592012-07-19 12:16:46 -07002693 if (os_strcmp(var, "priority") == 0) {
2694 cred->priority = atoi(value);
2695 return 0;
2696 }
2697
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002698 if (os_strcmp(var, "sp_priority") == 0) {
2699 int prio = atoi(value);
2700 if (prio < 0 || prio > 255)
2701 return -1;
2702 cred->sp_priority = prio;
2703 return 0;
2704 }
2705
Dmitry Shmidt04949592012-07-19 12:16:46 -07002706 if (os_strcmp(var, "pcsc") == 0) {
2707 cred->pcsc = atoi(value);
2708 return 0;
2709 }
2710
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002711 if (os_strcmp(var, "eap") == 0) {
2712 struct eap_method_type method;
2713 method.method = eap_peer_get_type(value, &method.vendor);
2714 if (method.vendor == EAP_VENDOR_IETF &&
2715 method.method == EAP_TYPE_NONE) {
2716 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2717 "for a credential", line, value);
2718 return -1;
2719 }
2720 os_free(cred->eap_method);
2721 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2722 if (cred->eap_method == NULL)
2723 return -1;
2724 os_memcpy(cred->eap_method, &method, sizeof(method));
2725 return 0;
2726 }
2727
2728 if (os_strcmp(var, "password") == 0 &&
2729 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002730 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002731 cred->password = os_strdup(value);
2732 cred->ext_password = 1;
2733 return 0;
2734 }
2735
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002736 if (os_strcmp(var, "update_identifier") == 0) {
2737 cred->update_identifier = atoi(value);
2738 return 0;
2739 }
2740
2741 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2742 cred->min_dl_bandwidth_home = atoi(value);
2743 return 0;
2744 }
2745
2746 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2747 cred->min_ul_bandwidth_home = atoi(value);
2748 return 0;
2749 }
2750
2751 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2752 cred->min_dl_bandwidth_roaming = atoi(value);
2753 return 0;
2754 }
2755
2756 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2757 cred->min_ul_bandwidth_roaming = atoi(value);
2758 return 0;
2759 }
2760
2761 if (os_strcmp(var, "max_bss_load") == 0) {
2762 cred->max_bss_load = atoi(value);
2763 return 0;
2764 }
2765
2766 if (os_strcmp(var, "req_conn_capab") == 0)
2767 return wpa_config_set_cred_req_conn_capab(cred, value);
2768
2769 if (os_strcmp(var, "ocsp") == 0) {
2770 cred->ocsp = atoi(value);
2771 return 0;
2772 }
2773
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002774 if (os_strcmp(var, "sim_num") == 0) {
2775 cred->sim_num = atoi(value);
2776 return 0;
2777 }
2778
Dmitry Shmidt04949592012-07-19 12:16:46 -07002779 val = wpa_config_parse_string(value, &len);
2780 if (val == NULL) {
2781 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2782 "value '%s'.", line, var, value);
2783 return -1;
2784 }
2785
2786 if (os_strcmp(var, "realm") == 0) {
2787 os_free(cred->realm);
2788 cred->realm = val;
2789 return 0;
2790 }
2791
2792 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002793 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002794 cred->username = val;
2795 return 0;
2796 }
2797
2798 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002799 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002800 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002801 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002802 return 0;
2803 }
2804
2805 if (os_strcmp(var, "ca_cert") == 0) {
2806 os_free(cred->ca_cert);
2807 cred->ca_cert = val;
2808 return 0;
2809 }
2810
2811 if (os_strcmp(var, "client_cert") == 0) {
2812 os_free(cred->client_cert);
2813 cred->client_cert = val;
2814 return 0;
2815 }
2816
2817 if (os_strcmp(var, "private_key") == 0) {
2818 os_free(cred->private_key);
2819 cred->private_key = val;
2820 return 0;
2821 }
2822
2823 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002824 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002825 cred->private_key_passwd = val;
2826 return 0;
2827 }
2828
2829 if (os_strcmp(var, "imsi") == 0) {
2830 os_free(cred->imsi);
2831 cred->imsi = val;
2832 return 0;
2833 }
2834
2835 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002836 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002837 cred->milenage = val;
2838 return 0;
2839 }
2840
Dmitry Shmidt051af732013-10-22 13:52:46 -07002841 if (os_strcmp(var, "domain_suffix_match") == 0) {
2842 os_free(cred->domain_suffix_match);
2843 cred->domain_suffix_match = val;
2844 return 0;
2845 }
2846
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002848 char **new_domain;
2849 new_domain = os_realloc_array(cred->domain,
2850 cred->num_domain + 1,
2851 sizeof(char *));
2852 if (new_domain == NULL) {
2853 os_free(val);
2854 return -1;
2855 }
2856 new_domain[cred->num_domain++] = val;
2857 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002858 return 0;
2859 }
2860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002861 if (os_strcmp(var, "phase1") == 0) {
2862 os_free(cred->phase1);
2863 cred->phase1 = val;
2864 return 0;
2865 }
2866
2867 if (os_strcmp(var, "phase2") == 0) {
2868 os_free(cred->phase2);
2869 cred->phase2 = val;
2870 return 0;
2871 }
2872
2873 if (os_strcmp(var, "roaming_consortium") == 0) {
2874 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2875 wpa_printf(MSG_ERROR, "Line %d: invalid "
2876 "roaming_consortium length %d (3..15 "
2877 "expected)", line, (int) len);
2878 os_free(val);
2879 return -1;
2880 }
2881 os_memcpy(cred->roaming_consortium, val, len);
2882 cred->roaming_consortium_len = len;
2883 os_free(val);
2884 return 0;
2885 }
2886
Dmitry Shmidt051af732013-10-22 13:52:46 -07002887 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2888 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2889 {
2890 wpa_printf(MSG_ERROR, "Line %d: invalid "
2891 "required_roaming_consortium length %d "
2892 "(3..15 expected)", line, (int) len);
2893 os_free(val);
2894 return -1;
2895 }
2896 os_memcpy(cred->required_roaming_consortium, val, len);
2897 cred->required_roaming_consortium_len = len;
2898 os_free(val);
2899 return 0;
2900 }
2901
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002902 if (os_strcmp(var, "excluded_ssid") == 0) {
2903 struct excluded_ssid *e;
2904
2905 if (len > MAX_SSID_LEN) {
2906 wpa_printf(MSG_ERROR, "Line %d: invalid "
2907 "excluded_ssid length %d", line, (int) len);
2908 os_free(val);
2909 return -1;
2910 }
2911
2912 e = os_realloc_array(cred->excluded_ssid,
2913 cred->num_excluded_ssid + 1,
2914 sizeof(struct excluded_ssid));
2915 if (e == NULL) {
2916 os_free(val);
2917 return -1;
2918 }
2919 cred->excluded_ssid = e;
2920
2921 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2922 os_memcpy(e->ssid, val, len);
2923 e->ssid_len = len;
2924
2925 os_free(val);
2926
2927 return 0;
2928 }
2929
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002930 if (os_strcmp(var, "roaming_partner") == 0) {
2931 struct roaming_partner *p;
2932 char *pos;
2933
2934 p = os_realloc_array(cred->roaming_partner,
2935 cred->num_roaming_partner + 1,
2936 sizeof(struct roaming_partner));
2937 if (p == NULL) {
2938 os_free(val);
2939 return -1;
2940 }
2941 cred->roaming_partner = p;
2942
2943 p = &cred->roaming_partner[cred->num_roaming_partner];
2944
2945 pos = os_strchr(val, ',');
2946 if (pos == NULL) {
2947 os_free(val);
2948 return -1;
2949 }
2950 *pos++ = '\0';
2951 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2952 os_free(val);
2953 return -1;
2954 }
2955 os_memcpy(p->fqdn, val, pos - val);
2956
2957 p->exact_match = atoi(pos);
2958
2959 pos = os_strchr(pos, ',');
2960 if (pos == NULL) {
2961 os_free(val);
2962 return -1;
2963 }
2964 *pos++ = '\0';
2965
2966 p->priority = atoi(pos);
2967
2968 pos = os_strchr(pos, ',');
2969 if (pos == NULL) {
2970 os_free(val);
2971 return -1;
2972 }
2973 *pos++ = '\0';
2974
2975 if (os_strlen(pos) >= sizeof(p->country)) {
2976 os_free(val);
2977 return -1;
2978 }
2979 os_memcpy(p->country, pos, os_strlen(pos) + 1);
2980
2981 cred->num_roaming_partner++;
2982 os_free(val);
2983
2984 return 0;
2985 }
2986
2987 if (os_strcmp(var, "provisioning_sp") == 0) {
2988 os_free(cred->provisioning_sp);
2989 cred->provisioning_sp = val;
2990 return 0;
2991 }
2992
Dmitry Shmidt04949592012-07-19 12:16:46 -07002993 if (line) {
2994 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2995 line, var);
2996 }
2997
2998 os_free(val);
2999
3000 return -1;
3001}
3002
3003
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003004static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003005{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003006 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003007 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003008 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003009
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003010 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003011 if (buf == NULL)
3012 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003013 res = os_snprintf(buf, bufsize, "%d", val);
3014 if (os_snprintf_error(bufsize, res)) {
3015 os_free(buf);
3016 buf = NULL;
3017 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003018 return buf;
3019}
3020
3021
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003022static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003023{
3024 if (str == NULL)
3025 return NULL;
3026 return os_strdup(str);
3027}
3028
3029
3030char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3031{
3032 if (os_strcmp(var, "temporary") == 0)
3033 return alloc_int_str(cred->temporary);
3034
3035 if (os_strcmp(var, "priority") == 0)
3036 return alloc_int_str(cred->priority);
3037
3038 if (os_strcmp(var, "sp_priority") == 0)
3039 return alloc_int_str(cred->sp_priority);
3040
3041 if (os_strcmp(var, "pcsc") == 0)
3042 return alloc_int_str(cred->pcsc);
3043
3044 if (os_strcmp(var, "eap") == 0) {
3045 if (!cred->eap_method)
3046 return NULL;
3047 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3048 cred->eap_method[0].method));
3049 }
3050
3051 if (os_strcmp(var, "update_identifier") == 0)
3052 return alloc_int_str(cred->update_identifier);
3053
3054 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3055 return alloc_int_str(cred->min_dl_bandwidth_home);
3056
3057 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3058 return alloc_int_str(cred->min_ul_bandwidth_home);
3059
3060 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3061 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3062
3063 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3064 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3065
3066 if (os_strcmp(var, "max_bss_load") == 0)
3067 return alloc_int_str(cred->max_bss_load);
3068
3069 if (os_strcmp(var, "req_conn_capab") == 0) {
3070 unsigned int i;
3071 char *buf, *end, *pos;
3072 int ret;
3073
3074 if (!cred->num_req_conn_capab)
3075 return NULL;
3076
3077 buf = os_malloc(4000);
3078 if (buf == NULL)
3079 return NULL;
3080 pos = buf;
3081 end = pos + 4000;
3082 for (i = 0; i < cred->num_req_conn_capab; i++) {
3083 int *ports;
3084
3085 ret = os_snprintf(pos, end - pos, "%s%u",
3086 i > 0 ? "\n" : "",
3087 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003088 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003089 return buf;
3090 pos += ret;
3091
3092 ports = cred->req_conn_capab_port[i];
3093 if (ports) {
3094 int j;
3095 for (j = 0; ports[j] != -1; j++) {
3096 ret = os_snprintf(pos, end - pos,
3097 "%s%d",
3098 j > 0 ? "," : ":",
3099 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003100 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003101 return buf;
3102 pos += ret;
3103 }
3104 }
3105 }
3106
3107 return buf;
3108 }
3109
3110 if (os_strcmp(var, "ocsp") == 0)
3111 return alloc_int_str(cred->ocsp);
3112
3113 if (os_strcmp(var, "realm") == 0)
3114 return alloc_strdup(cred->realm);
3115
3116 if (os_strcmp(var, "username") == 0)
3117 return alloc_strdup(cred->username);
3118
3119 if (os_strcmp(var, "password") == 0) {
3120 if (!cred->password)
3121 return NULL;
3122 return alloc_strdup("*");
3123 }
3124
3125 if (os_strcmp(var, "ca_cert") == 0)
3126 return alloc_strdup(cred->ca_cert);
3127
3128 if (os_strcmp(var, "client_cert") == 0)
3129 return alloc_strdup(cred->client_cert);
3130
3131 if (os_strcmp(var, "private_key") == 0)
3132 return alloc_strdup(cred->private_key);
3133
3134 if (os_strcmp(var, "private_key_passwd") == 0) {
3135 if (!cred->private_key_passwd)
3136 return NULL;
3137 return alloc_strdup("*");
3138 }
3139
3140 if (os_strcmp(var, "imsi") == 0)
3141 return alloc_strdup(cred->imsi);
3142
3143 if (os_strcmp(var, "milenage") == 0) {
3144 if (!(cred->milenage))
3145 return NULL;
3146 return alloc_strdup("*");
3147 }
3148
3149 if (os_strcmp(var, "domain_suffix_match") == 0)
3150 return alloc_strdup(cred->domain_suffix_match);
3151
3152 if (os_strcmp(var, "domain") == 0) {
3153 unsigned int i;
3154 char *buf, *end, *pos;
3155 int ret;
3156
3157 if (!cred->num_domain)
3158 return NULL;
3159
3160 buf = os_malloc(4000);
3161 if (buf == NULL)
3162 return NULL;
3163 pos = buf;
3164 end = pos + 4000;
3165
3166 for (i = 0; i < cred->num_domain; i++) {
3167 ret = os_snprintf(pos, end - pos, "%s%s",
3168 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003169 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003170 return buf;
3171 pos += ret;
3172 }
3173
3174 return buf;
3175 }
3176
3177 if (os_strcmp(var, "phase1") == 0)
3178 return alloc_strdup(cred->phase1);
3179
3180 if (os_strcmp(var, "phase2") == 0)
3181 return alloc_strdup(cred->phase2);
3182
3183 if (os_strcmp(var, "roaming_consortium") == 0) {
3184 size_t buflen;
3185 char *buf;
3186
3187 if (!cred->roaming_consortium_len)
3188 return NULL;
3189 buflen = cred->roaming_consortium_len * 2 + 1;
3190 buf = os_malloc(buflen);
3191 if (buf == NULL)
3192 return NULL;
3193 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3194 cred->roaming_consortium_len);
3195 return buf;
3196 }
3197
3198 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3199 size_t buflen;
3200 char *buf;
3201
3202 if (!cred->required_roaming_consortium_len)
3203 return NULL;
3204 buflen = cred->required_roaming_consortium_len * 2 + 1;
3205 buf = os_malloc(buflen);
3206 if (buf == NULL)
3207 return NULL;
3208 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3209 cred->required_roaming_consortium_len);
3210 return buf;
3211 }
3212
3213 if (os_strcmp(var, "excluded_ssid") == 0) {
3214 unsigned int i;
3215 char *buf, *end, *pos;
3216
3217 if (!cred->num_excluded_ssid)
3218 return NULL;
3219
3220 buf = os_malloc(4000);
3221 if (buf == NULL)
3222 return NULL;
3223 pos = buf;
3224 end = pos + 4000;
3225
3226 for (i = 0; i < cred->num_excluded_ssid; i++) {
3227 struct excluded_ssid *e;
3228 int ret;
3229
3230 e = &cred->excluded_ssid[i];
3231 ret = os_snprintf(pos, end - pos, "%s%s",
3232 i > 0 ? "\n" : "",
3233 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003234 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003235 return buf;
3236 pos += ret;
3237 }
3238
3239 return buf;
3240 }
3241
3242 if (os_strcmp(var, "roaming_partner") == 0) {
3243 unsigned int i;
3244 char *buf, *end, *pos;
3245
3246 if (!cred->num_roaming_partner)
3247 return NULL;
3248
3249 buf = os_malloc(4000);
3250 if (buf == NULL)
3251 return NULL;
3252 pos = buf;
3253 end = pos + 4000;
3254
3255 for (i = 0; i < cred->num_roaming_partner; i++) {
3256 struct roaming_partner *p;
3257 int ret;
3258
3259 p = &cred->roaming_partner[i];
3260 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3261 i > 0 ? "\n" : "",
3262 p->fqdn, p->exact_match, p->priority,
3263 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003264 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003265 return buf;
3266 pos += ret;
3267 }
3268
3269 return buf;
3270 }
3271
3272 if (os_strcmp(var, "provisioning_sp") == 0)
3273 return alloc_strdup(cred->provisioning_sp);
3274
3275 return NULL;
3276}
3277
3278
Dmitry Shmidt04949592012-07-19 12:16:46 -07003279struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3280{
3281 struct wpa_cred *cred;
3282
3283 cred = config->cred;
3284 while (cred) {
3285 if (id == cred->id)
3286 break;
3287 cred = cred->next;
3288 }
3289
3290 return cred;
3291}
3292
3293
3294struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3295{
3296 int id;
3297 struct wpa_cred *cred, *last = NULL;
3298
3299 id = -1;
3300 cred = config->cred;
3301 while (cred) {
3302 if (cred->id > id)
3303 id = cred->id;
3304 last = cred;
3305 cred = cred->next;
3306 }
3307 id++;
3308
3309 cred = os_zalloc(sizeof(*cred));
3310 if (cred == NULL)
3311 return NULL;
3312 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003313 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003314 if (last)
3315 last->next = cred;
3316 else
3317 config->cred = cred;
3318
3319 return cred;
3320}
3321
3322
3323int wpa_config_remove_cred(struct wpa_config *config, int id)
3324{
3325 struct wpa_cred *cred, *prev = NULL;
3326
3327 cred = config->cred;
3328 while (cred) {
3329 if (id == cred->id)
3330 break;
3331 prev = cred;
3332 cred = cred->next;
3333 }
3334
3335 if (cred == NULL)
3336 return -1;
3337
3338 if (prev)
3339 prev->next = cred->next;
3340 else
3341 config->cred = cred->next;
3342
3343 wpa_config_free_cred(cred);
3344 return 0;
3345}
3346
3347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348#ifndef CONFIG_NO_CONFIG_BLOBS
3349/**
3350 * wpa_config_get_blob - Get a named configuration blob
3351 * @config: Configuration data from wpa_config_read()
3352 * @name: Name of the blob
3353 * Returns: Pointer to blob data or %NULL if not found
3354 */
3355const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3356 const char *name)
3357{
3358 struct wpa_config_blob *blob = config->blobs;
3359
3360 while (blob) {
3361 if (os_strcmp(blob->name, name) == 0)
3362 return blob;
3363 blob = blob->next;
3364 }
3365 return NULL;
3366}
3367
3368
3369/**
3370 * wpa_config_set_blob - Set or add a named configuration blob
3371 * @config: Configuration data from wpa_config_read()
3372 * @blob: New value for the blob
3373 *
3374 * Adds a new configuration blob or replaces the current value of an existing
3375 * blob.
3376 */
3377void wpa_config_set_blob(struct wpa_config *config,
3378 struct wpa_config_blob *blob)
3379{
3380 wpa_config_remove_blob(config, blob->name);
3381 blob->next = config->blobs;
3382 config->blobs = blob;
3383}
3384
3385
3386/**
3387 * wpa_config_free_blob - Free blob data
3388 * @blob: Pointer to blob to be freed
3389 */
3390void wpa_config_free_blob(struct wpa_config_blob *blob)
3391{
3392 if (blob) {
3393 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003394 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 os_free(blob);
3396 }
3397}
3398
3399
3400/**
3401 * wpa_config_remove_blob - Remove a named configuration blob
3402 * @config: Configuration data from wpa_config_read()
3403 * @name: Name of the blob to remove
3404 * Returns: 0 if blob was removed or -1 if blob was not found
3405 */
3406int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3407{
3408 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3409
3410 while (pos) {
3411 if (os_strcmp(pos->name, name) == 0) {
3412 if (prev)
3413 prev->next = pos->next;
3414 else
3415 config->blobs = pos->next;
3416 wpa_config_free_blob(pos);
3417 return 0;
3418 }
3419 prev = pos;
3420 pos = pos->next;
3421 }
3422
3423 return -1;
3424}
3425#endif /* CONFIG_NO_CONFIG_BLOBS */
3426
3427
3428/**
3429 * wpa_config_alloc_empty - Allocate an empty configuration
3430 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3431 * socket
3432 * @driver_param: Driver parameters
3433 * Returns: Pointer to allocated configuration data or %NULL on failure
3434 */
3435struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3436 const char *driver_param)
3437{
3438 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003439 const int aCWmin = 4, aCWmax = 10;
3440 const struct hostapd_wmm_ac_params ac_bk =
3441 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3442 const struct hostapd_wmm_ac_params ac_be =
3443 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3444 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3445 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3446 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3447 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448
3449 config = os_zalloc(sizeof(*config));
3450 if (config == NULL)
3451 return NULL;
3452 config->eapol_version = DEFAULT_EAPOL_VERSION;
3453 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003454 config->user_mpm = DEFAULT_USER_MPM;
3455 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 config->fast_reauth = DEFAULT_FAST_REAUTH;
3457 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3458 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003459 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003460 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3462 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3463 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3464 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003465 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003466 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003467 config->wmm_ac_params[0] = ac_be;
3468 config->wmm_ac_params[1] = ac_bk;
3469 config->wmm_ac_params[2] = ac_vi;
3470 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003471 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003472 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003473 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474
3475 if (ctrl_interface)
3476 config->ctrl_interface = os_strdup(ctrl_interface);
3477 if (driver_param)
3478 config->driver_param = os_strdup(driver_param);
3479
3480 return config;
3481}
3482
3483
3484#ifndef CONFIG_NO_STDOUT_DEBUG
3485/**
3486 * wpa_config_debug_dump_networks - Debug dump of configured networks
3487 * @config: Configuration data from wpa_config_read()
3488 */
3489void wpa_config_debug_dump_networks(struct wpa_config *config)
3490{
3491 int prio;
3492 struct wpa_ssid *ssid;
3493
3494 for (prio = 0; prio < config->num_prio; prio++) {
3495 ssid = config->pssid[prio];
3496 wpa_printf(MSG_DEBUG, "Priority group %d",
3497 ssid->priority);
3498 while (ssid) {
3499 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3500 ssid->id,
3501 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3502 ssid = ssid->pnext;
3503 }
3504 }
3505}
3506#endif /* CONFIG_NO_STDOUT_DEBUG */
3507
3508
3509struct global_parse_data {
3510 char *name;
3511 int (*parser)(const struct global_parse_data *data,
3512 struct wpa_config *config, int line, const char *value);
3513 void *param1, *param2, *param3;
3514 unsigned int changed_flag;
3515};
3516
3517
3518static int wpa_global_config_parse_int(const struct global_parse_data *data,
3519 struct wpa_config *config, int line,
3520 const char *pos)
3521{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003522 int val, *dst;
3523 char *end;
3524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003526 val = strtol(pos, &end, 0);
3527 if (*end) {
3528 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3529 line, pos);
3530 return -1;
3531 }
3532 *dst = val;
3533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3535
3536 if (data->param2 && *dst < (long) data->param2) {
3537 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3538 "min_value=%ld)", line, data->name, *dst,
3539 (long) data->param2);
3540 *dst = (long) data->param2;
3541 return -1;
3542 }
3543
3544 if (data->param3 && *dst > (long) data->param3) {
3545 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3546 "max_value=%ld)", line, data->name, *dst,
3547 (long) data->param3);
3548 *dst = (long) data->param3;
3549 return -1;
3550 }
3551
3552 return 0;
3553}
3554
3555
3556static int wpa_global_config_parse_str(const struct global_parse_data *data,
3557 struct wpa_config *config, int line,
3558 const char *pos)
3559{
3560 size_t len;
3561 char **dst, *tmp;
3562
3563 len = os_strlen(pos);
3564 if (data->param2 && len < (size_t) data->param2) {
3565 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3566 "min_len=%ld)", line, data->name,
3567 (unsigned long) len, (long) data->param2);
3568 return -1;
3569 }
3570
3571 if (data->param3 && len > (size_t) data->param3) {
3572 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3573 "max_len=%ld)", line, data->name,
3574 (unsigned long) len, (long) data->param3);
3575 return -1;
3576 }
3577
3578 tmp = os_strdup(pos);
3579 if (tmp == NULL)
3580 return -1;
3581
3582 dst = (char **) (((u8 *) config) + (long) data->param1);
3583 os_free(*dst);
3584 *dst = tmp;
3585 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3586
3587 return 0;
3588}
3589
3590
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003591static int wpa_config_process_bgscan(const struct global_parse_data *data,
3592 struct wpa_config *config, int line,
3593 const char *pos)
3594{
3595 size_t len;
3596 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003597 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003598
3599 tmp = wpa_config_parse_string(pos, &len);
3600 if (tmp == NULL) {
3601 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3602 line, data->name);
3603 return -1;
3604 }
3605
Dmitry Shmidt97672262014-02-03 13:02:54 -08003606 res = wpa_global_config_parse_str(data, config, line, tmp);
3607 os_free(tmp);
3608 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003609}
3610
3611
Dmitry Shmidt04949592012-07-19 12:16:46 -07003612static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3613 struct wpa_config *config, int line,
3614 const char *pos)
3615{
3616 size_t len;
3617 struct wpabuf **dst, *tmp;
3618
3619 len = os_strlen(pos);
3620 if (len & 0x01)
3621 return -1;
3622
3623 tmp = wpabuf_alloc(len / 2);
3624 if (tmp == NULL)
3625 return -1;
3626
3627 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3628 wpabuf_free(tmp);
3629 return -1;
3630 }
3631
3632 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3633 wpabuf_free(*dst);
3634 *dst = tmp;
3635 wpa_printf(MSG_DEBUG, "%s", data->name);
3636
3637 return 0;
3638}
3639
3640
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003641static int wpa_config_process_freq_list(const struct global_parse_data *data,
3642 struct wpa_config *config, int line,
3643 const char *value)
3644{
3645 int *freqs;
3646
3647 freqs = wpa_config_parse_int_array(value);
3648 if (freqs == NULL)
3649 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003650 if (freqs[0] == 0) {
3651 os_free(freqs);
3652 freqs = NULL;
3653 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003654 os_free(config->freq_list);
3655 config->freq_list = freqs;
3656 return 0;
3657}
3658
3659
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003660#ifdef CONFIG_P2P
3661static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3662 struct wpa_config *config, int line,
3663 const char *pos)
3664{
3665 u32 *dst;
3666 struct hostapd_ip_addr addr;
3667
3668 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3669 return -1;
3670 if (addr.af != AF_INET)
3671 return -1;
3672
3673 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3674 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3675 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3676 WPA_GET_BE32((u8 *) dst));
3677
3678 return 0;
3679}
3680#endif /* CONFIG_P2P */
3681
3682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683static int wpa_config_process_country(const struct global_parse_data *data,
3684 struct wpa_config *config, int line,
3685 const char *pos)
3686{
3687 if (!pos[0] || !pos[1]) {
3688 wpa_printf(MSG_DEBUG, "Invalid country set");
3689 return -1;
3690 }
3691 config->country[0] = pos[0];
3692 config->country[1] = pos[1];
3693 wpa_printf(MSG_DEBUG, "country='%c%c'",
3694 config->country[0], config->country[1]);
3695 return 0;
3696}
3697
3698
3699static int wpa_config_process_load_dynamic_eap(
3700 const struct global_parse_data *data, struct wpa_config *config,
3701 int line, const char *so)
3702{
3703 int ret;
3704 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3705 ret = eap_peer_method_load(so);
3706 if (ret == -2) {
3707 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3708 "reloading.");
3709 } else if (ret) {
3710 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3711 "method '%s'.", line, so);
3712 return -1;
3713 }
3714
3715 return 0;
3716}
3717
3718
3719#ifdef CONFIG_WPS
3720
3721static int wpa_config_process_uuid(const struct global_parse_data *data,
3722 struct wpa_config *config, int line,
3723 const char *pos)
3724{
3725 char buf[40];
3726 if (uuid_str2bin(pos, config->uuid)) {
3727 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3728 return -1;
3729 }
3730 uuid_bin2str(config->uuid, buf, sizeof(buf));
3731 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3732 return 0;
3733}
3734
3735
3736static int wpa_config_process_device_type(
3737 const struct global_parse_data *data,
3738 struct wpa_config *config, int line, const char *pos)
3739{
3740 return wps_dev_type_str2bin(pos, config->device_type);
3741}
3742
3743
3744static int wpa_config_process_os_version(const struct global_parse_data *data,
3745 struct wpa_config *config, int line,
3746 const char *pos)
3747{
3748 if (hexstr2bin(pos, config->os_version, 4)) {
3749 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3750 return -1;
3751 }
3752 wpa_printf(MSG_DEBUG, "os_version=%08x",
3753 WPA_GET_BE32(config->os_version));
3754 return 0;
3755}
3756
Dmitry Shmidt04949592012-07-19 12:16:46 -07003757
3758static int wpa_config_process_wps_vendor_ext_m1(
3759 const struct global_parse_data *data,
3760 struct wpa_config *config, int line, const char *pos)
3761{
3762 struct wpabuf *tmp;
3763 int len = os_strlen(pos) / 2;
3764 u8 *p;
3765
3766 if (!len) {
3767 wpa_printf(MSG_ERROR, "Line %d: "
3768 "invalid wps_vendor_ext_m1", line);
3769 return -1;
3770 }
3771
3772 tmp = wpabuf_alloc(len);
3773 if (tmp) {
3774 p = wpabuf_put(tmp, len);
3775
3776 if (hexstr2bin(pos, p, len)) {
3777 wpa_printf(MSG_ERROR, "Line %d: "
3778 "invalid wps_vendor_ext_m1", line);
3779 wpabuf_free(tmp);
3780 return -1;
3781 }
3782
3783 wpabuf_free(config->wps_vendor_ext_m1);
3784 config->wps_vendor_ext_m1 = tmp;
3785 } else {
3786 wpa_printf(MSG_ERROR, "Can not allocate "
3787 "memory for wps_vendor_ext_m1");
3788 return -1;
3789 }
3790
3791 return 0;
3792}
3793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794#endif /* CONFIG_WPS */
3795
3796#ifdef CONFIG_P2P
3797static int wpa_config_process_sec_device_type(
3798 const struct global_parse_data *data,
3799 struct wpa_config *config, int line, const char *pos)
3800{
3801 int idx;
3802
3803 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3804 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3805 "items", line);
3806 return -1;
3807 }
3808
3809 idx = config->num_sec_device_types;
3810
3811 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3812 return -1;
3813
3814 config->num_sec_device_types++;
3815 return 0;
3816}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003817
3818
3819static int wpa_config_process_p2p_pref_chan(
3820 const struct global_parse_data *data,
3821 struct wpa_config *config, int line, const char *pos)
3822{
3823 struct p2p_channel *pref = NULL, *n;
3824 unsigned int num = 0;
3825 const char *pos2;
3826 u8 op_class, chan;
3827
3828 /* format: class:chan,class:chan,... */
3829
3830 while (*pos) {
3831 op_class = atoi(pos);
3832 pos2 = os_strchr(pos, ':');
3833 if (pos2 == NULL)
3834 goto fail;
3835 pos2++;
3836 chan = atoi(pos2);
3837
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003838 n = os_realloc_array(pref, num + 1,
3839 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003840 if (n == NULL)
3841 goto fail;
3842 pref = n;
3843 pref[num].op_class = op_class;
3844 pref[num].chan = chan;
3845 num++;
3846
3847 pos = os_strchr(pos2, ',');
3848 if (pos == NULL)
3849 break;
3850 pos++;
3851 }
3852
3853 os_free(config->p2p_pref_chan);
3854 config->p2p_pref_chan = pref;
3855 config->num_p2p_pref_chan = num;
3856 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3857 (u8 *) config->p2p_pref_chan,
3858 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3859
3860 return 0;
3861
3862fail:
3863 os_free(pref);
3864 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3865 return -1;
3866}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003867
3868
3869static int wpa_config_process_p2p_no_go_freq(
3870 const struct global_parse_data *data,
3871 struct wpa_config *config, int line, const char *pos)
3872{
3873 int ret;
3874
3875 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3876 if (ret < 0) {
3877 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3878 return -1;
3879 }
3880
3881 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3882 config->p2p_no_go_freq.num);
3883
3884 return 0;
3885}
3886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887#endif /* CONFIG_P2P */
3888
3889
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003890static int wpa_config_process_hessid(
3891 const struct global_parse_data *data,
3892 struct wpa_config *config, int line, const char *pos)
3893{
3894 if (hwaddr_aton2(pos, config->hessid) < 0) {
3895 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3896 line, pos);
3897 return -1;
3898 }
3899
3900 return 0;
3901}
3902
3903
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003904static int wpa_config_process_sae_groups(
3905 const struct global_parse_data *data,
3906 struct wpa_config *config, int line, const char *pos)
3907{
3908 int *groups = wpa_config_parse_int_array(pos);
3909 if (groups == NULL) {
3910 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3911 line, pos);
3912 return -1;
3913 }
3914
3915 os_free(config->sae_groups);
3916 config->sae_groups = groups;
3917
3918 return 0;
3919}
3920
3921
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003922static int wpa_config_process_ap_vendor_elements(
3923 const struct global_parse_data *data,
3924 struct wpa_config *config, int line, const char *pos)
3925{
3926 struct wpabuf *tmp;
3927 int len = os_strlen(pos) / 2;
3928 u8 *p;
3929
3930 if (!len) {
3931 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3932 line);
3933 return -1;
3934 }
3935
3936 tmp = wpabuf_alloc(len);
3937 if (tmp) {
3938 p = wpabuf_put(tmp, len);
3939
3940 if (hexstr2bin(pos, p, len)) {
3941 wpa_printf(MSG_ERROR, "Line %d: invalid "
3942 "ap_vendor_elements", line);
3943 wpabuf_free(tmp);
3944 return -1;
3945 }
3946
3947 wpabuf_free(config->ap_vendor_elements);
3948 config->ap_vendor_elements = tmp;
3949 } else {
3950 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3951 "ap_vendor_elements");
3952 return -1;
3953 }
3954
3955 return 0;
3956}
3957
3958
Dmitry Shmidt56052862013-10-04 10:23:25 -07003959#ifdef CONFIG_CTRL_IFACE
3960static int wpa_config_process_no_ctrl_interface(
3961 const struct global_parse_data *data,
3962 struct wpa_config *config, int line, const char *pos)
3963{
3964 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3965 os_free(config->ctrl_interface);
3966 config->ctrl_interface = NULL;
3967 return 0;
3968}
3969#endif /* CONFIG_CTRL_IFACE */
3970
3971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972#ifdef OFFSET
3973#undef OFFSET
3974#endif /* OFFSET */
3975/* OFFSET: Get offset of a variable within the wpa_config structure */
3976#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3977
3978#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3979#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3980#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3981#define INT(f) _INT(f), NULL, NULL
3982#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3983#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3984#define STR(f) _STR(f), NULL, NULL
3985#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07003986#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003987#define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988
3989static const struct global_parse_data global_fields[] = {
3990#ifdef CONFIG_CTRL_IFACE
3991 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003992 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 { STR(ctrl_interface_group), 0 } /* deprecated */,
3994#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003995#ifdef CONFIG_MACSEC
3996 { INT_RANGE(eapol_version, 1, 3), 0 },
3997#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003999#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004001 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004002#ifdef CONFIG_MESH
4003 { INT(user_mpm), 0 },
4004 { INT_RANGE(max_peer_links, 0, 255), 0 },
4005#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004006 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 { INT(fast_reauth), 0 },
4008 { STR(opensc_engine_path), 0 },
4009 { STR(pkcs11_engine_path), 0 },
4010 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004011 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004012 { STR(pcsc_reader), 0 },
4013 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004014 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 { STR(driver_param), 0 },
4016 { INT(dot11RSNAConfigPMKLifetime), 0 },
4017 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4018 { INT(dot11RSNAConfigSATimeout), 0 },
4019#ifndef CONFIG_NO_CONFIG_WRITE
4020 { INT(update_config), 0 },
4021#endif /* CONFIG_NO_CONFIG_WRITE */
4022 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4023#ifdef CONFIG_WPS
4024 { FUNC(uuid), CFG_CHANGED_UUID },
4025 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
4026 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4027 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4028 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4029 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4030 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4031 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4032 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4033 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004034 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035#endif /* CONFIG_WPS */
4036#ifdef CONFIG_P2P
4037 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
4038 { INT(p2p_listen_reg_class), 0 },
4039 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004040 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4041 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4043 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4044 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4045 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4046 { INT(p2p_group_idle), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004047 { INT_RANGE(p2p_passphrase_len, 8, 63),
4048 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004049 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004050 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4051 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004052 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004053 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004054 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004055 { INT(p2p_disabled), 0 },
4056 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004057 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004058 { IPV4(ip_addr_go), 0 },
4059 { IPV4(ip_addr_mask), 0 },
4060 { IPV4(ip_addr_start), 0 },
4061 { IPV4(ip_addr_end), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062#endif /* CONFIG_P2P */
4063 { FUNC(country), CFG_CHANGED_COUNTRY },
4064 { INT(bss_max_count), 0 },
4065 { INT(bss_expiration_age), 0 },
4066 { INT(bss_expiration_scan_count), 0 },
4067 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004068 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004070 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004071#ifdef CONFIG_HS20
4072 { INT_RANGE(hs20, 0, 1), 0 },
4073#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004074 { INT_RANGE(interworking, 0, 1), 0 },
4075 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 { INT_RANGE(access_network_type, 0, 15), 0 },
4077 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4078 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004079 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4080 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4081 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4082 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4083 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004084 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4085 { INT(p2p_go_max_inactivity), 0 },
4086 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004087 { INT(okc), 0 },
4088 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004089 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004090 { INT(dtim_period), 0 },
4091 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004092 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004093 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004094 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004095 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004096 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004097 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004098 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004099 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004100 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004101 { INT(mac_addr), 0 },
4102 { INT(rand_addr_lifetime), 0 },
4103 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004104 { INT(key_mgmt_offload), 0},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105};
4106
4107#undef FUNC
4108#undef _INT
4109#undef INT
4110#undef INT_RANGE
4111#undef _STR
4112#undef STR
4113#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004114#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004115#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004116#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117
4118
4119int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4120{
4121 size_t i;
4122 int ret = 0;
4123
4124 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4125 const struct global_parse_data *field = &global_fields[i];
4126 size_t flen = os_strlen(field->name);
4127 if (os_strncmp(pos, field->name, flen) != 0 ||
4128 pos[flen] != '=')
4129 continue;
4130
4131 if (field->parser(field, config, line, pos + flen + 1)) {
4132 wpa_printf(MSG_ERROR, "Line %d: failed to "
4133 "parse '%s'.", line, pos);
4134 ret = -1;
4135 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004136 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4137 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138 config->changed_parameters |= field->changed_flag;
4139 break;
4140 }
4141 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004142#ifdef CONFIG_AP
4143 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4144 char *tmp = os_strchr(pos, '=');
4145 if (tmp == NULL) {
4146 if (line < 0)
4147 return -1;
4148 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4149 "'%s'", line, pos);
4150 return -1;
4151 }
4152 *tmp++ = '\0';
4153 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4154 tmp)) {
4155 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4156 "AC item", line);
4157 return -1;
4158 }
4159 }
4160#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 if (line < 0)
4162 return -1;
4163 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4164 line, pos);
4165 ret = -1;
4166 }
4167
4168 return ret;
4169}