blob: a0d480ed91cc681d02e132e1711ec3d8912e5bdc [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 Shmidt29333592017-01-09 12:27:11 -080014#include "common/ieee802_1x_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "crypto/sha1.h"
16#include "rsn_supp/wpa.h"
17#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070018#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080019#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "config.h"
21
22
23#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
24#define NO_CONFIG_WRITE
25#endif
26
27/*
28 * Structure for network configuration parsing. This data is used to implement
29 * a generic parser for each network block variable. The table of configuration
30 * variables is defined below in this file (ssid_fields[]).
31 */
32struct parse_data {
33 /* Configuration variable name */
34 char *name;
35
Dmitry Shmidte4663042016-04-04 10:07:49 -070036 /* Parser function for this variable. The parser functions return 0 or 1
37 * to indicate success. Value 0 indicates that the parameter value may
38 * have changed while value 1 means that the value did not change.
39 * Error cases (failure to parse the string) are indicated by returning
40 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
42 int line, const char *value);
43
44#ifndef NO_CONFIG_WRITE
45 /* Writer function (i.e., to get the variable in text format from
46 * internal presentation). */
47 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
48#endif /* NO_CONFIG_WRITE */
49
50 /* Variable specific parameters for the parser. */
51 void *param1, *param2, *param3, *param4;
52
53 /* 0 = this variable can be included in debug output and ctrl_iface
54 * 1 = this variable contains key/private data and it must not be
55 * included in debug output unless explicitly requested. In
56 * addition, this variable will not be readable through the
57 * ctrl_iface.
58 */
59 int key_data;
60};
61
62
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063static int wpa_config_parse_str(const struct parse_data *data,
64 struct wpa_ssid *ssid,
65 int line, const char *value)
66{
Dmitry Shmidte4663042016-04-04 10:07:49 -070067 size_t res_len, *dst_len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 char **dst, *tmp;
69
70 if (os_strcmp(value, "NULL") == 0) {
71 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
72 data->name);
73 tmp = NULL;
74 res_len = 0;
75 goto set;
76 }
77
78 tmp = wpa_config_parse_string(value, &res_len);
79 if (tmp == NULL) {
80 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
81 line, data->name,
82 data->key_data ? "[KEY DATA REMOVED]" : value);
83 return -1;
84 }
85
86 if (data->key_data) {
87 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
88 (u8 *) tmp, res_len);
89 } else {
90 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
91 (u8 *) tmp, res_len);
92 }
93
94 if (data->param3 && res_len < (size_t) data->param3) {
95 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
96 "min_len=%ld)", line, data->name,
97 (unsigned long) res_len, (long) data->param3);
98 os_free(tmp);
99 return -1;
100 }
101
102 if (data->param4 && res_len > (size_t) data->param4) {
103 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
104 "max_len=%ld)", line, data->name,
105 (unsigned long) res_len, (long) data->param4);
106 os_free(tmp);
107 return -1;
108 }
109
110set:
111 dst = (char **) (((u8 *) ssid) + (long) data->param1);
112 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700113
114 if (data->param2)
115 prev_len = *dst_len;
116 else if (*dst)
117 prev_len = os_strlen(*dst);
118 else
119 prev_len = 0;
120 if ((*dst == NULL && tmp == NULL) ||
121 (*dst && tmp && prev_len == res_len &&
122 os_memcmp(*dst, tmp, res_len) == 0)) {
123 /* No change to the previously configured value */
124 os_free(tmp);
125 return 1;
126 }
127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 os_free(*dst);
129 *dst = tmp;
130 if (data->param2)
131 *dst_len = res_len;
132
133 return 0;
134}
135
136
137#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
139{
140 char *buf;
141
142 buf = os_malloc(len + 3);
143 if (buf == NULL)
144 return NULL;
145 buf[0] = '"';
146 os_memcpy(buf + 1, value, len);
147 buf[len + 1] = '"';
148 buf[len + 2] = '\0';
149
150 return buf;
151}
152
153
154static char * wpa_config_write_string_hex(const u8 *value, size_t len)
155{
156 char *buf;
157
158 buf = os_zalloc(2 * len + 1);
159 if (buf == NULL)
160 return NULL;
161 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
162
163 return buf;
164}
165
166
167static char * wpa_config_write_string(const u8 *value, size_t len)
168{
169 if (value == NULL)
170 return NULL;
171
172 if (is_hex(value, len))
173 return wpa_config_write_string_hex(value, len);
174 else
175 return wpa_config_write_string_ascii(value, len);
176}
177
178
179static char * wpa_config_write_str(const struct parse_data *data,
180 struct wpa_ssid *ssid)
181{
182 size_t len;
183 char **src;
184
185 src = (char **) (((u8 *) ssid) + (long) data->param1);
186 if (*src == NULL)
187 return NULL;
188
189 if (data->param2)
190 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
191 else
192 len = os_strlen(*src);
193
194 return wpa_config_write_string((const u8 *) *src, len);
195}
196#endif /* NO_CONFIG_WRITE */
197
198
199static int wpa_config_parse_int(const struct parse_data *data,
200 struct wpa_ssid *ssid,
201 int line, const char *value)
202{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700203 int val, *dst;
204 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205
206 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700207 val = strtol(value, &end, 0);
208 if (*end) {
209 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
210 line, value);
211 return -1;
212 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700213
214 if (*dst == val)
215 return 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700216 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
218
219 if (data->param3 && *dst < (long) data->param3) {
220 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
221 "min_value=%ld)", line, data->name, *dst,
222 (long) data->param3);
223 *dst = (long) data->param3;
224 return -1;
225 }
226
227 if (data->param4 && *dst > (long) data->param4) {
228 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
229 "max_value=%ld)", line, data->name, *dst,
230 (long) data->param4);
231 *dst = (long) data->param4;
232 return -1;
233 }
234
235 return 0;
236}
237
238
239#ifndef NO_CONFIG_WRITE
240static char * wpa_config_write_int(const struct parse_data *data,
241 struct wpa_ssid *ssid)
242{
243 int *src, res;
244 char *value;
245
246 src = (int *) (((u8 *) ssid) + (long) data->param1);
247
248 value = os_malloc(20);
249 if (value == NULL)
250 return NULL;
251 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800252 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 os_free(value);
254 return NULL;
255 }
256 value[20 - 1] = '\0';
257 return value;
258}
259#endif /* NO_CONFIG_WRITE */
260
261
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800262static int wpa_config_parse_addr_list(const struct parse_data *data,
263 int line, const char *value,
264 u8 **list, size_t *num, char *name,
265 u8 abort_on_error, u8 masked)
266{
267 const char *pos;
268 u8 *buf, *n, addr[2 * ETH_ALEN];
269 size_t count;
270
271 buf = NULL;
272 count = 0;
273
274 pos = value;
275 while (pos && *pos) {
276 while (*pos == ' ')
277 pos++;
278
279 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
280 if (abort_on_error || count == 0) {
281 wpa_printf(MSG_ERROR,
282 "Line %d: Invalid %s address '%s'",
283 line, name, value);
284 os_free(buf);
285 return -1;
286 }
287 /* continue anyway since this could have been from a
288 * truncated configuration file line */
289 wpa_printf(MSG_INFO,
290 "Line %d: Ignore likely truncated %s address '%s'",
291 line, name, pos);
292 } else {
293 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
294 if (n == NULL) {
295 os_free(buf);
296 return -1;
297 }
298 buf = n;
299 os_memmove(buf + 2 * ETH_ALEN, buf,
300 count * 2 * ETH_ALEN);
301 os_memcpy(buf, addr, 2 * ETH_ALEN);
302 count++;
303 wpa_printf(MSG_MSGDUMP,
304 "%s: addr=" MACSTR " mask=" MACSTR,
305 name, MAC2STR(addr),
306 MAC2STR(&addr[ETH_ALEN]));
307 }
308
309 pos = os_strchr(pos, ' ');
310 }
311
312 os_free(*list);
313 *list = buf;
314 *num = count;
315
316 return 0;
317}
318
319
320#ifndef NO_CONFIG_WRITE
321static char * wpa_config_write_addr_list(const struct parse_data *data,
322 const u8 *list, size_t num, char *name)
323{
324 char *value, *end, *pos;
325 int res;
326 size_t i;
327
328 if (list == NULL || num == 0)
329 return NULL;
330
331 value = os_malloc(2 * 20 * num);
332 if (value == NULL)
333 return NULL;
334 pos = value;
335 end = value + 2 * 20 * num;
336
337 for (i = num; i > 0; i--) {
338 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
339 const u8 *m = a + ETH_ALEN;
340
341 if (i < num)
342 *pos++ = ' ';
343 res = hwaddr_mask_txt(pos, end - pos, a, m);
344 if (res < 0) {
345 os_free(value);
346 return NULL;
347 }
348 pos += res;
349 }
350
351 return value;
352}
353#endif /* NO_CONFIG_WRITE */
354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355static int wpa_config_parse_bssid(const struct parse_data *data,
356 struct wpa_ssid *ssid, int line,
357 const char *value)
358{
359 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
360 os_strcmp(value, "any") == 0) {
361 ssid->bssid_set = 0;
362 wpa_printf(MSG_MSGDUMP, "BSSID any");
363 return 0;
364 }
365 if (hwaddr_aton(value, ssid->bssid)) {
366 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
367 line, value);
368 return -1;
369 }
370 ssid->bssid_set = 1;
371 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
372 return 0;
373}
374
375
376#ifndef NO_CONFIG_WRITE
377static char * wpa_config_write_bssid(const struct parse_data *data,
378 struct wpa_ssid *ssid)
379{
380 char *value;
381 int res;
382
383 if (!ssid->bssid_set)
384 return NULL;
385
386 value = os_malloc(20);
387 if (value == NULL)
388 return NULL;
389 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800390 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 os_free(value);
392 return NULL;
393 }
394 value[20 - 1] = '\0';
395 return value;
396}
397#endif /* NO_CONFIG_WRITE */
398
399
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700400static int wpa_config_parse_bssid_hint(const struct parse_data *data,
401 struct wpa_ssid *ssid, int line,
402 const char *value)
403{
404 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
405 os_strcmp(value, "any") == 0) {
406 ssid->bssid_hint_set = 0;
407 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
408 return 0;
409 }
410 if (hwaddr_aton(value, ssid->bssid_hint)) {
411 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
412 line, value);
413 return -1;
414 }
415 ssid->bssid_hint_set = 1;
416 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
417 return 0;
418}
419
420
421#ifndef NO_CONFIG_WRITE
422static char * wpa_config_write_bssid_hint(const struct parse_data *data,
423 struct wpa_ssid *ssid)
424{
425 char *value;
426 int res;
427
428 if (!ssid->bssid_hint_set)
429 return NULL;
430
431 value = os_malloc(20);
432 if (!value)
433 return NULL;
434 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
435 if (os_snprintf_error(20, res)) {
436 os_free(value);
437 return NULL;
438 }
439 return value;
440}
441#endif /* NO_CONFIG_WRITE */
442
443
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800444static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
445 struct wpa_ssid *ssid, int line,
446 const char *value)
447{
448 return wpa_config_parse_addr_list(data, line, value,
449 &ssid->bssid_blacklist,
450 &ssid->num_bssid_blacklist,
451 "bssid_blacklist", 1, 1);
452}
453
454
455#ifndef NO_CONFIG_WRITE
456static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
457 struct wpa_ssid *ssid)
458{
459 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
460 ssid->num_bssid_blacklist,
461 "bssid_blacklist");
462}
463#endif /* NO_CONFIG_WRITE */
464
465
466static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
467 struct wpa_ssid *ssid, int line,
468 const char *value)
469{
470 return wpa_config_parse_addr_list(data, line, value,
471 &ssid->bssid_whitelist,
472 &ssid->num_bssid_whitelist,
473 "bssid_whitelist", 1, 1);
474}
475
476
477#ifndef NO_CONFIG_WRITE
478static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
479 struct wpa_ssid *ssid)
480{
481 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
482 ssid->num_bssid_whitelist,
483 "bssid_whitelist");
484}
485#endif /* NO_CONFIG_WRITE */
486
487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488static int wpa_config_parse_psk(const struct parse_data *data,
489 struct wpa_ssid *ssid, int line,
490 const char *value)
491{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700492#ifdef CONFIG_EXT_PASSWORD
493 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700494 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700495 ssid->passphrase = NULL;
496 ssid->psk_set = 0;
497 os_free(ssid->ext_psk);
498 ssid->ext_psk = os_strdup(value + 4);
499 if (ssid->ext_psk == NULL)
500 return -1;
501 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
502 ssid->ext_psk);
503 return 0;
504 }
505#endif /* CONFIG_EXT_PASSWORD */
506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 if (*value == '"') {
508#ifndef CONFIG_NO_PBKDF2
509 const char *pos;
510 size_t len;
511
512 value++;
513 pos = os_strrchr(value, '"');
514 if (pos)
515 len = pos - value;
516 else
517 len = os_strlen(value);
518 if (len < 8 || len > 63) {
519 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
520 "length %lu (expected: 8..63) '%s'.",
521 line, (unsigned long) len, value);
522 return -1;
523 }
524 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
525 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700526 if (has_ctrl_char((u8 *) value, len)) {
527 wpa_printf(MSG_ERROR,
528 "Line %d: Invalid passphrase character",
529 line);
530 return -1;
531 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700533 os_memcmp(ssid->passphrase, value, len) == 0) {
534 /* No change to the previously configured value */
535 return 1;
536 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700538 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700539 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 if (ssid->passphrase == NULL)
541 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 return 0;
543#else /* CONFIG_NO_PBKDF2 */
544 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
545 "supported.", line);
546 return -1;
547#endif /* CONFIG_NO_PBKDF2 */
548 }
549
550 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
551 value[PMK_LEN * 2] != '\0') {
552 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
553 line, value);
554 return -1;
555 }
556
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700557 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558 ssid->passphrase = NULL;
559
560 ssid->psk_set = 1;
561 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
562 return 0;
563}
564
565
566#ifndef NO_CONFIG_WRITE
567static char * wpa_config_write_psk(const struct parse_data *data,
568 struct wpa_ssid *ssid)
569{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700570#ifdef CONFIG_EXT_PASSWORD
571 if (ssid->ext_psk) {
572 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
573 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800574 int res;
575
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700576 if (buf == NULL)
577 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
579 if (os_snprintf_error(len, res)) {
580 os_free(buf);
581 buf = NULL;
582 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700583 return buf;
584 }
585#endif /* CONFIG_EXT_PASSWORD */
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 if (ssid->passphrase)
588 return wpa_config_write_string_ascii(
589 (const u8 *) ssid->passphrase,
590 os_strlen(ssid->passphrase));
591
592 if (ssid->psk_set)
593 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
594
595 return NULL;
596}
597#endif /* NO_CONFIG_WRITE */
598
599
600static int wpa_config_parse_proto(const struct parse_data *data,
601 struct wpa_ssid *ssid, int line,
602 const char *value)
603{
604 int val = 0, last, errors = 0;
605 char *start, *end, *buf;
606
607 buf = os_strdup(value);
608 if (buf == NULL)
609 return -1;
610 start = buf;
611
612 while (*start != '\0') {
613 while (*start == ' ' || *start == '\t')
614 start++;
615 if (*start == '\0')
616 break;
617 end = start;
618 while (*end != ' ' && *end != '\t' && *end != '\0')
619 end++;
620 last = *end == '\0';
621 *end = '\0';
622 if (os_strcmp(start, "WPA") == 0)
623 val |= WPA_PROTO_WPA;
624 else if (os_strcmp(start, "RSN") == 0 ||
625 os_strcmp(start, "WPA2") == 0)
626 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800627 else if (os_strcmp(start, "OSEN") == 0)
628 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 else {
630 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
631 line, start);
632 errors++;
633 }
634
635 if (last)
636 break;
637 start = end + 1;
638 }
639 os_free(buf);
640
641 if (val == 0) {
642 wpa_printf(MSG_ERROR,
643 "Line %d: no proto values configured.", line);
644 errors++;
645 }
646
Dmitry Shmidte4663042016-04-04 10:07:49 -0700647 if (!errors && ssid->proto == val)
648 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
650 ssid->proto = val;
651 return errors ? -1 : 0;
652}
653
654
655#ifndef NO_CONFIG_WRITE
656static char * wpa_config_write_proto(const struct parse_data *data,
657 struct wpa_ssid *ssid)
658{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700659 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 char *buf, *pos, *end;
661
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800662 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 if (buf == NULL)
664 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800665 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666
667 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700668 ret = os_snprintf(pos, end - pos, "%sWPA",
669 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800670 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 return buf;
672 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 }
674
675 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700676 ret = os_snprintf(pos, end - pos, "%sRSN",
677 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800678 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 return buf;
680 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 }
682
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800683 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700684 ret = os_snprintf(pos, end - pos, "%sOSEN",
685 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800686 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800687 return buf;
688 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700689 }
690
691 if (pos == buf) {
692 os_free(buf);
693 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800694 }
695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 return buf;
697}
698#endif /* NO_CONFIG_WRITE */
699
700
701static int wpa_config_parse_key_mgmt(const struct parse_data *data,
702 struct wpa_ssid *ssid, int line,
703 const char *value)
704{
705 int val = 0, last, errors = 0;
706 char *start, *end, *buf;
707
708 buf = os_strdup(value);
709 if (buf == NULL)
710 return -1;
711 start = buf;
712
713 while (*start != '\0') {
714 while (*start == ' ' || *start == '\t')
715 start++;
716 if (*start == '\0')
717 break;
718 end = start;
719 while (*end != ' ' && *end != '\t' && *end != '\0')
720 end++;
721 last = *end == '\0';
722 *end = '\0';
723 if (os_strcmp(start, "WPA-PSK") == 0)
724 val |= WPA_KEY_MGMT_PSK;
725 else if (os_strcmp(start, "WPA-EAP") == 0)
726 val |= WPA_KEY_MGMT_IEEE8021X;
727 else if (os_strcmp(start, "IEEE8021X") == 0)
728 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
729 else if (os_strcmp(start, "NONE") == 0)
730 val |= WPA_KEY_MGMT_NONE;
731 else if (os_strcmp(start, "WPA-NONE") == 0)
732 val |= WPA_KEY_MGMT_WPA_NONE;
733#ifdef CONFIG_IEEE80211R
734 else if (os_strcmp(start, "FT-PSK") == 0)
735 val |= WPA_KEY_MGMT_FT_PSK;
736 else if (os_strcmp(start, "FT-EAP") == 0)
737 val |= WPA_KEY_MGMT_FT_IEEE8021X;
738#endif /* CONFIG_IEEE80211R */
739#ifdef CONFIG_IEEE80211W
740 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
741 val |= WPA_KEY_MGMT_PSK_SHA256;
742 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
743 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
744#endif /* CONFIG_IEEE80211W */
745#ifdef CONFIG_WPS
746 else if (os_strcmp(start, "WPS") == 0)
747 val |= WPA_KEY_MGMT_WPS;
748#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800749#ifdef CONFIG_SAE
750 else if (os_strcmp(start, "SAE") == 0)
751 val |= WPA_KEY_MGMT_SAE;
752 else if (os_strcmp(start, "FT-SAE") == 0)
753 val |= WPA_KEY_MGMT_FT_SAE;
754#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800755#ifdef CONFIG_HS20
756 else if (os_strcmp(start, "OSEN") == 0)
757 val |= WPA_KEY_MGMT_OSEN;
758#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800759#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800760 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
761 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800762#endif /* CONFIG_SUITEB */
763#ifdef CONFIG_SUITEB192
764 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
765 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
766#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800767#ifdef CONFIG_FILS
768 else if (os_strcmp(start, "FILS-SHA256") == 0)
769 val |= WPA_KEY_MGMT_FILS_SHA256;
770 else if (os_strcmp(start, "FILS-SHA384") == 0)
771 val |= WPA_KEY_MGMT_FILS_SHA384;
772#ifdef CONFIG_IEEE80211R
773 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
774 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
775 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
776 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
777#endif /* CONFIG_IEEE80211R */
778#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700779#ifdef CONFIG_OWE
780 else if (os_strcmp(start, "OWE") == 0)
781 val |= WPA_KEY_MGMT_OWE;
782#endif /* CONFIG_OWE */
783#ifdef CONFIG_DPP
784 else if (os_strcmp(start, "DPP") == 0)
785 val |= WPA_KEY_MGMT_DPP;
786#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 else {
788 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
789 line, start);
790 errors++;
791 }
792
793 if (last)
794 break;
795 start = end + 1;
796 }
797 os_free(buf);
798
799 if (val == 0) {
800 wpa_printf(MSG_ERROR,
801 "Line %d: no key_mgmt values configured.", line);
802 errors++;
803 }
804
Dmitry Shmidte4663042016-04-04 10:07:49 -0700805 if (!errors && ssid->key_mgmt == val)
806 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
808 ssid->key_mgmt = val;
809 return errors ? -1 : 0;
810}
811
812
813#ifndef NO_CONFIG_WRITE
814static char * wpa_config_write_key_mgmt(const struct parse_data *data,
815 struct wpa_ssid *ssid)
816{
817 char *buf, *pos, *end;
818 int ret;
819
Dmitry Shmidt96571392013-10-14 12:54:46 -0700820 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 if (buf == NULL)
822 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700823 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
825 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
826 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
827 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800828 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 end[-1] = '\0';
830 return buf;
831 }
832 pos += ret;
833 }
834
835 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
836 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
837 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800838 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 end[-1] = '\0';
840 return buf;
841 }
842 pos += ret;
843 }
844
845 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
846 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
847 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800848 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 end[-1] = '\0';
850 return buf;
851 }
852 pos += ret;
853 }
854
855 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
856 ret = os_snprintf(pos, end - pos, "%sNONE",
857 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859 end[-1] = '\0';
860 return buf;
861 }
862 pos += ret;
863 }
864
865 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
866 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
867 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800868 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 end[-1] = '\0';
870 return buf;
871 }
872 pos += ret;
873 }
874
875#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700876 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
877 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
878 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800879 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700880 end[-1] = '\0';
881 return buf;
882 }
883 pos += ret;
884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885
Dmitry Shmidt96571392013-10-14 12:54:46 -0700886 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
887 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
888 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800889 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700890 end[-1] = '\0';
891 return buf;
892 }
893 pos += ret;
894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895#endif /* CONFIG_IEEE80211R */
896
897#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700898 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
899 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
900 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800901 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700902 end[-1] = '\0';
903 return buf;
904 }
905 pos += ret;
906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907
Dmitry Shmidt96571392013-10-14 12:54:46 -0700908 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
909 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
910 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800911 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700912 end[-1] = '\0';
913 return buf;
914 }
915 pos += ret;
916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917#endif /* CONFIG_IEEE80211W */
918
919#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700920 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
921 ret = os_snprintf(pos, end - pos, "%sWPS",
922 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800923 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700924 end[-1] = '\0';
925 return buf;
926 }
927 pos += ret;
928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929#endif /* CONFIG_WPS */
930
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800931#ifdef CONFIG_SAE
932 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
933 ret = os_snprintf(pos, end - pos, "%sSAE",
934 pos == buf ? "" : " ");
935 if (os_snprintf_error(end - pos, ret)) {
936 end[-1] = '\0';
937 return buf;
938 }
939 pos += ret;
940 }
941
942 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
943 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
944 pos == buf ? "" : " ");
945 if (os_snprintf_error(end - pos, ret)) {
946 end[-1] = '\0';
947 return buf;
948 }
949 pos += ret;
950 }
951#endif /* CONFIG_SAE */
952
953#ifdef CONFIG_HS20
954 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
955 ret = os_snprintf(pos, end - pos, "%sOSEN",
956 pos == buf ? "" : " ");
957 if (os_snprintf_error(end - pos, ret)) {
958 end[-1] = '\0';
959 return buf;
960 }
961 pos += ret;
962 }
963#endif /* CONFIG_HS20 */
964
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800965#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800966 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
967 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
968 pos == buf ? "" : " ");
969 if (os_snprintf_error(end - pos, ret)) {
970 end[-1] = '\0';
971 return buf;
972 }
973 pos += ret;
974 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800975#endif /* CONFIG_SUITEB */
976
977#ifdef CONFIG_SUITEB192
978 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
979 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
980 pos == buf ? "" : " ");
981 if (os_snprintf_error(end - pos, ret)) {
982 end[-1] = '\0';
983 return buf;
984 }
985 pos += ret;
986 }
987#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800988
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700989#ifdef CONFIG_FILS
990 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
991 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
992 pos == buf ? "" : " ");
993 if (os_snprintf_error(end - pos, ret)) {
994 end[-1] = '\0';
995 return buf;
996 }
997 pos += ret;
998 }
999 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1000 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1001 pos == buf ? "" : " ");
1002 if (os_snprintf_error(end - pos, ret)) {
1003 end[-1] = '\0';
1004 return buf;
1005 }
1006 pos += ret;
1007 }
1008#ifdef CONFIG_IEEE80211R
1009 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1010 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1011 pos == buf ? "" : " ");
1012 if (os_snprintf_error(end - pos, ret)) {
1013 end[-1] = '\0';
1014 return buf;
1015 }
1016 pos += ret;
1017 }
1018 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1019 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1020 pos == buf ? "" : " ");
1021 if (os_snprintf_error(end - pos, ret)) {
1022 end[-1] = '\0';
1023 return buf;
1024 }
1025 pos += ret;
1026 }
1027#endif /* CONFIG_IEEE80211R */
1028#endif /* CONFIG_FILS */
1029
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001030 if (pos == buf) {
1031 os_free(buf);
1032 buf = NULL;
1033 }
1034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 return buf;
1036}
1037#endif /* NO_CONFIG_WRITE */
1038
1039
1040static int wpa_config_parse_cipher(int line, const char *value)
1041{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001042#ifdef CONFIG_NO_WPA
1043 return -1;
1044#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001045 int val = wpa_parse_cipher(value);
1046 if (val < 0) {
1047 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1048 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 if (val == 0) {
1052 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1053 line);
1054 return -1;
1055 }
1056 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001057#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058}
1059
1060
1061#ifndef NO_CONFIG_WRITE
1062static char * wpa_config_write_cipher(int cipher)
1063{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001064#ifdef CONFIG_NO_WPA
1065 return NULL;
1066#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001067 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 if (buf == NULL)
1069 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001071 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1072 os_free(buf);
1073 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 }
1075
1076 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001077#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078}
1079#endif /* NO_CONFIG_WRITE */
1080
1081
1082static int wpa_config_parse_pairwise(const struct parse_data *data,
1083 struct wpa_ssid *ssid, int line,
1084 const char *value)
1085{
1086 int val;
1087 val = wpa_config_parse_cipher(line, value);
1088 if (val == -1)
1089 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001090 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1092 "(0x%x).", line, val);
1093 return -1;
1094 }
1095
Dmitry Shmidte4663042016-04-04 10:07:49 -07001096 if (ssid->pairwise_cipher == val)
1097 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1099 ssid->pairwise_cipher = val;
1100 return 0;
1101}
1102
1103
1104#ifndef NO_CONFIG_WRITE
1105static char * wpa_config_write_pairwise(const struct parse_data *data,
1106 struct wpa_ssid *ssid)
1107{
1108 return wpa_config_write_cipher(ssid->pairwise_cipher);
1109}
1110#endif /* NO_CONFIG_WRITE */
1111
1112
1113static int wpa_config_parse_group(const struct parse_data *data,
1114 struct wpa_ssid *ssid, int line,
1115 const char *value)
1116{
1117 int val;
1118 val = wpa_config_parse_cipher(line, value);
1119 if (val == -1)
1120 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001121
1122 /*
1123 * Backwards compatibility - filter out WEP ciphers that were previously
1124 * allowed.
1125 */
1126 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1127
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001128 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1130 "(0x%x).", line, val);
1131 return -1;
1132 }
1133
Dmitry Shmidte4663042016-04-04 10:07:49 -07001134 if (ssid->group_cipher == val)
1135 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1137 ssid->group_cipher = val;
1138 return 0;
1139}
1140
1141
1142#ifndef NO_CONFIG_WRITE
1143static char * wpa_config_write_group(const struct parse_data *data,
1144 struct wpa_ssid *ssid)
1145{
1146 return wpa_config_write_cipher(ssid->group_cipher);
1147}
1148#endif /* NO_CONFIG_WRITE */
1149
1150
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001151static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1152 struct wpa_ssid *ssid, int line,
1153 const char *value)
1154{
1155 int val;
1156
1157 val = wpa_config_parse_cipher(line, value);
1158 if (val == -1)
1159 return -1;
1160
1161 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1162 wpa_printf(MSG_ERROR,
1163 "Line %d: not allowed group management cipher (0x%x).",
1164 line, val);
1165 return -1;
1166 }
1167
1168 if (ssid->group_mgmt_cipher == val)
1169 return 1;
1170 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1171 ssid->group_mgmt_cipher = val;
1172 return 0;
1173}
1174
1175
1176#ifndef NO_CONFIG_WRITE
1177static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1178 struct wpa_ssid *ssid)
1179{
1180 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1181}
1182#endif /* NO_CONFIG_WRITE */
1183
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185static int wpa_config_parse_auth_alg(const struct parse_data *data,
1186 struct wpa_ssid *ssid, int line,
1187 const char *value)
1188{
1189 int val = 0, last, errors = 0;
1190 char *start, *end, *buf;
1191
1192 buf = os_strdup(value);
1193 if (buf == NULL)
1194 return -1;
1195 start = buf;
1196
1197 while (*start != '\0') {
1198 while (*start == ' ' || *start == '\t')
1199 start++;
1200 if (*start == '\0')
1201 break;
1202 end = start;
1203 while (*end != ' ' && *end != '\t' && *end != '\0')
1204 end++;
1205 last = *end == '\0';
1206 *end = '\0';
1207 if (os_strcmp(start, "OPEN") == 0)
1208 val |= WPA_AUTH_ALG_OPEN;
1209 else if (os_strcmp(start, "SHARED") == 0)
1210 val |= WPA_AUTH_ALG_SHARED;
1211 else if (os_strcmp(start, "LEAP") == 0)
1212 val |= WPA_AUTH_ALG_LEAP;
1213 else {
1214 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1215 line, start);
1216 errors++;
1217 }
1218
1219 if (last)
1220 break;
1221 start = end + 1;
1222 }
1223 os_free(buf);
1224
1225 if (val == 0) {
1226 wpa_printf(MSG_ERROR,
1227 "Line %d: no auth_alg values configured.", line);
1228 errors++;
1229 }
1230
Dmitry Shmidte4663042016-04-04 10:07:49 -07001231 if (!errors && ssid->auth_alg == val)
1232 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1234 ssid->auth_alg = val;
1235 return errors ? -1 : 0;
1236}
1237
1238
1239#ifndef NO_CONFIG_WRITE
1240static char * wpa_config_write_auth_alg(const struct parse_data *data,
1241 struct wpa_ssid *ssid)
1242{
1243 char *buf, *pos, *end;
1244 int ret;
1245
1246 pos = buf = os_zalloc(30);
1247 if (buf == NULL)
1248 return NULL;
1249 end = buf + 30;
1250
1251 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1252 ret = os_snprintf(pos, end - pos, "%sOPEN",
1253 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001254 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 end[-1] = '\0';
1256 return buf;
1257 }
1258 pos += ret;
1259 }
1260
1261 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1262 ret = os_snprintf(pos, end - pos, "%sSHARED",
1263 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001264 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 end[-1] = '\0';
1266 return buf;
1267 }
1268 pos += ret;
1269 }
1270
1271 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1272 ret = os_snprintf(pos, end - pos, "%sLEAP",
1273 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001274 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 end[-1] = '\0';
1276 return buf;
1277 }
1278 pos += ret;
1279 }
1280
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001281 if (pos == buf) {
1282 os_free(buf);
1283 buf = NULL;
1284 }
1285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 return buf;
1287}
1288#endif /* NO_CONFIG_WRITE */
1289
1290
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001291static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292{
1293 int *freqs;
1294 size_t used, len;
1295 const char *pos;
1296
1297 used = 0;
1298 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001299 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 if (freqs == NULL)
1301 return NULL;
1302
1303 pos = value;
1304 while (pos) {
1305 while (*pos == ' ')
1306 pos++;
1307 if (used == len) {
1308 int *n;
1309 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001310 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 if (n == NULL) {
1312 os_free(freqs);
1313 return NULL;
1314 }
1315 for (i = len; i <= len * 2; i++)
1316 n[i] = 0;
1317 freqs = n;
1318 len *= 2;
1319 }
1320
1321 freqs[used] = atoi(pos);
1322 if (freqs[used] == 0)
1323 break;
1324 used++;
1325 pos = os_strchr(pos + 1, ' ');
1326 }
1327
1328 return freqs;
1329}
1330
1331
1332static int wpa_config_parse_scan_freq(const struct parse_data *data,
1333 struct wpa_ssid *ssid, int line,
1334 const char *value)
1335{
1336 int *freqs;
1337
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 if (freqs == NULL)
1340 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001341 if (freqs[0] == 0) {
1342 os_free(freqs);
1343 freqs = NULL;
1344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 os_free(ssid->scan_freq);
1346 ssid->scan_freq = freqs;
1347
1348 return 0;
1349}
1350
1351
1352static int wpa_config_parse_freq_list(const struct parse_data *data,
1353 struct wpa_ssid *ssid, int line,
1354 const char *value)
1355{
1356 int *freqs;
1357
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001358 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 if (freqs == NULL)
1360 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001361 if (freqs[0] == 0) {
1362 os_free(freqs);
1363 freqs = NULL;
1364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 os_free(ssid->freq_list);
1366 ssid->freq_list = freqs;
1367
1368 return 0;
1369}
1370
1371
1372#ifndef NO_CONFIG_WRITE
1373static char * wpa_config_write_freqs(const struct parse_data *data,
1374 const int *freqs)
1375{
1376 char *buf, *pos, *end;
1377 int i, ret;
1378 size_t count;
1379
1380 if (freqs == NULL)
1381 return NULL;
1382
1383 count = 0;
1384 for (i = 0; freqs[i]; i++)
1385 count++;
1386
1387 pos = buf = os_zalloc(10 * count + 1);
1388 if (buf == NULL)
1389 return NULL;
1390 end = buf + 10 * count + 1;
1391
1392 for (i = 0; freqs[i]; i++) {
1393 ret = os_snprintf(pos, end - pos, "%s%u",
1394 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001395 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 end[-1] = '\0';
1397 return buf;
1398 }
1399 pos += ret;
1400 }
1401
1402 return buf;
1403}
1404
1405
1406static char * wpa_config_write_scan_freq(const struct parse_data *data,
1407 struct wpa_ssid *ssid)
1408{
1409 return wpa_config_write_freqs(data, ssid->scan_freq);
1410}
1411
1412
1413static char * wpa_config_write_freq_list(const struct parse_data *data,
1414 struct wpa_ssid *ssid)
1415{
1416 return wpa_config_write_freqs(data, ssid->freq_list);
1417}
1418#endif /* NO_CONFIG_WRITE */
1419
1420
1421#ifdef IEEE8021X_EAPOL
1422static int wpa_config_parse_eap(const struct parse_data *data,
1423 struct wpa_ssid *ssid, int line,
1424 const char *value)
1425{
1426 int last, errors = 0;
1427 char *start, *end, *buf;
1428 struct eap_method_type *methods = NULL, *tmp;
1429 size_t num_methods = 0;
1430
1431 buf = os_strdup(value);
1432 if (buf == NULL)
1433 return -1;
1434 start = buf;
1435
1436 while (*start != '\0') {
1437 while (*start == ' ' || *start == '\t')
1438 start++;
1439 if (*start == '\0')
1440 break;
1441 end = start;
1442 while (*end != ' ' && *end != '\t' && *end != '\0')
1443 end++;
1444 last = *end == '\0';
1445 *end = '\0';
1446 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001447 methods = os_realloc_array(methods, num_methods + 1,
1448 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 if (methods == NULL) {
1450 os_free(tmp);
1451 os_free(buf);
1452 return -1;
1453 }
1454 methods[num_methods].method = eap_peer_get_type(
1455 start, &methods[num_methods].vendor);
1456 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1457 methods[num_methods].method == EAP_TYPE_NONE) {
1458 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1459 "'%s'", line, start);
1460 wpa_printf(MSG_ERROR, "You may need to add support for"
1461 " this EAP method during wpa_supplicant\n"
1462 "build time configuration.\n"
1463 "See README for more information.");
1464 errors++;
1465 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1466 methods[num_methods].method == EAP_TYPE_LEAP)
1467 ssid->leap++;
1468 else
1469 ssid->non_leap++;
1470 num_methods++;
1471 if (last)
1472 break;
1473 start = end + 1;
1474 }
1475 os_free(buf);
1476
1477 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001478 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 if (methods == NULL) {
1480 os_free(tmp);
1481 return -1;
1482 }
1483 methods[num_methods].vendor = EAP_VENDOR_IETF;
1484 methods[num_methods].method = EAP_TYPE_NONE;
1485 num_methods++;
1486
Dmitry Shmidte4663042016-04-04 10:07:49 -07001487 if (!errors && ssid->eap.eap_methods) {
1488 struct eap_method_type *prev_m;
1489 size_t i, j, prev_methods, match = 0;
1490
1491 prev_m = ssid->eap.eap_methods;
1492 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1493 prev_m[i].method != EAP_TYPE_NONE; i++) {
1494 /* Count the methods */
1495 }
1496 prev_methods = i + 1;
1497
1498 for (i = 0; prev_methods == num_methods && i < prev_methods;
1499 i++) {
1500 for (j = 0; j < num_methods; j++) {
1501 if (prev_m[i].vendor == methods[j].vendor &&
1502 prev_m[i].method == methods[j].method) {
1503 match++;
1504 break;
1505 }
1506 }
1507 }
1508 if (match == num_methods) {
1509 os_free(methods);
1510 return 1;
1511 }
1512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1514 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001515 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 ssid->eap.eap_methods = methods;
1517 return errors ? -1 : 0;
1518}
1519
1520
Dmitry Shmidt83474442015-04-15 13:47:09 -07001521#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522static char * wpa_config_write_eap(const struct parse_data *data,
1523 struct wpa_ssid *ssid)
1524{
1525 int i, ret;
1526 char *buf, *pos, *end;
1527 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1528 const char *name;
1529
1530 if (eap_methods == NULL)
1531 return NULL;
1532
1533 pos = buf = os_zalloc(100);
1534 if (buf == NULL)
1535 return NULL;
1536 end = buf + 100;
1537
1538 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1539 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1540 name = eap_get_name(eap_methods[i].vendor,
1541 eap_methods[i].method);
1542 if (name) {
1543 ret = os_snprintf(pos, end - pos, "%s%s",
1544 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001545 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 break;
1547 pos += ret;
1548 }
1549 }
1550
1551 end[-1] = '\0';
1552
1553 return buf;
1554}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001555#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
1557
1558static int wpa_config_parse_password(const struct parse_data *data,
1559 struct wpa_ssid *ssid, int line,
1560 const char *value)
1561{
1562 u8 *hash;
1563
1564 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001565 if (!ssid->eap.password)
1566 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001568 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 ssid->eap.password = NULL;
1570 ssid->eap.password_len = 0;
1571 return 0;
1572 }
1573
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001574#ifdef CONFIG_EXT_PASSWORD
1575 if (os_strncmp(value, "ext:", 4) == 0) {
1576 char *name = os_strdup(value + 4);
1577 if (name == NULL)
1578 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001579 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001580 ssid->eap.password = (u8 *) name;
1581 ssid->eap.password_len = os_strlen(name);
1582 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1583 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1584 return 0;
1585 }
1586#endif /* CONFIG_EXT_PASSWORD */
1587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 if (os_strncmp(value, "hash:", 5) != 0) {
1589 char *tmp;
1590 size_t res_len;
1591
1592 tmp = wpa_config_parse_string(value, &res_len);
1593 if (tmp == NULL) {
1594 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1595 "password.", line);
1596 return -1;
1597 }
1598 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1599 (u8 *) tmp, res_len);
1600
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001601 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 ssid->eap.password = (u8 *) tmp;
1603 ssid->eap.password_len = res_len;
1604 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001605 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606
1607 return 0;
1608 }
1609
1610
1611 /* NtPasswordHash: hash:<32 hex digits> */
1612 if (os_strlen(value + 5) != 2 * 16) {
1613 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1614 "(expected 32 hex digits)", line);
1615 return -1;
1616 }
1617
1618 hash = os_malloc(16);
1619 if (hash == NULL)
1620 return -1;
1621
1622 if (hexstr2bin(value + 5, hash, 16)) {
1623 os_free(hash);
1624 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1625 return -1;
1626 }
1627
1628 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1629
Dmitry Shmidte4663042016-04-04 10:07:49 -07001630 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1631 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1632 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1633 bin_clear_free(hash, 16);
1634 return 1;
1635 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001636 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 ssid->eap.password = hash;
1638 ssid->eap.password_len = 16;
1639 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001640 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641
1642 return 0;
1643}
1644
1645
Dmitry Shmidt83474442015-04-15 13:47:09 -07001646#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647static char * wpa_config_write_password(const struct parse_data *data,
1648 struct wpa_ssid *ssid)
1649{
1650 char *buf;
1651
1652 if (ssid->eap.password == NULL)
1653 return NULL;
1654
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001655#ifdef CONFIG_EXT_PASSWORD
1656 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1657 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1658 if (buf == NULL)
1659 return NULL;
1660 os_memcpy(buf, "ext:", 4);
1661 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1662 return buf;
1663 }
1664#endif /* CONFIG_EXT_PASSWORD */
1665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1667 return wpa_config_write_string(
1668 ssid->eap.password, ssid->eap.password_len);
1669 }
1670
1671 buf = os_malloc(5 + 32 + 1);
1672 if (buf == NULL)
1673 return NULL;
1674
1675 os_memcpy(buf, "hash:", 5);
1676 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1677
1678 return buf;
1679}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001680#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681#endif /* IEEE8021X_EAPOL */
1682
1683
1684static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1685 const char *value, int idx)
1686{
1687 char *buf, title[20];
1688 int res;
1689
1690 buf = wpa_config_parse_string(value, len);
1691 if (buf == NULL) {
1692 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1693 line, idx, value);
1694 return -1;
1695 }
1696 if (*len > MAX_WEP_KEY_LEN) {
1697 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1698 line, idx, value);
1699 os_free(buf);
1700 return -1;
1701 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001702 if (*len && *len != 5 && *len != 13 && *len != 16) {
1703 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1704 "this network block will be ignored",
1705 line, (unsigned int) *len);
1706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001708 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001710 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1712 return 0;
1713}
1714
1715
1716static int wpa_config_parse_wep_key0(const struct parse_data *data,
1717 struct wpa_ssid *ssid, int line,
1718 const char *value)
1719{
1720 return wpa_config_parse_wep_key(ssid->wep_key[0],
1721 &ssid->wep_key_len[0], line,
1722 value, 0);
1723}
1724
1725
1726static int wpa_config_parse_wep_key1(const struct parse_data *data,
1727 struct wpa_ssid *ssid, int line,
1728 const char *value)
1729{
1730 return wpa_config_parse_wep_key(ssid->wep_key[1],
1731 &ssid->wep_key_len[1], line,
1732 value, 1);
1733}
1734
1735
1736static int wpa_config_parse_wep_key2(const struct parse_data *data,
1737 struct wpa_ssid *ssid, int line,
1738 const char *value)
1739{
1740 return wpa_config_parse_wep_key(ssid->wep_key[2],
1741 &ssid->wep_key_len[2], line,
1742 value, 2);
1743}
1744
1745
1746static int wpa_config_parse_wep_key3(const struct parse_data *data,
1747 struct wpa_ssid *ssid, int line,
1748 const char *value)
1749{
1750 return wpa_config_parse_wep_key(ssid->wep_key[3],
1751 &ssid->wep_key_len[3], line,
1752 value, 3);
1753}
1754
1755
1756#ifndef NO_CONFIG_WRITE
1757static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1758{
1759 if (ssid->wep_key_len[idx] == 0)
1760 return NULL;
1761 return wpa_config_write_string(ssid->wep_key[idx],
1762 ssid->wep_key_len[idx]);
1763}
1764
1765
1766static char * wpa_config_write_wep_key0(const struct parse_data *data,
1767 struct wpa_ssid *ssid)
1768{
1769 return wpa_config_write_wep_key(ssid, 0);
1770}
1771
1772
1773static char * wpa_config_write_wep_key1(const struct parse_data *data,
1774 struct wpa_ssid *ssid)
1775{
1776 return wpa_config_write_wep_key(ssid, 1);
1777}
1778
1779
1780static char * wpa_config_write_wep_key2(const struct parse_data *data,
1781 struct wpa_ssid *ssid)
1782{
1783 return wpa_config_write_wep_key(ssid, 2);
1784}
1785
1786
1787static char * wpa_config_write_wep_key3(const struct parse_data *data,
1788 struct wpa_ssid *ssid)
1789{
1790 return wpa_config_write_wep_key(ssid, 3);
1791}
1792#endif /* NO_CONFIG_WRITE */
1793
1794
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001795#ifdef CONFIG_P2P
1796
Dmitry Shmidt54605472013-11-08 11:10:19 -08001797static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1798 struct wpa_ssid *ssid, int line,
1799 const char *value)
1800{
1801 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1802 os_strcmp(value, "any") == 0) {
1803 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1804 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1805 return 0;
1806 }
1807 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1808 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1809 line, value);
1810 return -1;
1811 }
1812 ssid->bssid_set = 1;
1813 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1814 MAC2STR(ssid->go_p2p_dev_addr));
1815 return 0;
1816}
1817
1818
1819#ifndef NO_CONFIG_WRITE
1820static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1821 struct wpa_ssid *ssid)
1822{
1823 char *value;
1824 int res;
1825
1826 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1827 return NULL;
1828
1829 value = os_malloc(20);
1830 if (value == NULL)
1831 return NULL;
1832 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001833 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001834 os_free(value);
1835 return NULL;
1836 }
1837 value[20 - 1] = '\0';
1838 return value;
1839}
1840#endif /* NO_CONFIG_WRITE */
1841
1842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001843static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1844 struct wpa_ssid *ssid, int line,
1845 const char *value)
1846{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001847 return wpa_config_parse_addr_list(data, line, value,
1848 &ssid->p2p_client_list,
1849 &ssid->num_p2p_clients,
1850 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001851}
1852
1853
1854#ifndef NO_CONFIG_WRITE
1855static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1856 struct wpa_ssid *ssid)
1857{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001858 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1859 ssid->num_p2p_clients,
1860 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861}
1862#endif /* NO_CONFIG_WRITE */
1863
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001864
1865static int wpa_config_parse_psk_list(const struct parse_data *data,
1866 struct wpa_ssid *ssid, int line,
1867 const char *value)
1868{
1869 struct psk_list_entry *p;
1870 const char *pos;
1871
1872 p = os_zalloc(sizeof(*p));
1873 if (p == NULL)
1874 return -1;
1875
1876 pos = value;
1877 if (os_strncmp(pos, "P2P-", 4) == 0) {
1878 p->p2p = 1;
1879 pos += 4;
1880 }
1881
1882 if (hwaddr_aton(pos, p->addr)) {
1883 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1884 line, pos);
1885 os_free(p);
1886 return -1;
1887 }
1888 pos += 17;
1889 if (*pos != '-') {
1890 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1891 line, pos);
1892 os_free(p);
1893 return -1;
1894 }
1895 pos++;
1896
1897 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1898 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1899 line, pos);
1900 os_free(p);
1901 return -1;
1902 }
1903
1904 dl_list_add(&ssid->psk_list, &p->list);
1905
1906 return 0;
1907}
1908
1909
1910#ifndef NO_CONFIG_WRITE
1911static char * wpa_config_write_psk_list(const struct parse_data *data,
1912 struct wpa_ssid *ssid)
1913{
1914 return NULL;
1915}
1916#endif /* NO_CONFIG_WRITE */
1917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001918#endif /* CONFIG_P2P */
1919
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001920
1921#ifdef CONFIG_MESH
1922
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001923static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1924 struct wpa_ssid *ssid, int line,
1925 const char *value)
1926{
1927 int *rates = wpa_config_parse_int_array(value);
1928
1929 if (rates == NULL) {
1930 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1931 line, value);
1932 return -1;
1933 }
1934 if (rates[0] == 0) {
1935 os_free(rates);
1936 rates = NULL;
1937 }
1938
1939 os_free(ssid->mesh_basic_rates);
1940 ssid->mesh_basic_rates = rates;
1941
1942 return 0;
1943}
1944
1945
1946#ifndef NO_CONFIG_WRITE
1947
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001948static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1949 struct wpa_ssid *ssid)
1950{
1951 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1952}
1953
1954#endif /* NO_CONFIG_WRITE */
1955
1956#endif /* CONFIG_MESH */
1957
1958
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001959#ifdef CONFIG_MACSEC
1960
1961static int wpa_config_parse_mka_cak(const struct parse_data *data,
1962 struct wpa_ssid *ssid, int line,
1963 const char *value)
1964{
1965 if (hexstr2bin(value, ssid->mka_cak, MACSEC_CAK_LEN) ||
1966 value[MACSEC_CAK_LEN * 2] != '\0') {
1967 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
1968 line, value);
1969 return -1;
1970 }
1971
1972 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
1973
1974 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak, MACSEC_CAK_LEN);
1975 return 0;
1976}
1977
1978
1979static int wpa_config_parse_mka_ckn(const struct parse_data *data,
1980 struct wpa_ssid *ssid, int line,
1981 const char *value)
1982{
1983 if (hexstr2bin(value, ssid->mka_ckn, MACSEC_CKN_LEN) ||
1984 value[MACSEC_CKN_LEN * 2] != '\0') {
1985 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
1986 line, value);
1987 return -1;
1988 }
1989
1990 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
1991
1992 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn, MACSEC_CKN_LEN);
1993 return 0;
1994}
1995
1996
1997#ifndef NO_CONFIG_WRITE
1998
1999static char * wpa_config_write_mka_cak(const struct parse_data *data,
2000 struct wpa_ssid *ssid)
2001{
2002 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2003 return NULL;
2004
2005 return wpa_config_write_string_hex(ssid->mka_cak, MACSEC_CAK_LEN);
2006}
2007
2008
2009static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2010 struct wpa_ssid *ssid)
2011{
2012 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2013 return NULL;
2014 return wpa_config_write_string_hex(ssid->mka_ckn, MACSEC_CKN_LEN);
2015}
2016
2017#endif /* NO_CONFIG_WRITE */
2018
2019#endif /* CONFIG_MACSEC */
2020
2021
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002022static int wpa_config_parse_peerkey(const struct parse_data *data,
2023 struct wpa_ssid *ssid, int line,
2024 const char *value)
2025{
2026 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2027 return 0;
2028}
2029
2030
2031#ifndef NO_CONFIG_WRITE
2032static char * wpa_config_write_peerkey(const struct parse_data *data,
2033 struct wpa_ssid *ssid)
2034{
2035 return NULL;
2036}
2037#endif /* NO_CONFIG_WRITE */
2038
2039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040/* Helper macros for network block parser */
2041
2042#ifdef OFFSET
2043#undef OFFSET
2044#endif /* OFFSET */
2045/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2046#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2047
2048/* STR: Define a string variable for an ASCII string; f = field name */
2049#ifdef NO_CONFIG_WRITE
2050#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
2051#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
2052#else /* NO_CONFIG_WRITE */
2053#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
2054#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
2055#endif /* NO_CONFIG_WRITE */
2056#define STR(f) _STR(f), NULL, NULL, NULL, 0
2057#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
2058#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
2059#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
2060
2061/* STR_LEN: Define a string variable with a separate variable for storing the
2062 * data length. Unlike STR(), this can be used to store arbitrary binary data
2063 * (i.e., even nul termination character). */
2064#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
2065#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
2066#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
2067#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
2068#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2069
2070/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2071 * explicitly specified. */
2072#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2073#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2074#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2075
2076#ifdef NO_CONFIG_WRITE
2077#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
2078#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
2079#else /* NO_CONFIG_WRITE */
2080#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2081 OFFSET(f), (void *) 0
2082#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2083 OFFSET(eap.f), (void *) 0
2084#endif /* NO_CONFIG_WRITE */
2085
2086/* INT: Define an integer variable */
2087#define INT(f) _INT(f), NULL, NULL, 0
2088#define INTe(f) _INTe(f), NULL, NULL, 0
2089
2090/* INT_RANGE: Define an integer variable with allowed value range */
2091#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2092
2093/* FUNC: Define a configuration variable that uses a custom function for
2094 * parsing and writing the value. */
2095#ifdef NO_CONFIG_WRITE
2096#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2097#else /* NO_CONFIG_WRITE */
2098#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2099 NULL, NULL, NULL, NULL
2100#endif /* NO_CONFIG_WRITE */
2101#define FUNC(f) _FUNC(f), 0
2102#define FUNC_KEY(f) _FUNC(f), 1
2103
2104/*
2105 * Table of network configuration variables. This table is used to parse each
2106 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2107 * that is inside a network block.
2108 *
2109 * This table is generated using the helper macros defined above and with
2110 * generous help from the C pre-processor. The field name is stored as a string
2111 * into .name and for STR and INT types, the offset of the target buffer within
2112 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2113 * offset to the field containing the length of the configuration variable.
2114 * .param3 and .param4 can be used to mark the allowed range (length for STR
2115 * and value for INT).
2116 *
2117 * For each configuration line in wpa_supplicant.conf, the parser goes through
2118 * this table and select the entry that matches with the field name. The parser
2119 * function (.parser) is then called to parse the actual value of the field.
2120 *
2121 * This kind of mechanism makes it easy to add new configuration parameters,
2122 * since only one line needs to be added into this table and into the
2123 * struct wpa_ssid definition if the new variable is either a string or
2124 * integer. More complex types will need to use their own parser and writer
2125 * functions.
2126 */
2127static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002128 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 { INT_RANGE(scan_ssid, 0, 1) },
2130 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002131 { FUNC(bssid_hint) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002132 { FUNC(bssid_blacklist) },
2133 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002135 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002136 { STR_KEY(sae_password) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 { FUNC(proto) },
2138 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002139 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 { FUNC(pairwise) },
2141 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002142 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 { FUNC(auth_alg) },
2144 { FUNC(scan_freq) },
2145 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002146 { INT_RANGE(ht, 0, 1) },
2147 { INT_RANGE(vht, 0, 1) },
2148 { INT_RANGE(ht40, -1, 1) },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002149 { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
2150 VHT_CHANWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002151 { INT(vht_center_freq1) },
2152 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153#ifdef IEEE8021X_EAPOL
2154 { FUNC(eap) },
2155 { STR_LENe(identity) },
2156 { STR_LENe(anonymous_identity) },
2157 { FUNC_KEY(password) },
2158 { STRe(ca_cert) },
2159 { STRe(ca_path) },
2160 { STRe(client_cert) },
2161 { STRe(private_key) },
2162 { STR_KEYe(private_key_passwd) },
2163 { STRe(dh_file) },
2164 { STRe(subject_match) },
2165 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07002166 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002167 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 { STRe(ca_cert2) },
2169 { STRe(ca_path2) },
2170 { STRe(client_cert2) },
2171 { STRe(private_key2) },
2172 { STR_KEYe(private_key2_passwd) },
2173 { STRe(dh_file2) },
2174 { STRe(subject_match2) },
2175 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07002176 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002177 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 { STRe(phase1) },
2179 { STRe(phase2) },
2180 { STRe(pcsc) },
2181 { STR_KEYe(pin) },
2182 { STRe(engine_id) },
2183 { STRe(key_id) },
2184 { STRe(cert_id) },
2185 { STRe(ca_cert_id) },
2186 { STR_KEYe(pin2) },
2187 { STRe(engine2_id) },
2188 { STRe(key2_id) },
2189 { STRe(cert2_id) },
2190 { STRe(ca_cert2_id) },
2191 { INTe(engine) },
2192 { INTe(engine2) },
2193 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002194 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002195 { STRe(openssl_ciphers) },
2196 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197#endif /* IEEE8021X_EAPOL */
2198 { FUNC_KEY(wep_key0) },
2199 { FUNC_KEY(wep_key1) },
2200 { FUNC_KEY(wep_key2) },
2201 { FUNC_KEY(wep_key3) },
2202 { INT(wep_tx_keyidx) },
2203 { INT(priority) },
2204#ifdef IEEE8021X_EAPOL
2205 { INT(eap_workaround) },
2206 { STRe(pac_file) },
2207 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002208 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210#ifdef CONFIG_MESH
2211 { INT_RANGE(mode, 0, 5) },
2212 { INT_RANGE(no_auto_peer, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002213 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002214#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002216#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 { INT_RANGE(proactive_key_caching, 0, 1) },
2218 { INT_RANGE(disabled, 0, 2) },
2219 { STR(id_str) },
2220#ifdef CONFIG_IEEE80211W
2221 { INT_RANGE(ieee80211w, 0, 2) },
2222#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002223 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002225 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002226 { INT_RANGE(fixed_freq, 0, 1) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002227#ifdef CONFIG_ACS
2228 { INT_RANGE(acs, 0, 1) },
2229#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002230#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002231 { FUNC(mesh_basic_rates) },
2232 { INT(dot11MeshMaxRetries) },
2233 { INT(dot11MeshRetryTimeout) },
2234 { INT(dot11MeshConfirmTimeout) },
2235 { INT(dot11MeshHoldingTimeout) },
2236#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 { INT(wpa_ptk_rekey) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002238 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002240 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002241#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002242 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002243 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002244 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002245#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002246#ifdef CONFIG_HT_OVERRIDES
2247 { INT_RANGE(disable_ht, 0, 1) },
2248 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002249 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002250 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002251 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002252 { INT_RANGE(disable_max_amsdu, -1, 1) },
2253 { INT_RANGE(ampdu_factor, -1, 3) },
2254 { INT_RANGE(ampdu_density, -1, 7) },
2255 { STR(ht_mcs) },
2256#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002257#ifdef CONFIG_VHT_OVERRIDES
2258 { INT_RANGE(disable_vht, 0, 1) },
2259 { INT(vht_capa) },
2260 { INT(vht_capa_mask) },
2261 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2262 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2263 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2264 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2265 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2266 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2267 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2268 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2269 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2270 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2271 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2272 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2273 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2274 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2275 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2276 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2277#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278 { INT(ap_max_inactivity) },
2279 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002280 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002281#ifdef CONFIG_MACSEC
2282 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002283 { INT_RANGE(macsec_integ_only, 0, 1) },
2284 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002285 { INT_RANGE(mka_priority, 0, 255) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002286 { FUNC_KEY(mka_cak) },
2287 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002288#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002289#ifdef CONFIG_HS20
2290 { INT(update_identifier) },
2291#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002292 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002293 { INT_RANGE(pbss, 0, 2) },
2294 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002295 { INT_RANGE(fils_dh_group, 0, 65535) },
2296#ifdef CONFIG_DPP
2297 { STR(dpp_connector) },
2298 { STR_LEN(dpp_netaccesskey) },
2299 { INT(dpp_netaccesskey_expiry) },
2300 { STR_LEN(dpp_csign) },
2301#endif /* CONFIG_DPP */
2302 { INT_RANGE(owe_group, 0, 65535) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303};
2304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305#undef OFFSET
2306#undef _STR
2307#undef STR
2308#undef STR_KEY
2309#undef _STR_LEN
2310#undef STR_LEN
2311#undef STR_LEN_KEY
2312#undef _STR_RANGE
2313#undef STR_RANGE
2314#undef STR_RANGE_KEY
2315#undef _INT
2316#undef INT
2317#undef INT_RANGE
2318#undef _FUNC
2319#undef FUNC
2320#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002321#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322
2323
2324/**
2325 * wpa_config_add_prio_network - Add a network to priority lists
2326 * @config: Configuration data from wpa_config_read()
2327 * @ssid: Pointer to the network configuration to be added to the list
2328 * Returns: 0 on success, -1 on failure
2329 *
2330 * This function is used to add a network block to the priority list of
2331 * networks. This must be called for each network when reading in the full
2332 * configuration. In addition, this can be used indirectly when updating
2333 * priorities by calling wpa_config_update_prio_list().
2334 */
2335int wpa_config_add_prio_network(struct wpa_config *config,
2336 struct wpa_ssid *ssid)
2337{
2338 int prio;
2339 struct wpa_ssid *prev, **nlist;
2340
2341 /*
2342 * Add to an existing priority list if one is available for the
2343 * configured priority level for this network.
2344 */
2345 for (prio = 0; prio < config->num_prio; prio++) {
2346 prev = config->pssid[prio];
2347 if (prev->priority == ssid->priority) {
2348 while (prev->pnext)
2349 prev = prev->pnext;
2350 prev->pnext = ssid;
2351 return 0;
2352 }
2353 }
2354
2355 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002356 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2357 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 if (nlist == NULL)
2359 return -1;
2360
2361 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002362 if (nlist[prio]->priority < ssid->priority) {
2363 os_memmove(&nlist[prio + 1], &nlist[prio],
2364 (config->num_prio - prio) *
2365 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 }
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 nlist[prio] = ssid;
2371 config->num_prio++;
2372 config->pssid = nlist;
2373
2374 return 0;
2375}
2376
2377
2378/**
2379 * wpa_config_update_prio_list - Update network priority list
2380 * @config: Configuration data from wpa_config_read()
2381 * Returns: 0 on success, -1 on failure
2382 *
2383 * This function is called to update the priority list of networks in the
2384 * configuration when a network is being added or removed. This is also called
2385 * if a priority for a network is changed.
2386 */
2387int wpa_config_update_prio_list(struct wpa_config *config)
2388{
2389 struct wpa_ssid *ssid;
2390 int ret = 0;
2391
2392 os_free(config->pssid);
2393 config->pssid = NULL;
2394 config->num_prio = 0;
2395
2396 ssid = config->ssid;
2397 while (ssid) {
2398 ssid->pnext = NULL;
2399 if (wpa_config_add_prio_network(config, ssid) < 0)
2400 ret = -1;
2401 ssid = ssid->next;
2402 }
2403
2404 return ret;
2405}
2406
2407
2408#ifdef IEEE8021X_EAPOL
2409static void eap_peer_config_free(struct eap_peer_config *eap)
2410{
2411 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002412 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002414 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 os_free(eap->ca_cert);
2416 os_free(eap->ca_path);
2417 os_free(eap->client_cert);
2418 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002419 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 os_free(eap->dh_file);
2421 os_free(eap->subject_match);
2422 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002423 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002424 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 os_free(eap->ca_cert2);
2426 os_free(eap->ca_path2);
2427 os_free(eap->client_cert2);
2428 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002429 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 os_free(eap->dh_file2);
2431 os_free(eap->subject_match2);
2432 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002433 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002434 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435 os_free(eap->phase1);
2436 os_free(eap->phase2);
2437 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002438 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 os_free(eap->engine_id);
2440 os_free(eap->key_id);
2441 os_free(eap->cert_id);
2442 os_free(eap->ca_cert_id);
2443 os_free(eap->key2_id);
2444 os_free(eap->cert2_id);
2445 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002446 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 os_free(eap->engine2_id);
2448 os_free(eap->otp);
2449 os_free(eap->pending_req_otp);
2450 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002451 bin_clear_free(eap->new_password, eap->new_password_len);
2452 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002453 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454}
2455#endif /* IEEE8021X_EAPOL */
2456
2457
2458/**
2459 * wpa_config_free_ssid - Free network/ssid configuration data
2460 * @ssid: Configuration data for the network
2461 *
2462 * This function frees all resources allocated for the network configuration
2463 * data.
2464 */
2465void wpa_config_free_ssid(struct wpa_ssid *ssid)
2466{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002467 struct psk_list_entry *psk;
2468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002470 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002471 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002472 str_clear_free(ssid->sae_password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473#ifdef IEEE8021X_EAPOL
2474 eap_peer_config_free(&ssid->eap);
2475#endif /* IEEE8021X_EAPOL */
2476 os_free(ssid->id_str);
2477 os_free(ssid->scan_freq);
2478 os_free(ssid->freq_list);
2479 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002480 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002481 os_free(ssid->bssid_blacklist);
2482 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002483#ifdef CONFIG_HT_OVERRIDES
2484 os_free(ssid->ht_mcs);
2485#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002486#ifdef CONFIG_MESH
2487 os_free(ssid->mesh_basic_rates);
2488#endif /* CONFIG_MESH */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002489 os_free(ssid->dpp_connector);
2490 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2491 os_free(ssid->dpp_csign);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002492 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2493 list))) {
2494 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002495 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002496 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002497 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498}
2499
2500
Dmitry Shmidt04949592012-07-19 12:16:46 -07002501void wpa_config_free_cred(struct wpa_cred *cred)
2502{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002503 size_t i;
2504
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002506 str_clear_free(cred->username);
2507 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002508 os_free(cred->ca_cert);
2509 os_free(cred->client_cert);
2510 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002511 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002512 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002513 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002514 for (i = 0; i < cred->num_domain; i++)
2515 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002516 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002517 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 os_free(cred->eap_method);
2519 os_free(cred->phase1);
2520 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002521 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002522 os_free(cred->roaming_partner);
2523 os_free(cred->provisioning_sp);
2524 for (i = 0; i < cred->num_req_conn_capab; i++)
2525 os_free(cred->req_conn_capab_port[i]);
2526 os_free(cred->req_conn_capab_port);
2527 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002528 os_free(cred);
2529}
2530
2531
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002532void wpa_config_flush_blobs(struct wpa_config *config)
2533{
2534#ifndef CONFIG_NO_CONFIG_BLOBS
2535 struct wpa_config_blob *blob, *prev;
2536
2537 blob = config->blobs;
2538 config->blobs = NULL;
2539 while (blob) {
2540 prev = blob;
2541 blob = blob->next;
2542 wpa_config_free_blob(prev);
2543 }
2544#endif /* CONFIG_NO_CONFIG_BLOBS */
2545}
2546
2547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548/**
2549 * wpa_config_free - Free configuration data
2550 * @config: Configuration data from wpa_config_read()
2551 *
2552 * This function frees all resources allocated for the configuration data by
2553 * wpa_config_read().
2554 */
2555void wpa_config_free(struct wpa_config *config)
2556{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002558 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002559 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
2561 ssid = config->ssid;
2562 while (ssid) {
2563 prev = ssid;
2564 ssid = ssid->next;
2565 wpa_config_free_ssid(prev);
2566 }
2567
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 cred = config->cred;
2569 while (cred) {
2570 cprev = cred;
2571 cred = cred->next;
2572 wpa_config_free_cred(cprev);
2573 }
2574
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002575 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576
Dmitry Shmidt04949592012-07-19 12:16:46 -07002577 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002578 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2579 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 os_free(config->ctrl_interface);
2581 os_free(config->ctrl_interface_group);
2582 os_free(config->opensc_engine_path);
2583 os_free(config->pkcs11_engine_path);
2584 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002585 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002586 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002587 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 os_free(config->driver_param);
2589 os_free(config->device_name);
2590 os_free(config->manufacturer);
2591 os_free(config->model_name);
2592 os_free(config->model_number);
2593 os_free(config->serial_number);
2594 os_free(config->config_methods);
2595 os_free(config->p2p_ssid_postfix);
2596 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002597 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002598 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002599 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002600 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002601 wpabuf_free(config->wps_nfc_dh_pubkey);
2602 wpabuf_free(config->wps_nfc_dh_privkey);
2603 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002604 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002605 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002606 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002607 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002608 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002609 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002610 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002611 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002612#ifdef CONFIG_MBO
2613 os_free(config->non_pref_chan);
2614#endif /* CONFIG_MBO */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 os_free(config);
2617}
2618
2619
2620/**
2621 * wpa_config_foreach_network - Iterate over each configured network
2622 * @config: Configuration data from wpa_config_read()
2623 * @func: Callback function to process each network
2624 * @arg: Opaque argument to pass to callback function
2625 *
2626 * Iterate over the set of configured networks calling the specified
2627 * function for each item. We guard against callbacks removing the
2628 * supplied network.
2629 */
2630void wpa_config_foreach_network(struct wpa_config *config,
2631 void (*func)(void *, struct wpa_ssid *),
2632 void *arg)
2633{
2634 struct wpa_ssid *ssid, *next;
2635
2636 ssid = config->ssid;
2637 while (ssid) {
2638 next = ssid->next;
2639 func(arg, ssid);
2640 ssid = next;
2641 }
2642}
2643
2644
2645/**
2646 * wpa_config_get_network - Get configured network based on id
2647 * @config: Configuration data from wpa_config_read()
2648 * @id: Unique network id to search for
2649 * Returns: Network configuration or %NULL if not found
2650 */
2651struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2652{
2653 struct wpa_ssid *ssid;
2654
2655 ssid = config->ssid;
2656 while (ssid) {
2657 if (id == ssid->id)
2658 break;
2659 ssid = ssid->next;
2660 }
2661
2662 return ssid;
2663}
2664
2665
2666/**
2667 * wpa_config_add_network - Add a new network with empty configuration
2668 * @config: Configuration data from wpa_config_read()
2669 * Returns: The new network configuration or %NULL if operation failed
2670 */
2671struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2672{
2673 int id;
2674 struct wpa_ssid *ssid, *last = NULL;
2675
2676 id = -1;
2677 ssid = config->ssid;
2678 while (ssid) {
2679 if (ssid->id > id)
2680 id = ssid->id;
2681 last = ssid;
2682 ssid = ssid->next;
2683 }
2684 id++;
2685
2686 ssid = os_zalloc(sizeof(*ssid));
2687 if (ssid == NULL)
2688 return NULL;
2689 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002690 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 if (last)
2692 last->next = ssid;
2693 else
2694 config->ssid = ssid;
2695
2696 wpa_config_update_prio_list(config);
2697
2698 return ssid;
2699}
2700
2701
2702/**
2703 * wpa_config_remove_network - Remove a configured network based on id
2704 * @config: Configuration data from wpa_config_read()
2705 * @id: Unique network id to search for
2706 * Returns: 0 on success, or -1 if the network was not found
2707 */
2708int wpa_config_remove_network(struct wpa_config *config, int id)
2709{
2710 struct wpa_ssid *ssid, *prev = NULL;
2711
2712 ssid = config->ssid;
2713 while (ssid) {
2714 if (id == ssid->id)
2715 break;
2716 prev = ssid;
2717 ssid = ssid->next;
2718 }
2719
2720 if (ssid == NULL)
2721 return -1;
2722
2723 if (prev)
2724 prev->next = ssid->next;
2725 else
2726 config->ssid = ssid->next;
2727
2728 wpa_config_update_prio_list(config);
2729 wpa_config_free_ssid(ssid);
2730 return 0;
2731}
2732
2733
2734/**
2735 * wpa_config_set_network_defaults - Set network default values
2736 * @ssid: Pointer to network configuration data
2737 */
2738void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2739{
2740 ssid->proto = DEFAULT_PROTO;
2741 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2742 ssid->group_cipher = DEFAULT_GROUP;
2743 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002744 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002745 ssid->ht = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746#ifdef IEEE8021X_EAPOL
2747 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2748 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2749 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002750 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002752#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002753 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2754 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2755 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2756 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002757 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002758#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002759#ifdef CONFIG_HT_OVERRIDES
2760 ssid->disable_ht = DEFAULT_DISABLE_HT;
2761 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002762 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002763 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002764 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2765 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2766 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2767#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002768#ifdef CONFIG_VHT_OVERRIDES
2769 ssid->vht_rx_mcs_nss_1 = -1;
2770 ssid->vht_rx_mcs_nss_2 = -1;
2771 ssid->vht_rx_mcs_nss_3 = -1;
2772 ssid->vht_rx_mcs_nss_4 = -1;
2773 ssid->vht_rx_mcs_nss_5 = -1;
2774 ssid->vht_rx_mcs_nss_6 = -1;
2775 ssid->vht_rx_mcs_nss_7 = -1;
2776 ssid->vht_rx_mcs_nss_8 = -1;
2777 ssid->vht_tx_mcs_nss_1 = -1;
2778 ssid->vht_tx_mcs_nss_2 = -1;
2779 ssid->vht_tx_mcs_nss_3 = -1;
2780 ssid->vht_tx_mcs_nss_4 = -1;
2781 ssid->vht_tx_mcs_nss_5 = -1;
2782 ssid->vht_tx_mcs_nss_6 = -1;
2783 ssid->vht_tx_mcs_nss_7 = -1;
2784 ssid->vht_tx_mcs_nss_8 = -1;
2785#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002786 ssid->proactive_key_caching = -1;
2787#ifdef CONFIG_IEEE80211W
2788 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2789#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt29333592017-01-09 12:27:11 -08002790#ifdef CONFIG_MACSEC
2791 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
2792#endif /* CONFIG_MACSEC */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002793 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794}
2795
2796
2797/**
2798 * wpa_config_set - Set a variable in network configuration
2799 * @ssid: Pointer to network configuration data
2800 * @var: Variable name, e.g., "ssid"
2801 * @value: Variable value
2802 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07002803 * Returns: 0 on success with possible change in the value, 1 on success with
2804 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 *
2806 * This function can be used to set network configuration variables based on
2807 * both the configuration file and management interface input. The value
2808 * parameter must be in the same format as the text-based configuration file is
2809 * using. For example, strings are using double quotation marks.
2810 */
2811int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2812 int line)
2813{
2814 size_t i;
2815 int ret = 0;
2816
2817 if (ssid == NULL || var == NULL || value == NULL)
2818 return -1;
2819
2820 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2821 const struct parse_data *field = &ssid_fields[i];
2822 if (os_strcmp(var, field->name) != 0)
2823 continue;
2824
Dmitry Shmidte4663042016-04-04 10:07:49 -07002825 ret = field->parser(field, ssid, line, value);
2826 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 if (line) {
2828 wpa_printf(MSG_ERROR, "Line %d: failed to "
2829 "parse %s '%s'.", line, var, value);
2830 }
2831 ret = -1;
2832 }
2833 break;
2834 }
2835 if (i == NUM_SSID_FIELDS) {
2836 if (line) {
2837 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2838 "'%s'.", line, var);
2839 }
2840 ret = -1;
2841 }
2842
2843 return ret;
2844}
2845
2846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2848 const char *value)
2849{
2850 size_t len;
2851 char *buf;
2852 int ret;
2853
2854 len = os_strlen(value);
2855 buf = os_malloc(len + 3);
2856 if (buf == NULL)
2857 return -1;
2858 buf[0] = '"';
2859 os_memcpy(buf + 1, value, len);
2860 buf[len + 1] = '"';
2861 buf[len + 2] = '\0';
2862 ret = wpa_config_set(ssid, var, buf, 0);
2863 os_free(buf);
2864 return ret;
2865}
2866
2867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868/**
2869 * wpa_config_get_all - Get all options from network configuration
2870 * @ssid: Pointer to network configuration data
2871 * @get_keys: Determines if keys/passwords will be included in returned list
2872 * (if they may be exported)
2873 * Returns: %NULL terminated list of all set keys and their values in the form
2874 * of [key1, val1, key2, val2, ... , NULL]
2875 *
2876 * This function can be used to get list of all configured network properties.
2877 * The caller is responsible for freeing the returned list and all its
2878 * elements.
2879 */
2880char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2881{
Dmitry Shmidt83474442015-04-15 13:47:09 -07002882#ifdef NO_CONFIG_WRITE
2883 return NULL;
2884#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 const struct parse_data *field;
2886 char *key, *value;
2887 size_t i;
2888 char **props;
2889 int fields_num;
2890
2891 get_keys = get_keys && ssid->export_keys;
2892
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 if (!props)
2895 return NULL;
2896
2897 fields_num = 0;
2898 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2899 field = &ssid_fields[i];
2900 if (field->key_data && !get_keys)
2901 continue;
2902 value = field->writer(field, ssid);
2903 if (value == NULL)
2904 continue;
2905 if (os_strlen(value) == 0) {
2906 os_free(value);
2907 continue;
2908 }
2909
2910 key = os_strdup(field->name);
2911 if (key == NULL) {
2912 os_free(value);
2913 goto err;
2914 }
2915
2916 props[fields_num * 2] = key;
2917 props[fields_num * 2 + 1] = value;
2918
2919 fields_num++;
2920 }
2921
2922 return props;
2923
2924err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002925 for (i = 0; props[i]; i++)
2926 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 os_free(props);
2928 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07002929#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930}
2931
2932
2933#ifndef NO_CONFIG_WRITE
2934/**
2935 * wpa_config_get - Get a variable in network configuration
2936 * @ssid: Pointer to network configuration data
2937 * @var: Variable name, e.g., "ssid"
2938 * Returns: Value of the variable or %NULL on failure
2939 *
2940 * This function can be used to get network configuration variables. The
2941 * returned value is a copy of the configuration variable in text format, i.e,.
2942 * the same format that the text-based configuration file and wpa_config_set()
2943 * are using for the value. The caller is responsible for freeing the returned
2944 * value.
2945 */
2946char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2947{
2948 size_t i;
2949
2950 if (ssid == NULL || var == NULL)
2951 return NULL;
2952
2953 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2954 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08002955 if (os_strcmp(var, field->name) == 0) {
2956 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002957
2958 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07002959 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002960 "Found newline in value for %s; not returning it",
2961 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08002962 os_free(ret);
2963 ret = NULL;
2964 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002965
Paul Stewart85c72c62016-03-03 15:33:47 -08002966 return ret;
2967 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 }
2969
2970 return NULL;
2971}
2972
2973
2974/**
2975 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2976 * @ssid: Pointer to network configuration data
2977 * @var: Variable name, e.g., "ssid"
2978 * Returns: Value of the variable or %NULL on failure
2979 *
2980 * This function can be used to get network configuration variable like
2981 * wpa_config_get(). The only difference is that this functions does not expose
2982 * key/password material from the configuration. In case a key/password field
2983 * is requested, the returned value is an empty string or %NULL if the variable
2984 * is not set or "*" if the variable is set (regardless of its value). The
2985 * returned value is a copy of the configuration variable in text format, i.e,.
2986 * the same format that the text-based configuration file and wpa_config_set()
2987 * are using for the value. The caller is responsible for freeing the returned
2988 * value.
2989 */
2990char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2991{
2992 size_t i;
2993
2994 if (ssid == NULL || var == NULL)
2995 return NULL;
2996
2997 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2998 const struct parse_data *field = &ssid_fields[i];
2999 if (os_strcmp(var, field->name) == 0) {
3000 char *res = field->writer(field, ssid);
3001 if (field->key_data) {
3002 if (res && res[0]) {
3003 wpa_printf(MSG_DEBUG, "Do not allow "
3004 "key_data field to be "
3005 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003006 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 return os_strdup("*");
3008 }
3009
3010 os_free(res);
3011 return NULL;
3012 }
3013 return res;
3014 }
3015 }
3016
3017 return NULL;
3018}
3019#endif /* NO_CONFIG_WRITE */
3020
3021
3022/**
3023 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3024 * @ssid: Pointer to network configuration data
3025 *
3026 * This function must be called to update WPA PSK when either SSID or the
3027 * passphrase has changed for the network configuration.
3028 */
3029void wpa_config_update_psk(struct wpa_ssid *ssid)
3030{
3031#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003032 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 ssid->psk, PMK_LEN);
3034 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3035 ssid->psk, PMK_LEN);
3036 ssid->psk_set = 1;
3037#endif /* CONFIG_NO_PBKDF2 */
3038}
3039
3040
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003041static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3042 const char *value)
3043{
3044 u8 *proto;
3045 int **port;
3046 int *ports, *nports;
3047 const char *pos;
3048 unsigned int num_ports;
3049
3050 proto = os_realloc_array(cred->req_conn_capab_proto,
3051 cred->num_req_conn_capab + 1, sizeof(u8));
3052 if (proto == NULL)
3053 return -1;
3054 cred->req_conn_capab_proto = proto;
3055
3056 port = os_realloc_array(cred->req_conn_capab_port,
3057 cred->num_req_conn_capab + 1, sizeof(int *));
3058 if (port == NULL)
3059 return -1;
3060 cred->req_conn_capab_port = port;
3061
3062 proto[cred->num_req_conn_capab] = atoi(value);
3063
3064 pos = os_strchr(value, ':');
3065 if (pos == NULL) {
3066 port[cred->num_req_conn_capab] = NULL;
3067 cred->num_req_conn_capab++;
3068 return 0;
3069 }
3070 pos++;
3071
3072 ports = NULL;
3073 num_ports = 0;
3074
3075 while (*pos) {
3076 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3077 if (nports == NULL) {
3078 os_free(ports);
3079 return -1;
3080 }
3081 ports = nports;
3082 ports[num_ports++] = atoi(pos);
3083
3084 pos = os_strchr(pos, ',');
3085 if (pos == NULL)
3086 break;
3087 pos++;
3088 }
3089
3090 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3091 if (nports == NULL) {
3092 os_free(ports);
3093 return -1;
3094 }
3095 ports = nports;
3096 ports[num_ports] = -1;
3097
3098 port[cred->num_req_conn_capab] = ports;
3099 cred->num_req_conn_capab++;
3100 return 0;
3101}
3102
3103
Dmitry Shmidt04949592012-07-19 12:16:46 -07003104int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3105 const char *value, int line)
3106{
3107 char *val;
3108 size_t len;
3109
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003110 if (os_strcmp(var, "temporary") == 0) {
3111 cred->temporary = atoi(value);
3112 return 0;
3113 }
3114
Dmitry Shmidt04949592012-07-19 12:16:46 -07003115 if (os_strcmp(var, "priority") == 0) {
3116 cred->priority = atoi(value);
3117 return 0;
3118 }
3119
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003120 if (os_strcmp(var, "sp_priority") == 0) {
3121 int prio = atoi(value);
3122 if (prio < 0 || prio > 255)
3123 return -1;
3124 cred->sp_priority = prio;
3125 return 0;
3126 }
3127
Dmitry Shmidt04949592012-07-19 12:16:46 -07003128 if (os_strcmp(var, "pcsc") == 0) {
3129 cred->pcsc = atoi(value);
3130 return 0;
3131 }
3132
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003133 if (os_strcmp(var, "eap") == 0) {
3134 struct eap_method_type method;
3135 method.method = eap_peer_get_type(value, &method.vendor);
3136 if (method.vendor == EAP_VENDOR_IETF &&
3137 method.method == EAP_TYPE_NONE) {
3138 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3139 "for a credential", line, value);
3140 return -1;
3141 }
3142 os_free(cred->eap_method);
3143 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3144 if (cred->eap_method == NULL)
3145 return -1;
3146 os_memcpy(cred->eap_method, &method, sizeof(method));
3147 return 0;
3148 }
3149
3150 if (os_strcmp(var, "password") == 0 &&
3151 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003152 if (has_newline(value))
3153 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003154 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003155 cred->password = os_strdup(value);
3156 cred->ext_password = 1;
3157 return 0;
3158 }
3159
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003160 if (os_strcmp(var, "update_identifier") == 0) {
3161 cred->update_identifier = atoi(value);
3162 return 0;
3163 }
3164
3165 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3166 cred->min_dl_bandwidth_home = atoi(value);
3167 return 0;
3168 }
3169
3170 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3171 cred->min_ul_bandwidth_home = atoi(value);
3172 return 0;
3173 }
3174
3175 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3176 cred->min_dl_bandwidth_roaming = atoi(value);
3177 return 0;
3178 }
3179
3180 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3181 cred->min_ul_bandwidth_roaming = atoi(value);
3182 return 0;
3183 }
3184
3185 if (os_strcmp(var, "max_bss_load") == 0) {
3186 cred->max_bss_load = atoi(value);
3187 return 0;
3188 }
3189
3190 if (os_strcmp(var, "req_conn_capab") == 0)
3191 return wpa_config_set_cred_req_conn_capab(cred, value);
3192
3193 if (os_strcmp(var, "ocsp") == 0) {
3194 cred->ocsp = atoi(value);
3195 return 0;
3196 }
3197
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003198 if (os_strcmp(var, "sim_num") == 0) {
3199 cred->sim_num = atoi(value);
3200 return 0;
3201 }
3202
Dmitry Shmidt04949592012-07-19 12:16:46 -07003203 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003204 if (val == NULL ||
3205 (os_strcmp(var, "excluded_ssid") != 0 &&
3206 os_strcmp(var, "roaming_consortium") != 0 &&
3207 os_strcmp(var, "required_roaming_consortium") != 0 &&
3208 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003209 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3210 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003211 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003212 return -1;
3213 }
3214
3215 if (os_strcmp(var, "realm") == 0) {
3216 os_free(cred->realm);
3217 cred->realm = val;
3218 return 0;
3219 }
3220
3221 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003222 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003223 cred->username = val;
3224 return 0;
3225 }
3226
3227 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003228 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003229 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003230 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003231 return 0;
3232 }
3233
3234 if (os_strcmp(var, "ca_cert") == 0) {
3235 os_free(cred->ca_cert);
3236 cred->ca_cert = val;
3237 return 0;
3238 }
3239
3240 if (os_strcmp(var, "client_cert") == 0) {
3241 os_free(cred->client_cert);
3242 cred->client_cert = val;
3243 return 0;
3244 }
3245
3246 if (os_strcmp(var, "private_key") == 0) {
3247 os_free(cred->private_key);
3248 cred->private_key = val;
3249 return 0;
3250 }
3251
3252 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003253 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003254 cred->private_key_passwd = val;
3255 return 0;
3256 }
3257
3258 if (os_strcmp(var, "imsi") == 0) {
3259 os_free(cred->imsi);
3260 cred->imsi = val;
3261 return 0;
3262 }
3263
3264 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003265 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003266 cred->milenage = val;
3267 return 0;
3268 }
3269
Dmitry Shmidt051af732013-10-22 13:52:46 -07003270 if (os_strcmp(var, "domain_suffix_match") == 0) {
3271 os_free(cred->domain_suffix_match);
3272 cred->domain_suffix_match = val;
3273 return 0;
3274 }
3275
Dmitry Shmidt04949592012-07-19 12:16:46 -07003276 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003277 char **new_domain;
3278 new_domain = os_realloc_array(cred->domain,
3279 cred->num_domain + 1,
3280 sizeof(char *));
3281 if (new_domain == NULL) {
3282 os_free(val);
3283 return -1;
3284 }
3285 new_domain[cred->num_domain++] = val;
3286 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003287 return 0;
3288 }
3289
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003290 if (os_strcmp(var, "phase1") == 0) {
3291 os_free(cred->phase1);
3292 cred->phase1 = val;
3293 return 0;
3294 }
3295
3296 if (os_strcmp(var, "phase2") == 0) {
3297 os_free(cred->phase2);
3298 cred->phase2 = val;
3299 return 0;
3300 }
3301
3302 if (os_strcmp(var, "roaming_consortium") == 0) {
3303 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3304 wpa_printf(MSG_ERROR, "Line %d: invalid "
3305 "roaming_consortium length %d (3..15 "
3306 "expected)", line, (int) len);
3307 os_free(val);
3308 return -1;
3309 }
3310 os_memcpy(cred->roaming_consortium, val, len);
3311 cred->roaming_consortium_len = len;
3312 os_free(val);
3313 return 0;
3314 }
3315
Dmitry Shmidt051af732013-10-22 13:52:46 -07003316 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3317 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3318 {
3319 wpa_printf(MSG_ERROR, "Line %d: invalid "
3320 "required_roaming_consortium length %d "
3321 "(3..15 expected)", line, (int) len);
3322 os_free(val);
3323 return -1;
3324 }
3325 os_memcpy(cred->required_roaming_consortium, val, len);
3326 cred->required_roaming_consortium_len = len;
3327 os_free(val);
3328 return 0;
3329 }
3330
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003331 if (os_strcmp(var, "excluded_ssid") == 0) {
3332 struct excluded_ssid *e;
3333
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003334 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003335 wpa_printf(MSG_ERROR, "Line %d: invalid "
3336 "excluded_ssid length %d", line, (int) len);
3337 os_free(val);
3338 return -1;
3339 }
3340
3341 e = os_realloc_array(cred->excluded_ssid,
3342 cred->num_excluded_ssid + 1,
3343 sizeof(struct excluded_ssid));
3344 if (e == NULL) {
3345 os_free(val);
3346 return -1;
3347 }
3348 cred->excluded_ssid = e;
3349
3350 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3351 os_memcpy(e->ssid, val, len);
3352 e->ssid_len = len;
3353
3354 os_free(val);
3355
3356 return 0;
3357 }
3358
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003359 if (os_strcmp(var, "roaming_partner") == 0) {
3360 struct roaming_partner *p;
3361 char *pos;
3362
3363 p = os_realloc_array(cred->roaming_partner,
3364 cred->num_roaming_partner + 1,
3365 sizeof(struct roaming_partner));
3366 if (p == NULL) {
3367 os_free(val);
3368 return -1;
3369 }
3370 cred->roaming_partner = p;
3371
3372 p = &cred->roaming_partner[cred->num_roaming_partner];
3373
3374 pos = os_strchr(val, ',');
3375 if (pos == NULL) {
3376 os_free(val);
3377 return -1;
3378 }
3379 *pos++ = '\0';
3380 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3381 os_free(val);
3382 return -1;
3383 }
3384 os_memcpy(p->fqdn, val, pos - val);
3385
3386 p->exact_match = atoi(pos);
3387
3388 pos = os_strchr(pos, ',');
3389 if (pos == NULL) {
3390 os_free(val);
3391 return -1;
3392 }
3393 *pos++ = '\0';
3394
3395 p->priority = atoi(pos);
3396
3397 pos = os_strchr(pos, ',');
3398 if (pos == NULL) {
3399 os_free(val);
3400 return -1;
3401 }
3402 *pos++ = '\0';
3403
3404 if (os_strlen(pos) >= sizeof(p->country)) {
3405 os_free(val);
3406 return -1;
3407 }
3408 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3409
3410 cred->num_roaming_partner++;
3411 os_free(val);
3412
3413 return 0;
3414 }
3415
3416 if (os_strcmp(var, "provisioning_sp") == 0) {
3417 os_free(cred->provisioning_sp);
3418 cred->provisioning_sp = val;
3419 return 0;
3420 }
3421
Dmitry Shmidt04949592012-07-19 12:16:46 -07003422 if (line) {
3423 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3424 line, var);
3425 }
3426
3427 os_free(val);
3428
3429 return -1;
3430}
3431
3432
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003433static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003434{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003436 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003437 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003438
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003439 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003440 if (buf == NULL)
3441 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003442 res = os_snprintf(buf, bufsize, "%d", val);
3443 if (os_snprintf_error(bufsize, res)) {
3444 os_free(buf);
3445 buf = NULL;
3446 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003447 return buf;
3448}
3449
3450
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003451static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003452{
3453 if (str == NULL)
3454 return NULL;
3455 return os_strdup(str);
3456}
3457
3458
3459char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3460{
3461 if (os_strcmp(var, "temporary") == 0)
3462 return alloc_int_str(cred->temporary);
3463
3464 if (os_strcmp(var, "priority") == 0)
3465 return alloc_int_str(cred->priority);
3466
3467 if (os_strcmp(var, "sp_priority") == 0)
3468 return alloc_int_str(cred->sp_priority);
3469
3470 if (os_strcmp(var, "pcsc") == 0)
3471 return alloc_int_str(cred->pcsc);
3472
3473 if (os_strcmp(var, "eap") == 0) {
3474 if (!cred->eap_method)
3475 return NULL;
3476 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3477 cred->eap_method[0].method));
3478 }
3479
3480 if (os_strcmp(var, "update_identifier") == 0)
3481 return alloc_int_str(cred->update_identifier);
3482
3483 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3484 return alloc_int_str(cred->min_dl_bandwidth_home);
3485
3486 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3487 return alloc_int_str(cred->min_ul_bandwidth_home);
3488
3489 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3490 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3491
3492 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3493 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3494
3495 if (os_strcmp(var, "max_bss_load") == 0)
3496 return alloc_int_str(cred->max_bss_load);
3497
3498 if (os_strcmp(var, "req_conn_capab") == 0) {
3499 unsigned int i;
3500 char *buf, *end, *pos;
3501 int ret;
3502
3503 if (!cred->num_req_conn_capab)
3504 return NULL;
3505
3506 buf = os_malloc(4000);
3507 if (buf == NULL)
3508 return NULL;
3509 pos = buf;
3510 end = pos + 4000;
3511 for (i = 0; i < cred->num_req_conn_capab; i++) {
3512 int *ports;
3513
3514 ret = os_snprintf(pos, end - pos, "%s%u",
3515 i > 0 ? "\n" : "",
3516 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003517 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003518 return buf;
3519 pos += ret;
3520
3521 ports = cred->req_conn_capab_port[i];
3522 if (ports) {
3523 int j;
3524 for (j = 0; ports[j] != -1; j++) {
3525 ret = os_snprintf(pos, end - pos,
3526 "%s%d",
3527 j > 0 ? "," : ":",
3528 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003529 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003530 return buf;
3531 pos += ret;
3532 }
3533 }
3534 }
3535
3536 return buf;
3537 }
3538
3539 if (os_strcmp(var, "ocsp") == 0)
3540 return alloc_int_str(cred->ocsp);
3541
3542 if (os_strcmp(var, "realm") == 0)
3543 return alloc_strdup(cred->realm);
3544
3545 if (os_strcmp(var, "username") == 0)
3546 return alloc_strdup(cred->username);
3547
3548 if (os_strcmp(var, "password") == 0) {
3549 if (!cred->password)
3550 return NULL;
3551 return alloc_strdup("*");
3552 }
3553
3554 if (os_strcmp(var, "ca_cert") == 0)
3555 return alloc_strdup(cred->ca_cert);
3556
3557 if (os_strcmp(var, "client_cert") == 0)
3558 return alloc_strdup(cred->client_cert);
3559
3560 if (os_strcmp(var, "private_key") == 0)
3561 return alloc_strdup(cred->private_key);
3562
3563 if (os_strcmp(var, "private_key_passwd") == 0) {
3564 if (!cred->private_key_passwd)
3565 return NULL;
3566 return alloc_strdup("*");
3567 }
3568
3569 if (os_strcmp(var, "imsi") == 0)
3570 return alloc_strdup(cred->imsi);
3571
3572 if (os_strcmp(var, "milenage") == 0) {
3573 if (!(cred->milenage))
3574 return NULL;
3575 return alloc_strdup("*");
3576 }
3577
3578 if (os_strcmp(var, "domain_suffix_match") == 0)
3579 return alloc_strdup(cred->domain_suffix_match);
3580
3581 if (os_strcmp(var, "domain") == 0) {
3582 unsigned int i;
3583 char *buf, *end, *pos;
3584 int ret;
3585
3586 if (!cred->num_domain)
3587 return NULL;
3588
3589 buf = os_malloc(4000);
3590 if (buf == NULL)
3591 return NULL;
3592 pos = buf;
3593 end = pos + 4000;
3594
3595 for (i = 0; i < cred->num_domain; i++) {
3596 ret = os_snprintf(pos, end - pos, "%s%s",
3597 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003598 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003599 return buf;
3600 pos += ret;
3601 }
3602
3603 return buf;
3604 }
3605
3606 if (os_strcmp(var, "phase1") == 0)
3607 return alloc_strdup(cred->phase1);
3608
3609 if (os_strcmp(var, "phase2") == 0)
3610 return alloc_strdup(cred->phase2);
3611
3612 if (os_strcmp(var, "roaming_consortium") == 0) {
3613 size_t buflen;
3614 char *buf;
3615
3616 if (!cred->roaming_consortium_len)
3617 return NULL;
3618 buflen = cred->roaming_consortium_len * 2 + 1;
3619 buf = os_malloc(buflen);
3620 if (buf == NULL)
3621 return NULL;
3622 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3623 cred->roaming_consortium_len);
3624 return buf;
3625 }
3626
3627 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3628 size_t buflen;
3629 char *buf;
3630
3631 if (!cred->required_roaming_consortium_len)
3632 return NULL;
3633 buflen = cred->required_roaming_consortium_len * 2 + 1;
3634 buf = os_malloc(buflen);
3635 if (buf == NULL)
3636 return NULL;
3637 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3638 cred->required_roaming_consortium_len);
3639 return buf;
3640 }
3641
3642 if (os_strcmp(var, "excluded_ssid") == 0) {
3643 unsigned int i;
3644 char *buf, *end, *pos;
3645
3646 if (!cred->num_excluded_ssid)
3647 return NULL;
3648
3649 buf = os_malloc(4000);
3650 if (buf == NULL)
3651 return NULL;
3652 pos = buf;
3653 end = pos + 4000;
3654
3655 for (i = 0; i < cred->num_excluded_ssid; i++) {
3656 struct excluded_ssid *e;
3657 int ret;
3658
3659 e = &cred->excluded_ssid[i];
3660 ret = os_snprintf(pos, end - pos, "%s%s",
3661 i > 0 ? "\n" : "",
3662 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003663 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003664 return buf;
3665 pos += ret;
3666 }
3667
3668 return buf;
3669 }
3670
3671 if (os_strcmp(var, "roaming_partner") == 0) {
3672 unsigned int i;
3673 char *buf, *end, *pos;
3674
3675 if (!cred->num_roaming_partner)
3676 return NULL;
3677
3678 buf = os_malloc(4000);
3679 if (buf == NULL)
3680 return NULL;
3681 pos = buf;
3682 end = pos + 4000;
3683
3684 for (i = 0; i < cred->num_roaming_partner; i++) {
3685 struct roaming_partner *p;
3686 int ret;
3687
3688 p = &cred->roaming_partner[i];
3689 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3690 i > 0 ? "\n" : "",
3691 p->fqdn, p->exact_match, p->priority,
3692 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003693 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003694 return buf;
3695 pos += ret;
3696 }
3697
3698 return buf;
3699 }
3700
3701 if (os_strcmp(var, "provisioning_sp") == 0)
3702 return alloc_strdup(cred->provisioning_sp);
3703
3704 return NULL;
3705}
3706
3707
Dmitry Shmidt04949592012-07-19 12:16:46 -07003708struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3709{
3710 struct wpa_cred *cred;
3711
3712 cred = config->cred;
3713 while (cred) {
3714 if (id == cred->id)
3715 break;
3716 cred = cred->next;
3717 }
3718
3719 return cred;
3720}
3721
3722
3723struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3724{
3725 int id;
3726 struct wpa_cred *cred, *last = NULL;
3727
3728 id = -1;
3729 cred = config->cred;
3730 while (cred) {
3731 if (cred->id > id)
3732 id = cred->id;
3733 last = cred;
3734 cred = cred->next;
3735 }
3736 id++;
3737
3738 cred = os_zalloc(sizeof(*cred));
3739 if (cred == NULL)
3740 return NULL;
3741 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003742 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003743 if (last)
3744 last->next = cred;
3745 else
3746 config->cred = cred;
3747
3748 return cred;
3749}
3750
3751
3752int wpa_config_remove_cred(struct wpa_config *config, int id)
3753{
3754 struct wpa_cred *cred, *prev = NULL;
3755
3756 cred = config->cred;
3757 while (cred) {
3758 if (id == cred->id)
3759 break;
3760 prev = cred;
3761 cred = cred->next;
3762 }
3763
3764 if (cred == NULL)
3765 return -1;
3766
3767 if (prev)
3768 prev->next = cred->next;
3769 else
3770 config->cred = cred->next;
3771
3772 wpa_config_free_cred(cred);
3773 return 0;
3774}
3775
3776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777#ifndef CONFIG_NO_CONFIG_BLOBS
3778/**
3779 * wpa_config_get_blob - Get a named configuration blob
3780 * @config: Configuration data from wpa_config_read()
3781 * @name: Name of the blob
3782 * Returns: Pointer to blob data or %NULL if not found
3783 */
3784const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3785 const char *name)
3786{
3787 struct wpa_config_blob *blob = config->blobs;
3788
3789 while (blob) {
3790 if (os_strcmp(blob->name, name) == 0)
3791 return blob;
3792 blob = blob->next;
3793 }
3794 return NULL;
3795}
3796
3797
3798/**
3799 * wpa_config_set_blob - Set or add a named configuration blob
3800 * @config: Configuration data from wpa_config_read()
3801 * @blob: New value for the blob
3802 *
3803 * Adds a new configuration blob or replaces the current value of an existing
3804 * blob.
3805 */
3806void wpa_config_set_blob(struct wpa_config *config,
3807 struct wpa_config_blob *blob)
3808{
3809 wpa_config_remove_blob(config, blob->name);
3810 blob->next = config->blobs;
3811 config->blobs = blob;
3812}
3813
3814
3815/**
3816 * wpa_config_free_blob - Free blob data
3817 * @blob: Pointer to blob to be freed
3818 */
3819void wpa_config_free_blob(struct wpa_config_blob *blob)
3820{
3821 if (blob) {
3822 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003823 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 os_free(blob);
3825 }
3826}
3827
3828
3829/**
3830 * wpa_config_remove_blob - Remove a named configuration blob
3831 * @config: Configuration data from wpa_config_read()
3832 * @name: Name of the blob to remove
3833 * Returns: 0 if blob was removed or -1 if blob was not found
3834 */
3835int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3836{
3837 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3838
3839 while (pos) {
3840 if (os_strcmp(pos->name, name) == 0) {
3841 if (prev)
3842 prev->next = pos->next;
3843 else
3844 config->blobs = pos->next;
3845 wpa_config_free_blob(pos);
3846 return 0;
3847 }
3848 prev = pos;
3849 pos = pos->next;
3850 }
3851
3852 return -1;
3853}
3854#endif /* CONFIG_NO_CONFIG_BLOBS */
3855
3856
3857/**
3858 * wpa_config_alloc_empty - Allocate an empty configuration
3859 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3860 * socket
3861 * @driver_param: Driver parameters
3862 * Returns: Pointer to allocated configuration data or %NULL on failure
3863 */
3864struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3865 const char *driver_param)
3866{
3867 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003868 const int aCWmin = 4, aCWmax = 10;
3869 const struct hostapd_wmm_ac_params ac_bk =
3870 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3871 const struct hostapd_wmm_ac_params ac_be =
3872 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3873 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3874 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3875 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3876 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877
3878 config = os_zalloc(sizeof(*config));
3879 if (config == NULL)
3880 return NULL;
3881 config->eapol_version = DEFAULT_EAPOL_VERSION;
3882 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 config->user_mpm = DEFAULT_USER_MPM;
3884 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003885 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003886 config->dot11RSNASAERetransPeriod =
3887 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 config->fast_reauth = DEFAULT_FAST_REAUTH;
3889 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3890 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003891 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003893 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003894 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3896 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3897 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3898 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003899 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003900 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003901 config->wmm_ac_params[0] = ac_be;
3902 config->wmm_ac_params[1] = ac_bk;
3903 config->wmm_ac_params[2] = ac_vi;
3904 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003905 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003906 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003907 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003908 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003909 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003911#ifdef CONFIG_MBO
3912 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003913 config->disassoc_imminent_rssi_threshold =
3914 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
3915 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003916#endif /* CONFIG_MBO */
3917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 if (ctrl_interface)
3919 config->ctrl_interface = os_strdup(ctrl_interface);
3920 if (driver_param)
3921 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003922 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923
3924 return config;
3925}
3926
3927
3928#ifndef CONFIG_NO_STDOUT_DEBUG
3929/**
3930 * wpa_config_debug_dump_networks - Debug dump of configured networks
3931 * @config: Configuration data from wpa_config_read()
3932 */
3933void wpa_config_debug_dump_networks(struct wpa_config *config)
3934{
3935 int prio;
3936 struct wpa_ssid *ssid;
3937
3938 for (prio = 0; prio < config->num_prio; prio++) {
3939 ssid = config->pssid[prio];
3940 wpa_printf(MSG_DEBUG, "Priority group %d",
3941 ssid->priority);
3942 while (ssid) {
3943 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3944 ssid->id,
3945 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3946 ssid = ssid->pnext;
3947 }
3948 }
3949}
3950#endif /* CONFIG_NO_STDOUT_DEBUG */
3951
3952
3953struct global_parse_data {
3954 char *name;
3955 int (*parser)(const struct global_parse_data *data,
3956 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003957 int (*get)(const char *name, struct wpa_config *config, long offset,
3958 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959 void *param1, *param2, *param3;
3960 unsigned int changed_flag;
3961};
3962
3963
3964static int wpa_global_config_parse_int(const struct global_parse_data *data,
3965 struct wpa_config *config, int line,
3966 const char *pos)
3967{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003968 int val, *dst;
3969 char *end;
3970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003972 val = strtol(pos, &end, 0);
3973 if (*end) {
3974 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3975 line, pos);
3976 return -1;
3977 }
3978 *dst = val;
3979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3981
3982 if (data->param2 && *dst < (long) data->param2) {
3983 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3984 "min_value=%ld)", line, data->name, *dst,
3985 (long) data->param2);
3986 *dst = (long) data->param2;
3987 return -1;
3988 }
3989
3990 if (data->param3 && *dst > (long) data->param3) {
3991 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3992 "max_value=%ld)", line, data->name, *dst,
3993 (long) data->param3);
3994 *dst = (long) data->param3;
3995 return -1;
3996 }
3997
3998 return 0;
3999}
4000
4001
4002static int wpa_global_config_parse_str(const struct global_parse_data *data,
4003 struct wpa_config *config, int line,
4004 const char *pos)
4005{
4006 size_t len;
4007 char **dst, *tmp;
4008
4009 len = os_strlen(pos);
4010 if (data->param2 && len < (size_t) data->param2) {
4011 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4012 "min_len=%ld)", line, data->name,
4013 (unsigned long) len, (long) data->param2);
4014 return -1;
4015 }
4016
4017 if (data->param3 && len > (size_t) data->param3) {
4018 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4019 "max_len=%ld)", line, data->name,
4020 (unsigned long) len, (long) data->param3);
4021 return -1;
4022 }
4023
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004024 if (has_newline(pos)) {
4025 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4026 line, data->name);
4027 return -1;
4028 }
4029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 tmp = os_strdup(pos);
4031 if (tmp == NULL)
4032 return -1;
4033
4034 dst = (char **) (((u8 *) config) + (long) data->param1);
4035 os_free(*dst);
4036 *dst = tmp;
4037 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4038
4039 return 0;
4040}
4041
4042
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004043static int wpa_config_process_bgscan(const struct global_parse_data *data,
4044 struct wpa_config *config, int line,
4045 const char *pos)
4046{
4047 size_t len;
4048 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004049 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004050
4051 tmp = wpa_config_parse_string(pos, &len);
4052 if (tmp == NULL) {
4053 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4054 line, data->name);
4055 return -1;
4056 }
4057
Dmitry Shmidt97672262014-02-03 13:02:54 -08004058 res = wpa_global_config_parse_str(data, config, line, tmp);
4059 os_free(tmp);
4060 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004061}
4062
4063
Dmitry Shmidt04949592012-07-19 12:16:46 -07004064static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4065 struct wpa_config *config, int line,
4066 const char *pos)
4067{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004068 struct wpabuf **dst, *tmp;
4069
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004070 tmp = wpabuf_parse_bin(pos);
4071 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004072 return -1;
4073
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
4075 wpabuf_free(*dst);
4076 *dst = tmp;
4077 wpa_printf(MSG_DEBUG, "%s", data->name);
4078
4079 return 0;
4080}
4081
4082
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004083static int wpa_config_process_freq_list(const struct global_parse_data *data,
4084 struct wpa_config *config, int line,
4085 const char *value)
4086{
4087 int *freqs;
4088
4089 freqs = wpa_config_parse_int_array(value);
4090 if (freqs == NULL)
4091 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004092 if (freqs[0] == 0) {
4093 os_free(freqs);
4094 freqs = NULL;
4095 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004096 os_free(config->freq_list);
4097 config->freq_list = freqs;
4098 return 0;
4099}
4100
4101
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004102#ifdef CONFIG_P2P
4103static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4104 struct wpa_config *config, int line,
4105 const char *pos)
4106{
4107 u32 *dst;
4108 struct hostapd_ip_addr addr;
4109
4110 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4111 return -1;
4112 if (addr.af != AF_INET)
4113 return -1;
4114
4115 dst = (u32 *) (((u8 *) config) + (long) data->param1);
4116 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4117 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4118 WPA_GET_BE32((u8 *) dst));
4119
4120 return 0;
4121}
4122#endif /* CONFIG_P2P */
4123
4124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125static int wpa_config_process_country(const struct global_parse_data *data,
4126 struct wpa_config *config, int line,
4127 const char *pos)
4128{
4129 if (!pos[0] || !pos[1]) {
4130 wpa_printf(MSG_DEBUG, "Invalid country set");
4131 return -1;
4132 }
4133 config->country[0] = pos[0];
4134 config->country[1] = pos[1];
4135 wpa_printf(MSG_DEBUG, "country='%c%c'",
4136 config->country[0], config->country[1]);
4137 return 0;
4138}
4139
4140
4141static int wpa_config_process_load_dynamic_eap(
4142 const struct global_parse_data *data, struct wpa_config *config,
4143 int line, const char *so)
4144{
4145 int ret;
4146 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4147 ret = eap_peer_method_load(so);
4148 if (ret == -2) {
4149 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4150 "reloading.");
4151 } else if (ret) {
4152 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4153 "method '%s'.", line, so);
4154 return -1;
4155 }
4156
4157 return 0;
4158}
4159
4160
4161#ifdef CONFIG_WPS
4162
4163static int wpa_config_process_uuid(const struct global_parse_data *data,
4164 struct wpa_config *config, int line,
4165 const char *pos)
4166{
4167 char buf[40];
4168 if (uuid_str2bin(pos, config->uuid)) {
4169 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4170 return -1;
4171 }
4172 uuid_bin2str(config->uuid, buf, sizeof(buf));
4173 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4174 return 0;
4175}
4176
4177
4178static int wpa_config_process_device_type(
4179 const struct global_parse_data *data,
4180 struct wpa_config *config, int line, const char *pos)
4181{
4182 return wps_dev_type_str2bin(pos, config->device_type);
4183}
4184
4185
4186static int wpa_config_process_os_version(const struct global_parse_data *data,
4187 struct wpa_config *config, int line,
4188 const char *pos)
4189{
4190 if (hexstr2bin(pos, config->os_version, 4)) {
4191 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4192 return -1;
4193 }
4194 wpa_printf(MSG_DEBUG, "os_version=%08x",
4195 WPA_GET_BE32(config->os_version));
4196 return 0;
4197}
4198
Dmitry Shmidt04949592012-07-19 12:16:46 -07004199
4200static int wpa_config_process_wps_vendor_ext_m1(
4201 const struct global_parse_data *data,
4202 struct wpa_config *config, int line, const char *pos)
4203{
4204 struct wpabuf *tmp;
4205 int len = os_strlen(pos) / 2;
4206 u8 *p;
4207
4208 if (!len) {
4209 wpa_printf(MSG_ERROR, "Line %d: "
4210 "invalid wps_vendor_ext_m1", line);
4211 return -1;
4212 }
4213
4214 tmp = wpabuf_alloc(len);
4215 if (tmp) {
4216 p = wpabuf_put(tmp, len);
4217
4218 if (hexstr2bin(pos, p, len)) {
4219 wpa_printf(MSG_ERROR, "Line %d: "
4220 "invalid wps_vendor_ext_m1", line);
4221 wpabuf_free(tmp);
4222 return -1;
4223 }
4224
4225 wpabuf_free(config->wps_vendor_ext_m1);
4226 config->wps_vendor_ext_m1 = tmp;
4227 } else {
4228 wpa_printf(MSG_ERROR, "Can not allocate "
4229 "memory for wps_vendor_ext_m1");
4230 return -1;
4231 }
4232
4233 return 0;
4234}
4235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236#endif /* CONFIG_WPS */
4237
4238#ifdef CONFIG_P2P
4239static int wpa_config_process_sec_device_type(
4240 const struct global_parse_data *data,
4241 struct wpa_config *config, int line, const char *pos)
4242{
4243 int idx;
4244
4245 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4246 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4247 "items", line);
4248 return -1;
4249 }
4250
4251 idx = config->num_sec_device_types;
4252
4253 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4254 return -1;
4255
4256 config->num_sec_device_types++;
4257 return 0;
4258}
Dmitry Shmidt04949592012-07-19 12:16:46 -07004259
4260
4261static int wpa_config_process_p2p_pref_chan(
4262 const struct global_parse_data *data,
4263 struct wpa_config *config, int line, const char *pos)
4264{
4265 struct p2p_channel *pref = NULL, *n;
4266 unsigned int num = 0;
4267 const char *pos2;
4268 u8 op_class, chan;
4269
4270 /* format: class:chan,class:chan,... */
4271
4272 while (*pos) {
4273 op_class = atoi(pos);
4274 pos2 = os_strchr(pos, ':');
4275 if (pos2 == NULL)
4276 goto fail;
4277 pos2++;
4278 chan = atoi(pos2);
4279
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004280 n = os_realloc_array(pref, num + 1,
4281 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 if (n == NULL)
4283 goto fail;
4284 pref = n;
4285 pref[num].op_class = op_class;
4286 pref[num].chan = chan;
4287 num++;
4288
4289 pos = os_strchr(pos2, ',');
4290 if (pos == NULL)
4291 break;
4292 pos++;
4293 }
4294
4295 os_free(config->p2p_pref_chan);
4296 config->p2p_pref_chan = pref;
4297 config->num_p2p_pref_chan = num;
4298 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4299 (u8 *) config->p2p_pref_chan,
4300 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4301
4302 return 0;
4303
4304fail:
4305 os_free(pref);
4306 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4307 return -1;
4308}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004309
4310
4311static int wpa_config_process_p2p_no_go_freq(
4312 const struct global_parse_data *data,
4313 struct wpa_config *config, int line, const char *pos)
4314{
4315 int ret;
4316
4317 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4318 if (ret < 0) {
4319 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4320 return -1;
4321 }
4322
4323 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4324 config->p2p_no_go_freq.num);
4325
4326 return 0;
4327}
4328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329#endif /* CONFIG_P2P */
4330
4331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004332static int wpa_config_process_hessid(
4333 const struct global_parse_data *data,
4334 struct wpa_config *config, int line, const char *pos)
4335{
4336 if (hwaddr_aton2(pos, config->hessid) < 0) {
4337 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4338 line, pos);
4339 return -1;
4340 }
4341
4342 return 0;
4343}
4344
4345
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004346static int wpa_config_process_sae_groups(
4347 const struct global_parse_data *data,
4348 struct wpa_config *config, int line, const char *pos)
4349{
4350 int *groups = wpa_config_parse_int_array(pos);
4351 if (groups == NULL) {
4352 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4353 line, pos);
4354 return -1;
4355 }
4356
4357 os_free(config->sae_groups);
4358 config->sae_groups = groups;
4359
4360 return 0;
4361}
4362
4363
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004364static int wpa_config_process_ap_vendor_elements(
4365 const struct global_parse_data *data,
4366 struct wpa_config *config, int line, const char *pos)
4367{
4368 struct wpabuf *tmp;
4369 int len = os_strlen(pos) / 2;
4370 u8 *p;
4371
4372 if (!len) {
4373 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4374 line);
4375 return -1;
4376 }
4377
4378 tmp = wpabuf_alloc(len);
4379 if (tmp) {
4380 p = wpabuf_put(tmp, len);
4381
4382 if (hexstr2bin(pos, p, len)) {
4383 wpa_printf(MSG_ERROR, "Line %d: invalid "
4384 "ap_vendor_elements", line);
4385 wpabuf_free(tmp);
4386 return -1;
4387 }
4388
4389 wpabuf_free(config->ap_vendor_elements);
4390 config->ap_vendor_elements = tmp;
4391 } else {
4392 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4393 "ap_vendor_elements");
4394 return -1;
4395 }
4396
4397 return 0;
4398}
4399
4400
Dmitry Shmidt56052862013-10-04 10:23:25 -07004401#ifdef CONFIG_CTRL_IFACE
4402static int wpa_config_process_no_ctrl_interface(
4403 const struct global_parse_data *data,
4404 struct wpa_config *config, int line, const char *pos)
4405{
4406 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4407 os_free(config->ctrl_interface);
4408 config->ctrl_interface = NULL;
4409 return 0;
4410}
4411#endif /* CONFIG_CTRL_IFACE */
4412
4413
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004414static int wpa_config_get_int(const char *name, struct wpa_config *config,
4415 long offset, char *buf, size_t buflen,
4416 int pretty_print)
4417{
4418 int *val = (int *) (((u8 *) config) + (long) offset);
4419
4420 if (pretty_print)
4421 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4422 return os_snprintf(buf, buflen, "%d", *val);
4423}
4424
4425
4426static int wpa_config_get_str(const char *name, struct wpa_config *config,
4427 long offset, char *buf, size_t buflen,
4428 int pretty_print)
4429{
4430 char **val = (char **) (((u8 *) config) + (long) offset);
4431 int res;
4432
4433 if (pretty_print)
4434 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4435 *val ? *val : "null");
4436 else if (!*val)
4437 return -1;
4438 else
4439 res = os_snprintf(buf, buflen, "%s", *val);
4440 if (os_snprintf_error(buflen, res))
4441 res = -1;
4442
4443 return res;
4444}
4445
4446
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004447#ifdef CONFIG_P2P
4448static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4449 long offset, char *buf, size_t buflen,
4450 int pretty_print)
4451{
4452 void *val = ((u8 *) config) + (long) offset;
4453 int res;
4454 char addr[INET_ADDRSTRLEN];
4455
4456 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4457 return -1;
4458
4459 if (pretty_print)
4460 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4461 else
4462 res = os_snprintf(buf, buflen, "%s", addr);
4463
4464 if (os_snprintf_error(buflen, res))
4465 res = -1;
4466
4467 return res;
4468}
4469#endif /* CONFIG_P2P */
4470
4471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472#ifdef OFFSET
4473#undef OFFSET
4474#endif /* OFFSET */
4475/* OFFSET: Get offset of a variable within the wpa_config structure */
4476#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4477
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004478#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4479#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4480#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481#define INT(f) _INT(f), NULL, NULL
4482#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004483#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484#define STR(f) _STR(f), NULL, NULL
4485#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004486#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004487#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4488 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489
4490static const struct global_parse_data global_fields[] = {
4491#ifdef CONFIG_CTRL_IFACE
4492 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004493 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 { STR(ctrl_interface_group), 0 } /* deprecated */,
4495#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004496#ifdef CONFIG_MACSEC
4497 { INT_RANGE(eapol_version, 1, 3), 0 },
4498#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004500#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004502 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004503#ifdef CONFIG_MESH
4504 { INT(user_mpm), 0 },
4505 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004506 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004507 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004508#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004509 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 { INT(fast_reauth), 0 },
4511 { STR(opensc_engine_path), 0 },
4512 { STR(pkcs11_engine_path), 0 },
4513 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004514 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004515 { STR(pcsc_reader), 0 },
4516 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004517 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 { STR(driver_param), 0 },
4519 { INT(dot11RSNAConfigPMKLifetime), 0 },
4520 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4521 { INT(dot11RSNAConfigSATimeout), 0 },
4522#ifndef CONFIG_NO_CONFIG_WRITE
4523 { INT(update_config), 0 },
4524#endif /* CONFIG_NO_CONFIG_WRITE */
4525 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4526#ifdef CONFIG_WPS
4527 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004528 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004529 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4530 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4532 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4533 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4534 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4535 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4536 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4537 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4538 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004539 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540#endif /* CONFIG_WPS */
4541#ifdef CONFIG_P2P
4542 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004543 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4544 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004545 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4546 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4548 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4549 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4550 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4551 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004552 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004553 { INT_RANGE(p2p_passphrase_len, 8, 63),
4554 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004555 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004556 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4557 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004558 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004559 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004560 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004561 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004562 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004563 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004564 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004565 { IPV4(ip_addr_go), 0 },
4566 { IPV4(ip_addr_mask), 0 },
4567 { IPV4(ip_addr_start), 0 },
4568 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004569 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570#endif /* CONFIG_P2P */
4571 { FUNC(country), CFG_CHANGED_COUNTRY },
4572 { INT(bss_max_count), 0 },
4573 { INT(bss_expiration_age), 0 },
4574 { INT(bss_expiration_scan_count), 0 },
4575 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004576 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004578 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004579#ifdef CONFIG_HS20
4580 { INT_RANGE(hs20, 0, 1), 0 },
4581#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004582 { INT_RANGE(interworking, 0, 1), 0 },
4583 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004584 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004585 { INT_RANGE(go_interworking, 0, 1), 0 },
4586 { INT_RANGE(go_access_network_type, 0, 15), 0 },
4587 { INT_RANGE(go_internet, 0, 1), 0 },
4588 { INT_RANGE(go_venue_group, 0, 255), 0 },
4589 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004590 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4591 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004592 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4593 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4594 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4595 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4596 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004597 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4598 { INT(p2p_go_max_inactivity), 0 },
4599 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004600 { INT(okc), 0 },
4601 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004602 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004603 { INT(dtim_period), 0 },
4604 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004605 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004606 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004607 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004608 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004609 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004610 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004611 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004612 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004613 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004614 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004615 { INT(mac_addr), 0 },
4616 { INT(rand_addr_lifetime), 0 },
4617 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004618 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004619 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004620 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004621 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004622#ifdef CONFIG_FST
4623 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4624 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4625 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4626#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08004627 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004628 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004629 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004630#ifdef CONFIG_MBO
4631 { STR(non_pref_chan), 0 },
4632 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
4633 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004634 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
4635 { INT_RANGE(oce, 0, 3), 0 },
4636#endif /* CONFIG_MBO */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004637 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07004638 { INT_RANGE(ftm_responder, 0, 1), 0 },
4639 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004640 { INT(gas_rand_addr_lifetime), 0 },
4641 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004642 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643};
4644
4645#undef FUNC
4646#undef _INT
4647#undef INT
4648#undef INT_RANGE
4649#undef _STR
4650#undef STR
4651#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004652#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004653#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004654#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655
4656
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004657int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4658{
4659 int result = 0;
4660 size_t i;
4661
4662 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4663 const struct global_parse_data *field = &global_fields[i];
4664 int tmp;
4665
4666 if (!field->get)
4667 continue;
4668
4669 tmp = field->get(field->name, config, (long) field->param1,
4670 buf, buflen, 1);
4671 if (tmp < 0)
4672 return -1;
4673 buf += tmp;
4674 buflen -= tmp;
4675 result += tmp;
4676 }
4677 return result;
4678}
4679
4680
4681int wpa_config_get_value(const char *name, struct wpa_config *config,
4682 char *buf, size_t buflen)
4683{
4684 size_t i;
4685
4686 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4687 const struct global_parse_data *field = &global_fields[i];
4688
4689 if (os_strcmp(name, field->name) != 0)
4690 continue;
4691 if (!field->get)
4692 break;
4693 return field->get(name, config, (long) field->param1,
4694 buf, buflen, 0);
4695 }
4696
4697 return -1;
4698}
4699
4700
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004701int wpa_config_get_num_global_field_names(void)
4702{
4703 return NUM_GLOBAL_FIELDS;
4704}
4705
4706
4707const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4708{
4709 if (i >= NUM_GLOBAL_FIELDS)
4710 return NULL;
4711
4712 if (no_var)
4713 *no_var = !global_fields[i].param1;
4714 return global_fields[i].name;
4715}
4716
4717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4719{
4720 size_t i;
4721 int ret = 0;
4722
4723 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4724 const struct global_parse_data *field = &global_fields[i];
4725 size_t flen = os_strlen(field->name);
4726 if (os_strncmp(pos, field->name, flen) != 0 ||
4727 pos[flen] != '=')
4728 continue;
4729
4730 if (field->parser(field, config, line, pos + flen + 1)) {
4731 wpa_printf(MSG_ERROR, "Line %d: failed to "
4732 "parse '%s'.", line, pos);
4733 ret = -1;
4734 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004735 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4736 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 config->changed_parameters |= field->changed_flag;
4738 break;
4739 }
4740 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004741#ifdef CONFIG_AP
4742 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4743 char *tmp = os_strchr(pos, '=');
4744 if (tmp == NULL) {
4745 if (line < 0)
4746 return -1;
4747 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4748 "'%s'", line, pos);
4749 return -1;
4750 }
4751 *tmp++ = '\0';
4752 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4753 tmp)) {
4754 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4755 "AC item", line);
4756 return -1;
4757 }
4758 }
4759#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 if (line < 0)
4761 return -1;
4762 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4763 line, pos);
4764 ret = -1;
4765 }
4766
4767 return ret;
4768}