blob: 796977b0235ab0a6cff26500281d23604f2f0af4 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, 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 Shmidtd80a4012015-11-05 16:35:40 -080018#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "config.h"
20
21
22#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
23#define NO_CONFIG_WRITE
24#endif
25
26/*
27 * Structure for network configuration parsing. This data is used to implement
28 * a generic parser for each network block variable. The table of configuration
29 * variables is defined below in this file (ssid_fields[]).
30 */
31struct parse_data {
32 /* Configuration variable name */
33 char *name;
34
35 /* Parser function for this variable */
36 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
37 int line, const char *value);
38
39#ifndef NO_CONFIG_WRITE
40 /* Writer function (i.e., to get the variable in text format from
41 * internal presentation). */
42 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
43#endif /* NO_CONFIG_WRITE */
44
45 /* Variable specific parameters for the parser. */
46 void *param1, *param2, *param3, *param4;
47
48 /* 0 = this variable can be included in debug output and ctrl_iface
49 * 1 = this variable contains key/private data and it must not be
50 * included in debug output unless explicitly requested. In
51 * addition, this variable will not be readable through the
52 * ctrl_iface.
53 */
54 int key_data;
55};
56
57
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058static int wpa_config_parse_str(const struct parse_data *data,
59 struct wpa_ssid *ssid,
60 int line, const char *value)
61{
62 size_t res_len, *dst_len;
63 char **dst, *tmp;
64
65 if (os_strcmp(value, "NULL") == 0) {
66 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
67 data->name);
68 tmp = NULL;
69 res_len = 0;
70 goto set;
71 }
72
73 tmp = wpa_config_parse_string(value, &res_len);
74 if (tmp == NULL) {
75 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
76 line, data->name,
77 data->key_data ? "[KEY DATA REMOVED]" : value);
78 return -1;
79 }
80
81 if (data->key_data) {
82 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
83 (u8 *) tmp, res_len);
84 } else {
85 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
86 (u8 *) tmp, res_len);
87 }
88
89 if (data->param3 && res_len < (size_t) data->param3) {
90 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
91 "min_len=%ld)", line, data->name,
92 (unsigned long) res_len, (long) data->param3);
93 os_free(tmp);
94 return -1;
95 }
96
97 if (data->param4 && res_len > (size_t) data->param4) {
98 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
99 "max_len=%ld)", line, data->name,
100 (unsigned long) res_len, (long) data->param4);
101 os_free(tmp);
102 return -1;
103 }
104
105set:
106 dst = (char **) (((u8 *) ssid) + (long) data->param1);
107 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
108 os_free(*dst);
109 *dst = tmp;
110 if (data->param2)
111 *dst_len = res_len;
112
113 return 0;
114}
115
116
117#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
119{
120 char *buf;
121
122 buf = os_malloc(len + 3);
123 if (buf == NULL)
124 return NULL;
125 buf[0] = '"';
126 os_memcpy(buf + 1, value, len);
127 buf[len + 1] = '"';
128 buf[len + 2] = '\0';
129
130 return buf;
131}
132
133
134static char * wpa_config_write_string_hex(const u8 *value, size_t len)
135{
136 char *buf;
137
138 buf = os_zalloc(2 * len + 1);
139 if (buf == NULL)
140 return NULL;
141 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
142
143 return buf;
144}
145
146
147static char * wpa_config_write_string(const u8 *value, size_t len)
148{
149 if (value == NULL)
150 return NULL;
151
152 if (is_hex(value, len))
153 return wpa_config_write_string_hex(value, len);
154 else
155 return wpa_config_write_string_ascii(value, len);
156}
157
158
159static char * wpa_config_write_str(const struct parse_data *data,
160 struct wpa_ssid *ssid)
161{
162 size_t len;
163 char **src;
164
165 src = (char **) (((u8 *) ssid) + (long) data->param1);
166 if (*src == NULL)
167 return NULL;
168
169 if (data->param2)
170 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
171 else
172 len = os_strlen(*src);
173
174 return wpa_config_write_string((const u8 *) *src, len);
175}
176#endif /* NO_CONFIG_WRITE */
177
178
179static int wpa_config_parse_int(const struct parse_data *data,
180 struct wpa_ssid *ssid,
181 int line, const char *value)
182{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700183 int val, *dst;
184 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185
186 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700187 val = strtol(value, &end, 0);
188 if (*end) {
189 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
190 line, value);
191 return -1;
192 }
193 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
195
196 if (data->param3 && *dst < (long) data->param3) {
197 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
198 "min_value=%ld)", line, data->name, *dst,
199 (long) data->param3);
200 *dst = (long) data->param3;
201 return -1;
202 }
203
204 if (data->param4 && *dst > (long) data->param4) {
205 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
206 "max_value=%ld)", line, data->name, *dst,
207 (long) data->param4);
208 *dst = (long) data->param4;
209 return -1;
210 }
211
212 return 0;
213}
214
215
216#ifndef NO_CONFIG_WRITE
217static char * wpa_config_write_int(const struct parse_data *data,
218 struct wpa_ssid *ssid)
219{
220 int *src, res;
221 char *value;
222
223 src = (int *) (((u8 *) ssid) + (long) data->param1);
224
225 value = os_malloc(20);
226 if (value == NULL)
227 return NULL;
228 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800229 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 os_free(value);
231 return NULL;
232 }
233 value[20 - 1] = '\0';
234 return value;
235}
236#endif /* NO_CONFIG_WRITE */
237
238
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800239static int wpa_config_parse_addr_list(const struct parse_data *data,
240 int line, const char *value,
241 u8 **list, size_t *num, char *name,
242 u8 abort_on_error, u8 masked)
243{
244 const char *pos;
245 u8 *buf, *n, addr[2 * ETH_ALEN];
246 size_t count;
247
248 buf = NULL;
249 count = 0;
250
251 pos = value;
252 while (pos && *pos) {
253 while (*pos == ' ')
254 pos++;
255
256 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
257 if (abort_on_error || count == 0) {
258 wpa_printf(MSG_ERROR,
259 "Line %d: Invalid %s address '%s'",
260 line, name, value);
261 os_free(buf);
262 return -1;
263 }
264 /* continue anyway since this could have been from a
265 * truncated configuration file line */
266 wpa_printf(MSG_INFO,
267 "Line %d: Ignore likely truncated %s address '%s'",
268 line, name, pos);
269 } else {
270 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
271 if (n == NULL) {
272 os_free(buf);
273 return -1;
274 }
275 buf = n;
276 os_memmove(buf + 2 * ETH_ALEN, buf,
277 count * 2 * ETH_ALEN);
278 os_memcpy(buf, addr, 2 * ETH_ALEN);
279 count++;
280 wpa_printf(MSG_MSGDUMP,
281 "%s: addr=" MACSTR " mask=" MACSTR,
282 name, MAC2STR(addr),
283 MAC2STR(&addr[ETH_ALEN]));
284 }
285
286 pos = os_strchr(pos, ' ');
287 }
288
289 os_free(*list);
290 *list = buf;
291 *num = count;
292
293 return 0;
294}
295
296
297#ifndef NO_CONFIG_WRITE
298static char * wpa_config_write_addr_list(const struct parse_data *data,
299 const u8 *list, size_t num, char *name)
300{
301 char *value, *end, *pos;
302 int res;
303 size_t i;
304
305 if (list == NULL || num == 0)
306 return NULL;
307
308 value = os_malloc(2 * 20 * num);
309 if (value == NULL)
310 return NULL;
311 pos = value;
312 end = value + 2 * 20 * num;
313
314 for (i = num; i > 0; i--) {
315 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
316 const u8 *m = a + ETH_ALEN;
317
318 if (i < num)
319 *pos++ = ' ';
320 res = hwaddr_mask_txt(pos, end - pos, a, m);
321 if (res < 0) {
322 os_free(value);
323 return NULL;
324 }
325 pos += res;
326 }
327
328 return value;
329}
330#endif /* NO_CONFIG_WRITE */
331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332static int wpa_config_parse_bssid(const struct parse_data *data,
333 struct wpa_ssid *ssid, int line,
334 const char *value)
335{
336 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
337 os_strcmp(value, "any") == 0) {
338 ssid->bssid_set = 0;
339 wpa_printf(MSG_MSGDUMP, "BSSID any");
340 return 0;
341 }
342 if (hwaddr_aton(value, ssid->bssid)) {
343 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
344 line, value);
345 return -1;
346 }
347 ssid->bssid_set = 1;
348 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
349 return 0;
350}
351
352
353#ifndef NO_CONFIG_WRITE
354static char * wpa_config_write_bssid(const struct parse_data *data,
355 struct wpa_ssid *ssid)
356{
357 char *value;
358 int res;
359
360 if (!ssid->bssid_set)
361 return NULL;
362
363 value = os_malloc(20);
364 if (value == NULL)
365 return NULL;
366 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 os_free(value);
369 return NULL;
370 }
371 value[20 - 1] = '\0';
372 return value;
373}
374#endif /* NO_CONFIG_WRITE */
375
376
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800377static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
378 struct wpa_ssid *ssid, int line,
379 const char *value)
380{
381 return wpa_config_parse_addr_list(data, line, value,
382 &ssid->bssid_blacklist,
383 &ssid->num_bssid_blacklist,
384 "bssid_blacklist", 1, 1);
385}
386
387
388#ifndef NO_CONFIG_WRITE
389static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
390 struct wpa_ssid *ssid)
391{
392 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
393 ssid->num_bssid_blacklist,
394 "bssid_blacklist");
395}
396#endif /* NO_CONFIG_WRITE */
397
398
399static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
400 struct wpa_ssid *ssid, int line,
401 const char *value)
402{
403 return wpa_config_parse_addr_list(data, line, value,
404 &ssid->bssid_whitelist,
405 &ssid->num_bssid_whitelist,
406 "bssid_whitelist", 1, 1);
407}
408
409
410#ifndef NO_CONFIG_WRITE
411static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
412 struct wpa_ssid *ssid)
413{
414 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
415 ssid->num_bssid_whitelist,
416 "bssid_whitelist");
417}
418#endif /* NO_CONFIG_WRITE */
419
420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421static int wpa_config_parse_psk(const struct parse_data *data,
422 struct wpa_ssid *ssid, int line,
423 const char *value)
424{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700425#ifdef CONFIG_EXT_PASSWORD
426 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700427 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700428 ssid->passphrase = NULL;
429 ssid->psk_set = 0;
430 os_free(ssid->ext_psk);
431 ssid->ext_psk = os_strdup(value + 4);
432 if (ssid->ext_psk == NULL)
433 return -1;
434 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
435 ssid->ext_psk);
436 return 0;
437 }
438#endif /* CONFIG_EXT_PASSWORD */
439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 if (*value == '"') {
441#ifndef CONFIG_NO_PBKDF2
442 const char *pos;
443 size_t len;
444
445 value++;
446 pos = os_strrchr(value, '"');
447 if (pos)
448 len = pos - value;
449 else
450 len = os_strlen(value);
451 if (len < 8 || len > 63) {
452 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
453 "length %lu (expected: 8..63) '%s'.",
454 line, (unsigned long) len, value);
455 return -1;
456 }
457 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
458 (u8 *) value, len);
459 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
460 os_memcmp(ssid->passphrase, value, len) == 0)
461 return 0;
462 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700463 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700464 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 if (ssid->passphrase == NULL)
466 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 return 0;
468#else /* CONFIG_NO_PBKDF2 */
469 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
470 "supported.", line);
471 return -1;
472#endif /* CONFIG_NO_PBKDF2 */
473 }
474
475 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
476 value[PMK_LEN * 2] != '\0') {
477 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
478 line, value);
479 return -1;
480 }
481
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700482 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 ssid->passphrase = NULL;
484
485 ssid->psk_set = 1;
486 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
487 return 0;
488}
489
490
491#ifndef NO_CONFIG_WRITE
492static char * wpa_config_write_psk(const struct parse_data *data,
493 struct wpa_ssid *ssid)
494{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700495#ifdef CONFIG_EXT_PASSWORD
496 if (ssid->ext_psk) {
497 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
498 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800499 int res;
500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700501 if (buf == NULL)
502 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
504 if (os_snprintf_error(len, res)) {
505 os_free(buf);
506 buf = NULL;
507 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700508 return buf;
509 }
510#endif /* CONFIG_EXT_PASSWORD */
511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 if (ssid->passphrase)
513 return wpa_config_write_string_ascii(
514 (const u8 *) ssid->passphrase,
515 os_strlen(ssid->passphrase));
516
517 if (ssid->psk_set)
518 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
519
520 return NULL;
521}
522#endif /* NO_CONFIG_WRITE */
523
524
525static int wpa_config_parse_proto(const struct parse_data *data,
526 struct wpa_ssid *ssid, int line,
527 const char *value)
528{
529 int val = 0, last, errors = 0;
530 char *start, *end, *buf;
531
532 buf = os_strdup(value);
533 if (buf == NULL)
534 return -1;
535 start = buf;
536
537 while (*start != '\0') {
538 while (*start == ' ' || *start == '\t')
539 start++;
540 if (*start == '\0')
541 break;
542 end = start;
543 while (*end != ' ' && *end != '\t' && *end != '\0')
544 end++;
545 last = *end == '\0';
546 *end = '\0';
547 if (os_strcmp(start, "WPA") == 0)
548 val |= WPA_PROTO_WPA;
549 else if (os_strcmp(start, "RSN") == 0 ||
550 os_strcmp(start, "WPA2") == 0)
551 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800552 else if (os_strcmp(start, "OSEN") == 0)
553 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 else {
555 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
556 line, start);
557 errors++;
558 }
559
560 if (last)
561 break;
562 start = end + 1;
563 }
564 os_free(buf);
565
566 if (val == 0) {
567 wpa_printf(MSG_ERROR,
568 "Line %d: no proto values configured.", line);
569 errors++;
570 }
571
572 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
573 ssid->proto = val;
574 return errors ? -1 : 0;
575}
576
577
578#ifndef NO_CONFIG_WRITE
579static char * wpa_config_write_proto(const struct parse_data *data,
580 struct wpa_ssid *ssid)
581{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700582 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700583 char *buf, *pos, *end;
584
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800585 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 if (buf == NULL)
587 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800588 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589
590 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700591 ret = os_snprintf(pos, end - pos, "%sWPA",
592 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800593 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 return buf;
595 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 }
597
598 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700599 ret = os_snprintf(pos, end - pos, "%sRSN",
600 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800601 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 return buf;
603 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 }
605
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800606 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700607 ret = os_snprintf(pos, end - pos, "%sOSEN",
608 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800609 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800610 return buf;
611 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700612 }
613
614 if (pos == buf) {
615 os_free(buf);
616 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800617 }
618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 return buf;
620}
621#endif /* NO_CONFIG_WRITE */
622
623
624static int wpa_config_parse_key_mgmt(const struct parse_data *data,
625 struct wpa_ssid *ssid, int line,
626 const char *value)
627{
628 int val = 0, last, errors = 0;
629 char *start, *end, *buf;
630
631 buf = os_strdup(value);
632 if (buf == NULL)
633 return -1;
634 start = buf;
635
636 while (*start != '\0') {
637 while (*start == ' ' || *start == '\t')
638 start++;
639 if (*start == '\0')
640 break;
641 end = start;
642 while (*end != ' ' && *end != '\t' && *end != '\0')
643 end++;
644 last = *end == '\0';
645 *end = '\0';
646 if (os_strcmp(start, "WPA-PSK") == 0)
647 val |= WPA_KEY_MGMT_PSK;
648 else if (os_strcmp(start, "WPA-EAP") == 0)
649 val |= WPA_KEY_MGMT_IEEE8021X;
650 else if (os_strcmp(start, "IEEE8021X") == 0)
651 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
652 else if (os_strcmp(start, "NONE") == 0)
653 val |= WPA_KEY_MGMT_NONE;
654 else if (os_strcmp(start, "WPA-NONE") == 0)
655 val |= WPA_KEY_MGMT_WPA_NONE;
656#ifdef CONFIG_IEEE80211R
657 else if (os_strcmp(start, "FT-PSK") == 0)
658 val |= WPA_KEY_MGMT_FT_PSK;
659 else if (os_strcmp(start, "FT-EAP") == 0)
660 val |= WPA_KEY_MGMT_FT_IEEE8021X;
661#endif /* CONFIG_IEEE80211R */
662#ifdef CONFIG_IEEE80211W
663 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
664 val |= WPA_KEY_MGMT_PSK_SHA256;
665 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
666 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
667#endif /* CONFIG_IEEE80211W */
668#ifdef CONFIG_WPS
669 else if (os_strcmp(start, "WPS") == 0)
670 val |= WPA_KEY_MGMT_WPS;
671#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800672#ifdef CONFIG_SAE
673 else if (os_strcmp(start, "SAE") == 0)
674 val |= WPA_KEY_MGMT_SAE;
675 else if (os_strcmp(start, "FT-SAE") == 0)
676 val |= WPA_KEY_MGMT_FT_SAE;
677#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800678#ifdef CONFIG_HS20
679 else if (os_strcmp(start, "OSEN") == 0)
680 val |= WPA_KEY_MGMT_OSEN;
681#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800682#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800683 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
684 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800685#endif /* CONFIG_SUITEB */
686#ifdef CONFIG_SUITEB192
687 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
688 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
689#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 else {
691 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
692 line, start);
693 errors++;
694 }
695
696 if (last)
697 break;
698 start = end + 1;
699 }
700 os_free(buf);
701
702 if (val == 0) {
703 wpa_printf(MSG_ERROR,
704 "Line %d: no key_mgmt values configured.", line);
705 errors++;
706 }
707
708 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
709 ssid->key_mgmt = val;
710 return errors ? -1 : 0;
711}
712
713
714#ifndef NO_CONFIG_WRITE
715static char * wpa_config_write_key_mgmt(const struct parse_data *data,
716 struct wpa_ssid *ssid)
717{
718 char *buf, *pos, *end;
719 int ret;
720
Dmitry Shmidt96571392013-10-14 12:54:46 -0700721 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 if (buf == NULL)
723 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700724 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725
726 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
727 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
728 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800729 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 end[-1] = '\0';
731 return buf;
732 }
733 pos += ret;
734 }
735
736 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
737 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
738 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800739 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 end[-1] = '\0';
741 return buf;
742 }
743 pos += ret;
744 }
745
746 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
747 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
748 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800749 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 end[-1] = '\0';
751 return buf;
752 }
753 pos += ret;
754 }
755
756 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
757 ret = os_snprintf(pos, end - pos, "%sNONE",
758 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800759 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 end[-1] = '\0';
761 return buf;
762 }
763 pos += ret;
764 }
765
766 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
767 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
768 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800769 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 end[-1] = '\0';
771 return buf;
772 }
773 pos += ret;
774 }
775
776#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700777 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
778 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
779 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800780 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700781 end[-1] = '\0';
782 return buf;
783 }
784 pos += ret;
785 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786
Dmitry Shmidt96571392013-10-14 12:54:46 -0700787 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
788 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
789 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800790 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700791 end[-1] = '\0';
792 return buf;
793 }
794 pos += ret;
795 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796#endif /* CONFIG_IEEE80211R */
797
798#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700799 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
800 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
801 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800802 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700803 end[-1] = '\0';
804 return buf;
805 }
806 pos += ret;
807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808
Dmitry Shmidt96571392013-10-14 12:54:46 -0700809 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
810 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
811 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800812 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700813 end[-1] = '\0';
814 return buf;
815 }
816 pos += ret;
817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818#endif /* CONFIG_IEEE80211W */
819
820#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700821 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
822 ret = os_snprintf(pos, end - pos, "%sWPS",
823 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800824 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700825 end[-1] = '\0';
826 return buf;
827 }
828 pos += ret;
829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830#endif /* CONFIG_WPS */
831
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800832#ifdef CONFIG_SAE
833 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
834 ret = os_snprintf(pos, end - pos, "%sSAE",
835 pos == buf ? "" : " ");
836 if (os_snprintf_error(end - pos, ret)) {
837 end[-1] = '\0';
838 return buf;
839 }
840 pos += ret;
841 }
842
843 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
844 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
845 pos == buf ? "" : " ");
846 if (os_snprintf_error(end - pos, ret)) {
847 end[-1] = '\0';
848 return buf;
849 }
850 pos += ret;
851 }
852#endif /* CONFIG_SAE */
853
854#ifdef CONFIG_HS20
855 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
856 ret = os_snprintf(pos, end - pos, "%sOSEN",
857 pos == buf ? "" : " ");
858 if (os_snprintf_error(end - pos, ret)) {
859 end[-1] = '\0';
860 return buf;
861 }
862 pos += ret;
863 }
864#endif /* CONFIG_HS20 */
865
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800866#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800867 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
868 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
869 pos == buf ? "" : " ");
870 if (os_snprintf_error(end - pos, ret)) {
871 end[-1] = '\0';
872 return buf;
873 }
874 pos += ret;
875 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800876#endif /* CONFIG_SUITEB */
877
878#ifdef CONFIG_SUITEB192
879 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
880 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
881 pos == buf ? "" : " ");
882 if (os_snprintf_error(end - pos, ret)) {
883 end[-1] = '\0';
884 return buf;
885 }
886 pos += ret;
887 }
888#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800889
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700890 if (pos == buf) {
891 os_free(buf);
892 buf = NULL;
893 }
894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 return buf;
896}
897#endif /* NO_CONFIG_WRITE */
898
899
900static int wpa_config_parse_cipher(int line, const char *value)
901{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800902#ifdef CONFIG_NO_WPA
903 return -1;
904#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800905 int val = wpa_parse_cipher(value);
906 if (val < 0) {
907 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
908 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 if (val == 0) {
912 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
913 line);
914 return -1;
915 }
916 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800917#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918}
919
920
921#ifndef NO_CONFIG_WRITE
922static char * wpa_config_write_cipher(int cipher)
923{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800924#ifdef CONFIG_NO_WPA
925 return NULL;
926#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800927 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 if (buf == NULL)
929 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800931 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
932 os_free(buf);
933 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 }
935
936 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800937#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938}
939#endif /* NO_CONFIG_WRITE */
940
941
942static int wpa_config_parse_pairwise(const struct parse_data *data,
943 struct wpa_ssid *ssid, int line,
944 const char *value)
945{
946 int val;
947 val = wpa_config_parse_cipher(line, value);
948 if (val == -1)
949 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800950 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
952 "(0x%x).", line, val);
953 return -1;
954 }
955
956 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
957 ssid->pairwise_cipher = val;
958 return 0;
959}
960
961
962#ifndef NO_CONFIG_WRITE
963static char * wpa_config_write_pairwise(const struct parse_data *data,
964 struct wpa_ssid *ssid)
965{
966 return wpa_config_write_cipher(ssid->pairwise_cipher);
967}
968#endif /* NO_CONFIG_WRITE */
969
970
971static int wpa_config_parse_group(const struct parse_data *data,
972 struct wpa_ssid *ssid, int line,
973 const char *value)
974{
975 int val;
976 val = wpa_config_parse_cipher(line, value);
977 if (val == -1)
978 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700979
980 /*
981 * Backwards compatibility - filter out WEP ciphers that were previously
982 * allowed.
983 */
984 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
985
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800986 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
988 "(0x%x).", line, val);
989 return -1;
990 }
991
992 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
993 ssid->group_cipher = val;
994 return 0;
995}
996
997
998#ifndef NO_CONFIG_WRITE
999static char * wpa_config_write_group(const struct parse_data *data,
1000 struct wpa_ssid *ssid)
1001{
1002 return wpa_config_write_cipher(ssid->group_cipher);
1003}
1004#endif /* NO_CONFIG_WRITE */
1005
1006
1007static int wpa_config_parse_auth_alg(const struct parse_data *data,
1008 struct wpa_ssid *ssid, int line,
1009 const char *value)
1010{
1011 int val = 0, last, errors = 0;
1012 char *start, *end, *buf;
1013
1014 buf = os_strdup(value);
1015 if (buf == NULL)
1016 return -1;
1017 start = buf;
1018
1019 while (*start != '\0') {
1020 while (*start == ' ' || *start == '\t')
1021 start++;
1022 if (*start == '\0')
1023 break;
1024 end = start;
1025 while (*end != ' ' && *end != '\t' && *end != '\0')
1026 end++;
1027 last = *end == '\0';
1028 *end = '\0';
1029 if (os_strcmp(start, "OPEN") == 0)
1030 val |= WPA_AUTH_ALG_OPEN;
1031 else if (os_strcmp(start, "SHARED") == 0)
1032 val |= WPA_AUTH_ALG_SHARED;
1033 else if (os_strcmp(start, "LEAP") == 0)
1034 val |= WPA_AUTH_ALG_LEAP;
1035 else {
1036 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1037 line, start);
1038 errors++;
1039 }
1040
1041 if (last)
1042 break;
1043 start = end + 1;
1044 }
1045 os_free(buf);
1046
1047 if (val == 0) {
1048 wpa_printf(MSG_ERROR,
1049 "Line %d: no auth_alg values configured.", line);
1050 errors++;
1051 }
1052
1053 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1054 ssid->auth_alg = val;
1055 return errors ? -1 : 0;
1056}
1057
1058
1059#ifndef NO_CONFIG_WRITE
1060static char * wpa_config_write_auth_alg(const struct parse_data *data,
1061 struct wpa_ssid *ssid)
1062{
1063 char *buf, *pos, *end;
1064 int ret;
1065
1066 pos = buf = os_zalloc(30);
1067 if (buf == NULL)
1068 return NULL;
1069 end = buf + 30;
1070
1071 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1072 ret = os_snprintf(pos, end - pos, "%sOPEN",
1073 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001074 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 end[-1] = '\0';
1076 return buf;
1077 }
1078 pos += ret;
1079 }
1080
1081 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1082 ret = os_snprintf(pos, end - pos, "%sSHARED",
1083 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001084 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 end[-1] = '\0';
1086 return buf;
1087 }
1088 pos += ret;
1089 }
1090
1091 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1092 ret = os_snprintf(pos, end - pos, "%sLEAP",
1093 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001094 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 end[-1] = '\0';
1096 return buf;
1097 }
1098 pos += ret;
1099 }
1100
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001101 if (pos == buf) {
1102 os_free(buf);
1103 buf = NULL;
1104 }
1105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 return buf;
1107}
1108#endif /* NO_CONFIG_WRITE */
1109
1110
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001111static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112{
1113 int *freqs;
1114 size_t used, len;
1115 const char *pos;
1116
1117 used = 0;
1118 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 if (freqs == NULL)
1121 return NULL;
1122
1123 pos = value;
1124 while (pos) {
1125 while (*pos == ' ')
1126 pos++;
1127 if (used == len) {
1128 int *n;
1129 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001130 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 if (n == NULL) {
1132 os_free(freqs);
1133 return NULL;
1134 }
1135 for (i = len; i <= len * 2; i++)
1136 n[i] = 0;
1137 freqs = n;
1138 len *= 2;
1139 }
1140
1141 freqs[used] = atoi(pos);
1142 if (freqs[used] == 0)
1143 break;
1144 used++;
1145 pos = os_strchr(pos + 1, ' ');
1146 }
1147
1148 return freqs;
1149}
1150
1151
1152static int wpa_config_parse_scan_freq(const struct parse_data *data,
1153 struct wpa_ssid *ssid, int line,
1154 const char *value)
1155{
1156 int *freqs;
1157
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001158 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 if (freqs == NULL)
1160 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001161 if (freqs[0] == 0) {
1162 os_free(freqs);
1163 freqs = NULL;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 os_free(ssid->scan_freq);
1166 ssid->scan_freq = freqs;
1167
1168 return 0;
1169}
1170
1171
1172static int wpa_config_parse_freq_list(const struct parse_data *data,
1173 struct wpa_ssid *ssid, int line,
1174 const char *value)
1175{
1176 int *freqs;
1177
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001178 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 if (freqs == NULL)
1180 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001181 if (freqs[0] == 0) {
1182 os_free(freqs);
1183 freqs = NULL;
1184 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 os_free(ssid->freq_list);
1186 ssid->freq_list = freqs;
1187
1188 return 0;
1189}
1190
1191
1192#ifndef NO_CONFIG_WRITE
1193static char * wpa_config_write_freqs(const struct parse_data *data,
1194 const int *freqs)
1195{
1196 char *buf, *pos, *end;
1197 int i, ret;
1198 size_t count;
1199
1200 if (freqs == NULL)
1201 return NULL;
1202
1203 count = 0;
1204 for (i = 0; freqs[i]; i++)
1205 count++;
1206
1207 pos = buf = os_zalloc(10 * count + 1);
1208 if (buf == NULL)
1209 return NULL;
1210 end = buf + 10 * count + 1;
1211
1212 for (i = 0; freqs[i]; i++) {
1213 ret = os_snprintf(pos, end - pos, "%s%u",
1214 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001215 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 end[-1] = '\0';
1217 return buf;
1218 }
1219 pos += ret;
1220 }
1221
1222 return buf;
1223}
1224
1225
1226static char * wpa_config_write_scan_freq(const struct parse_data *data,
1227 struct wpa_ssid *ssid)
1228{
1229 return wpa_config_write_freqs(data, ssid->scan_freq);
1230}
1231
1232
1233static char * wpa_config_write_freq_list(const struct parse_data *data,
1234 struct wpa_ssid *ssid)
1235{
1236 return wpa_config_write_freqs(data, ssid->freq_list);
1237}
1238#endif /* NO_CONFIG_WRITE */
1239
1240
1241#ifdef IEEE8021X_EAPOL
1242static int wpa_config_parse_eap(const struct parse_data *data,
1243 struct wpa_ssid *ssid, int line,
1244 const char *value)
1245{
1246 int last, errors = 0;
1247 char *start, *end, *buf;
1248 struct eap_method_type *methods = NULL, *tmp;
1249 size_t num_methods = 0;
1250
1251 buf = os_strdup(value);
1252 if (buf == NULL)
1253 return -1;
1254 start = buf;
1255
1256 while (*start != '\0') {
1257 while (*start == ' ' || *start == '\t')
1258 start++;
1259 if (*start == '\0')
1260 break;
1261 end = start;
1262 while (*end != ' ' && *end != '\t' && *end != '\0')
1263 end++;
1264 last = *end == '\0';
1265 *end = '\0';
1266 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001267 methods = os_realloc_array(methods, num_methods + 1,
1268 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 if (methods == NULL) {
1270 os_free(tmp);
1271 os_free(buf);
1272 return -1;
1273 }
1274 methods[num_methods].method = eap_peer_get_type(
1275 start, &methods[num_methods].vendor);
1276 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1277 methods[num_methods].method == EAP_TYPE_NONE) {
1278 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1279 "'%s'", line, start);
1280 wpa_printf(MSG_ERROR, "You may need to add support for"
1281 " this EAP method during wpa_supplicant\n"
1282 "build time configuration.\n"
1283 "See README for more information.");
1284 errors++;
1285 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1286 methods[num_methods].method == EAP_TYPE_LEAP)
1287 ssid->leap++;
1288 else
1289 ssid->non_leap++;
1290 num_methods++;
1291 if (last)
1292 break;
1293 start = end + 1;
1294 }
1295 os_free(buf);
1296
1297 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 if (methods == NULL) {
1300 os_free(tmp);
1301 return -1;
1302 }
1303 methods[num_methods].vendor = EAP_VENDOR_IETF;
1304 methods[num_methods].method = EAP_TYPE_NONE;
1305 num_methods++;
1306
1307 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1308 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001309 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 ssid->eap.eap_methods = methods;
1311 return errors ? -1 : 0;
1312}
1313
1314
Dmitry Shmidt83474442015-04-15 13:47:09 -07001315#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316static char * wpa_config_write_eap(const struct parse_data *data,
1317 struct wpa_ssid *ssid)
1318{
1319 int i, ret;
1320 char *buf, *pos, *end;
1321 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1322 const char *name;
1323
1324 if (eap_methods == NULL)
1325 return NULL;
1326
1327 pos = buf = os_zalloc(100);
1328 if (buf == NULL)
1329 return NULL;
1330 end = buf + 100;
1331
1332 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1333 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1334 name = eap_get_name(eap_methods[i].vendor,
1335 eap_methods[i].method);
1336 if (name) {
1337 ret = os_snprintf(pos, end - pos, "%s%s",
1338 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001339 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 break;
1341 pos += ret;
1342 }
1343 }
1344
1345 end[-1] = '\0';
1346
1347 return buf;
1348}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001349#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350
1351
1352static int wpa_config_parse_password(const struct parse_data *data,
1353 struct wpa_ssid *ssid, int line,
1354 const char *value)
1355{
1356 u8 *hash;
1357
1358 if (os_strcmp(value, "NULL") == 0) {
1359 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001360 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 ssid->eap.password = NULL;
1362 ssid->eap.password_len = 0;
1363 return 0;
1364 }
1365
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001366#ifdef CONFIG_EXT_PASSWORD
1367 if (os_strncmp(value, "ext:", 4) == 0) {
1368 char *name = os_strdup(value + 4);
1369 if (name == NULL)
1370 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001371 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001372 ssid->eap.password = (u8 *) name;
1373 ssid->eap.password_len = os_strlen(name);
1374 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1375 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1376 return 0;
1377 }
1378#endif /* CONFIG_EXT_PASSWORD */
1379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 if (os_strncmp(value, "hash:", 5) != 0) {
1381 char *tmp;
1382 size_t res_len;
1383
1384 tmp = wpa_config_parse_string(value, &res_len);
1385 if (tmp == NULL) {
1386 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1387 "password.", line);
1388 return -1;
1389 }
1390 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1391 (u8 *) tmp, res_len);
1392
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001393 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 ssid->eap.password = (u8 *) tmp;
1395 ssid->eap.password_len = res_len;
1396 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001397 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398
1399 return 0;
1400 }
1401
1402
1403 /* NtPasswordHash: hash:<32 hex digits> */
1404 if (os_strlen(value + 5) != 2 * 16) {
1405 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1406 "(expected 32 hex digits)", line);
1407 return -1;
1408 }
1409
1410 hash = os_malloc(16);
1411 if (hash == NULL)
1412 return -1;
1413
1414 if (hexstr2bin(value + 5, hash, 16)) {
1415 os_free(hash);
1416 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1417 return -1;
1418 }
1419
1420 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1421
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001422 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 ssid->eap.password = hash;
1424 ssid->eap.password_len = 16;
1425 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001426 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427
1428 return 0;
1429}
1430
1431
Dmitry Shmidt83474442015-04-15 13:47:09 -07001432#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433static char * wpa_config_write_password(const struct parse_data *data,
1434 struct wpa_ssid *ssid)
1435{
1436 char *buf;
1437
1438 if (ssid->eap.password == NULL)
1439 return NULL;
1440
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001441#ifdef CONFIG_EXT_PASSWORD
1442 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1443 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1444 if (buf == NULL)
1445 return NULL;
1446 os_memcpy(buf, "ext:", 4);
1447 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1448 return buf;
1449 }
1450#endif /* CONFIG_EXT_PASSWORD */
1451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1453 return wpa_config_write_string(
1454 ssid->eap.password, ssid->eap.password_len);
1455 }
1456
1457 buf = os_malloc(5 + 32 + 1);
1458 if (buf == NULL)
1459 return NULL;
1460
1461 os_memcpy(buf, "hash:", 5);
1462 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1463
1464 return buf;
1465}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001466#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467#endif /* IEEE8021X_EAPOL */
1468
1469
1470static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1471 const char *value, int idx)
1472{
1473 char *buf, title[20];
1474 int res;
1475
1476 buf = wpa_config_parse_string(value, len);
1477 if (buf == NULL) {
1478 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1479 line, idx, value);
1480 return -1;
1481 }
1482 if (*len > MAX_WEP_KEY_LEN) {
1483 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1484 line, idx, value);
1485 os_free(buf);
1486 return -1;
1487 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001488 if (*len && *len != 5 && *len != 13 && *len != 16) {
1489 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1490 "this network block will be ignored",
1491 line, (unsigned int) *len);
1492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001494 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001496 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1498 return 0;
1499}
1500
1501
1502static int wpa_config_parse_wep_key0(const struct parse_data *data,
1503 struct wpa_ssid *ssid, int line,
1504 const char *value)
1505{
1506 return wpa_config_parse_wep_key(ssid->wep_key[0],
1507 &ssid->wep_key_len[0], line,
1508 value, 0);
1509}
1510
1511
1512static int wpa_config_parse_wep_key1(const struct parse_data *data,
1513 struct wpa_ssid *ssid, int line,
1514 const char *value)
1515{
1516 return wpa_config_parse_wep_key(ssid->wep_key[1],
1517 &ssid->wep_key_len[1], line,
1518 value, 1);
1519}
1520
1521
1522static int wpa_config_parse_wep_key2(const struct parse_data *data,
1523 struct wpa_ssid *ssid, int line,
1524 const char *value)
1525{
1526 return wpa_config_parse_wep_key(ssid->wep_key[2],
1527 &ssid->wep_key_len[2], line,
1528 value, 2);
1529}
1530
1531
1532static int wpa_config_parse_wep_key3(const struct parse_data *data,
1533 struct wpa_ssid *ssid, int line,
1534 const char *value)
1535{
1536 return wpa_config_parse_wep_key(ssid->wep_key[3],
1537 &ssid->wep_key_len[3], line,
1538 value, 3);
1539}
1540
1541
1542#ifndef NO_CONFIG_WRITE
1543static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1544{
1545 if (ssid->wep_key_len[idx] == 0)
1546 return NULL;
1547 return wpa_config_write_string(ssid->wep_key[idx],
1548 ssid->wep_key_len[idx]);
1549}
1550
1551
1552static char * wpa_config_write_wep_key0(const struct parse_data *data,
1553 struct wpa_ssid *ssid)
1554{
1555 return wpa_config_write_wep_key(ssid, 0);
1556}
1557
1558
1559static char * wpa_config_write_wep_key1(const struct parse_data *data,
1560 struct wpa_ssid *ssid)
1561{
1562 return wpa_config_write_wep_key(ssid, 1);
1563}
1564
1565
1566static char * wpa_config_write_wep_key2(const struct parse_data *data,
1567 struct wpa_ssid *ssid)
1568{
1569 return wpa_config_write_wep_key(ssid, 2);
1570}
1571
1572
1573static char * wpa_config_write_wep_key3(const struct parse_data *data,
1574 struct wpa_ssid *ssid)
1575{
1576 return wpa_config_write_wep_key(ssid, 3);
1577}
1578#endif /* NO_CONFIG_WRITE */
1579
1580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581#ifdef CONFIG_P2P
1582
Dmitry Shmidt54605472013-11-08 11:10:19 -08001583static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1584 struct wpa_ssid *ssid, int line,
1585 const char *value)
1586{
1587 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1588 os_strcmp(value, "any") == 0) {
1589 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1590 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1591 return 0;
1592 }
1593 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1594 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1595 line, value);
1596 return -1;
1597 }
1598 ssid->bssid_set = 1;
1599 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1600 MAC2STR(ssid->go_p2p_dev_addr));
1601 return 0;
1602}
1603
1604
1605#ifndef NO_CONFIG_WRITE
1606static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1607 struct wpa_ssid *ssid)
1608{
1609 char *value;
1610 int res;
1611
1612 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1613 return NULL;
1614
1615 value = os_malloc(20);
1616 if (value == NULL)
1617 return NULL;
1618 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001619 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001620 os_free(value);
1621 return NULL;
1622 }
1623 value[20 - 1] = '\0';
1624 return value;
1625}
1626#endif /* NO_CONFIG_WRITE */
1627
1628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001629static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1630 struct wpa_ssid *ssid, int line,
1631 const char *value)
1632{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001633 return wpa_config_parse_addr_list(data, line, value,
1634 &ssid->p2p_client_list,
1635 &ssid->num_p2p_clients,
1636 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001637}
1638
1639
1640#ifndef NO_CONFIG_WRITE
1641static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1642 struct wpa_ssid *ssid)
1643{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001644 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1645 ssid->num_p2p_clients,
1646 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647}
1648#endif /* NO_CONFIG_WRITE */
1649
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001650
1651static int wpa_config_parse_psk_list(const struct parse_data *data,
1652 struct wpa_ssid *ssid, int line,
1653 const char *value)
1654{
1655 struct psk_list_entry *p;
1656 const char *pos;
1657
1658 p = os_zalloc(sizeof(*p));
1659 if (p == NULL)
1660 return -1;
1661
1662 pos = value;
1663 if (os_strncmp(pos, "P2P-", 4) == 0) {
1664 p->p2p = 1;
1665 pos += 4;
1666 }
1667
1668 if (hwaddr_aton(pos, p->addr)) {
1669 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1670 line, pos);
1671 os_free(p);
1672 return -1;
1673 }
1674 pos += 17;
1675 if (*pos != '-') {
1676 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1677 line, pos);
1678 os_free(p);
1679 return -1;
1680 }
1681 pos++;
1682
1683 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1684 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1685 line, pos);
1686 os_free(p);
1687 return -1;
1688 }
1689
1690 dl_list_add(&ssid->psk_list, &p->list);
1691
1692 return 0;
1693}
1694
1695
1696#ifndef NO_CONFIG_WRITE
1697static char * wpa_config_write_psk_list(const struct parse_data *data,
1698 struct wpa_ssid *ssid)
1699{
1700 return NULL;
1701}
1702#endif /* NO_CONFIG_WRITE */
1703
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704#endif /* CONFIG_P2P */
1705
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001706
1707#ifdef CONFIG_MESH
1708
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001709static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1710 struct wpa_ssid *ssid, int line,
1711 const char *value)
1712{
1713 int *rates = wpa_config_parse_int_array(value);
1714
1715 if (rates == NULL) {
1716 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1717 line, value);
1718 return -1;
1719 }
1720 if (rates[0] == 0) {
1721 os_free(rates);
1722 rates = NULL;
1723 }
1724
1725 os_free(ssid->mesh_basic_rates);
1726 ssid->mesh_basic_rates = rates;
1727
1728 return 0;
1729}
1730
1731
1732#ifndef NO_CONFIG_WRITE
1733
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001734static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1735 struct wpa_ssid *ssid)
1736{
1737 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1738}
1739
1740#endif /* NO_CONFIG_WRITE */
1741
1742#endif /* CONFIG_MESH */
1743
1744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745/* Helper macros for network block parser */
1746
1747#ifdef OFFSET
1748#undef OFFSET
1749#endif /* OFFSET */
1750/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1751#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1752
1753/* STR: Define a string variable for an ASCII string; f = field name */
1754#ifdef NO_CONFIG_WRITE
1755#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1756#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1757#else /* NO_CONFIG_WRITE */
1758#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1759#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1760#endif /* NO_CONFIG_WRITE */
1761#define STR(f) _STR(f), NULL, NULL, NULL, 0
1762#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1763#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1764#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1765
1766/* STR_LEN: Define a string variable with a separate variable for storing the
1767 * data length. Unlike STR(), this can be used to store arbitrary binary data
1768 * (i.e., even nul termination character). */
1769#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1770#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1771#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1772#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1773#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1774
1775/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1776 * explicitly specified. */
1777#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1778#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1779#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1780
1781#ifdef NO_CONFIG_WRITE
1782#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1783#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1784#else /* NO_CONFIG_WRITE */
1785#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1786 OFFSET(f), (void *) 0
1787#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1788 OFFSET(eap.f), (void *) 0
1789#endif /* NO_CONFIG_WRITE */
1790
1791/* INT: Define an integer variable */
1792#define INT(f) _INT(f), NULL, NULL, 0
1793#define INTe(f) _INTe(f), NULL, NULL, 0
1794
1795/* INT_RANGE: Define an integer variable with allowed value range */
1796#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1797
1798/* FUNC: Define a configuration variable that uses a custom function for
1799 * parsing and writing the value. */
1800#ifdef NO_CONFIG_WRITE
1801#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1802#else /* NO_CONFIG_WRITE */
1803#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1804 NULL, NULL, NULL, NULL
1805#endif /* NO_CONFIG_WRITE */
1806#define FUNC(f) _FUNC(f), 0
1807#define FUNC_KEY(f) _FUNC(f), 1
1808
1809/*
1810 * Table of network configuration variables. This table is used to parse each
1811 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1812 * that is inside a network block.
1813 *
1814 * This table is generated using the helper macros defined above and with
1815 * generous help from the C pre-processor. The field name is stored as a string
1816 * into .name and for STR and INT types, the offset of the target buffer within
1817 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1818 * offset to the field containing the length of the configuration variable.
1819 * .param3 and .param4 can be used to mark the allowed range (length for STR
1820 * and value for INT).
1821 *
1822 * For each configuration line in wpa_supplicant.conf, the parser goes through
1823 * this table and select the entry that matches with the field name. The parser
1824 * function (.parser) is then called to parse the actual value of the field.
1825 *
1826 * This kind of mechanism makes it easy to add new configuration parameters,
1827 * since only one line needs to be added into this table and into the
1828 * struct wpa_ssid definition if the new variable is either a string or
1829 * integer. More complex types will need to use their own parser and writer
1830 * functions.
1831 */
1832static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001833 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 { INT_RANGE(scan_ssid, 0, 1) },
1835 { FUNC(bssid) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001836 { FUNC(bssid_blacklist) },
1837 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001839 { INT(mem_only_psk) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 { FUNC(proto) },
1841 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001842 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 { FUNC(pairwise) },
1844 { FUNC(group) },
1845 { FUNC(auth_alg) },
1846 { FUNC(scan_freq) },
1847 { FUNC(freq_list) },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001848 { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
1849 VHT_CHANWIDTH_80P80MHZ) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850#ifdef IEEE8021X_EAPOL
1851 { FUNC(eap) },
1852 { STR_LENe(identity) },
1853 { STR_LENe(anonymous_identity) },
1854 { FUNC_KEY(password) },
1855 { STRe(ca_cert) },
1856 { STRe(ca_path) },
1857 { STRe(client_cert) },
1858 { STRe(private_key) },
1859 { STR_KEYe(private_key_passwd) },
1860 { STRe(dh_file) },
1861 { STRe(subject_match) },
1862 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001863 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001864 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 { STRe(ca_cert2) },
1866 { STRe(ca_path2) },
1867 { STRe(client_cert2) },
1868 { STRe(private_key2) },
1869 { STR_KEYe(private_key2_passwd) },
1870 { STRe(dh_file2) },
1871 { STRe(subject_match2) },
1872 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001873 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001874 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 { STRe(phase1) },
1876 { STRe(phase2) },
1877 { STRe(pcsc) },
1878 { STR_KEYe(pin) },
1879 { STRe(engine_id) },
1880 { STRe(key_id) },
1881 { STRe(cert_id) },
1882 { STRe(ca_cert_id) },
1883 { STR_KEYe(pin2) },
1884 { STRe(engine2_id) },
1885 { STRe(key2_id) },
1886 { STRe(cert2_id) },
1887 { STRe(ca_cert2_id) },
1888 { INTe(engine) },
1889 { INTe(engine2) },
1890 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001891 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001892 { STRe(openssl_ciphers) },
1893 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894#endif /* IEEE8021X_EAPOL */
1895 { FUNC_KEY(wep_key0) },
1896 { FUNC_KEY(wep_key1) },
1897 { FUNC_KEY(wep_key2) },
1898 { FUNC_KEY(wep_key3) },
1899 { INT(wep_tx_keyidx) },
1900 { INT(priority) },
1901#ifdef IEEE8021X_EAPOL
1902 { INT(eap_workaround) },
1903 { STRe(pac_file) },
1904 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001905 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907#ifdef CONFIG_MESH
1908 { INT_RANGE(mode, 0, 5) },
1909 { INT_RANGE(no_auto_peer, 0, 1) },
1910#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001912#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 { INT_RANGE(proactive_key_caching, 0, 1) },
1914 { INT_RANGE(disabled, 0, 2) },
1915 { STR(id_str) },
1916#ifdef CONFIG_IEEE80211W
1917 { INT_RANGE(ieee80211w, 0, 2) },
1918#endif /* CONFIG_IEEE80211W */
1919 { INT_RANGE(peerkey, 0, 1) },
1920 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001921 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001922 { INT_RANGE(fixed_freq, 0, 1) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08001923#ifdef CONFIG_ACS
1924 { INT_RANGE(acs, 0, 1) },
1925#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001926#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001927 { FUNC(mesh_basic_rates) },
1928 { INT(dot11MeshMaxRetries) },
1929 { INT(dot11MeshRetryTimeout) },
1930 { INT(dot11MeshConfirmTimeout) },
1931 { INT(dot11MeshHoldingTimeout) },
1932#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 { INT(wpa_ptk_rekey) },
1934 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001935 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001936#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001937 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001938 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001939 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001940#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001941#ifdef CONFIG_HT_OVERRIDES
1942 { INT_RANGE(disable_ht, 0, 1) },
1943 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001944 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001945 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001946 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001947 { INT_RANGE(disable_max_amsdu, -1, 1) },
1948 { INT_RANGE(ampdu_factor, -1, 3) },
1949 { INT_RANGE(ampdu_density, -1, 7) },
1950 { STR(ht_mcs) },
1951#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001952#ifdef CONFIG_VHT_OVERRIDES
1953 { INT_RANGE(disable_vht, 0, 1) },
1954 { INT(vht_capa) },
1955 { INT(vht_capa_mask) },
1956 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1957 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1958 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1959 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1960 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1961 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1962 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1963 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1964 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1965 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1966 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1967 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1968 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1969 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1970 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1971 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1972#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001973 { INT(ap_max_inactivity) },
1974 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001975 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001976#ifdef CONFIG_MACSEC
1977 { INT_RANGE(macsec_policy, 0, 1) },
1978#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001979#ifdef CONFIG_HS20
1980 { INT(update_identifier) },
1981#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001982 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001983 { INT_RANGE(pbss, 0, 1) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984};
1985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986#undef OFFSET
1987#undef _STR
1988#undef STR
1989#undef STR_KEY
1990#undef _STR_LEN
1991#undef STR_LEN
1992#undef STR_LEN_KEY
1993#undef _STR_RANGE
1994#undef STR_RANGE
1995#undef STR_RANGE_KEY
1996#undef _INT
1997#undef INT
1998#undef INT_RANGE
1999#undef _FUNC
2000#undef FUNC
2001#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002002#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003
2004
2005/**
2006 * wpa_config_add_prio_network - Add a network to priority lists
2007 * @config: Configuration data from wpa_config_read()
2008 * @ssid: Pointer to the network configuration to be added to the list
2009 * Returns: 0 on success, -1 on failure
2010 *
2011 * This function is used to add a network block to the priority list of
2012 * networks. This must be called for each network when reading in the full
2013 * configuration. In addition, this can be used indirectly when updating
2014 * priorities by calling wpa_config_update_prio_list().
2015 */
2016int wpa_config_add_prio_network(struct wpa_config *config,
2017 struct wpa_ssid *ssid)
2018{
2019 int prio;
2020 struct wpa_ssid *prev, **nlist;
2021
2022 /*
2023 * Add to an existing priority list if one is available for the
2024 * configured priority level for this network.
2025 */
2026 for (prio = 0; prio < config->num_prio; prio++) {
2027 prev = config->pssid[prio];
2028 if (prev->priority == ssid->priority) {
2029 while (prev->pnext)
2030 prev = prev->pnext;
2031 prev->pnext = ssid;
2032 return 0;
2033 }
2034 }
2035
2036 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002037 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2038 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 if (nlist == NULL)
2040 return -1;
2041
2042 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002043 if (nlist[prio]->priority < ssid->priority) {
2044 os_memmove(&nlist[prio + 1], &nlist[prio],
2045 (config->num_prio - prio) *
2046 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 }
2050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 nlist[prio] = ssid;
2052 config->num_prio++;
2053 config->pssid = nlist;
2054
2055 return 0;
2056}
2057
2058
2059/**
2060 * wpa_config_update_prio_list - Update network priority list
2061 * @config: Configuration data from wpa_config_read()
2062 * Returns: 0 on success, -1 on failure
2063 *
2064 * This function is called to update the priority list of networks in the
2065 * configuration when a network is being added or removed. This is also called
2066 * if a priority for a network is changed.
2067 */
2068int wpa_config_update_prio_list(struct wpa_config *config)
2069{
2070 struct wpa_ssid *ssid;
2071 int ret = 0;
2072
2073 os_free(config->pssid);
2074 config->pssid = NULL;
2075 config->num_prio = 0;
2076
2077 ssid = config->ssid;
2078 while (ssid) {
2079 ssid->pnext = NULL;
2080 if (wpa_config_add_prio_network(config, ssid) < 0)
2081 ret = -1;
2082 ssid = ssid->next;
2083 }
2084
2085 return ret;
2086}
2087
2088
2089#ifdef IEEE8021X_EAPOL
2090static void eap_peer_config_free(struct eap_peer_config *eap)
2091{
2092 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002093 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002095 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 os_free(eap->ca_cert);
2097 os_free(eap->ca_path);
2098 os_free(eap->client_cert);
2099 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002100 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 os_free(eap->dh_file);
2102 os_free(eap->subject_match);
2103 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002104 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002105 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 os_free(eap->ca_cert2);
2107 os_free(eap->ca_path2);
2108 os_free(eap->client_cert2);
2109 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002110 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 os_free(eap->dh_file2);
2112 os_free(eap->subject_match2);
2113 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002114 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002115 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 os_free(eap->phase1);
2117 os_free(eap->phase2);
2118 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002119 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 os_free(eap->engine_id);
2121 os_free(eap->key_id);
2122 os_free(eap->cert_id);
2123 os_free(eap->ca_cert_id);
2124 os_free(eap->key2_id);
2125 os_free(eap->cert2_id);
2126 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002127 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 os_free(eap->engine2_id);
2129 os_free(eap->otp);
2130 os_free(eap->pending_req_otp);
2131 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002132 bin_clear_free(eap->new_password, eap->new_password_len);
2133 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002134 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135}
2136#endif /* IEEE8021X_EAPOL */
2137
2138
2139/**
2140 * wpa_config_free_ssid - Free network/ssid configuration data
2141 * @ssid: Configuration data for the network
2142 *
2143 * This function frees all resources allocated for the network configuration
2144 * data.
2145 */
2146void wpa_config_free_ssid(struct wpa_ssid *ssid)
2147{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002148 struct psk_list_entry *psk;
2149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002151 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002152 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153#ifdef IEEE8021X_EAPOL
2154 eap_peer_config_free(&ssid->eap);
2155#endif /* IEEE8021X_EAPOL */
2156 os_free(ssid->id_str);
2157 os_free(ssid->scan_freq);
2158 os_free(ssid->freq_list);
2159 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002160 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002161 os_free(ssid->bssid_blacklist);
2162 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002163#ifdef CONFIG_HT_OVERRIDES
2164 os_free(ssid->ht_mcs);
2165#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002166#ifdef CONFIG_MESH
2167 os_free(ssid->mesh_basic_rates);
2168#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002169 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2170 list))) {
2171 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002172 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002173 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002174 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175}
2176
2177
Dmitry Shmidt04949592012-07-19 12:16:46 -07002178void wpa_config_free_cred(struct wpa_cred *cred)
2179{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002180 size_t i;
2181
Dmitry Shmidt04949592012-07-19 12:16:46 -07002182 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002183 str_clear_free(cred->username);
2184 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002185 os_free(cred->ca_cert);
2186 os_free(cred->client_cert);
2187 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002188 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002189 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002190 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002191 for (i = 0; i < cred->num_domain; i++)
2192 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002193 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002194 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002195 os_free(cred->eap_method);
2196 os_free(cred->phase1);
2197 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002198 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002199 os_free(cred->roaming_partner);
2200 os_free(cred->provisioning_sp);
2201 for (i = 0; i < cred->num_req_conn_capab; i++)
2202 os_free(cred->req_conn_capab_port[i]);
2203 os_free(cred->req_conn_capab_port);
2204 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002205 os_free(cred);
2206}
2207
2208
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002209void wpa_config_flush_blobs(struct wpa_config *config)
2210{
2211#ifndef CONFIG_NO_CONFIG_BLOBS
2212 struct wpa_config_blob *blob, *prev;
2213
2214 blob = config->blobs;
2215 config->blobs = NULL;
2216 while (blob) {
2217 prev = blob;
2218 blob = blob->next;
2219 wpa_config_free_blob(prev);
2220 }
2221#endif /* CONFIG_NO_CONFIG_BLOBS */
2222}
2223
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225/**
2226 * wpa_config_free - Free configuration data
2227 * @config: Configuration data from wpa_config_read()
2228 *
2229 * This function frees all resources allocated for the configuration data by
2230 * wpa_config_read().
2231 */
2232void wpa_config_free(struct wpa_config *config)
2233{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002236 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237
2238 ssid = config->ssid;
2239 while (ssid) {
2240 prev = ssid;
2241 ssid = ssid->next;
2242 wpa_config_free_ssid(prev);
2243 }
2244
Dmitry Shmidt04949592012-07-19 12:16:46 -07002245 cred = config->cred;
2246 while (cred) {
2247 cprev = cred;
2248 cred = cred->next;
2249 wpa_config_free_cred(cprev);
2250 }
2251
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002252 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002255 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2256 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 os_free(config->ctrl_interface);
2258 os_free(config->ctrl_interface_group);
2259 os_free(config->opensc_engine_path);
2260 os_free(config->pkcs11_engine_path);
2261 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002262 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002263 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002264 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 os_free(config->driver_param);
2266 os_free(config->device_name);
2267 os_free(config->manufacturer);
2268 os_free(config->model_name);
2269 os_free(config->model_number);
2270 os_free(config->serial_number);
2271 os_free(config->config_methods);
2272 os_free(config->p2p_ssid_postfix);
2273 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002274 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002275 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002276 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002277 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278 wpabuf_free(config->wps_nfc_dh_pubkey);
2279 wpabuf_free(config->wps_nfc_dh_privkey);
2280 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002281 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002282 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002283 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002284 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002285 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002286 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002287 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002288 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002289#ifdef CONFIG_MBO
2290 os_free(config->non_pref_chan);
2291#endif /* CONFIG_MBO */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 os_free(config);
2294}
2295
2296
2297/**
2298 * wpa_config_foreach_network - Iterate over each configured network
2299 * @config: Configuration data from wpa_config_read()
2300 * @func: Callback function to process each network
2301 * @arg: Opaque argument to pass to callback function
2302 *
2303 * Iterate over the set of configured networks calling the specified
2304 * function for each item. We guard against callbacks removing the
2305 * supplied network.
2306 */
2307void wpa_config_foreach_network(struct wpa_config *config,
2308 void (*func)(void *, struct wpa_ssid *),
2309 void *arg)
2310{
2311 struct wpa_ssid *ssid, *next;
2312
2313 ssid = config->ssid;
2314 while (ssid) {
2315 next = ssid->next;
2316 func(arg, ssid);
2317 ssid = next;
2318 }
2319}
2320
2321
2322/**
2323 * wpa_config_get_network - Get configured network based on id
2324 * @config: Configuration data from wpa_config_read()
2325 * @id: Unique network id to search for
2326 * Returns: Network configuration or %NULL if not found
2327 */
2328struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2329{
2330 struct wpa_ssid *ssid;
2331
2332 ssid = config->ssid;
2333 while (ssid) {
2334 if (id == ssid->id)
2335 break;
2336 ssid = ssid->next;
2337 }
2338
2339 return ssid;
2340}
2341
2342
2343/**
2344 * wpa_config_add_network - Add a new network with empty configuration
2345 * @config: Configuration data from wpa_config_read()
2346 * Returns: The new network configuration or %NULL if operation failed
2347 */
2348struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2349{
2350 int id;
2351 struct wpa_ssid *ssid, *last = NULL;
2352
2353 id = -1;
2354 ssid = config->ssid;
2355 while (ssid) {
2356 if (ssid->id > id)
2357 id = ssid->id;
2358 last = ssid;
2359 ssid = ssid->next;
2360 }
2361 id++;
2362
2363 ssid = os_zalloc(sizeof(*ssid));
2364 if (ssid == NULL)
2365 return NULL;
2366 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002367 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 if (last)
2369 last->next = ssid;
2370 else
2371 config->ssid = ssid;
2372
2373 wpa_config_update_prio_list(config);
2374
2375 return ssid;
2376}
2377
2378
2379/**
2380 * wpa_config_remove_network - Remove a configured network based on id
2381 * @config: Configuration data from wpa_config_read()
2382 * @id: Unique network id to search for
2383 * Returns: 0 on success, or -1 if the network was not found
2384 */
2385int wpa_config_remove_network(struct wpa_config *config, int id)
2386{
2387 struct wpa_ssid *ssid, *prev = NULL;
2388
2389 ssid = config->ssid;
2390 while (ssid) {
2391 if (id == ssid->id)
2392 break;
2393 prev = ssid;
2394 ssid = ssid->next;
2395 }
2396
2397 if (ssid == NULL)
2398 return -1;
2399
2400 if (prev)
2401 prev->next = ssid->next;
2402 else
2403 config->ssid = ssid->next;
2404
2405 wpa_config_update_prio_list(config);
2406 wpa_config_free_ssid(ssid);
2407 return 0;
2408}
2409
2410
2411/**
2412 * wpa_config_set_network_defaults - Set network default values
2413 * @ssid: Pointer to network configuration data
2414 */
2415void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2416{
2417 ssid->proto = DEFAULT_PROTO;
2418 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2419 ssid->group_cipher = DEFAULT_GROUP;
2420 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002421 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422#ifdef IEEE8021X_EAPOL
2423 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2424 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2425 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002426 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002428#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002429 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2430 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2431 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2432 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2433#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002434#ifdef CONFIG_HT_OVERRIDES
2435 ssid->disable_ht = DEFAULT_DISABLE_HT;
2436 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002437 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002438 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002439 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2440 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2441 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2442#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002443#ifdef CONFIG_VHT_OVERRIDES
2444 ssid->vht_rx_mcs_nss_1 = -1;
2445 ssid->vht_rx_mcs_nss_2 = -1;
2446 ssid->vht_rx_mcs_nss_3 = -1;
2447 ssid->vht_rx_mcs_nss_4 = -1;
2448 ssid->vht_rx_mcs_nss_5 = -1;
2449 ssid->vht_rx_mcs_nss_6 = -1;
2450 ssid->vht_rx_mcs_nss_7 = -1;
2451 ssid->vht_rx_mcs_nss_8 = -1;
2452 ssid->vht_tx_mcs_nss_1 = -1;
2453 ssid->vht_tx_mcs_nss_2 = -1;
2454 ssid->vht_tx_mcs_nss_3 = -1;
2455 ssid->vht_tx_mcs_nss_4 = -1;
2456 ssid->vht_tx_mcs_nss_5 = -1;
2457 ssid->vht_tx_mcs_nss_6 = -1;
2458 ssid->vht_tx_mcs_nss_7 = -1;
2459 ssid->vht_tx_mcs_nss_8 = -1;
2460#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002461 ssid->proactive_key_caching = -1;
2462#ifdef CONFIG_IEEE80211W
2463 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2464#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002465 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466}
2467
2468
2469/**
2470 * wpa_config_set - Set a variable in network configuration
2471 * @ssid: Pointer to network configuration data
2472 * @var: Variable name, e.g., "ssid"
2473 * @value: Variable value
2474 * @line: Line number in configuration file or 0 if not used
2475 * Returns: 0 on success, -1 on failure
2476 *
2477 * This function can be used to set network configuration variables based on
2478 * both the configuration file and management interface input. The value
2479 * parameter must be in the same format as the text-based configuration file is
2480 * using. For example, strings are using double quotation marks.
2481 */
2482int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2483 int line)
2484{
2485 size_t i;
2486 int ret = 0;
2487
2488 if (ssid == NULL || var == NULL || value == NULL)
2489 return -1;
2490
2491 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2492 const struct parse_data *field = &ssid_fields[i];
2493 if (os_strcmp(var, field->name) != 0)
2494 continue;
2495
2496 if (field->parser(field, ssid, line, value)) {
2497 if (line) {
2498 wpa_printf(MSG_ERROR, "Line %d: failed to "
2499 "parse %s '%s'.", line, var, value);
2500 }
2501 ret = -1;
2502 }
2503 break;
2504 }
2505 if (i == NUM_SSID_FIELDS) {
2506 if (line) {
2507 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2508 "'%s'.", line, var);
2509 }
2510 ret = -1;
2511 }
2512
2513 return ret;
2514}
2515
2516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002517int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2518 const char *value)
2519{
2520 size_t len;
2521 char *buf;
2522 int ret;
2523
2524 len = os_strlen(value);
2525 buf = os_malloc(len + 3);
2526 if (buf == NULL)
2527 return -1;
2528 buf[0] = '"';
2529 os_memcpy(buf + 1, value, len);
2530 buf[len + 1] = '"';
2531 buf[len + 2] = '\0';
2532 ret = wpa_config_set(ssid, var, buf, 0);
2533 os_free(buf);
2534 return ret;
2535}
2536
2537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538/**
2539 * wpa_config_get_all - Get all options from network configuration
2540 * @ssid: Pointer to network configuration data
2541 * @get_keys: Determines if keys/passwords will be included in returned list
2542 * (if they may be exported)
2543 * Returns: %NULL terminated list of all set keys and their values in the form
2544 * of [key1, val1, key2, val2, ... , NULL]
2545 *
2546 * This function can be used to get list of all configured network properties.
2547 * The caller is responsible for freeing the returned list and all its
2548 * elements.
2549 */
2550char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2551{
Dmitry Shmidt83474442015-04-15 13:47:09 -07002552#ifdef NO_CONFIG_WRITE
2553 return NULL;
2554#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 const struct parse_data *field;
2556 char *key, *value;
2557 size_t i;
2558 char **props;
2559 int fields_num;
2560
2561 get_keys = get_keys && ssid->export_keys;
2562
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564 if (!props)
2565 return NULL;
2566
2567 fields_num = 0;
2568 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2569 field = &ssid_fields[i];
2570 if (field->key_data && !get_keys)
2571 continue;
2572 value = field->writer(field, ssid);
2573 if (value == NULL)
2574 continue;
2575 if (os_strlen(value) == 0) {
2576 os_free(value);
2577 continue;
2578 }
2579
2580 key = os_strdup(field->name);
2581 if (key == NULL) {
2582 os_free(value);
2583 goto err;
2584 }
2585
2586 props[fields_num * 2] = key;
2587 props[fields_num * 2 + 1] = value;
2588
2589 fields_num++;
2590 }
2591
2592 return props;
2593
2594err:
2595 value = *props;
2596 while (value)
2597 os_free(value++);
2598 os_free(props);
2599 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07002600#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601}
2602
2603
2604#ifndef NO_CONFIG_WRITE
2605/**
2606 * wpa_config_get - Get a variable in network configuration
2607 * @ssid: Pointer to network configuration data
2608 * @var: Variable name, e.g., "ssid"
2609 * Returns: Value of the variable or %NULL on failure
2610 *
2611 * This function can be used to get network configuration variables. The
2612 * returned value is a copy of the configuration variable in text format, i.e,.
2613 * the same format that the text-based configuration file and wpa_config_set()
2614 * are using for the value. The caller is responsible for freeing the returned
2615 * value.
2616 */
2617char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2618{
2619 size_t i;
2620
2621 if (ssid == NULL || var == NULL)
2622 return NULL;
2623
2624 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2625 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08002626 if (os_strcmp(var, field->name) == 0) {
2627 char *ret = field->writer(field, ssid);
Paul Stewart35800752016-03-18 18:14:26 -07002628 if (ret != NULL && (os_strchr(ret, '\r') != NULL ||
2629 os_strchr(ret, '\n') != NULL)) {
2630 wpa_printf(MSG_ERROR,
2631 "Found newline in value for %s; "
Paul Stewart85c72c62016-03-03 15:33:47 -08002632 "not returning it", var);
2633 os_free(ret);
2634 ret = NULL;
2635 }
2636 return ret;
2637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 }
2639
2640 return NULL;
2641}
2642
2643
2644/**
2645 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2646 * @ssid: Pointer to network configuration data
2647 * @var: Variable name, e.g., "ssid"
2648 * Returns: Value of the variable or %NULL on failure
2649 *
2650 * This function can be used to get network configuration variable like
2651 * wpa_config_get(). The only difference is that this functions does not expose
2652 * key/password material from the configuration. In case a key/password field
2653 * is requested, the returned value is an empty string or %NULL if the variable
2654 * is not set or "*" if the variable is set (regardless of its value). The
2655 * returned value is a copy of the configuration variable in text format, i.e,.
2656 * the same format that the text-based configuration file and wpa_config_set()
2657 * are using for the value. The caller is responsible for freeing the returned
2658 * value.
2659 */
2660char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2661{
2662 size_t i;
2663
2664 if (ssid == NULL || var == NULL)
2665 return NULL;
2666
2667 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2668 const struct parse_data *field = &ssid_fields[i];
2669 if (os_strcmp(var, field->name) == 0) {
2670 char *res = field->writer(field, ssid);
2671 if (field->key_data) {
2672 if (res && res[0]) {
2673 wpa_printf(MSG_DEBUG, "Do not allow "
2674 "key_data field to be "
2675 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002676 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 return os_strdup("*");
2678 }
2679
2680 os_free(res);
2681 return NULL;
2682 }
2683 return res;
2684 }
2685 }
2686
2687 return NULL;
2688}
2689#endif /* NO_CONFIG_WRITE */
2690
2691
2692/**
2693 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2694 * @ssid: Pointer to network configuration data
2695 *
2696 * This function must be called to update WPA PSK when either SSID or the
2697 * passphrase has changed for the network configuration.
2698 */
2699void wpa_config_update_psk(struct wpa_ssid *ssid)
2700{
2701#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002702 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 ssid->psk, PMK_LEN);
2704 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2705 ssid->psk, PMK_LEN);
2706 ssid->psk_set = 1;
2707#endif /* CONFIG_NO_PBKDF2 */
2708}
2709
2710
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002711static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2712 const char *value)
2713{
2714 u8 *proto;
2715 int **port;
2716 int *ports, *nports;
2717 const char *pos;
2718 unsigned int num_ports;
2719
2720 proto = os_realloc_array(cred->req_conn_capab_proto,
2721 cred->num_req_conn_capab + 1, sizeof(u8));
2722 if (proto == NULL)
2723 return -1;
2724 cred->req_conn_capab_proto = proto;
2725
2726 port = os_realloc_array(cred->req_conn_capab_port,
2727 cred->num_req_conn_capab + 1, sizeof(int *));
2728 if (port == NULL)
2729 return -1;
2730 cred->req_conn_capab_port = port;
2731
2732 proto[cred->num_req_conn_capab] = atoi(value);
2733
2734 pos = os_strchr(value, ':');
2735 if (pos == NULL) {
2736 port[cred->num_req_conn_capab] = NULL;
2737 cred->num_req_conn_capab++;
2738 return 0;
2739 }
2740 pos++;
2741
2742 ports = NULL;
2743 num_ports = 0;
2744
2745 while (*pos) {
2746 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2747 if (nports == NULL) {
2748 os_free(ports);
2749 return -1;
2750 }
2751 ports = nports;
2752 ports[num_ports++] = atoi(pos);
2753
2754 pos = os_strchr(pos, ',');
2755 if (pos == NULL)
2756 break;
2757 pos++;
2758 }
2759
2760 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2761 if (nports == NULL) {
2762 os_free(ports);
2763 return -1;
2764 }
2765 ports = nports;
2766 ports[num_ports] = -1;
2767
2768 port[cred->num_req_conn_capab] = ports;
2769 cred->num_req_conn_capab++;
2770 return 0;
2771}
2772
2773
Dmitry Shmidt04949592012-07-19 12:16:46 -07002774int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2775 const char *value, int line)
2776{
2777 char *val;
2778 size_t len;
2779
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002780 if (os_strcmp(var, "temporary") == 0) {
2781 cred->temporary = atoi(value);
2782 return 0;
2783 }
2784
Dmitry Shmidt04949592012-07-19 12:16:46 -07002785 if (os_strcmp(var, "priority") == 0) {
2786 cred->priority = atoi(value);
2787 return 0;
2788 }
2789
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002790 if (os_strcmp(var, "sp_priority") == 0) {
2791 int prio = atoi(value);
2792 if (prio < 0 || prio > 255)
2793 return -1;
2794 cred->sp_priority = prio;
2795 return 0;
2796 }
2797
Dmitry Shmidt04949592012-07-19 12:16:46 -07002798 if (os_strcmp(var, "pcsc") == 0) {
2799 cred->pcsc = atoi(value);
2800 return 0;
2801 }
2802
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002803 if (os_strcmp(var, "eap") == 0) {
2804 struct eap_method_type method;
2805 method.method = eap_peer_get_type(value, &method.vendor);
2806 if (method.vendor == EAP_VENDOR_IETF &&
2807 method.method == EAP_TYPE_NONE) {
2808 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2809 "for a credential", line, value);
2810 return -1;
2811 }
2812 os_free(cred->eap_method);
2813 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2814 if (cred->eap_method == NULL)
2815 return -1;
2816 os_memcpy(cred->eap_method, &method, sizeof(method));
2817 return 0;
2818 }
2819
2820 if (os_strcmp(var, "password") == 0 &&
2821 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002822 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002823 cred->password = os_strdup(value);
2824 cred->ext_password = 1;
2825 return 0;
2826 }
2827
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002828 if (os_strcmp(var, "update_identifier") == 0) {
2829 cred->update_identifier = atoi(value);
2830 return 0;
2831 }
2832
2833 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2834 cred->min_dl_bandwidth_home = atoi(value);
2835 return 0;
2836 }
2837
2838 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2839 cred->min_ul_bandwidth_home = atoi(value);
2840 return 0;
2841 }
2842
2843 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2844 cred->min_dl_bandwidth_roaming = atoi(value);
2845 return 0;
2846 }
2847
2848 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2849 cred->min_ul_bandwidth_roaming = atoi(value);
2850 return 0;
2851 }
2852
2853 if (os_strcmp(var, "max_bss_load") == 0) {
2854 cred->max_bss_load = atoi(value);
2855 return 0;
2856 }
2857
2858 if (os_strcmp(var, "req_conn_capab") == 0)
2859 return wpa_config_set_cred_req_conn_capab(cred, value);
2860
2861 if (os_strcmp(var, "ocsp") == 0) {
2862 cred->ocsp = atoi(value);
2863 return 0;
2864 }
2865
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002866 if (os_strcmp(var, "sim_num") == 0) {
2867 cred->sim_num = atoi(value);
2868 return 0;
2869 }
2870
Dmitry Shmidt04949592012-07-19 12:16:46 -07002871 val = wpa_config_parse_string(value, &len);
2872 if (val == NULL) {
2873 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2874 "value '%s'.", line, var, value);
2875 return -1;
2876 }
2877
2878 if (os_strcmp(var, "realm") == 0) {
2879 os_free(cred->realm);
2880 cred->realm = val;
2881 return 0;
2882 }
2883
2884 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002885 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002886 cred->username = val;
2887 return 0;
2888 }
2889
2890 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002891 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002892 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002894 return 0;
2895 }
2896
2897 if (os_strcmp(var, "ca_cert") == 0) {
2898 os_free(cred->ca_cert);
2899 cred->ca_cert = val;
2900 return 0;
2901 }
2902
2903 if (os_strcmp(var, "client_cert") == 0) {
2904 os_free(cred->client_cert);
2905 cred->client_cert = val;
2906 return 0;
2907 }
2908
2909 if (os_strcmp(var, "private_key") == 0) {
2910 os_free(cred->private_key);
2911 cred->private_key = val;
2912 return 0;
2913 }
2914
2915 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002916 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002917 cred->private_key_passwd = val;
2918 return 0;
2919 }
2920
2921 if (os_strcmp(var, "imsi") == 0) {
2922 os_free(cred->imsi);
2923 cred->imsi = val;
2924 return 0;
2925 }
2926
2927 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002928 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002929 cred->milenage = val;
2930 return 0;
2931 }
2932
Dmitry Shmidt051af732013-10-22 13:52:46 -07002933 if (os_strcmp(var, "domain_suffix_match") == 0) {
2934 os_free(cred->domain_suffix_match);
2935 cred->domain_suffix_match = val;
2936 return 0;
2937 }
2938
Dmitry Shmidt04949592012-07-19 12:16:46 -07002939 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002940 char **new_domain;
2941 new_domain = os_realloc_array(cred->domain,
2942 cred->num_domain + 1,
2943 sizeof(char *));
2944 if (new_domain == NULL) {
2945 os_free(val);
2946 return -1;
2947 }
2948 new_domain[cred->num_domain++] = val;
2949 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002950 return 0;
2951 }
2952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953 if (os_strcmp(var, "phase1") == 0) {
2954 os_free(cred->phase1);
2955 cred->phase1 = val;
2956 return 0;
2957 }
2958
2959 if (os_strcmp(var, "phase2") == 0) {
2960 os_free(cred->phase2);
2961 cred->phase2 = val;
2962 return 0;
2963 }
2964
2965 if (os_strcmp(var, "roaming_consortium") == 0) {
2966 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2967 wpa_printf(MSG_ERROR, "Line %d: invalid "
2968 "roaming_consortium length %d (3..15 "
2969 "expected)", line, (int) len);
2970 os_free(val);
2971 return -1;
2972 }
2973 os_memcpy(cred->roaming_consortium, val, len);
2974 cred->roaming_consortium_len = len;
2975 os_free(val);
2976 return 0;
2977 }
2978
Dmitry Shmidt051af732013-10-22 13:52:46 -07002979 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2980 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2981 {
2982 wpa_printf(MSG_ERROR, "Line %d: invalid "
2983 "required_roaming_consortium length %d "
2984 "(3..15 expected)", line, (int) len);
2985 os_free(val);
2986 return -1;
2987 }
2988 os_memcpy(cred->required_roaming_consortium, val, len);
2989 cred->required_roaming_consortium_len = len;
2990 os_free(val);
2991 return 0;
2992 }
2993
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002994 if (os_strcmp(var, "excluded_ssid") == 0) {
2995 struct excluded_ssid *e;
2996
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002997 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002998 wpa_printf(MSG_ERROR, "Line %d: invalid "
2999 "excluded_ssid length %d", line, (int) len);
3000 os_free(val);
3001 return -1;
3002 }
3003
3004 e = os_realloc_array(cred->excluded_ssid,
3005 cred->num_excluded_ssid + 1,
3006 sizeof(struct excluded_ssid));
3007 if (e == NULL) {
3008 os_free(val);
3009 return -1;
3010 }
3011 cred->excluded_ssid = e;
3012
3013 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3014 os_memcpy(e->ssid, val, len);
3015 e->ssid_len = len;
3016
3017 os_free(val);
3018
3019 return 0;
3020 }
3021
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003022 if (os_strcmp(var, "roaming_partner") == 0) {
3023 struct roaming_partner *p;
3024 char *pos;
3025
3026 p = os_realloc_array(cred->roaming_partner,
3027 cred->num_roaming_partner + 1,
3028 sizeof(struct roaming_partner));
3029 if (p == NULL) {
3030 os_free(val);
3031 return -1;
3032 }
3033 cred->roaming_partner = p;
3034
3035 p = &cred->roaming_partner[cred->num_roaming_partner];
3036
3037 pos = os_strchr(val, ',');
3038 if (pos == NULL) {
3039 os_free(val);
3040 return -1;
3041 }
3042 *pos++ = '\0';
3043 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3044 os_free(val);
3045 return -1;
3046 }
3047 os_memcpy(p->fqdn, val, pos - val);
3048
3049 p->exact_match = atoi(pos);
3050
3051 pos = os_strchr(pos, ',');
3052 if (pos == NULL) {
3053 os_free(val);
3054 return -1;
3055 }
3056 *pos++ = '\0';
3057
3058 p->priority = atoi(pos);
3059
3060 pos = os_strchr(pos, ',');
3061 if (pos == NULL) {
3062 os_free(val);
3063 return -1;
3064 }
3065 *pos++ = '\0';
3066
3067 if (os_strlen(pos) >= sizeof(p->country)) {
3068 os_free(val);
3069 return -1;
3070 }
3071 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3072
3073 cred->num_roaming_partner++;
3074 os_free(val);
3075
3076 return 0;
3077 }
3078
3079 if (os_strcmp(var, "provisioning_sp") == 0) {
3080 os_free(cred->provisioning_sp);
3081 cred->provisioning_sp = val;
3082 return 0;
3083 }
3084
Dmitry Shmidt04949592012-07-19 12:16:46 -07003085 if (line) {
3086 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3087 line, var);
3088 }
3089
3090 os_free(val);
3091
3092 return -1;
3093}
3094
3095
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003096static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003097{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003098 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003099 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003100 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003101
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003102 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003103 if (buf == NULL)
3104 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003105 res = os_snprintf(buf, bufsize, "%d", val);
3106 if (os_snprintf_error(bufsize, res)) {
3107 os_free(buf);
3108 buf = NULL;
3109 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003110 return buf;
3111}
3112
3113
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003114static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003115{
3116 if (str == NULL)
3117 return NULL;
3118 return os_strdup(str);
3119}
3120
3121
3122char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3123{
3124 if (os_strcmp(var, "temporary") == 0)
3125 return alloc_int_str(cred->temporary);
3126
3127 if (os_strcmp(var, "priority") == 0)
3128 return alloc_int_str(cred->priority);
3129
3130 if (os_strcmp(var, "sp_priority") == 0)
3131 return alloc_int_str(cred->sp_priority);
3132
3133 if (os_strcmp(var, "pcsc") == 0)
3134 return alloc_int_str(cred->pcsc);
3135
3136 if (os_strcmp(var, "eap") == 0) {
3137 if (!cred->eap_method)
3138 return NULL;
3139 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3140 cred->eap_method[0].method));
3141 }
3142
3143 if (os_strcmp(var, "update_identifier") == 0)
3144 return alloc_int_str(cred->update_identifier);
3145
3146 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3147 return alloc_int_str(cred->min_dl_bandwidth_home);
3148
3149 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3150 return alloc_int_str(cred->min_ul_bandwidth_home);
3151
3152 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3153 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3154
3155 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3156 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3157
3158 if (os_strcmp(var, "max_bss_load") == 0)
3159 return alloc_int_str(cred->max_bss_load);
3160
3161 if (os_strcmp(var, "req_conn_capab") == 0) {
3162 unsigned int i;
3163 char *buf, *end, *pos;
3164 int ret;
3165
3166 if (!cred->num_req_conn_capab)
3167 return NULL;
3168
3169 buf = os_malloc(4000);
3170 if (buf == NULL)
3171 return NULL;
3172 pos = buf;
3173 end = pos + 4000;
3174 for (i = 0; i < cred->num_req_conn_capab; i++) {
3175 int *ports;
3176
3177 ret = os_snprintf(pos, end - pos, "%s%u",
3178 i > 0 ? "\n" : "",
3179 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003180 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003181 return buf;
3182 pos += ret;
3183
3184 ports = cred->req_conn_capab_port[i];
3185 if (ports) {
3186 int j;
3187 for (j = 0; ports[j] != -1; j++) {
3188 ret = os_snprintf(pos, end - pos,
3189 "%s%d",
3190 j > 0 ? "," : ":",
3191 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003192 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003193 return buf;
3194 pos += ret;
3195 }
3196 }
3197 }
3198
3199 return buf;
3200 }
3201
3202 if (os_strcmp(var, "ocsp") == 0)
3203 return alloc_int_str(cred->ocsp);
3204
3205 if (os_strcmp(var, "realm") == 0)
3206 return alloc_strdup(cred->realm);
3207
3208 if (os_strcmp(var, "username") == 0)
3209 return alloc_strdup(cred->username);
3210
3211 if (os_strcmp(var, "password") == 0) {
3212 if (!cred->password)
3213 return NULL;
3214 return alloc_strdup("*");
3215 }
3216
3217 if (os_strcmp(var, "ca_cert") == 0)
3218 return alloc_strdup(cred->ca_cert);
3219
3220 if (os_strcmp(var, "client_cert") == 0)
3221 return alloc_strdup(cred->client_cert);
3222
3223 if (os_strcmp(var, "private_key") == 0)
3224 return alloc_strdup(cred->private_key);
3225
3226 if (os_strcmp(var, "private_key_passwd") == 0) {
3227 if (!cred->private_key_passwd)
3228 return NULL;
3229 return alloc_strdup("*");
3230 }
3231
3232 if (os_strcmp(var, "imsi") == 0)
3233 return alloc_strdup(cred->imsi);
3234
3235 if (os_strcmp(var, "milenage") == 0) {
3236 if (!(cred->milenage))
3237 return NULL;
3238 return alloc_strdup("*");
3239 }
3240
3241 if (os_strcmp(var, "domain_suffix_match") == 0)
3242 return alloc_strdup(cred->domain_suffix_match);
3243
3244 if (os_strcmp(var, "domain") == 0) {
3245 unsigned int i;
3246 char *buf, *end, *pos;
3247 int ret;
3248
3249 if (!cred->num_domain)
3250 return NULL;
3251
3252 buf = os_malloc(4000);
3253 if (buf == NULL)
3254 return NULL;
3255 pos = buf;
3256 end = pos + 4000;
3257
3258 for (i = 0; i < cred->num_domain; i++) {
3259 ret = os_snprintf(pos, end - pos, "%s%s",
3260 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003261 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003262 return buf;
3263 pos += ret;
3264 }
3265
3266 return buf;
3267 }
3268
3269 if (os_strcmp(var, "phase1") == 0)
3270 return alloc_strdup(cred->phase1);
3271
3272 if (os_strcmp(var, "phase2") == 0)
3273 return alloc_strdup(cred->phase2);
3274
3275 if (os_strcmp(var, "roaming_consortium") == 0) {
3276 size_t buflen;
3277 char *buf;
3278
3279 if (!cred->roaming_consortium_len)
3280 return NULL;
3281 buflen = cred->roaming_consortium_len * 2 + 1;
3282 buf = os_malloc(buflen);
3283 if (buf == NULL)
3284 return NULL;
3285 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3286 cred->roaming_consortium_len);
3287 return buf;
3288 }
3289
3290 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3291 size_t buflen;
3292 char *buf;
3293
3294 if (!cred->required_roaming_consortium_len)
3295 return NULL;
3296 buflen = cred->required_roaming_consortium_len * 2 + 1;
3297 buf = os_malloc(buflen);
3298 if (buf == NULL)
3299 return NULL;
3300 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3301 cred->required_roaming_consortium_len);
3302 return buf;
3303 }
3304
3305 if (os_strcmp(var, "excluded_ssid") == 0) {
3306 unsigned int i;
3307 char *buf, *end, *pos;
3308
3309 if (!cred->num_excluded_ssid)
3310 return NULL;
3311
3312 buf = os_malloc(4000);
3313 if (buf == NULL)
3314 return NULL;
3315 pos = buf;
3316 end = pos + 4000;
3317
3318 for (i = 0; i < cred->num_excluded_ssid; i++) {
3319 struct excluded_ssid *e;
3320 int ret;
3321
3322 e = &cred->excluded_ssid[i];
3323 ret = os_snprintf(pos, end - pos, "%s%s",
3324 i > 0 ? "\n" : "",
3325 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003326 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003327 return buf;
3328 pos += ret;
3329 }
3330
3331 return buf;
3332 }
3333
3334 if (os_strcmp(var, "roaming_partner") == 0) {
3335 unsigned int i;
3336 char *buf, *end, *pos;
3337
3338 if (!cred->num_roaming_partner)
3339 return NULL;
3340
3341 buf = os_malloc(4000);
3342 if (buf == NULL)
3343 return NULL;
3344 pos = buf;
3345 end = pos + 4000;
3346
3347 for (i = 0; i < cred->num_roaming_partner; i++) {
3348 struct roaming_partner *p;
3349 int ret;
3350
3351 p = &cred->roaming_partner[i];
3352 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3353 i > 0 ? "\n" : "",
3354 p->fqdn, p->exact_match, p->priority,
3355 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003357 return buf;
3358 pos += ret;
3359 }
3360
3361 return buf;
3362 }
3363
3364 if (os_strcmp(var, "provisioning_sp") == 0)
3365 return alloc_strdup(cred->provisioning_sp);
3366
3367 return NULL;
3368}
3369
3370
Dmitry Shmidt04949592012-07-19 12:16:46 -07003371struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3372{
3373 struct wpa_cred *cred;
3374
3375 cred = config->cred;
3376 while (cred) {
3377 if (id == cred->id)
3378 break;
3379 cred = cred->next;
3380 }
3381
3382 return cred;
3383}
3384
3385
3386struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3387{
3388 int id;
3389 struct wpa_cred *cred, *last = NULL;
3390
3391 id = -1;
3392 cred = config->cred;
3393 while (cred) {
3394 if (cred->id > id)
3395 id = cred->id;
3396 last = cred;
3397 cred = cred->next;
3398 }
3399 id++;
3400
3401 cred = os_zalloc(sizeof(*cred));
3402 if (cred == NULL)
3403 return NULL;
3404 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003405 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 if (last)
3407 last->next = cred;
3408 else
3409 config->cred = cred;
3410
3411 return cred;
3412}
3413
3414
3415int wpa_config_remove_cred(struct wpa_config *config, int id)
3416{
3417 struct wpa_cred *cred, *prev = NULL;
3418
3419 cred = config->cred;
3420 while (cred) {
3421 if (id == cred->id)
3422 break;
3423 prev = cred;
3424 cred = cred->next;
3425 }
3426
3427 if (cred == NULL)
3428 return -1;
3429
3430 if (prev)
3431 prev->next = cred->next;
3432 else
3433 config->cred = cred->next;
3434
3435 wpa_config_free_cred(cred);
3436 return 0;
3437}
3438
3439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440#ifndef CONFIG_NO_CONFIG_BLOBS
3441/**
3442 * wpa_config_get_blob - Get a named configuration blob
3443 * @config: Configuration data from wpa_config_read()
3444 * @name: Name of the blob
3445 * Returns: Pointer to blob data or %NULL if not found
3446 */
3447const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3448 const char *name)
3449{
3450 struct wpa_config_blob *blob = config->blobs;
3451
3452 while (blob) {
3453 if (os_strcmp(blob->name, name) == 0)
3454 return blob;
3455 blob = blob->next;
3456 }
3457 return NULL;
3458}
3459
3460
3461/**
3462 * wpa_config_set_blob - Set or add a named configuration blob
3463 * @config: Configuration data from wpa_config_read()
3464 * @blob: New value for the blob
3465 *
3466 * Adds a new configuration blob or replaces the current value of an existing
3467 * blob.
3468 */
3469void wpa_config_set_blob(struct wpa_config *config,
3470 struct wpa_config_blob *blob)
3471{
3472 wpa_config_remove_blob(config, blob->name);
3473 blob->next = config->blobs;
3474 config->blobs = blob;
3475}
3476
3477
3478/**
3479 * wpa_config_free_blob - Free blob data
3480 * @blob: Pointer to blob to be freed
3481 */
3482void wpa_config_free_blob(struct wpa_config_blob *blob)
3483{
3484 if (blob) {
3485 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003486 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003487 os_free(blob);
3488 }
3489}
3490
3491
3492/**
3493 * wpa_config_remove_blob - Remove a named configuration blob
3494 * @config: Configuration data from wpa_config_read()
3495 * @name: Name of the blob to remove
3496 * Returns: 0 if blob was removed or -1 if blob was not found
3497 */
3498int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3499{
3500 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3501
3502 while (pos) {
3503 if (os_strcmp(pos->name, name) == 0) {
3504 if (prev)
3505 prev->next = pos->next;
3506 else
3507 config->blobs = pos->next;
3508 wpa_config_free_blob(pos);
3509 return 0;
3510 }
3511 prev = pos;
3512 pos = pos->next;
3513 }
3514
3515 return -1;
3516}
3517#endif /* CONFIG_NO_CONFIG_BLOBS */
3518
3519
3520/**
3521 * wpa_config_alloc_empty - Allocate an empty configuration
3522 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3523 * socket
3524 * @driver_param: Driver parameters
3525 * Returns: Pointer to allocated configuration data or %NULL on failure
3526 */
3527struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3528 const char *driver_param)
3529{
3530 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003531 const int aCWmin = 4, aCWmax = 10;
3532 const struct hostapd_wmm_ac_params ac_bk =
3533 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3534 const struct hostapd_wmm_ac_params ac_be =
3535 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3536 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3537 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3538 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3539 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540
3541 config = os_zalloc(sizeof(*config));
3542 if (config == NULL)
3543 return NULL;
3544 config->eapol_version = DEFAULT_EAPOL_VERSION;
3545 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003546 config->user_mpm = DEFAULT_USER_MPM;
3547 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003548 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003549 config->dot11RSNASAERetransPeriod =
3550 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 config->fast_reauth = DEFAULT_FAST_REAUTH;
3552 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3553 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003554 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003555 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003556 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003557 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3559 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3560 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3561 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003562 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003563 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003564 config->wmm_ac_params[0] = ac_be;
3565 config->wmm_ac_params[1] = ac_bk;
3566 config->wmm_ac_params[2] = ac_vi;
3567 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003568 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003569 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003570 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003571 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003572 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003574#ifdef CONFIG_MBO
3575 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
3576#endif /* CONFIG_MBO */
3577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 if (ctrl_interface)
3579 config->ctrl_interface = os_strdup(ctrl_interface);
3580 if (driver_param)
3581 config->driver_param = os_strdup(driver_param);
3582
3583 return config;
3584}
3585
3586
3587#ifndef CONFIG_NO_STDOUT_DEBUG
3588/**
3589 * wpa_config_debug_dump_networks - Debug dump of configured networks
3590 * @config: Configuration data from wpa_config_read()
3591 */
3592void wpa_config_debug_dump_networks(struct wpa_config *config)
3593{
3594 int prio;
3595 struct wpa_ssid *ssid;
3596
3597 for (prio = 0; prio < config->num_prio; prio++) {
3598 ssid = config->pssid[prio];
3599 wpa_printf(MSG_DEBUG, "Priority group %d",
3600 ssid->priority);
3601 while (ssid) {
3602 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3603 ssid->id,
3604 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3605 ssid = ssid->pnext;
3606 }
3607 }
3608}
3609#endif /* CONFIG_NO_STDOUT_DEBUG */
3610
3611
3612struct global_parse_data {
3613 char *name;
3614 int (*parser)(const struct global_parse_data *data,
3615 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003616 int (*get)(const char *name, struct wpa_config *config, long offset,
3617 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 void *param1, *param2, *param3;
3619 unsigned int changed_flag;
3620};
3621
3622
3623static int wpa_global_config_parse_int(const struct global_parse_data *data,
3624 struct wpa_config *config, int line,
3625 const char *pos)
3626{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003627 int val, *dst;
3628 char *end;
3629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003631 val = strtol(pos, &end, 0);
3632 if (*end) {
3633 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3634 line, pos);
3635 return -1;
3636 }
3637 *dst = val;
3638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3640
3641 if (data->param2 && *dst < (long) data->param2) {
3642 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3643 "min_value=%ld)", line, data->name, *dst,
3644 (long) data->param2);
3645 *dst = (long) data->param2;
3646 return -1;
3647 }
3648
3649 if (data->param3 && *dst > (long) data->param3) {
3650 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3651 "max_value=%ld)", line, data->name, *dst,
3652 (long) data->param3);
3653 *dst = (long) data->param3;
3654 return -1;
3655 }
3656
3657 return 0;
3658}
3659
3660
3661static int wpa_global_config_parse_str(const struct global_parse_data *data,
3662 struct wpa_config *config, int line,
3663 const char *pos)
3664{
3665 size_t len;
3666 char **dst, *tmp;
3667
3668 len = os_strlen(pos);
3669 if (data->param2 && len < (size_t) data->param2) {
3670 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3671 "min_len=%ld)", line, data->name,
3672 (unsigned long) len, (long) data->param2);
3673 return -1;
3674 }
3675
3676 if (data->param3 && len > (size_t) data->param3) {
3677 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3678 "max_len=%ld)", line, data->name,
3679 (unsigned long) len, (long) data->param3);
3680 return -1;
3681 }
3682
3683 tmp = os_strdup(pos);
3684 if (tmp == NULL)
3685 return -1;
3686
3687 dst = (char **) (((u8 *) config) + (long) data->param1);
3688 os_free(*dst);
3689 *dst = tmp;
3690 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3691
3692 return 0;
3693}
3694
3695
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003696static int wpa_config_process_bgscan(const struct global_parse_data *data,
3697 struct wpa_config *config, int line,
3698 const char *pos)
3699{
3700 size_t len;
3701 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003702 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003703
3704 tmp = wpa_config_parse_string(pos, &len);
3705 if (tmp == NULL) {
3706 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3707 line, data->name);
3708 return -1;
3709 }
3710
Dmitry Shmidt97672262014-02-03 13:02:54 -08003711 res = wpa_global_config_parse_str(data, config, line, tmp);
3712 os_free(tmp);
3713 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003714}
3715
3716
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3718 struct wpa_config *config, int line,
3719 const char *pos)
3720{
3721 size_t len;
3722 struct wpabuf **dst, *tmp;
3723
3724 len = os_strlen(pos);
3725 if (len & 0x01)
3726 return -1;
3727
3728 tmp = wpabuf_alloc(len / 2);
3729 if (tmp == NULL)
3730 return -1;
3731
3732 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3733 wpabuf_free(tmp);
3734 return -1;
3735 }
3736
3737 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3738 wpabuf_free(*dst);
3739 *dst = tmp;
3740 wpa_printf(MSG_DEBUG, "%s", data->name);
3741
3742 return 0;
3743}
3744
3745
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003746static int wpa_config_process_freq_list(const struct global_parse_data *data,
3747 struct wpa_config *config, int line,
3748 const char *value)
3749{
3750 int *freqs;
3751
3752 freqs = wpa_config_parse_int_array(value);
3753 if (freqs == NULL)
3754 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003755 if (freqs[0] == 0) {
3756 os_free(freqs);
3757 freqs = NULL;
3758 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003759 os_free(config->freq_list);
3760 config->freq_list = freqs;
3761 return 0;
3762}
3763
3764
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003765#ifdef CONFIG_P2P
3766static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3767 struct wpa_config *config, int line,
3768 const char *pos)
3769{
3770 u32 *dst;
3771 struct hostapd_ip_addr addr;
3772
3773 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3774 return -1;
3775 if (addr.af != AF_INET)
3776 return -1;
3777
3778 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3779 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3780 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3781 WPA_GET_BE32((u8 *) dst));
3782
3783 return 0;
3784}
3785#endif /* CONFIG_P2P */
3786
3787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788static int wpa_config_process_country(const struct global_parse_data *data,
3789 struct wpa_config *config, int line,
3790 const char *pos)
3791{
3792 if (!pos[0] || !pos[1]) {
3793 wpa_printf(MSG_DEBUG, "Invalid country set");
3794 return -1;
3795 }
3796 config->country[0] = pos[0];
3797 config->country[1] = pos[1];
3798 wpa_printf(MSG_DEBUG, "country='%c%c'",
3799 config->country[0], config->country[1]);
3800 return 0;
3801}
3802
3803
3804static int wpa_config_process_load_dynamic_eap(
3805 const struct global_parse_data *data, struct wpa_config *config,
3806 int line, const char *so)
3807{
3808 int ret;
3809 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3810 ret = eap_peer_method_load(so);
3811 if (ret == -2) {
3812 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3813 "reloading.");
3814 } else if (ret) {
3815 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3816 "method '%s'.", line, so);
3817 return -1;
3818 }
3819
3820 return 0;
3821}
3822
3823
3824#ifdef CONFIG_WPS
3825
3826static int wpa_config_process_uuid(const struct global_parse_data *data,
3827 struct wpa_config *config, int line,
3828 const char *pos)
3829{
3830 char buf[40];
3831 if (uuid_str2bin(pos, config->uuid)) {
3832 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3833 return -1;
3834 }
3835 uuid_bin2str(config->uuid, buf, sizeof(buf));
3836 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3837 return 0;
3838}
3839
3840
3841static int wpa_config_process_device_type(
3842 const struct global_parse_data *data,
3843 struct wpa_config *config, int line, const char *pos)
3844{
3845 return wps_dev_type_str2bin(pos, config->device_type);
3846}
3847
3848
3849static int wpa_config_process_os_version(const struct global_parse_data *data,
3850 struct wpa_config *config, int line,
3851 const char *pos)
3852{
3853 if (hexstr2bin(pos, config->os_version, 4)) {
3854 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3855 return -1;
3856 }
3857 wpa_printf(MSG_DEBUG, "os_version=%08x",
3858 WPA_GET_BE32(config->os_version));
3859 return 0;
3860}
3861
Dmitry Shmidt04949592012-07-19 12:16:46 -07003862
3863static int wpa_config_process_wps_vendor_ext_m1(
3864 const struct global_parse_data *data,
3865 struct wpa_config *config, int line, const char *pos)
3866{
3867 struct wpabuf *tmp;
3868 int len = os_strlen(pos) / 2;
3869 u8 *p;
3870
3871 if (!len) {
3872 wpa_printf(MSG_ERROR, "Line %d: "
3873 "invalid wps_vendor_ext_m1", line);
3874 return -1;
3875 }
3876
3877 tmp = wpabuf_alloc(len);
3878 if (tmp) {
3879 p = wpabuf_put(tmp, len);
3880
3881 if (hexstr2bin(pos, p, len)) {
3882 wpa_printf(MSG_ERROR, "Line %d: "
3883 "invalid wps_vendor_ext_m1", line);
3884 wpabuf_free(tmp);
3885 return -1;
3886 }
3887
3888 wpabuf_free(config->wps_vendor_ext_m1);
3889 config->wps_vendor_ext_m1 = tmp;
3890 } else {
3891 wpa_printf(MSG_ERROR, "Can not allocate "
3892 "memory for wps_vendor_ext_m1");
3893 return -1;
3894 }
3895
3896 return 0;
3897}
3898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899#endif /* CONFIG_WPS */
3900
3901#ifdef CONFIG_P2P
3902static int wpa_config_process_sec_device_type(
3903 const struct global_parse_data *data,
3904 struct wpa_config *config, int line, const char *pos)
3905{
3906 int idx;
3907
3908 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3909 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3910 "items", line);
3911 return -1;
3912 }
3913
3914 idx = config->num_sec_device_types;
3915
3916 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3917 return -1;
3918
3919 config->num_sec_device_types++;
3920 return 0;
3921}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003922
3923
3924static int wpa_config_process_p2p_pref_chan(
3925 const struct global_parse_data *data,
3926 struct wpa_config *config, int line, const char *pos)
3927{
3928 struct p2p_channel *pref = NULL, *n;
3929 unsigned int num = 0;
3930 const char *pos2;
3931 u8 op_class, chan;
3932
3933 /* format: class:chan,class:chan,... */
3934
3935 while (*pos) {
3936 op_class = atoi(pos);
3937 pos2 = os_strchr(pos, ':');
3938 if (pos2 == NULL)
3939 goto fail;
3940 pos2++;
3941 chan = atoi(pos2);
3942
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003943 n = os_realloc_array(pref, num + 1,
3944 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003945 if (n == NULL)
3946 goto fail;
3947 pref = n;
3948 pref[num].op_class = op_class;
3949 pref[num].chan = chan;
3950 num++;
3951
3952 pos = os_strchr(pos2, ',');
3953 if (pos == NULL)
3954 break;
3955 pos++;
3956 }
3957
3958 os_free(config->p2p_pref_chan);
3959 config->p2p_pref_chan = pref;
3960 config->num_p2p_pref_chan = num;
3961 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3962 (u8 *) config->p2p_pref_chan,
3963 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3964
3965 return 0;
3966
3967fail:
3968 os_free(pref);
3969 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3970 return -1;
3971}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003972
3973
3974static int wpa_config_process_p2p_no_go_freq(
3975 const struct global_parse_data *data,
3976 struct wpa_config *config, int line, const char *pos)
3977{
3978 int ret;
3979
3980 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3981 if (ret < 0) {
3982 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3983 return -1;
3984 }
3985
3986 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3987 config->p2p_no_go_freq.num);
3988
3989 return 0;
3990}
3991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992#endif /* CONFIG_P2P */
3993
3994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003995static int wpa_config_process_hessid(
3996 const struct global_parse_data *data,
3997 struct wpa_config *config, int line, const char *pos)
3998{
3999 if (hwaddr_aton2(pos, config->hessid) < 0) {
4000 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4001 line, pos);
4002 return -1;
4003 }
4004
4005 return 0;
4006}
4007
4008
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004009static int wpa_config_process_sae_groups(
4010 const struct global_parse_data *data,
4011 struct wpa_config *config, int line, const char *pos)
4012{
4013 int *groups = wpa_config_parse_int_array(pos);
4014 if (groups == NULL) {
4015 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4016 line, pos);
4017 return -1;
4018 }
4019
4020 os_free(config->sae_groups);
4021 config->sae_groups = groups;
4022
4023 return 0;
4024}
4025
4026
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004027static int wpa_config_process_ap_vendor_elements(
4028 const struct global_parse_data *data,
4029 struct wpa_config *config, int line, const char *pos)
4030{
4031 struct wpabuf *tmp;
4032 int len = os_strlen(pos) / 2;
4033 u8 *p;
4034
4035 if (!len) {
4036 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4037 line);
4038 return -1;
4039 }
4040
4041 tmp = wpabuf_alloc(len);
4042 if (tmp) {
4043 p = wpabuf_put(tmp, len);
4044
4045 if (hexstr2bin(pos, p, len)) {
4046 wpa_printf(MSG_ERROR, "Line %d: invalid "
4047 "ap_vendor_elements", line);
4048 wpabuf_free(tmp);
4049 return -1;
4050 }
4051
4052 wpabuf_free(config->ap_vendor_elements);
4053 config->ap_vendor_elements = tmp;
4054 } else {
4055 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4056 "ap_vendor_elements");
4057 return -1;
4058 }
4059
4060 return 0;
4061}
4062
4063
Dmitry Shmidt56052862013-10-04 10:23:25 -07004064#ifdef CONFIG_CTRL_IFACE
4065static int wpa_config_process_no_ctrl_interface(
4066 const struct global_parse_data *data,
4067 struct wpa_config *config, int line, const char *pos)
4068{
4069 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4070 os_free(config->ctrl_interface);
4071 config->ctrl_interface = NULL;
4072 return 0;
4073}
4074#endif /* CONFIG_CTRL_IFACE */
4075
4076
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004077static int wpa_config_get_int(const char *name, struct wpa_config *config,
4078 long offset, char *buf, size_t buflen,
4079 int pretty_print)
4080{
4081 int *val = (int *) (((u8 *) config) + (long) offset);
4082
4083 if (pretty_print)
4084 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4085 return os_snprintf(buf, buflen, "%d", *val);
4086}
4087
4088
4089static int wpa_config_get_str(const char *name, struct wpa_config *config,
4090 long offset, char *buf, size_t buflen,
4091 int pretty_print)
4092{
4093 char **val = (char **) (((u8 *) config) + (long) offset);
4094 int res;
4095
4096 if (pretty_print)
4097 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4098 *val ? *val : "null");
4099 else if (!*val)
4100 return -1;
4101 else
4102 res = os_snprintf(buf, buflen, "%s", *val);
4103 if (os_snprintf_error(buflen, res))
4104 res = -1;
4105
4106 return res;
4107}
4108
4109
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004110#ifdef CONFIG_P2P
4111static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4112 long offset, char *buf, size_t buflen,
4113 int pretty_print)
4114{
4115 void *val = ((u8 *) config) + (long) offset;
4116 int res;
4117 char addr[INET_ADDRSTRLEN];
4118
4119 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4120 return -1;
4121
4122 if (pretty_print)
4123 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4124 else
4125 res = os_snprintf(buf, buflen, "%s", addr);
4126
4127 if (os_snprintf_error(buflen, res))
4128 res = -1;
4129
4130 return res;
4131}
4132#endif /* CONFIG_P2P */
4133
4134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135#ifdef OFFSET
4136#undef OFFSET
4137#endif /* OFFSET */
4138/* OFFSET: Get offset of a variable within the wpa_config structure */
4139#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4140
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004141#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4142#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4143#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144#define INT(f) _INT(f), NULL, NULL
4145#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004146#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147#define STR(f) _STR(f), NULL, NULL
4148#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004149#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004150#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4151 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152
4153static const struct global_parse_data global_fields[] = {
4154#ifdef CONFIG_CTRL_IFACE
4155 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004156 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 { STR(ctrl_interface_group), 0 } /* deprecated */,
4158#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004159#ifdef CONFIG_MACSEC
4160 { INT_RANGE(eapol_version, 1, 3), 0 },
4161#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004163#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004165 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004166#ifdef CONFIG_MESH
4167 { INT(user_mpm), 0 },
4168 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004169 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004170 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004172 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 { INT(fast_reauth), 0 },
4174 { STR(opensc_engine_path), 0 },
4175 { STR(pkcs11_engine_path), 0 },
4176 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004177 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 { STR(pcsc_reader), 0 },
4179 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004180 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181 { STR(driver_param), 0 },
4182 { INT(dot11RSNAConfigPMKLifetime), 0 },
4183 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4184 { INT(dot11RSNAConfigSATimeout), 0 },
4185#ifndef CONFIG_NO_CONFIG_WRITE
4186 { INT(update_config), 0 },
4187#endif /* CONFIG_NO_CONFIG_WRITE */
4188 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4189#ifdef CONFIG_WPS
4190 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004191 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4192 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4194 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4195 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4196 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4197 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4198 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4199 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4200 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004201 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202#endif /* CONFIG_WPS */
4203#ifdef CONFIG_P2P
4204 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004205 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4206 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004207 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4208 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4210 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4211 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4212 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4213 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004214 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004215 { INT_RANGE(p2p_passphrase_len, 8, 63),
4216 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004217 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004218 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4219 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004220 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004221 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004222 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004223 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004224 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004225 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004226 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004227 { IPV4(ip_addr_go), 0 },
4228 { IPV4(ip_addr_mask), 0 },
4229 { IPV4(ip_addr_start), 0 },
4230 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004231 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232#endif /* CONFIG_P2P */
4233 { FUNC(country), CFG_CHANGED_COUNTRY },
4234 { INT(bss_max_count), 0 },
4235 { INT(bss_expiration_age), 0 },
4236 { INT(bss_expiration_scan_count), 0 },
4237 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004238 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004241#ifdef CONFIG_HS20
4242 { INT_RANGE(hs20, 0, 1), 0 },
4243#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004244 { INT_RANGE(interworking, 0, 1), 0 },
4245 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004246 { INT_RANGE(access_network_type, 0, 15), 0 },
4247 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4248 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004249 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4250 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4251 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4252 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4253 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004254 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4255 { INT(p2p_go_max_inactivity), 0 },
4256 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004257 { INT(okc), 0 },
4258 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004259 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004260 { INT(dtim_period), 0 },
4261 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004262 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004263 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004264 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004265 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004266 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004267 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004268 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004269 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004270 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004271 { INT(mac_addr), 0 },
4272 { INT(rand_addr_lifetime), 0 },
4273 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004274 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004275 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004276 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004277 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004278#ifdef CONFIG_FST
4279 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4280 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4281 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4282#endif /* CONFIG_FST */
4283 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004284 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004285#ifdef CONFIG_MBO
4286 { STR(non_pref_chan), 0 },
4287 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
4288 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
4289#endif /*CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290};
4291
4292#undef FUNC
4293#undef _INT
4294#undef INT
4295#undef INT_RANGE
4296#undef _STR
4297#undef STR
4298#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004299#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004300#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004301#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302
4303
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004304int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4305{
4306 int result = 0;
4307 size_t i;
4308
4309 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4310 const struct global_parse_data *field = &global_fields[i];
4311 int tmp;
4312
4313 if (!field->get)
4314 continue;
4315
4316 tmp = field->get(field->name, config, (long) field->param1,
4317 buf, buflen, 1);
4318 if (tmp < 0)
4319 return -1;
4320 buf += tmp;
4321 buflen -= tmp;
4322 result += tmp;
4323 }
4324 return result;
4325}
4326
4327
4328int wpa_config_get_value(const char *name, struct wpa_config *config,
4329 char *buf, size_t buflen)
4330{
4331 size_t i;
4332
4333 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4334 const struct global_parse_data *field = &global_fields[i];
4335
4336 if (os_strcmp(name, field->name) != 0)
4337 continue;
4338 if (!field->get)
4339 break;
4340 return field->get(name, config, (long) field->param1,
4341 buf, buflen, 0);
4342 }
4343
4344 return -1;
4345}
4346
4347
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004348int wpa_config_get_num_global_field_names(void)
4349{
4350 return NUM_GLOBAL_FIELDS;
4351}
4352
4353
4354const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4355{
4356 if (i >= NUM_GLOBAL_FIELDS)
4357 return NULL;
4358
4359 if (no_var)
4360 *no_var = !global_fields[i].param1;
4361 return global_fields[i].name;
4362}
4363
4364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4366{
4367 size_t i;
4368 int ret = 0;
4369
4370 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4371 const struct global_parse_data *field = &global_fields[i];
4372 size_t flen = os_strlen(field->name);
4373 if (os_strncmp(pos, field->name, flen) != 0 ||
4374 pos[flen] != '=')
4375 continue;
4376
4377 if (field->parser(field, config, line, pos + flen + 1)) {
4378 wpa_printf(MSG_ERROR, "Line %d: failed to "
4379 "parse '%s'.", line, pos);
4380 ret = -1;
4381 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004382 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4383 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 config->changed_parameters |= field->changed_flag;
4385 break;
4386 }
4387 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004388#ifdef CONFIG_AP
4389 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4390 char *tmp = os_strchr(pos, '=');
4391 if (tmp == NULL) {
4392 if (line < 0)
4393 return -1;
4394 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4395 "'%s'", line, pos);
4396 return -1;
4397 }
4398 *tmp++ = '\0';
4399 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4400 tmp)) {
4401 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4402 "AC item", line);
4403 return -1;
4404 }
4405 }
4406#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 if (line < 0)
4408 return -1;
4409 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4410 line, pos);
4411 ret = -1;
4412 }
4413
4414 return ret;
4415}