blob: a0b64b22a236d54907cfb7f948fc2916408e9dfa [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/uuid.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/sha1.h"
15#include "rsn_supp/wpa.h"
16#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080018#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "config.h"
20
21
22#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
23#define NO_CONFIG_WRITE
24#endif
25
26/*
27 * Structure for network configuration parsing. This data is used to implement
28 * a generic parser for each network block variable. The table of configuration
29 * variables is defined below in this file (ssid_fields[]).
30 */
31struct parse_data {
32 /* Configuration variable name */
33 char *name;
34
Dmitry Shmidte4663042016-04-04 10:07:49 -070035 /* Parser function for this variable. The parser functions return 0 or 1
36 * to indicate success. Value 0 indicates that the parameter value may
37 * have changed while value 1 means that the value did not change.
38 * Error cases (failure to parse the string) are indicated by returning
39 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
41 int line, const char *value);
42
43#ifndef NO_CONFIG_WRITE
44 /* Writer function (i.e., to get the variable in text format from
45 * internal presentation). */
46 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
47#endif /* NO_CONFIG_WRITE */
48
49 /* Variable specific parameters for the parser. */
50 void *param1, *param2, *param3, *param4;
51
52 /* 0 = this variable can be included in debug output and ctrl_iface
53 * 1 = this variable contains key/private data and it must not be
54 * included in debug output unless explicitly requested. In
55 * addition, this variable will not be readable through the
56 * ctrl_iface.
57 */
58 int key_data;
59};
60
61
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062static int wpa_config_parse_str(const struct parse_data *data,
63 struct wpa_ssid *ssid,
64 int line, const char *value)
65{
Dmitry Shmidte4663042016-04-04 10:07:49 -070066 size_t res_len, *dst_len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067 char **dst, *tmp;
68
69 if (os_strcmp(value, "NULL") == 0) {
70 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
71 data->name);
72 tmp = NULL;
73 res_len = 0;
74 goto set;
75 }
76
77 tmp = wpa_config_parse_string(value, &res_len);
78 if (tmp == NULL) {
79 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
80 line, data->name,
81 data->key_data ? "[KEY DATA REMOVED]" : value);
82 return -1;
83 }
84
85 if (data->key_data) {
86 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
87 (u8 *) tmp, res_len);
88 } else {
89 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
90 (u8 *) tmp, res_len);
91 }
92
93 if (data->param3 && res_len < (size_t) data->param3) {
94 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
95 "min_len=%ld)", line, data->name,
96 (unsigned long) res_len, (long) data->param3);
97 os_free(tmp);
98 return -1;
99 }
100
101 if (data->param4 && res_len > (size_t) data->param4) {
102 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
103 "max_len=%ld)", line, data->name,
104 (unsigned long) res_len, (long) data->param4);
105 os_free(tmp);
106 return -1;
107 }
108
109set:
110 dst = (char **) (((u8 *) ssid) + (long) data->param1);
111 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700112
113 if (data->param2)
114 prev_len = *dst_len;
115 else if (*dst)
116 prev_len = os_strlen(*dst);
117 else
118 prev_len = 0;
119 if ((*dst == NULL && tmp == NULL) ||
120 (*dst && tmp && prev_len == res_len &&
121 os_memcmp(*dst, tmp, res_len) == 0)) {
122 /* No change to the previously configured value */
123 os_free(tmp);
124 return 1;
125 }
126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 os_free(*dst);
128 *dst = tmp;
129 if (data->param2)
130 *dst_len = res_len;
131
132 return 0;
133}
134
135
136#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
138{
139 char *buf;
140
141 buf = os_malloc(len + 3);
142 if (buf == NULL)
143 return NULL;
144 buf[0] = '"';
145 os_memcpy(buf + 1, value, len);
146 buf[len + 1] = '"';
147 buf[len + 2] = '\0';
148
149 return buf;
150}
151
152
153static char * wpa_config_write_string_hex(const u8 *value, size_t len)
154{
155 char *buf;
156
157 buf = os_zalloc(2 * len + 1);
158 if (buf == NULL)
159 return NULL;
160 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
161
162 return buf;
163}
164
165
166static char * wpa_config_write_string(const u8 *value, size_t len)
167{
168 if (value == NULL)
169 return NULL;
170
171 if (is_hex(value, len))
172 return wpa_config_write_string_hex(value, len);
173 else
174 return wpa_config_write_string_ascii(value, len);
175}
176
177
178static char * wpa_config_write_str(const struct parse_data *data,
179 struct wpa_ssid *ssid)
180{
181 size_t len;
182 char **src;
183
184 src = (char **) (((u8 *) ssid) + (long) data->param1);
185 if (*src == NULL)
186 return NULL;
187
188 if (data->param2)
189 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
190 else
191 len = os_strlen(*src);
192
193 return wpa_config_write_string((const u8 *) *src, len);
194}
195#endif /* NO_CONFIG_WRITE */
196
197
198static int wpa_config_parse_int(const struct parse_data *data,
199 struct wpa_ssid *ssid,
200 int line, const char *value)
201{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700202 int val, *dst;
203 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204
205 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700206 val = strtol(value, &end, 0);
207 if (*end) {
208 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
209 line, value);
210 return -1;
211 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700212
213 if (*dst == val)
214 return 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700215 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
217
218 if (data->param3 && *dst < (long) data->param3) {
219 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
220 "min_value=%ld)", line, data->name, *dst,
221 (long) data->param3);
222 *dst = (long) data->param3;
223 return -1;
224 }
225
226 if (data->param4 && *dst > (long) data->param4) {
227 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
228 "max_value=%ld)", line, data->name, *dst,
229 (long) data->param4);
230 *dst = (long) data->param4;
231 return -1;
232 }
233
234 return 0;
235}
236
237
238#ifndef NO_CONFIG_WRITE
239static char * wpa_config_write_int(const struct parse_data *data,
240 struct wpa_ssid *ssid)
241{
242 int *src, res;
243 char *value;
244
245 src = (int *) (((u8 *) ssid) + (long) data->param1);
246
247 value = os_malloc(20);
248 if (value == NULL)
249 return NULL;
250 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800251 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 os_free(value);
253 return NULL;
254 }
255 value[20 - 1] = '\0';
256 return value;
257}
258#endif /* NO_CONFIG_WRITE */
259
260
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800261static int wpa_config_parse_addr_list(const struct parse_data *data,
262 int line, const char *value,
263 u8 **list, size_t *num, char *name,
264 u8 abort_on_error, u8 masked)
265{
266 const char *pos;
267 u8 *buf, *n, addr[2 * ETH_ALEN];
268 size_t count;
269
270 buf = NULL;
271 count = 0;
272
273 pos = value;
274 while (pos && *pos) {
275 while (*pos == ' ')
276 pos++;
277
278 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
279 if (abort_on_error || count == 0) {
280 wpa_printf(MSG_ERROR,
281 "Line %d: Invalid %s address '%s'",
282 line, name, value);
283 os_free(buf);
284 return -1;
285 }
286 /* continue anyway since this could have been from a
287 * truncated configuration file line */
288 wpa_printf(MSG_INFO,
289 "Line %d: Ignore likely truncated %s address '%s'",
290 line, name, pos);
291 } else {
292 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
293 if (n == NULL) {
294 os_free(buf);
295 return -1;
296 }
297 buf = n;
298 os_memmove(buf + 2 * ETH_ALEN, buf,
299 count * 2 * ETH_ALEN);
300 os_memcpy(buf, addr, 2 * ETH_ALEN);
301 count++;
302 wpa_printf(MSG_MSGDUMP,
303 "%s: addr=" MACSTR " mask=" MACSTR,
304 name, MAC2STR(addr),
305 MAC2STR(&addr[ETH_ALEN]));
306 }
307
308 pos = os_strchr(pos, ' ');
309 }
310
311 os_free(*list);
312 *list = buf;
313 *num = count;
314
315 return 0;
316}
317
318
319#ifndef NO_CONFIG_WRITE
320static char * wpa_config_write_addr_list(const struct parse_data *data,
321 const u8 *list, size_t num, char *name)
322{
323 char *value, *end, *pos;
324 int res;
325 size_t i;
326
327 if (list == NULL || num == 0)
328 return NULL;
329
330 value = os_malloc(2 * 20 * num);
331 if (value == NULL)
332 return NULL;
333 pos = value;
334 end = value + 2 * 20 * num;
335
336 for (i = num; i > 0; i--) {
337 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
338 const u8 *m = a + ETH_ALEN;
339
340 if (i < num)
341 *pos++ = ' ';
342 res = hwaddr_mask_txt(pos, end - pos, a, m);
343 if (res < 0) {
344 os_free(value);
345 return NULL;
346 }
347 pos += res;
348 }
349
350 return value;
351}
352#endif /* NO_CONFIG_WRITE */
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354static int wpa_config_parse_bssid(const struct parse_data *data,
355 struct wpa_ssid *ssid, int line,
356 const char *value)
357{
358 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
359 os_strcmp(value, "any") == 0) {
360 ssid->bssid_set = 0;
361 wpa_printf(MSG_MSGDUMP, "BSSID any");
362 return 0;
363 }
364 if (hwaddr_aton(value, ssid->bssid)) {
365 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
366 line, value);
367 return -1;
368 }
369 ssid->bssid_set = 1;
370 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
371 return 0;
372}
373
374
375#ifndef NO_CONFIG_WRITE
376static char * wpa_config_write_bssid(const struct parse_data *data,
377 struct wpa_ssid *ssid)
378{
379 char *value;
380 int res;
381
382 if (!ssid->bssid_set)
383 return NULL;
384
385 value = os_malloc(20);
386 if (value == NULL)
387 return NULL;
388 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800389 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 os_free(value);
391 return NULL;
392 }
393 value[20 - 1] = '\0';
394 return value;
395}
396#endif /* NO_CONFIG_WRITE */
397
398
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800399static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
400 struct wpa_ssid *ssid, int line,
401 const char *value)
402{
403 return wpa_config_parse_addr_list(data, line, value,
404 &ssid->bssid_blacklist,
405 &ssid->num_bssid_blacklist,
406 "bssid_blacklist", 1, 1);
407}
408
409
410#ifndef NO_CONFIG_WRITE
411static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
412 struct wpa_ssid *ssid)
413{
414 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
415 ssid->num_bssid_blacklist,
416 "bssid_blacklist");
417}
418#endif /* NO_CONFIG_WRITE */
419
420
421static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
422 struct wpa_ssid *ssid, int line,
423 const char *value)
424{
425 return wpa_config_parse_addr_list(data, line, value,
426 &ssid->bssid_whitelist,
427 &ssid->num_bssid_whitelist,
428 "bssid_whitelist", 1, 1);
429}
430
431
432#ifndef NO_CONFIG_WRITE
433static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
434 struct wpa_ssid *ssid)
435{
436 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
437 ssid->num_bssid_whitelist,
438 "bssid_whitelist");
439}
440#endif /* NO_CONFIG_WRITE */
441
442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443static int wpa_config_parse_psk(const struct parse_data *data,
444 struct wpa_ssid *ssid, int line,
445 const char *value)
446{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700447#ifdef CONFIG_EXT_PASSWORD
448 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700449 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700450 ssid->passphrase = NULL;
451 ssid->psk_set = 0;
452 os_free(ssid->ext_psk);
453 ssid->ext_psk = os_strdup(value + 4);
454 if (ssid->ext_psk == NULL)
455 return -1;
456 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
457 ssid->ext_psk);
458 return 0;
459 }
460#endif /* CONFIG_EXT_PASSWORD */
461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 if (*value == '"') {
463#ifndef CONFIG_NO_PBKDF2
464 const char *pos;
465 size_t len;
466
467 value++;
468 pos = os_strrchr(value, '"');
469 if (pos)
470 len = pos - value;
471 else
472 len = os_strlen(value);
473 if (len < 8 || len > 63) {
474 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
475 "length %lu (expected: 8..63) '%s'.",
476 line, (unsigned long) len, value);
477 return -1;
478 }
479 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
480 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700481 if (has_ctrl_char((u8 *) value, len)) {
482 wpa_printf(MSG_ERROR,
483 "Line %d: Invalid passphrase character",
484 line);
485 return -1;
486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700488 os_memcmp(ssid->passphrase, value, len) == 0) {
489 /* No change to the previously configured value */
490 return 1;
491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700493 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700494 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 if (ssid->passphrase == NULL)
496 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 return 0;
498#else /* CONFIG_NO_PBKDF2 */
499 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
500 "supported.", line);
501 return -1;
502#endif /* CONFIG_NO_PBKDF2 */
503 }
504
505 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
506 value[PMK_LEN * 2] != '\0') {
507 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
508 line, value);
509 return -1;
510 }
511
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700512 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 ssid->passphrase = NULL;
514
515 ssid->psk_set = 1;
516 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
517 return 0;
518}
519
520
521#ifndef NO_CONFIG_WRITE
522static char * wpa_config_write_psk(const struct parse_data *data,
523 struct wpa_ssid *ssid)
524{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700525#ifdef CONFIG_EXT_PASSWORD
526 if (ssid->ext_psk) {
527 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
528 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800529 int res;
530
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700531 if (buf == NULL)
532 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
534 if (os_snprintf_error(len, res)) {
535 os_free(buf);
536 buf = NULL;
537 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700538 return buf;
539 }
540#endif /* CONFIG_EXT_PASSWORD */
541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 if (ssid->passphrase)
543 return wpa_config_write_string_ascii(
544 (const u8 *) ssid->passphrase,
545 os_strlen(ssid->passphrase));
546
547 if (ssid->psk_set)
548 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
549
550 return NULL;
551}
552#endif /* NO_CONFIG_WRITE */
553
554
555static int wpa_config_parse_proto(const struct parse_data *data,
556 struct wpa_ssid *ssid, int line,
557 const char *value)
558{
559 int val = 0, last, errors = 0;
560 char *start, *end, *buf;
561
562 buf = os_strdup(value);
563 if (buf == NULL)
564 return -1;
565 start = buf;
566
567 while (*start != '\0') {
568 while (*start == ' ' || *start == '\t')
569 start++;
570 if (*start == '\0')
571 break;
572 end = start;
573 while (*end != ' ' && *end != '\t' && *end != '\0')
574 end++;
575 last = *end == '\0';
576 *end = '\0';
577 if (os_strcmp(start, "WPA") == 0)
578 val |= WPA_PROTO_WPA;
579 else if (os_strcmp(start, "RSN") == 0 ||
580 os_strcmp(start, "WPA2") == 0)
581 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800582 else if (os_strcmp(start, "OSEN") == 0)
583 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 else {
585 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
586 line, start);
587 errors++;
588 }
589
590 if (last)
591 break;
592 start = end + 1;
593 }
594 os_free(buf);
595
596 if (val == 0) {
597 wpa_printf(MSG_ERROR,
598 "Line %d: no proto values configured.", line);
599 errors++;
600 }
601
Dmitry Shmidte4663042016-04-04 10:07:49 -0700602 if (!errors && ssid->proto == val)
603 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
605 ssid->proto = val;
606 return errors ? -1 : 0;
607}
608
609
610#ifndef NO_CONFIG_WRITE
611static char * wpa_config_write_proto(const struct parse_data *data,
612 struct wpa_ssid *ssid)
613{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700614 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 char *buf, *pos, *end;
616
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800617 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 if (buf == NULL)
619 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800620 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621
622 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700623 ret = os_snprintf(pos, end - pos, "%sWPA",
624 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800625 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 return buf;
627 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 }
629
630 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700631 ret = os_snprintf(pos, end - pos, "%sRSN",
632 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800633 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 return buf;
635 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 }
637
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800638 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700639 ret = os_snprintf(pos, end - pos, "%sOSEN",
640 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800641 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800642 return buf;
643 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700644 }
645
646 if (pos == buf) {
647 os_free(buf);
648 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800649 }
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 return buf;
652}
653#endif /* NO_CONFIG_WRITE */
654
655
656static int wpa_config_parse_key_mgmt(const struct parse_data *data,
657 struct wpa_ssid *ssid, int line,
658 const char *value)
659{
660 int val = 0, last, errors = 0;
661 char *start, *end, *buf;
662
663 buf = os_strdup(value);
664 if (buf == NULL)
665 return -1;
666 start = buf;
667
668 while (*start != '\0') {
669 while (*start == ' ' || *start == '\t')
670 start++;
671 if (*start == '\0')
672 break;
673 end = start;
674 while (*end != ' ' && *end != '\t' && *end != '\0')
675 end++;
676 last = *end == '\0';
677 *end = '\0';
678 if (os_strcmp(start, "WPA-PSK") == 0)
679 val |= WPA_KEY_MGMT_PSK;
680 else if (os_strcmp(start, "WPA-EAP") == 0)
681 val |= WPA_KEY_MGMT_IEEE8021X;
682 else if (os_strcmp(start, "IEEE8021X") == 0)
683 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
684 else if (os_strcmp(start, "NONE") == 0)
685 val |= WPA_KEY_MGMT_NONE;
686 else if (os_strcmp(start, "WPA-NONE") == 0)
687 val |= WPA_KEY_MGMT_WPA_NONE;
688#ifdef CONFIG_IEEE80211R
689 else if (os_strcmp(start, "FT-PSK") == 0)
690 val |= WPA_KEY_MGMT_FT_PSK;
691 else if (os_strcmp(start, "FT-EAP") == 0)
692 val |= WPA_KEY_MGMT_FT_IEEE8021X;
693#endif /* CONFIG_IEEE80211R */
694#ifdef CONFIG_IEEE80211W
695 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
696 val |= WPA_KEY_MGMT_PSK_SHA256;
697 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
698 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
699#endif /* CONFIG_IEEE80211W */
700#ifdef CONFIG_WPS
701 else if (os_strcmp(start, "WPS") == 0)
702 val |= WPA_KEY_MGMT_WPS;
703#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800704#ifdef CONFIG_SAE
705 else if (os_strcmp(start, "SAE") == 0)
706 val |= WPA_KEY_MGMT_SAE;
707 else if (os_strcmp(start, "FT-SAE") == 0)
708 val |= WPA_KEY_MGMT_FT_SAE;
709#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800710#ifdef CONFIG_HS20
711 else if (os_strcmp(start, "OSEN") == 0)
712 val |= WPA_KEY_MGMT_OSEN;
713#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800714#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800715 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
716 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800717#endif /* CONFIG_SUITEB */
718#ifdef CONFIG_SUITEB192
719 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
720 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
721#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800722#ifdef CONFIG_FILS
723 else if (os_strcmp(start, "FILS-SHA256") == 0)
724 val |= WPA_KEY_MGMT_FILS_SHA256;
725 else if (os_strcmp(start, "FILS-SHA384") == 0)
726 val |= WPA_KEY_MGMT_FILS_SHA384;
727#ifdef CONFIG_IEEE80211R
728 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
729 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
730 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
731 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
732#endif /* CONFIG_IEEE80211R */
733#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 else {
735 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
736 line, start);
737 errors++;
738 }
739
740 if (last)
741 break;
742 start = end + 1;
743 }
744 os_free(buf);
745
746 if (val == 0) {
747 wpa_printf(MSG_ERROR,
748 "Line %d: no key_mgmt values configured.", line);
749 errors++;
750 }
751
Dmitry Shmidte4663042016-04-04 10:07:49 -0700752 if (!errors && ssid->key_mgmt == val)
753 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
755 ssid->key_mgmt = val;
756 return errors ? -1 : 0;
757}
758
759
760#ifndef NO_CONFIG_WRITE
761static char * wpa_config_write_key_mgmt(const struct parse_data *data,
762 struct wpa_ssid *ssid)
763{
764 char *buf, *pos, *end;
765 int ret;
766
Dmitry Shmidt96571392013-10-14 12:54:46 -0700767 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 if (buf == NULL)
769 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700770 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771
772 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
773 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
774 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800775 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 end[-1] = '\0';
777 return buf;
778 }
779 pos += ret;
780 }
781
782 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
783 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
784 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800785 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 end[-1] = '\0';
787 return buf;
788 }
789 pos += ret;
790 }
791
792 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
793 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
794 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800795 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 end[-1] = '\0';
797 return buf;
798 }
799 pos += ret;
800 }
801
802 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
803 ret = os_snprintf(pos, end - pos, "%sNONE",
804 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800805 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 end[-1] = '\0';
807 return buf;
808 }
809 pos += ret;
810 }
811
812 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
813 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
814 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800815 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 end[-1] = '\0';
817 return buf;
818 }
819 pos += ret;
820 }
821
822#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700823 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
824 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
825 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800826 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700827 end[-1] = '\0';
828 return buf;
829 }
830 pos += ret;
831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832
Dmitry Shmidt96571392013-10-14 12:54:46 -0700833 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
834 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
835 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800836 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700837 end[-1] = '\0';
838 return buf;
839 }
840 pos += ret;
841 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842#endif /* CONFIG_IEEE80211R */
843
844#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700845 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
846 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
847 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800848 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700849 end[-1] = '\0';
850 return buf;
851 }
852 pos += ret;
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854
Dmitry Shmidt96571392013-10-14 12:54:46 -0700855 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
856 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
857 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700859 end[-1] = '\0';
860 return buf;
861 }
862 pos += ret;
863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864#endif /* CONFIG_IEEE80211W */
865
866#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700867 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
868 ret = os_snprintf(pos, end - pos, "%sWPS",
869 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700871 end[-1] = '\0';
872 return buf;
873 }
874 pos += ret;
875 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876#endif /* CONFIG_WPS */
877
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800878#ifdef CONFIG_SAE
879 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
880 ret = os_snprintf(pos, end - pos, "%sSAE",
881 pos == buf ? "" : " ");
882 if (os_snprintf_error(end - pos, ret)) {
883 end[-1] = '\0';
884 return buf;
885 }
886 pos += ret;
887 }
888
889 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
890 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
891 pos == buf ? "" : " ");
892 if (os_snprintf_error(end - pos, ret)) {
893 end[-1] = '\0';
894 return buf;
895 }
896 pos += ret;
897 }
898#endif /* CONFIG_SAE */
899
900#ifdef CONFIG_HS20
901 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
902 ret = os_snprintf(pos, end - pos, "%sOSEN",
903 pos == buf ? "" : " ");
904 if (os_snprintf_error(end - pos, ret)) {
905 end[-1] = '\0';
906 return buf;
907 }
908 pos += ret;
909 }
910#endif /* CONFIG_HS20 */
911
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800912#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800913 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
914 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
915 pos == buf ? "" : " ");
916 if (os_snprintf_error(end - pos, ret)) {
917 end[-1] = '\0';
918 return buf;
919 }
920 pos += ret;
921 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800922#endif /* CONFIG_SUITEB */
923
924#ifdef CONFIG_SUITEB192
925 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
926 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
927 pos == buf ? "" : " ");
928 if (os_snprintf_error(end - pos, ret)) {
929 end[-1] = '\0';
930 return buf;
931 }
932 pos += ret;
933 }
934#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800935
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700936 if (pos == buf) {
937 os_free(buf);
938 buf = NULL;
939 }
940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return buf;
942}
943#endif /* NO_CONFIG_WRITE */
944
945
946static int wpa_config_parse_cipher(int line, const char *value)
947{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800948#ifdef CONFIG_NO_WPA
949 return -1;
950#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800951 int val = wpa_parse_cipher(value);
952 if (val < 0) {
953 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
954 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 if (val == 0) {
958 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
959 line);
960 return -1;
961 }
962 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800963#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964}
965
966
967#ifndef NO_CONFIG_WRITE
968static char * wpa_config_write_cipher(int cipher)
969{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800970#ifdef CONFIG_NO_WPA
971 return NULL;
972#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800973 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 if (buf == NULL)
975 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800977 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
978 os_free(buf);
979 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 }
981
982 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800983#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984}
985#endif /* NO_CONFIG_WRITE */
986
987
988static int wpa_config_parse_pairwise(const struct parse_data *data,
989 struct wpa_ssid *ssid, int line,
990 const char *value)
991{
992 int val;
993 val = wpa_config_parse_cipher(line, value);
994 if (val == -1)
995 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800996 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
998 "(0x%x).", line, val);
999 return -1;
1000 }
1001
Dmitry Shmidte4663042016-04-04 10:07:49 -07001002 if (ssid->pairwise_cipher == val)
1003 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1005 ssid->pairwise_cipher = val;
1006 return 0;
1007}
1008
1009
1010#ifndef NO_CONFIG_WRITE
1011static char * wpa_config_write_pairwise(const struct parse_data *data,
1012 struct wpa_ssid *ssid)
1013{
1014 return wpa_config_write_cipher(ssid->pairwise_cipher);
1015}
1016#endif /* NO_CONFIG_WRITE */
1017
1018
1019static int wpa_config_parse_group(const struct parse_data *data,
1020 struct wpa_ssid *ssid, int line,
1021 const char *value)
1022{
1023 int val;
1024 val = wpa_config_parse_cipher(line, value);
1025 if (val == -1)
1026 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001027
1028 /*
1029 * Backwards compatibility - filter out WEP ciphers that were previously
1030 * allowed.
1031 */
1032 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1033
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001034 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1036 "(0x%x).", line, val);
1037 return -1;
1038 }
1039
Dmitry Shmidte4663042016-04-04 10:07:49 -07001040 if (ssid->group_cipher == val)
1041 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1043 ssid->group_cipher = val;
1044 return 0;
1045}
1046
1047
1048#ifndef NO_CONFIG_WRITE
1049static char * wpa_config_write_group(const struct parse_data *data,
1050 struct wpa_ssid *ssid)
1051{
1052 return wpa_config_write_cipher(ssid->group_cipher);
1053}
1054#endif /* NO_CONFIG_WRITE */
1055
1056
1057static int wpa_config_parse_auth_alg(const struct parse_data *data,
1058 struct wpa_ssid *ssid, int line,
1059 const char *value)
1060{
1061 int val = 0, last, errors = 0;
1062 char *start, *end, *buf;
1063
1064 buf = os_strdup(value);
1065 if (buf == NULL)
1066 return -1;
1067 start = buf;
1068
1069 while (*start != '\0') {
1070 while (*start == ' ' || *start == '\t')
1071 start++;
1072 if (*start == '\0')
1073 break;
1074 end = start;
1075 while (*end != ' ' && *end != '\t' && *end != '\0')
1076 end++;
1077 last = *end == '\0';
1078 *end = '\0';
1079 if (os_strcmp(start, "OPEN") == 0)
1080 val |= WPA_AUTH_ALG_OPEN;
1081 else if (os_strcmp(start, "SHARED") == 0)
1082 val |= WPA_AUTH_ALG_SHARED;
1083 else if (os_strcmp(start, "LEAP") == 0)
1084 val |= WPA_AUTH_ALG_LEAP;
1085 else {
1086 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1087 line, start);
1088 errors++;
1089 }
1090
1091 if (last)
1092 break;
1093 start = end + 1;
1094 }
1095 os_free(buf);
1096
1097 if (val == 0) {
1098 wpa_printf(MSG_ERROR,
1099 "Line %d: no auth_alg values configured.", line);
1100 errors++;
1101 }
1102
Dmitry Shmidte4663042016-04-04 10:07:49 -07001103 if (!errors && ssid->auth_alg == val)
1104 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1106 ssid->auth_alg = val;
1107 return errors ? -1 : 0;
1108}
1109
1110
1111#ifndef NO_CONFIG_WRITE
1112static char * wpa_config_write_auth_alg(const struct parse_data *data,
1113 struct wpa_ssid *ssid)
1114{
1115 char *buf, *pos, *end;
1116 int ret;
1117
1118 pos = buf = os_zalloc(30);
1119 if (buf == NULL)
1120 return NULL;
1121 end = buf + 30;
1122
1123 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1124 ret = os_snprintf(pos, end - pos, "%sOPEN",
1125 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001126 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 end[-1] = '\0';
1128 return buf;
1129 }
1130 pos += ret;
1131 }
1132
1133 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1134 ret = os_snprintf(pos, end - pos, "%sSHARED",
1135 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001136 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 end[-1] = '\0';
1138 return buf;
1139 }
1140 pos += ret;
1141 }
1142
1143 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1144 ret = os_snprintf(pos, end - pos, "%sLEAP",
1145 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001146 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 end[-1] = '\0';
1148 return buf;
1149 }
1150 pos += ret;
1151 }
1152
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001153 if (pos == buf) {
1154 os_free(buf);
1155 buf = NULL;
1156 }
1157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 return buf;
1159}
1160#endif /* NO_CONFIG_WRITE */
1161
1162
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001163static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164{
1165 int *freqs;
1166 size_t used, len;
1167 const char *pos;
1168
1169 used = 0;
1170 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001171 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 if (freqs == NULL)
1173 return NULL;
1174
1175 pos = value;
1176 while (pos) {
1177 while (*pos == ' ')
1178 pos++;
1179 if (used == len) {
1180 int *n;
1181 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001182 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 if (n == NULL) {
1184 os_free(freqs);
1185 return NULL;
1186 }
1187 for (i = len; i <= len * 2; i++)
1188 n[i] = 0;
1189 freqs = n;
1190 len *= 2;
1191 }
1192
1193 freqs[used] = atoi(pos);
1194 if (freqs[used] == 0)
1195 break;
1196 used++;
1197 pos = os_strchr(pos + 1, ' ');
1198 }
1199
1200 return freqs;
1201}
1202
1203
1204static int wpa_config_parse_scan_freq(const struct parse_data *data,
1205 struct wpa_ssid *ssid, int line,
1206 const char *value)
1207{
1208 int *freqs;
1209
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001210 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 if (freqs == NULL)
1212 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001213 if (freqs[0] == 0) {
1214 os_free(freqs);
1215 freqs = NULL;
1216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 os_free(ssid->scan_freq);
1218 ssid->scan_freq = freqs;
1219
1220 return 0;
1221}
1222
1223
1224static int wpa_config_parse_freq_list(const struct parse_data *data,
1225 struct wpa_ssid *ssid, int line,
1226 const char *value)
1227{
1228 int *freqs;
1229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001230 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 if (freqs == NULL)
1232 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001233 if (freqs[0] == 0) {
1234 os_free(freqs);
1235 freqs = NULL;
1236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 os_free(ssid->freq_list);
1238 ssid->freq_list = freqs;
1239
1240 return 0;
1241}
1242
1243
1244#ifndef NO_CONFIG_WRITE
1245static char * wpa_config_write_freqs(const struct parse_data *data,
1246 const int *freqs)
1247{
1248 char *buf, *pos, *end;
1249 int i, ret;
1250 size_t count;
1251
1252 if (freqs == NULL)
1253 return NULL;
1254
1255 count = 0;
1256 for (i = 0; freqs[i]; i++)
1257 count++;
1258
1259 pos = buf = os_zalloc(10 * count + 1);
1260 if (buf == NULL)
1261 return NULL;
1262 end = buf + 10 * count + 1;
1263
1264 for (i = 0; freqs[i]; i++) {
1265 ret = os_snprintf(pos, end - pos, "%s%u",
1266 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001267 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 end[-1] = '\0';
1269 return buf;
1270 }
1271 pos += ret;
1272 }
1273
1274 return buf;
1275}
1276
1277
1278static char * wpa_config_write_scan_freq(const struct parse_data *data,
1279 struct wpa_ssid *ssid)
1280{
1281 return wpa_config_write_freqs(data, ssid->scan_freq);
1282}
1283
1284
1285static char * wpa_config_write_freq_list(const struct parse_data *data,
1286 struct wpa_ssid *ssid)
1287{
1288 return wpa_config_write_freqs(data, ssid->freq_list);
1289}
1290#endif /* NO_CONFIG_WRITE */
1291
1292
1293#ifdef IEEE8021X_EAPOL
1294static int wpa_config_parse_eap(const struct parse_data *data,
1295 struct wpa_ssid *ssid, int line,
1296 const char *value)
1297{
1298 int last, errors = 0;
1299 char *start, *end, *buf;
1300 struct eap_method_type *methods = NULL, *tmp;
1301 size_t num_methods = 0;
1302
1303 buf = os_strdup(value);
1304 if (buf == NULL)
1305 return -1;
1306 start = buf;
1307
1308 while (*start != '\0') {
1309 while (*start == ' ' || *start == '\t')
1310 start++;
1311 if (*start == '\0')
1312 break;
1313 end = start;
1314 while (*end != ' ' && *end != '\t' && *end != '\0')
1315 end++;
1316 last = *end == '\0';
1317 *end = '\0';
1318 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 methods = os_realloc_array(methods, num_methods + 1,
1320 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 if (methods == NULL) {
1322 os_free(tmp);
1323 os_free(buf);
1324 return -1;
1325 }
1326 methods[num_methods].method = eap_peer_get_type(
1327 start, &methods[num_methods].vendor);
1328 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1329 methods[num_methods].method == EAP_TYPE_NONE) {
1330 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1331 "'%s'", line, start);
1332 wpa_printf(MSG_ERROR, "You may need to add support for"
1333 " this EAP method during wpa_supplicant\n"
1334 "build time configuration.\n"
1335 "See README for more information.");
1336 errors++;
1337 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1338 methods[num_methods].method == EAP_TYPE_LEAP)
1339 ssid->leap++;
1340 else
1341 ssid->non_leap++;
1342 num_methods++;
1343 if (last)
1344 break;
1345 start = end + 1;
1346 }
1347 os_free(buf);
1348
1349 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001350 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 if (methods == NULL) {
1352 os_free(tmp);
1353 return -1;
1354 }
1355 methods[num_methods].vendor = EAP_VENDOR_IETF;
1356 methods[num_methods].method = EAP_TYPE_NONE;
1357 num_methods++;
1358
Dmitry Shmidte4663042016-04-04 10:07:49 -07001359 if (!errors && ssid->eap.eap_methods) {
1360 struct eap_method_type *prev_m;
1361 size_t i, j, prev_methods, match = 0;
1362
1363 prev_m = ssid->eap.eap_methods;
1364 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1365 prev_m[i].method != EAP_TYPE_NONE; i++) {
1366 /* Count the methods */
1367 }
1368 prev_methods = i + 1;
1369
1370 for (i = 0; prev_methods == num_methods && i < prev_methods;
1371 i++) {
1372 for (j = 0; j < num_methods; j++) {
1373 if (prev_m[i].vendor == methods[j].vendor &&
1374 prev_m[i].method == methods[j].method) {
1375 match++;
1376 break;
1377 }
1378 }
1379 }
1380 if (match == num_methods) {
1381 os_free(methods);
1382 return 1;
1383 }
1384 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1386 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001387 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 ssid->eap.eap_methods = methods;
1389 return errors ? -1 : 0;
1390}
1391
1392
Dmitry Shmidt83474442015-04-15 13:47:09 -07001393#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394static char * wpa_config_write_eap(const struct parse_data *data,
1395 struct wpa_ssid *ssid)
1396{
1397 int i, ret;
1398 char *buf, *pos, *end;
1399 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1400 const char *name;
1401
1402 if (eap_methods == NULL)
1403 return NULL;
1404
1405 pos = buf = os_zalloc(100);
1406 if (buf == NULL)
1407 return NULL;
1408 end = buf + 100;
1409
1410 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1411 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1412 name = eap_get_name(eap_methods[i].vendor,
1413 eap_methods[i].method);
1414 if (name) {
1415 ret = os_snprintf(pos, end - pos, "%s%s",
1416 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001417 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 break;
1419 pos += ret;
1420 }
1421 }
1422
1423 end[-1] = '\0';
1424
1425 return buf;
1426}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001427#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428
1429
1430static int wpa_config_parse_password(const struct parse_data *data,
1431 struct wpa_ssid *ssid, int line,
1432 const char *value)
1433{
1434 u8 *hash;
1435
1436 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001437 if (!ssid->eap.password)
1438 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001440 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 ssid->eap.password = NULL;
1442 ssid->eap.password_len = 0;
1443 return 0;
1444 }
1445
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001446#ifdef CONFIG_EXT_PASSWORD
1447 if (os_strncmp(value, "ext:", 4) == 0) {
1448 char *name = os_strdup(value + 4);
1449 if (name == NULL)
1450 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001451 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001452 ssid->eap.password = (u8 *) name;
1453 ssid->eap.password_len = os_strlen(name);
1454 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1455 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1456 return 0;
1457 }
1458#endif /* CONFIG_EXT_PASSWORD */
1459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (os_strncmp(value, "hash:", 5) != 0) {
1461 char *tmp;
1462 size_t res_len;
1463
1464 tmp = wpa_config_parse_string(value, &res_len);
1465 if (tmp == NULL) {
1466 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1467 "password.", line);
1468 return -1;
1469 }
1470 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1471 (u8 *) tmp, res_len);
1472
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001473 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 ssid->eap.password = (u8 *) tmp;
1475 ssid->eap.password_len = res_len;
1476 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001477 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 return 0;
1480 }
1481
1482
1483 /* NtPasswordHash: hash:<32 hex digits> */
1484 if (os_strlen(value + 5) != 2 * 16) {
1485 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1486 "(expected 32 hex digits)", line);
1487 return -1;
1488 }
1489
1490 hash = os_malloc(16);
1491 if (hash == NULL)
1492 return -1;
1493
1494 if (hexstr2bin(value + 5, hash, 16)) {
1495 os_free(hash);
1496 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1497 return -1;
1498 }
1499
1500 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1501
Dmitry Shmidte4663042016-04-04 10:07:49 -07001502 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1503 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1504 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1505 bin_clear_free(hash, 16);
1506 return 1;
1507 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001508 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 ssid->eap.password = hash;
1510 ssid->eap.password_len = 16;
1511 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001512 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513
1514 return 0;
1515}
1516
1517
Dmitry Shmidt83474442015-04-15 13:47:09 -07001518#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519static char * wpa_config_write_password(const struct parse_data *data,
1520 struct wpa_ssid *ssid)
1521{
1522 char *buf;
1523
1524 if (ssid->eap.password == NULL)
1525 return NULL;
1526
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001527#ifdef CONFIG_EXT_PASSWORD
1528 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1529 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1530 if (buf == NULL)
1531 return NULL;
1532 os_memcpy(buf, "ext:", 4);
1533 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1534 return buf;
1535 }
1536#endif /* CONFIG_EXT_PASSWORD */
1537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1539 return wpa_config_write_string(
1540 ssid->eap.password, ssid->eap.password_len);
1541 }
1542
1543 buf = os_malloc(5 + 32 + 1);
1544 if (buf == NULL)
1545 return NULL;
1546
1547 os_memcpy(buf, "hash:", 5);
1548 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1549
1550 return buf;
1551}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001552#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553#endif /* IEEE8021X_EAPOL */
1554
1555
1556static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1557 const char *value, int idx)
1558{
1559 char *buf, title[20];
1560 int res;
1561
1562 buf = wpa_config_parse_string(value, len);
1563 if (buf == NULL) {
1564 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1565 line, idx, value);
1566 return -1;
1567 }
1568 if (*len > MAX_WEP_KEY_LEN) {
1569 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1570 line, idx, value);
1571 os_free(buf);
1572 return -1;
1573 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001574 if (*len && *len != 5 && *len != 13 && *len != 16) {
1575 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1576 "this network block will be ignored",
1577 line, (unsigned int) *len);
1578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001580 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001582 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1584 return 0;
1585}
1586
1587
1588static int wpa_config_parse_wep_key0(const struct parse_data *data,
1589 struct wpa_ssid *ssid, int line,
1590 const char *value)
1591{
1592 return wpa_config_parse_wep_key(ssid->wep_key[0],
1593 &ssid->wep_key_len[0], line,
1594 value, 0);
1595}
1596
1597
1598static int wpa_config_parse_wep_key1(const struct parse_data *data,
1599 struct wpa_ssid *ssid, int line,
1600 const char *value)
1601{
1602 return wpa_config_parse_wep_key(ssid->wep_key[1],
1603 &ssid->wep_key_len[1], line,
1604 value, 1);
1605}
1606
1607
1608static int wpa_config_parse_wep_key2(const struct parse_data *data,
1609 struct wpa_ssid *ssid, int line,
1610 const char *value)
1611{
1612 return wpa_config_parse_wep_key(ssid->wep_key[2],
1613 &ssid->wep_key_len[2], line,
1614 value, 2);
1615}
1616
1617
1618static int wpa_config_parse_wep_key3(const struct parse_data *data,
1619 struct wpa_ssid *ssid, int line,
1620 const char *value)
1621{
1622 return wpa_config_parse_wep_key(ssid->wep_key[3],
1623 &ssid->wep_key_len[3], line,
1624 value, 3);
1625}
1626
1627
1628#ifndef NO_CONFIG_WRITE
1629static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1630{
1631 if (ssid->wep_key_len[idx] == 0)
1632 return NULL;
1633 return wpa_config_write_string(ssid->wep_key[idx],
1634 ssid->wep_key_len[idx]);
1635}
1636
1637
1638static char * wpa_config_write_wep_key0(const struct parse_data *data,
1639 struct wpa_ssid *ssid)
1640{
1641 return wpa_config_write_wep_key(ssid, 0);
1642}
1643
1644
1645static char * wpa_config_write_wep_key1(const struct parse_data *data,
1646 struct wpa_ssid *ssid)
1647{
1648 return wpa_config_write_wep_key(ssid, 1);
1649}
1650
1651
1652static char * wpa_config_write_wep_key2(const struct parse_data *data,
1653 struct wpa_ssid *ssid)
1654{
1655 return wpa_config_write_wep_key(ssid, 2);
1656}
1657
1658
1659static char * wpa_config_write_wep_key3(const struct parse_data *data,
1660 struct wpa_ssid *ssid)
1661{
1662 return wpa_config_write_wep_key(ssid, 3);
1663}
1664#endif /* NO_CONFIG_WRITE */
1665
1666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001667#ifdef CONFIG_P2P
1668
Dmitry Shmidt54605472013-11-08 11:10:19 -08001669static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1670 struct wpa_ssid *ssid, int line,
1671 const char *value)
1672{
1673 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1674 os_strcmp(value, "any") == 0) {
1675 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1676 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1677 return 0;
1678 }
1679 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1680 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1681 line, value);
1682 return -1;
1683 }
1684 ssid->bssid_set = 1;
1685 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1686 MAC2STR(ssid->go_p2p_dev_addr));
1687 return 0;
1688}
1689
1690
1691#ifndef NO_CONFIG_WRITE
1692static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1693 struct wpa_ssid *ssid)
1694{
1695 char *value;
1696 int res;
1697
1698 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1699 return NULL;
1700
1701 value = os_malloc(20);
1702 if (value == NULL)
1703 return NULL;
1704 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001705 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001706 os_free(value);
1707 return NULL;
1708 }
1709 value[20 - 1] = '\0';
1710 return value;
1711}
1712#endif /* NO_CONFIG_WRITE */
1713
1714
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1716 struct wpa_ssid *ssid, int line,
1717 const char *value)
1718{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001719 return wpa_config_parse_addr_list(data, line, value,
1720 &ssid->p2p_client_list,
1721 &ssid->num_p2p_clients,
1722 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001723}
1724
1725
1726#ifndef NO_CONFIG_WRITE
1727static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1728 struct wpa_ssid *ssid)
1729{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001730 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1731 ssid->num_p2p_clients,
1732 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001733}
1734#endif /* NO_CONFIG_WRITE */
1735
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001736
1737static int wpa_config_parse_psk_list(const struct parse_data *data,
1738 struct wpa_ssid *ssid, int line,
1739 const char *value)
1740{
1741 struct psk_list_entry *p;
1742 const char *pos;
1743
1744 p = os_zalloc(sizeof(*p));
1745 if (p == NULL)
1746 return -1;
1747
1748 pos = value;
1749 if (os_strncmp(pos, "P2P-", 4) == 0) {
1750 p->p2p = 1;
1751 pos += 4;
1752 }
1753
1754 if (hwaddr_aton(pos, p->addr)) {
1755 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1756 line, pos);
1757 os_free(p);
1758 return -1;
1759 }
1760 pos += 17;
1761 if (*pos != '-') {
1762 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1763 line, pos);
1764 os_free(p);
1765 return -1;
1766 }
1767 pos++;
1768
1769 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1770 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1771 line, pos);
1772 os_free(p);
1773 return -1;
1774 }
1775
1776 dl_list_add(&ssid->psk_list, &p->list);
1777
1778 return 0;
1779}
1780
1781
1782#ifndef NO_CONFIG_WRITE
1783static char * wpa_config_write_psk_list(const struct parse_data *data,
1784 struct wpa_ssid *ssid)
1785{
1786 return NULL;
1787}
1788#endif /* NO_CONFIG_WRITE */
1789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001790#endif /* CONFIG_P2P */
1791
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001792
1793#ifdef CONFIG_MESH
1794
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001795static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1796 struct wpa_ssid *ssid, int line,
1797 const char *value)
1798{
1799 int *rates = wpa_config_parse_int_array(value);
1800
1801 if (rates == NULL) {
1802 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1803 line, value);
1804 return -1;
1805 }
1806 if (rates[0] == 0) {
1807 os_free(rates);
1808 rates = NULL;
1809 }
1810
1811 os_free(ssid->mesh_basic_rates);
1812 ssid->mesh_basic_rates = rates;
1813
1814 return 0;
1815}
1816
1817
1818#ifndef NO_CONFIG_WRITE
1819
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001820static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1821 struct wpa_ssid *ssid)
1822{
1823 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1824}
1825
1826#endif /* NO_CONFIG_WRITE */
1827
1828#endif /* CONFIG_MESH */
1829
1830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831/* Helper macros for network block parser */
1832
1833#ifdef OFFSET
1834#undef OFFSET
1835#endif /* OFFSET */
1836/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1837#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1838
1839/* STR: Define a string variable for an ASCII string; f = field name */
1840#ifdef NO_CONFIG_WRITE
1841#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1842#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1843#else /* NO_CONFIG_WRITE */
1844#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1845#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1846#endif /* NO_CONFIG_WRITE */
1847#define STR(f) _STR(f), NULL, NULL, NULL, 0
1848#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1849#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1850#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1851
1852/* STR_LEN: Define a string variable with a separate variable for storing the
1853 * data length. Unlike STR(), this can be used to store arbitrary binary data
1854 * (i.e., even nul termination character). */
1855#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1856#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1857#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1858#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1859#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1860
1861/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1862 * explicitly specified. */
1863#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1864#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1865#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1866
1867#ifdef NO_CONFIG_WRITE
1868#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1869#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1870#else /* NO_CONFIG_WRITE */
1871#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1872 OFFSET(f), (void *) 0
1873#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1874 OFFSET(eap.f), (void *) 0
1875#endif /* NO_CONFIG_WRITE */
1876
1877/* INT: Define an integer variable */
1878#define INT(f) _INT(f), NULL, NULL, 0
1879#define INTe(f) _INTe(f), NULL, NULL, 0
1880
1881/* INT_RANGE: Define an integer variable with allowed value range */
1882#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1883
1884/* FUNC: Define a configuration variable that uses a custom function for
1885 * parsing and writing the value. */
1886#ifdef NO_CONFIG_WRITE
1887#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1888#else /* NO_CONFIG_WRITE */
1889#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1890 NULL, NULL, NULL, NULL
1891#endif /* NO_CONFIG_WRITE */
1892#define FUNC(f) _FUNC(f), 0
1893#define FUNC_KEY(f) _FUNC(f), 1
1894
1895/*
1896 * Table of network configuration variables. This table is used to parse each
1897 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1898 * that is inside a network block.
1899 *
1900 * This table is generated using the helper macros defined above and with
1901 * generous help from the C pre-processor. The field name is stored as a string
1902 * into .name and for STR and INT types, the offset of the target buffer within
1903 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1904 * offset to the field containing the length of the configuration variable.
1905 * .param3 and .param4 can be used to mark the allowed range (length for STR
1906 * and value for INT).
1907 *
1908 * For each configuration line in wpa_supplicant.conf, the parser goes through
1909 * this table and select the entry that matches with the field name. The parser
1910 * function (.parser) is then called to parse the actual value of the field.
1911 *
1912 * This kind of mechanism makes it easy to add new configuration parameters,
1913 * since only one line needs to be added into this table and into the
1914 * struct wpa_ssid definition if the new variable is either a string or
1915 * integer. More complex types will need to use their own parser and writer
1916 * functions.
1917 */
1918static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001919 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 { INT_RANGE(scan_ssid, 0, 1) },
1921 { FUNC(bssid) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001922 { FUNC(bssid_blacklist) },
1923 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001925 { INT(mem_only_psk) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 { FUNC(proto) },
1927 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001928 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 { FUNC(pairwise) },
1930 { FUNC(group) },
1931 { FUNC(auth_alg) },
1932 { FUNC(scan_freq) },
1933 { FUNC(freq_list) },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001934 { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
1935 VHT_CHANWIDTH_80P80MHZ) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936#ifdef IEEE8021X_EAPOL
1937 { FUNC(eap) },
1938 { STR_LENe(identity) },
1939 { STR_LENe(anonymous_identity) },
1940 { FUNC_KEY(password) },
1941 { STRe(ca_cert) },
1942 { STRe(ca_path) },
1943 { STRe(client_cert) },
1944 { STRe(private_key) },
1945 { STR_KEYe(private_key_passwd) },
1946 { STRe(dh_file) },
1947 { STRe(subject_match) },
1948 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001949 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001950 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 { STRe(ca_cert2) },
1952 { STRe(ca_path2) },
1953 { STRe(client_cert2) },
1954 { STRe(private_key2) },
1955 { STR_KEYe(private_key2_passwd) },
1956 { STRe(dh_file2) },
1957 { STRe(subject_match2) },
1958 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001959 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001960 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 { STRe(phase1) },
1962 { STRe(phase2) },
1963 { STRe(pcsc) },
1964 { STR_KEYe(pin) },
1965 { STRe(engine_id) },
1966 { STRe(key_id) },
1967 { STRe(cert_id) },
1968 { STRe(ca_cert_id) },
1969 { STR_KEYe(pin2) },
1970 { STRe(engine2_id) },
1971 { STRe(key2_id) },
1972 { STRe(cert2_id) },
1973 { STRe(ca_cert2_id) },
1974 { INTe(engine) },
1975 { INTe(engine2) },
1976 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001977 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001978 { STRe(openssl_ciphers) },
1979 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980#endif /* IEEE8021X_EAPOL */
1981 { FUNC_KEY(wep_key0) },
1982 { FUNC_KEY(wep_key1) },
1983 { FUNC_KEY(wep_key2) },
1984 { FUNC_KEY(wep_key3) },
1985 { INT(wep_tx_keyidx) },
1986 { INT(priority) },
1987#ifdef IEEE8021X_EAPOL
1988 { INT(eap_workaround) },
1989 { STRe(pac_file) },
1990 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001991 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001993#ifdef CONFIG_MESH
1994 { INT_RANGE(mode, 0, 5) },
1995 { INT_RANGE(no_auto_peer, 0, 1) },
1996#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001998#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 { INT_RANGE(proactive_key_caching, 0, 1) },
2000 { INT_RANGE(disabled, 0, 2) },
2001 { STR(id_str) },
2002#ifdef CONFIG_IEEE80211W
2003 { INT_RANGE(ieee80211w, 0, 2) },
2004#endif /* CONFIG_IEEE80211W */
2005 { INT_RANGE(peerkey, 0, 1) },
2006 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002007 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002008 { INT_RANGE(fixed_freq, 0, 1) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002009#ifdef CONFIG_ACS
2010 { INT_RANGE(acs, 0, 1) },
2011#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002012#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002013 { FUNC(mesh_basic_rates) },
2014 { INT(dot11MeshMaxRetries) },
2015 { INT(dot11MeshRetryTimeout) },
2016 { INT(dot11MeshConfirmTimeout) },
2017 { INT(dot11MeshHoldingTimeout) },
2018#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 { INT(wpa_ptk_rekey) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002020 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002022 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002024 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002025 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002026 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002027#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002028#ifdef CONFIG_HT_OVERRIDES
2029 { INT_RANGE(disable_ht, 0, 1) },
2030 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002031 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002032 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002033 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002034 { INT_RANGE(disable_max_amsdu, -1, 1) },
2035 { INT_RANGE(ampdu_factor, -1, 3) },
2036 { INT_RANGE(ampdu_density, -1, 7) },
2037 { STR(ht_mcs) },
2038#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002039#ifdef CONFIG_VHT_OVERRIDES
2040 { INT_RANGE(disable_vht, 0, 1) },
2041 { INT(vht_capa) },
2042 { INT(vht_capa_mask) },
2043 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2044 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2045 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2046 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2047 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2048 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2049 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2050 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2051 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2052 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2053 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2054 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2055 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2056 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2057 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2058 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2059#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002060 { INT(ap_max_inactivity) },
2061 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002062 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002063#ifdef CONFIG_MACSEC
2064 { INT_RANGE(macsec_policy, 0, 1) },
2065#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002066#ifdef CONFIG_HS20
2067 { INT(update_identifier) },
2068#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002069 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002070 { INT_RANGE(pbss, 0, 2) },
2071 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072};
2073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074#undef OFFSET
2075#undef _STR
2076#undef STR
2077#undef STR_KEY
2078#undef _STR_LEN
2079#undef STR_LEN
2080#undef STR_LEN_KEY
2081#undef _STR_RANGE
2082#undef STR_RANGE
2083#undef STR_RANGE_KEY
2084#undef _INT
2085#undef INT
2086#undef INT_RANGE
2087#undef _FUNC
2088#undef FUNC
2089#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002090#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091
2092
2093/**
2094 * wpa_config_add_prio_network - Add a network to priority lists
2095 * @config: Configuration data from wpa_config_read()
2096 * @ssid: Pointer to the network configuration to be added to the list
2097 * Returns: 0 on success, -1 on failure
2098 *
2099 * This function is used to add a network block to the priority list of
2100 * networks. This must be called for each network when reading in the full
2101 * configuration. In addition, this can be used indirectly when updating
2102 * priorities by calling wpa_config_update_prio_list().
2103 */
2104int wpa_config_add_prio_network(struct wpa_config *config,
2105 struct wpa_ssid *ssid)
2106{
2107 int prio;
2108 struct wpa_ssid *prev, **nlist;
2109
2110 /*
2111 * Add to an existing priority list if one is available for the
2112 * configured priority level for this network.
2113 */
2114 for (prio = 0; prio < config->num_prio; prio++) {
2115 prev = config->pssid[prio];
2116 if (prev->priority == ssid->priority) {
2117 while (prev->pnext)
2118 prev = prev->pnext;
2119 prev->pnext = ssid;
2120 return 0;
2121 }
2122 }
2123
2124 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002125 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2126 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 if (nlist == NULL)
2128 return -1;
2129
2130 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002131 if (nlist[prio]->priority < ssid->priority) {
2132 os_memmove(&nlist[prio + 1], &nlist[prio],
2133 (config->num_prio - prio) *
2134 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 }
2138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 nlist[prio] = ssid;
2140 config->num_prio++;
2141 config->pssid = nlist;
2142
2143 return 0;
2144}
2145
2146
2147/**
2148 * wpa_config_update_prio_list - Update network priority list
2149 * @config: Configuration data from wpa_config_read()
2150 * Returns: 0 on success, -1 on failure
2151 *
2152 * This function is called to update the priority list of networks in the
2153 * configuration when a network is being added or removed. This is also called
2154 * if a priority for a network is changed.
2155 */
2156int wpa_config_update_prio_list(struct wpa_config *config)
2157{
2158 struct wpa_ssid *ssid;
2159 int ret = 0;
2160
2161 os_free(config->pssid);
2162 config->pssid = NULL;
2163 config->num_prio = 0;
2164
2165 ssid = config->ssid;
2166 while (ssid) {
2167 ssid->pnext = NULL;
2168 if (wpa_config_add_prio_network(config, ssid) < 0)
2169 ret = -1;
2170 ssid = ssid->next;
2171 }
2172
2173 return ret;
2174}
2175
2176
2177#ifdef IEEE8021X_EAPOL
2178static void eap_peer_config_free(struct eap_peer_config *eap)
2179{
2180 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002181 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002183 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 os_free(eap->ca_cert);
2185 os_free(eap->ca_path);
2186 os_free(eap->client_cert);
2187 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002188 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 os_free(eap->dh_file);
2190 os_free(eap->subject_match);
2191 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002192 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002193 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 os_free(eap->ca_cert2);
2195 os_free(eap->ca_path2);
2196 os_free(eap->client_cert2);
2197 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002198 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 os_free(eap->dh_file2);
2200 os_free(eap->subject_match2);
2201 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002202 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002203 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 os_free(eap->phase1);
2205 os_free(eap->phase2);
2206 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002207 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 os_free(eap->engine_id);
2209 os_free(eap->key_id);
2210 os_free(eap->cert_id);
2211 os_free(eap->ca_cert_id);
2212 os_free(eap->key2_id);
2213 os_free(eap->cert2_id);
2214 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002215 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216 os_free(eap->engine2_id);
2217 os_free(eap->otp);
2218 os_free(eap->pending_req_otp);
2219 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002220 bin_clear_free(eap->new_password, eap->new_password_len);
2221 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002222 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223}
2224#endif /* IEEE8021X_EAPOL */
2225
2226
2227/**
2228 * wpa_config_free_ssid - Free network/ssid configuration data
2229 * @ssid: Configuration data for the network
2230 *
2231 * This function frees all resources allocated for the network configuration
2232 * data.
2233 */
2234void wpa_config_free_ssid(struct wpa_ssid *ssid)
2235{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002236 struct psk_list_entry *psk;
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002239 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002240 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241#ifdef IEEE8021X_EAPOL
2242 eap_peer_config_free(&ssid->eap);
2243#endif /* IEEE8021X_EAPOL */
2244 os_free(ssid->id_str);
2245 os_free(ssid->scan_freq);
2246 os_free(ssid->freq_list);
2247 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002248 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002249 os_free(ssid->bssid_blacklist);
2250 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002251#ifdef CONFIG_HT_OVERRIDES
2252 os_free(ssid->ht_mcs);
2253#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002254#ifdef CONFIG_MESH
2255 os_free(ssid->mesh_basic_rates);
2256#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002257 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2258 list))) {
2259 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002260 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002261 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002262 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263}
2264
2265
Dmitry Shmidt04949592012-07-19 12:16:46 -07002266void wpa_config_free_cred(struct wpa_cred *cred)
2267{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002268 size_t i;
2269
Dmitry Shmidt04949592012-07-19 12:16:46 -07002270 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002271 str_clear_free(cred->username);
2272 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002273 os_free(cred->ca_cert);
2274 os_free(cred->client_cert);
2275 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002276 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002277 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002278 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002279 for (i = 0; i < cred->num_domain; i++)
2280 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002281 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002282 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002283 os_free(cred->eap_method);
2284 os_free(cred->phase1);
2285 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002286 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002287 os_free(cred->roaming_partner);
2288 os_free(cred->provisioning_sp);
2289 for (i = 0; i < cred->num_req_conn_capab; i++)
2290 os_free(cred->req_conn_capab_port[i]);
2291 os_free(cred->req_conn_capab_port);
2292 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002293 os_free(cred);
2294}
2295
2296
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002297void wpa_config_flush_blobs(struct wpa_config *config)
2298{
2299#ifndef CONFIG_NO_CONFIG_BLOBS
2300 struct wpa_config_blob *blob, *prev;
2301
2302 blob = config->blobs;
2303 config->blobs = NULL;
2304 while (blob) {
2305 prev = blob;
2306 blob = blob->next;
2307 wpa_config_free_blob(prev);
2308 }
2309#endif /* CONFIG_NO_CONFIG_BLOBS */
2310}
2311
2312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313/**
2314 * wpa_config_free - Free configuration data
2315 * @config: Configuration data from wpa_config_read()
2316 *
2317 * This function frees all resources allocated for the configuration data by
2318 * wpa_config_read().
2319 */
2320void wpa_config_free(struct wpa_config *config)
2321{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002323 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002324 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325
2326 ssid = config->ssid;
2327 while (ssid) {
2328 prev = ssid;
2329 ssid = ssid->next;
2330 wpa_config_free_ssid(prev);
2331 }
2332
Dmitry Shmidt04949592012-07-19 12:16:46 -07002333 cred = config->cred;
2334 while (cred) {
2335 cprev = cred;
2336 cred = cred->next;
2337 wpa_config_free_cred(cprev);
2338 }
2339
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002340 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341
Dmitry Shmidt04949592012-07-19 12:16:46 -07002342 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002343 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2344 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 os_free(config->ctrl_interface);
2346 os_free(config->ctrl_interface_group);
2347 os_free(config->opensc_engine_path);
2348 os_free(config->pkcs11_engine_path);
2349 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002350 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002351 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002352 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 os_free(config->driver_param);
2354 os_free(config->device_name);
2355 os_free(config->manufacturer);
2356 os_free(config->model_name);
2357 os_free(config->model_number);
2358 os_free(config->serial_number);
2359 os_free(config->config_methods);
2360 os_free(config->p2p_ssid_postfix);
2361 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002362 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002363 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002364 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002365 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002366 wpabuf_free(config->wps_nfc_dh_pubkey);
2367 wpabuf_free(config->wps_nfc_dh_privkey);
2368 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002369 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002370 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002371 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002372 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002373 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002374 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002375 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002376 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002377#ifdef CONFIG_MBO
2378 os_free(config->non_pref_chan);
2379#endif /* CONFIG_MBO */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 os_free(config);
2382}
2383
2384
2385/**
2386 * wpa_config_foreach_network - Iterate over each configured network
2387 * @config: Configuration data from wpa_config_read()
2388 * @func: Callback function to process each network
2389 * @arg: Opaque argument to pass to callback function
2390 *
2391 * Iterate over the set of configured networks calling the specified
2392 * function for each item. We guard against callbacks removing the
2393 * supplied network.
2394 */
2395void wpa_config_foreach_network(struct wpa_config *config,
2396 void (*func)(void *, struct wpa_ssid *),
2397 void *arg)
2398{
2399 struct wpa_ssid *ssid, *next;
2400
2401 ssid = config->ssid;
2402 while (ssid) {
2403 next = ssid->next;
2404 func(arg, ssid);
2405 ssid = next;
2406 }
2407}
2408
2409
2410/**
2411 * wpa_config_get_network - Get configured network based on id
2412 * @config: Configuration data from wpa_config_read()
2413 * @id: Unique network id to search for
2414 * Returns: Network configuration or %NULL if not found
2415 */
2416struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2417{
2418 struct wpa_ssid *ssid;
2419
2420 ssid = config->ssid;
2421 while (ssid) {
2422 if (id == ssid->id)
2423 break;
2424 ssid = ssid->next;
2425 }
2426
2427 return ssid;
2428}
2429
2430
2431/**
2432 * wpa_config_add_network - Add a new network with empty configuration
2433 * @config: Configuration data from wpa_config_read()
2434 * Returns: The new network configuration or %NULL if operation failed
2435 */
2436struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2437{
2438 int id;
2439 struct wpa_ssid *ssid, *last = NULL;
2440
2441 id = -1;
2442 ssid = config->ssid;
2443 while (ssid) {
2444 if (ssid->id > id)
2445 id = ssid->id;
2446 last = ssid;
2447 ssid = ssid->next;
2448 }
2449 id++;
2450
2451 ssid = os_zalloc(sizeof(*ssid));
2452 if (ssid == NULL)
2453 return NULL;
2454 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002455 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 if (last)
2457 last->next = ssid;
2458 else
2459 config->ssid = ssid;
2460
2461 wpa_config_update_prio_list(config);
2462
2463 return ssid;
2464}
2465
2466
2467/**
2468 * wpa_config_remove_network - Remove a configured network based on id
2469 * @config: Configuration data from wpa_config_read()
2470 * @id: Unique network id to search for
2471 * Returns: 0 on success, or -1 if the network was not found
2472 */
2473int wpa_config_remove_network(struct wpa_config *config, int id)
2474{
2475 struct wpa_ssid *ssid, *prev = NULL;
2476
2477 ssid = config->ssid;
2478 while (ssid) {
2479 if (id == ssid->id)
2480 break;
2481 prev = ssid;
2482 ssid = ssid->next;
2483 }
2484
2485 if (ssid == NULL)
2486 return -1;
2487
2488 if (prev)
2489 prev->next = ssid->next;
2490 else
2491 config->ssid = ssid->next;
2492
2493 wpa_config_update_prio_list(config);
2494 wpa_config_free_ssid(ssid);
2495 return 0;
2496}
2497
2498
2499/**
2500 * wpa_config_set_network_defaults - Set network default values
2501 * @ssid: Pointer to network configuration data
2502 */
2503void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2504{
2505 ssid->proto = DEFAULT_PROTO;
2506 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2507 ssid->group_cipher = DEFAULT_GROUP;
2508 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002509 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510#ifdef IEEE8021X_EAPOL
2511 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2512 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2513 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002514 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002516#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002517 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2518 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2519 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2520 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2521#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002522#ifdef CONFIG_HT_OVERRIDES
2523 ssid->disable_ht = DEFAULT_DISABLE_HT;
2524 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002525 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002526 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002527 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2528 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2529 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2530#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002531#ifdef CONFIG_VHT_OVERRIDES
2532 ssid->vht_rx_mcs_nss_1 = -1;
2533 ssid->vht_rx_mcs_nss_2 = -1;
2534 ssid->vht_rx_mcs_nss_3 = -1;
2535 ssid->vht_rx_mcs_nss_4 = -1;
2536 ssid->vht_rx_mcs_nss_5 = -1;
2537 ssid->vht_rx_mcs_nss_6 = -1;
2538 ssid->vht_rx_mcs_nss_7 = -1;
2539 ssid->vht_rx_mcs_nss_8 = -1;
2540 ssid->vht_tx_mcs_nss_1 = -1;
2541 ssid->vht_tx_mcs_nss_2 = -1;
2542 ssid->vht_tx_mcs_nss_3 = -1;
2543 ssid->vht_tx_mcs_nss_4 = -1;
2544 ssid->vht_tx_mcs_nss_5 = -1;
2545 ssid->vht_tx_mcs_nss_6 = -1;
2546 ssid->vht_tx_mcs_nss_7 = -1;
2547 ssid->vht_tx_mcs_nss_8 = -1;
2548#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002549 ssid->proactive_key_caching = -1;
2550#ifdef CONFIG_IEEE80211W
2551 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2552#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002553 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554}
2555
2556
2557/**
2558 * wpa_config_set - Set a variable in network configuration
2559 * @ssid: Pointer to network configuration data
2560 * @var: Variable name, e.g., "ssid"
2561 * @value: Variable value
2562 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07002563 * Returns: 0 on success with possible change in the value, 1 on success with
2564 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 *
2566 * This function can be used to set network configuration variables based on
2567 * both the configuration file and management interface input. The value
2568 * parameter must be in the same format as the text-based configuration file is
2569 * using. For example, strings are using double quotation marks.
2570 */
2571int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2572 int line)
2573{
2574 size_t i;
2575 int ret = 0;
2576
2577 if (ssid == NULL || var == NULL || value == NULL)
2578 return -1;
2579
2580 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2581 const struct parse_data *field = &ssid_fields[i];
2582 if (os_strcmp(var, field->name) != 0)
2583 continue;
2584
Dmitry Shmidte4663042016-04-04 10:07:49 -07002585 ret = field->parser(field, ssid, line, value);
2586 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 if (line) {
2588 wpa_printf(MSG_ERROR, "Line %d: failed to "
2589 "parse %s '%s'.", line, var, value);
2590 }
2591 ret = -1;
2592 }
2593 break;
2594 }
2595 if (i == NUM_SSID_FIELDS) {
2596 if (line) {
2597 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2598 "'%s'.", line, var);
2599 }
2600 ret = -1;
2601 }
2602
2603 return ret;
2604}
2605
2606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002607int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2608 const char *value)
2609{
2610 size_t len;
2611 char *buf;
2612 int ret;
2613
2614 len = os_strlen(value);
2615 buf = os_malloc(len + 3);
2616 if (buf == NULL)
2617 return -1;
2618 buf[0] = '"';
2619 os_memcpy(buf + 1, value, len);
2620 buf[len + 1] = '"';
2621 buf[len + 2] = '\0';
2622 ret = wpa_config_set(ssid, var, buf, 0);
2623 os_free(buf);
2624 return ret;
2625}
2626
2627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628/**
2629 * wpa_config_get_all - Get all options from network configuration
2630 * @ssid: Pointer to network configuration data
2631 * @get_keys: Determines if keys/passwords will be included in returned list
2632 * (if they may be exported)
2633 * Returns: %NULL terminated list of all set keys and their values in the form
2634 * of [key1, val1, key2, val2, ... , NULL]
2635 *
2636 * This function can be used to get list of all configured network properties.
2637 * The caller is responsible for freeing the returned list and all its
2638 * elements.
2639 */
2640char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2641{
Dmitry Shmidt83474442015-04-15 13:47:09 -07002642#ifdef NO_CONFIG_WRITE
2643 return NULL;
2644#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 const struct parse_data *field;
2646 char *key, *value;
2647 size_t i;
2648 char **props;
2649 int fields_num;
2650
2651 get_keys = get_keys && ssid->export_keys;
2652
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002653 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 if (!props)
2655 return NULL;
2656
2657 fields_num = 0;
2658 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2659 field = &ssid_fields[i];
2660 if (field->key_data && !get_keys)
2661 continue;
2662 value = field->writer(field, ssid);
2663 if (value == NULL)
2664 continue;
2665 if (os_strlen(value) == 0) {
2666 os_free(value);
2667 continue;
2668 }
2669
2670 key = os_strdup(field->name);
2671 if (key == NULL) {
2672 os_free(value);
2673 goto err;
2674 }
2675
2676 props[fields_num * 2] = key;
2677 props[fields_num * 2 + 1] = value;
2678
2679 fields_num++;
2680 }
2681
2682 return props;
2683
2684err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002685 for (i = 0; props[i]; i++)
2686 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687 os_free(props);
2688 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07002689#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690}
2691
2692
2693#ifndef NO_CONFIG_WRITE
2694/**
2695 * wpa_config_get - Get a variable in network configuration
2696 * @ssid: Pointer to network configuration data
2697 * @var: Variable name, e.g., "ssid"
2698 * Returns: Value of the variable or %NULL on failure
2699 *
2700 * This function can be used to get network configuration variables. The
2701 * returned value is a copy of the configuration variable in text format, i.e,.
2702 * the same format that the text-based configuration file and wpa_config_set()
2703 * are using for the value. The caller is responsible for freeing the returned
2704 * value.
2705 */
2706char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2707{
2708 size_t i;
2709
2710 if (ssid == NULL || var == NULL)
2711 return NULL;
2712
2713 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2714 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08002715 if (os_strcmp(var, field->name) == 0) {
2716 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002717
2718 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07002719 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002720 "Found newline in value for %s; not returning it",
2721 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08002722 os_free(ret);
2723 ret = NULL;
2724 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002725
Paul Stewart85c72c62016-03-03 15:33:47 -08002726 return ret;
2727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 }
2729
2730 return NULL;
2731}
2732
2733
2734/**
2735 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2736 * @ssid: Pointer to network configuration data
2737 * @var: Variable name, e.g., "ssid"
2738 * Returns: Value of the variable or %NULL on failure
2739 *
2740 * This function can be used to get network configuration variable like
2741 * wpa_config_get(). The only difference is that this functions does not expose
2742 * key/password material from the configuration. In case a key/password field
2743 * is requested, the returned value is an empty string or %NULL if the variable
2744 * is not set or "*" if the variable is set (regardless of its value). The
2745 * returned value is a copy of the configuration variable in text format, i.e,.
2746 * the same format that the text-based configuration file and wpa_config_set()
2747 * are using for the value. The caller is responsible for freeing the returned
2748 * value.
2749 */
2750char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2751{
2752 size_t i;
2753
2754 if (ssid == NULL || var == NULL)
2755 return NULL;
2756
2757 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2758 const struct parse_data *field = &ssid_fields[i];
2759 if (os_strcmp(var, field->name) == 0) {
2760 char *res = field->writer(field, ssid);
2761 if (field->key_data) {
2762 if (res && res[0]) {
2763 wpa_printf(MSG_DEBUG, "Do not allow "
2764 "key_data field to be "
2765 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002766 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 return os_strdup("*");
2768 }
2769
2770 os_free(res);
2771 return NULL;
2772 }
2773 return res;
2774 }
2775 }
2776
2777 return NULL;
2778}
2779#endif /* NO_CONFIG_WRITE */
2780
2781
2782/**
2783 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2784 * @ssid: Pointer to network configuration data
2785 *
2786 * This function must be called to update WPA PSK when either SSID or the
2787 * passphrase has changed for the network configuration.
2788 */
2789void wpa_config_update_psk(struct wpa_ssid *ssid)
2790{
2791#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002792 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 ssid->psk, PMK_LEN);
2794 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2795 ssid->psk, PMK_LEN);
2796 ssid->psk_set = 1;
2797#endif /* CONFIG_NO_PBKDF2 */
2798}
2799
2800
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002801static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2802 const char *value)
2803{
2804 u8 *proto;
2805 int **port;
2806 int *ports, *nports;
2807 const char *pos;
2808 unsigned int num_ports;
2809
2810 proto = os_realloc_array(cred->req_conn_capab_proto,
2811 cred->num_req_conn_capab + 1, sizeof(u8));
2812 if (proto == NULL)
2813 return -1;
2814 cred->req_conn_capab_proto = proto;
2815
2816 port = os_realloc_array(cred->req_conn_capab_port,
2817 cred->num_req_conn_capab + 1, sizeof(int *));
2818 if (port == NULL)
2819 return -1;
2820 cred->req_conn_capab_port = port;
2821
2822 proto[cred->num_req_conn_capab] = atoi(value);
2823
2824 pos = os_strchr(value, ':');
2825 if (pos == NULL) {
2826 port[cred->num_req_conn_capab] = NULL;
2827 cred->num_req_conn_capab++;
2828 return 0;
2829 }
2830 pos++;
2831
2832 ports = NULL;
2833 num_ports = 0;
2834
2835 while (*pos) {
2836 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2837 if (nports == NULL) {
2838 os_free(ports);
2839 return -1;
2840 }
2841 ports = nports;
2842 ports[num_ports++] = atoi(pos);
2843
2844 pos = os_strchr(pos, ',');
2845 if (pos == NULL)
2846 break;
2847 pos++;
2848 }
2849
2850 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2851 if (nports == NULL) {
2852 os_free(ports);
2853 return -1;
2854 }
2855 ports = nports;
2856 ports[num_ports] = -1;
2857
2858 port[cred->num_req_conn_capab] = ports;
2859 cred->num_req_conn_capab++;
2860 return 0;
2861}
2862
2863
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2865 const char *value, int line)
2866{
2867 char *val;
2868 size_t len;
2869
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002870 if (os_strcmp(var, "temporary") == 0) {
2871 cred->temporary = atoi(value);
2872 return 0;
2873 }
2874
Dmitry Shmidt04949592012-07-19 12:16:46 -07002875 if (os_strcmp(var, "priority") == 0) {
2876 cred->priority = atoi(value);
2877 return 0;
2878 }
2879
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002880 if (os_strcmp(var, "sp_priority") == 0) {
2881 int prio = atoi(value);
2882 if (prio < 0 || prio > 255)
2883 return -1;
2884 cred->sp_priority = prio;
2885 return 0;
2886 }
2887
Dmitry Shmidt04949592012-07-19 12:16:46 -07002888 if (os_strcmp(var, "pcsc") == 0) {
2889 cred->pcsc = atoi(value);
2890 return 0;
2891 }
2892
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002893 if (os_strcmp(var, "eap") == 0) {
2894 struct eap_method_type method;
2895 method.method = eap_peer_get_type(value, &method.vendor);
2896 if (method.vendor == EAP_VENDOR_IETF &&
2897 method.method == EAP_TYPE_NONE) {
2898 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2899 "for a credential", line, value);
2900 return -1;
2901 }
2902 os_free(cred->eap_method);
2903 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2904 if (cred->eap_method == NULL)
2905 return -1;
2906 os_memcpy(cred->eap_method, &method, sizeof(method));
2907 return 0;
2908 }
2909
2910 if (os_strcmp(var, "password") == 0 &&
2911 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002912 if (has_newline(value))
2913 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002914 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002915 cred->password = os_strdup(value);
2916 cred->ext_password = 1;
2917 return 0;
2918 }
2919
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002920 if (os_strcmp(var, "update_identifier") == 0) {
2921 cred->update_identifier = atoi(value);
2922 return 0;
2923 }
2924
2925 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2926 cred->min_dl_bandwidth_home = atoi(value);
2927 return 0;
2928 }
2929
2930 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2931 cred->min_ul_bandwidth_home = atoi(value);
2932 return 0;
2933 }
2934
2935 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2936 cred->min_dl_bandwidth_roaming = atoi(value);
2937 return 0;
2938 }
2939
2940 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2941 cred->min_ul_bandwidth_roaming = atoi(value);
2942 return 0;
2943 }
2944
2945 if (os_strcmp(var, "max_bss_load") == 0) {
2946 cred->max_bss_load = atoi(value);
2947 return 0;
2948 }
2949
2950 if (os_strcmp(var, "req_conn_capab") == 0)
2951 return wpa_config_set_cred_req_conn_capab(cred, value);
2952
2953 if (os_strcmp(var, "ocsp") == 0) {
2954 cred->ocsp = atoi(value);
2955 return 0;
2956 }
2957
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002958 if (os_strcmp(var, "sim_num") == 0) {
2959 cred->sim_num = atoi(value);
2960 return 0;
2961 }
2962
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002964 if (val == NULL ||
2965 (os_strcmp(var, "excluded_ssid") != 0 &&
2966 os_strcmp(var, "roaming_consortium") != 0 &&
2967 os_strcmp(var, "required_roaming_consortium") != 0 &&
2968 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002969 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2970 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002971 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972 return -1;
2973 }
2974
2975 if (os_strcmp(var, "realm") == 0) {
2976 os_free(cred->realm);
2977 cred->realm = val;
2978 return 0;
2979 }
2980
2981 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002982 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983 cred->username = val;
2984 return 0;
2985 }
2986
2987 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002988 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002989 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002990 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002991 return 0;
2992 }
2993
2994 if (os_strcmp(var, "ca_cert") == 0) {
2995 os_free(cred->ca_cert);
2996 cred->ca_cert = val;
2997 return 0;
2998 }
2999
3000 if (os_strcmp(var, "client_cert") == 0) {
3001 os_free(cred->client_cert);
3002 cred->client_cert = val;
3003 return 0;
3004 }
3005
3006 if (os_strcmp(var, "private_key") == 0) {
3007 os_free(cred->private_key);
3008 cred->private_key = val;
3009 return 0;
3010 }
3011
3012 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003013 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003014 cred->private_key_passwd = val;
3015 return 0;
3016 }
3017
3018 if (os_strcmp(var, "imsi") == 0) {
3019 os_free(cred->imsi);
3020 cred->imsi = val;
3021 return 0;
3022 }
3023
3024 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003025 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003026 cred->milenage = val;
3027 return 0;
3028 }
3029
Dmitry Shmidt051af732013-10-22 13:52:46 -07003030 if (os_strcmp(var, "domain_suffix_match") == 0) {
3031 os_free(cred->domain_suffix_match);
3032 cred->domain_suffix_match = val;
3033 return 0;
3034 }
3035
Dmitry Shmidt04949592012-07-19 12:16:46 -07003036 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003037 char **new_domain;
3038 new_domain = os_realloc_array(cred->domain,
3039 cred->num_domain + 1,
3040 sizeof(char *));
3041 if (new_domain == NULL) {
3042 os_free(val);
3043 return -1;
3044 }
3045 new_domain[cred->num_domain++] = val;
3046 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003047 return 0;
3048 }
3049
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003050 if (os_strcmp(var, "phase1") == 0) {
3051 os_free(cred->phase1);
3052 cred->phase1 = val;
3053 return 0;
3054 }
3055
3056 if (os_strcmp(var, "phase2") == 0) {
3057 os_free(cred->phase2);
3058 cred->phase2 = val;
3059 return 0;
3060 }
3061
3062 if (os_strcmp(var, "roaming_consortium") == 0) {
3063 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3064 wpa_printf(MSG_ERROR, "Line %d: invalid "
3065 "roaming_consortium length %d (3..15 "
3066 "expected)", line, (int) len);
3067 os_free(val);
3068 return -1;
3069 }
3070 os_memcpy(cred->roaming_consortium, val, len);
3071 cred->roaming_consortium_len = len;
3072 os_free(val);
3073 return 0;
3074 }
3075
Dmitry Shmidt051af732013-10-22 13:52:46 -07003076 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3077 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3078 {
3079 wpa_printf(MSG_ERROR, "Line %d: invalid "
3080 "required_roaming_consortium length %d "
3081 "(3..15 expected)", line, (int) len);
3082 os_free(val);
3083 return -1;
3084 }
3085 os_memcpy(cred->required_roaming_consortium, val, len);
3086 cred->required_roaming_consortium_len = len;
3087 os_free(val);
3088 return 0;
3089 }
3090
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003091 if (os_strcmp(var, "excluded_ssid") == 0) {
3092 struct excluded_ssid *e;
3093
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003094 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003095 wpa_printf(MSG_ERROR, "Line %d: invalid "
3096 "excluded_ssid length %d", line, (int) len);
3097 os_free(val);
3098 return -1;
3099 }
3100
3101 e = os_realloc_array(cred->excluded_ssid,
3102 cred->num_excluded_ssid + 1,
3103 sizeof(struct excluded_ssid));
3104 if (e == NULL) {
3105 os_free(val);
3106 return -1;
3107 }
3108 cred->excluded_ssid = e;
3109
3110 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3111 os_memcpy(e->ssid, val, len);
3112 e->ssid_len = len;
3113
3114 os_free(val);
3115
3116 return 0;
3117 }
3118
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003119 if (os_strcmp(var, "roaming_partner") == 0) {
3120 struct roaming_partner *p;
3121 char *pos;
3122
3123 p = os_realloc_array(cred->roaming_partner,
3124 cred->num_roaming_partner + 1,
3125 sizeof(struct roaming_partner));
3126 if (p == NULL) {
3127 os_free(val);
3128 return -1;
3129 }
3130 cred->roaming_partner = p;
3131
3132 p = &cred->roaming_partner[cred->num_roaming_partner];
3133
3134 pos = os_strchr(val, ',');
3135 if (pos == NULL) {
3136 os_free(val);
3137 return -1;
3138 }
3139 *pos++ = '\0';
3140 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3141 os_free(val);
3142 return -1;
3143 }
3144 os_memcpy(p->fqdn, val, pos - val);
3145
3146 p->exact_match = atoi(pos);
3147
3148 pos = os_strchr(pos, ',');
3149 if (pos == NULL) {
3150 os_free(val);
3151 return -1;
3152 }
3153 *pos++ = '\0';
3154
3155 p->priority = atoi(pos);
3156
3157 pos = os_strchr(pos, ',');
3158 if (pos == NULL) {
3159 os_free(val);
3160 return -1;
3161 }
3162 *pos++ = '\0';
3163
3164 if (os_strlen(pos) >= sizeof(p->country)) {
3165 os_free(val);
3166 return -1;
3167 }
3168 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3169
3170 cred->num_roaming_partner++;
3171 os_free(val);
3172
3173 return 0;
3174 }
3175
3176 if (os_strcmp(var, "provisioning_sp") == 0) {
3177 os_free(cred->provisioning_sp);
3178 cred->provisioning_sp = val;
3179 return 0;
3180 }
3181
Dmitry Shmidt04949592012-07-19 12:16:46 -07003182 if (line) {
3183 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3184 line, var);
3185 }
3186
3187 os_free(val);
3188
3189 return -1;
3190}
3191
3192
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003193static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003194{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003195 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003196 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003197 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003199 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003200 if (buf == NULL)
3201 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003202 res = os_snprintf(buf, bufsize, "%d", val);
3203 if (os_snprintf_error(bufsize, res)) {
3204 os_free(buf);
3205 buf = NULL;
3206 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003207 return buf;
3208}
3209
3210
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003211static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003212{
3213 if (str == NULL)
3214 return NULL;
3215 return os_strdup(str);
3216}
3217
3218
3219char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3220{
3221 if (os_strcmp(var, "temporary") == 0)
3222 return alloc_int_str(cred->temporary);
3223
3224 if (os_strcmp(var, "priority") == 0)
3225 return alloc_int_str(cred->priority);
3226
3227 if (os_strcmp(var, "sp_priority") == 0)
3228 return alloc_int_str(cred->sp_priority);
3229
3230 if (os_strcmp(var, "pcsc") == 0)
3231 return alloc_int_str(cred->pcsc);
3232
3233 if (os_strcmp(var, "eap") == 0) {
3234 if (!cred->eap_method)
3235 return NULL;
3236 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3237 cred->eap_method[0].method));
3238 }
3239
3240 if (os_strcmp(var, "update_identifier") == 0)
3241 return alloc_int_str(cred->update_identifier);
3242
3243 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3244 return alloc_int_str(cred->min_dl_bandwidth_home);
3245
3246 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3247 return alloc_int_str(cred->min_ul_bandwidth_home);
3248
3249 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3250 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3251
3252 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3253 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3254
3255 if (os_strcmp(var, "max_bss_load") == 0)
3256 return alloc_int_str(cred->max_bss_load);
3257
3258 if (os_strcmp(var, "req_conn_capab") == 0) {
3259 unsigned int i;
3260 char *buf, *end, *pos;
3261 int ret;
3262
3263 if (!cred->num_req_conn_capab)
3264 return NULL;
3265
3266 buf = os_malloc(4000);
3267 if (buf == NULL)
3268 return NULL;
3269 pos = buf;
3270 end = pos + 4000;
3271 for (i = 0; i < cred->num_req_conn_capab; i++) {
3272 int *ports;
3273
3274 ret = os_snprintf(pos, end - pos, "%s%u",
3275 i > 0 ? "\n" : "",
3276 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003277 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003278 return buf;
3279 pos += ret;
3280
3281 ports = cred->req_conn_capab_port[i];
3282 if (ports) {
3283 int j;
3284 for (j = 0; ports[j] != -1; j++) {
3285 ret = os_snprintf(pos, end - pos,
3286 "%s%d",
3287 j > 0 ? "," : ":",
3288 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003289 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003290 return buf;
3291 pos += ret;
3292 }
3293 }
3294 }
3295
3296 return buf;
3297 }
3298
3299 if (os_strcmp(var, "ocsp") == 0)
3300 return alloc_int_str(cred->ocsp);
3301
3302 if (os_strcmp(var, "realm") == 0)
3303 return alloc_strdup(cred->realm);
3304
3305 if (os_strcmp(var, "username") == 0)
3306 return alloc_strdup(cred->username);
3307
3308 if (os_strcmp(var, "password") == 0) {
3309 if (!cred->password)
3310 return NULL;
3311 return alloc_strdup("*");
3312 }
3313
3314 if (os_strcmp(var, "ca_cert") == 0)
3315 return alloc_strdup(cred->ca_cert);
3316
3317 if (os_strcmp(var, "client_cert") == 0)
3318 return alloc_strdup(cred->client_cert);
3319
3320 if (os_strcmp(var, "private_key") == 0)
3321 return alloc_strdup(cred->private_key);
3322
3323 if (os_strcmp(var, "private_key_passwd") == 0) {
3324 if (!cred->private_key_passwd)
3325 return NULL;
3326 return alloc_strdup("*");
3327 }
3328
3329 if (os_strcmp(var, "imsi") == 0)
3330 return alloc_strdup(cred->imsi);
3331
3332 if (os_strcmp(var, "milenage") == 0) {
3333 if (!(cred->milenage))
3334 return NULL;
3335 return alloc_strdup("*");
3336 }
3337
3338 if (os_strcmp(var, "domain_suffix_match") == 0)
3339 return alloc_strdup(cred->domain_suffix_match);
3340
3341 if (os_strcmp(var, "domain") == 0) {
3342 unsigned int i;
3343 char *buf, *end, *pos;
3344 int ret;
3345
3346 if (!cred->num_domain)
3347 return NULL;
3348
3349 buf = os_malloc(4000);
3350 if (buf == NULL)
3351 return NULL;
3352 pos = buf;
3353 end = pos + 4000;
3354
3355 for (i = 0; i < cred->num_domain; i++) {
3356 ret = os_snprintf(pos, end - pos, "%s%s",
3357 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003358 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003359 return buf;
3360 pos += ret;
3361 }
3362
3363 return buf;
3364 }
3365
3366 if (os_strcmp(var, "phase1") == 0)
3367 return alloc_strdup(cred->phase1);
3368
3369 if (os_strcmp(var, "phase2") == 0)
3370 return alloc_strdup(cred->phase2);
3371
3372 if (os_strcmp(var, "roaming_consortium") == 0) {
3373 size_t buflen;
3374 char *buf;
3375
3376 if (!cred->roaming_consortium_len)
3377 return NULL;
3378 buflen = cred->roaming_consortium_len * 2 + 1;
3379 buf = os_malloc(buflen);
3380 if (buf == NULL)
3381 return NULL;
3382 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3383 cred->roaming_consortium_len);
3384 return buf;
3385 }
3386
3387 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3388 size_t buflen;
3389 char *buf;
3390
3391 if (!cred->required_roaming_consortium_len)
3392 return NULL;
3393 buflen = cred->required_roaming_consortium_len * 2 + 1;
3394 buf = os_malloc(buflen);
3395 if (buf == NULL)
3396 return NULL;
3397 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3398 cred->required_roaming_consortium_len);
3399 return buf;
3400 }
3401
3402 if (os_strcmp(var, "excluded_ssid") == 0) {
3403 unsigned int i;
3404 char *buf, *end, *pos;
3405
3406 if (!cred->num_excluded_ssid)
3407 return NULL;
3408
3409 buf = os_malloc(4000);
3410 if (buf == NULL)
3411 return NULL;
3412 pos = buf;
3413 end = pos + 4000;
3414
3415 for (i = 0; i < cred->num_excluded_ssid; i++) {
3416 struct excluded_ssid *e;
3417 int ret;
3418
3419 e = &cred->excluded_ssid[i];
3420 ret = os_snprintf(pos, end - pos, "%s%s",
3421 i > 0 ? "\n" : "",
3422 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003424 return buf;
3425 pos += ret;
3426 }
3427
3428 return buf;
3429 }
3430
3431 if (os_strcmp(var, "roaming_partner") == 0) {
3432 unsigned int i;
3433 char *buf, *end, *pos;
3434
3435 if (!cred->num_roaming_partner)
3436 return NULL;
3437
3438 buf = os_malloc(4000);
3439 if (buf == NULL)
3440 return NULL;
3441 pos = buf;
3442 end = pos + 4000;
3443
3444 for (i = 0; i < cred->num_roaming_partner; i++) {
3445 struct roaming_partner *p;
3446 int ret;
3447
3448 p = &cred->roaming_partner[i];
3449 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3450 i > 0 ? "\n" : "",
3451 p->fqdn, p->exact_match, p->priority,
3452 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003453 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003454 return buf;
3455 pos += ret;
3456 }
3457
3458 return buf;
3459 }
3460
3461 if (os_strcmp(var, "provisioning_sp") == 0)
3462 return alloc_strdup(cred->provisioning_sp);
3463
3464 return NULL;
3465}
3466
3467
Dmitry Shmidt04949592012-07-19 12:16:46 -07003468struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3469{
3470 struct wpa_cred *cred;
3471
3472 cred = config->cred;
3473 while (cred) {
3474 if (id == cred->id)
3475 break;
3476 cred = cred->next;
3477 }
3478
3479 return cred;
3480}
3481
3482
3483struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3484{
3485 int id;
3486 struct wpa_cred *cred, *last = NULL;
3487
3488 id = -1;
3489 cred = config->cred;
3490 while (cred) {
3491 if (cred->id > id)
3492 id = cred->id;
3493 last = cred;
3494 cred = cred->next;
3495 }
3496 id++;
3497
3498 cred = os_zalloc(sizeof(*cred));
3499 if (cred == NULL)
3500 return NULL;
3501 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003502 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003503 if (last)
3504 last->next = cred;
3505 else
3506 config->cred = cred;
3507
3508 return cred;
3509}
3510
3511
3512int wpa_config_remove_cred(struct wpa_config *config, int id)
3513{
3514 struct wpa_cred *cred, *prev = NULL;
3515
3516 cred = config->cred;
3517 while (cred) {
3518 if (id == cred->id)
3519 break;
3520 prev = cred;
3521 cred = cred->next;
3522 }
3523
3524 if (cred == NULL)
3525 return -1;
3526
3527 if (prev)
3528 prev->next = cred->next;
3529 else
3530 config->cred = cred->next;
3531
3532 wpa_config_free_cred(cred);
3533 return 0;
3534}
3535
3536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537#ifndef CONFIG_NO_CONFIG_BLOBS
3538/**
3539 * wpa_config_get_blob - Get a named configuration blob
3540 * @config: Configuration data from wpa_config_read()
3541 * @name: Name of the blob
3542 * Returns: Pointer to blob data or %NULL if not found
3543 */
3544const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3545 const char *name)
3546{
3547 struct wpa_config_blob *blob = config->blobs;
3548
3549 while (blob) {
3550 if (os_strcmp(blob->name, name) == 0)
3551 return blob;
3552 blob = blob->next;
3553 }
3554 return NULL;
3555}
3556
3557
3558/**
3559 * wpa_config_set_blob - Set or add a named configuration blob
3560 * @config: Configuration data from wpa_config_read()
3561 * @blob: New value for the blob
3562 *
3563 * Adds a new configuration blob or replaces the current value of an existing
3564 * blob.
3565 */
3566void wpa_config_set_blob(struct wpa_config *config,
3567 struct wpa_config_blob *blob)
3568{
3569 wpa_config_remove_blob(config, blob->name);
3570 blob->next = config->blobs;
3571 config->blobs = blob;
3572}
3573
3574
3575/**
3576 * wpa_config_free_blob - Free blob data
3577 * @blob: Pointer to blob to be freed
3578 */
3579void wpa_config_free_blob(struct wpa_config_blob *blob)
3580{
3581 if (blob) {
3582 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003583 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 os_free(blob);
3585 }
3586}
3587
3588
3589/**
3590 * wpa_config_remove_blob - Remove a named configuration blob
3591 * @config: Configuration data from wpa_config_read()
3592 * @name: Name of the blob to remove
3593 * Returns: 0 if blob was removed or -1 if blob was not found
3594 */
3595int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3596{
3597 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3598
3599 while (pos) {
3600 if (os_strcmp(pos->name, name) == 0) {
3601 if (prev)
3602 prev->next = pos->next;
3603 else
3604 config->blobs = pos->next;
3605 wpa_config_free_blob(pos);
3606 return 0;
3607 }
3608 prev = pos;
3609 pos = pos->next;
3610 }
3611
3612 return -1;
3613}
3614#endif /* CONFIG_NO_CONFIG_BLOBS */
3615
3616
3617/**
3618 * wpa_config_alloc_empty - Allocate an empty configuration
3619 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3620 * socket
3621 * @driver_param: Driver parameters
3622 * Returns: Pointer to allocated configuration data or %NULL on failure
3623 */
3624struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3625 const char *driver_param)
3626{
3627 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003628 const int aCWmin = 4, aCWmax = 10;
3629 const struct hostapd_wmm_ac_params ac_bk =
3630 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3631 const struct hostapd_wmm_ac_params ac_be =
3632 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3633 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3634 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3635 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3636 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637
3638 config = os_zalloc(sizeof(*config));
3639 if (config == NULL)
3640 return NULL;
3641 config->eapol_version = DEFAULT_EAPOL_VERSION;
3642 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003643 config->user_mpm = DEFAULT_USER_MPM;
3644 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003645 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003646 config->dot11RSNASAERetransPeriod =
3647 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 config->fast_reauth = DEFAULT_FAST_REAUTH;
3649 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3650 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003651 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003652 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003653 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003654 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3656 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3657 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3658 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003659 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003660 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003661 config->wmm_ac_params[0] = ac_be;
3662 config->wmm_ac_params[1] = ac_bk;
3663 config->wmm_ac_params[2] = ac_vi;
3664 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003665 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003666 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003668 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003669 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003671#ifdef CONFIG_MBO
3672 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
3673#endif /* CONFIG_MBO */
3674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 if (ctrl_interface)
3676 config->ctrl_interface = os_strdup(ctrl_interface);
3677 if (driver_param)
3678 config->driver_param = os_strdup(driver_param);
3679
3680 return config;
3681}
3682
3683
3684#ifndef CONFIG_NO_STDOUT_DEBUG
3685/**
3686 * wpa_config_debug_dump_networks - Debug dump of configured networks
3687 * @config: Configuration data from wpa_config_read()
3688 */
3689void wpa_config_debug_dump_networks(struct wpa_config *config)
3690{
3691 int prio;
3692 struct wpa_ssid *ssid;
3693
3694 for (prio = 0; prio < config->num_prio; prio++) {
3695 ssid = config->pssid[prio];
3696 wpa_printf(MSG_DEBUG, "Priority group %d",
3697 ssid->priority);
3698 while (ssid) {
3699 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3700 ssid->id,
3701 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3702 ssid = ssid->pnext;
3703 }
3704 }
3705}
3706#endif /* CONFIG_NO_STDOUT_DEBUG */
3707
3708
3709struct global_parse_data {
3710 char *name;
3711 int (*parser)(const struct global_parse_data *data,
3712 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003713 int (*get)(const char *name, struct wpa_config *config, long offset,
3714 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 void *param1, *param2, *param3;
3716 unsigned int changed_flag;
3717};
3718
3719
3720static int wpa_global_config_parse_int(const struct global_parse_data *data,
3721 struct wpa_config *config, int line,
3722 const char *pos)
3723{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003724 int val, *dst;
3725 char *end;
3726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003728 val = strtol(pos, &end, 0);
3729 if (*end) {
3730 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3731 line, pos);
3732 return -1;
3733 }
3734 *dst = val;
3735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3737
3738 if (data->param2 && *dst < (long) data->param2) {
3739 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3740 "min_value=%ld)", line, data->name, *dst,
3741 (long) data->param2);
3742 *dst = (long) data->param2;
3743 return -1;
3744 }
3745
3746 if (data->param3 && *dst > (long) data->param3) {
3747 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3748 "max_value=%ld)", line, data->name, *dst,
3749 (long) data->param3);
3750 *dst = (long) data->param3;
3751 return -1;
3752 }
3753
3754 return 0;
3755}
3756
3757
3758static int wpa_global_config_parse_str(const struct global_parse_data *data,
3759 struct wpa_config *config, int line,
3760 const char *pos)
3761{
3762 size_t len;
3763 char **dst, *tmp;
3764
3765 len = os_strlen(pos);
3766 if (data->param2 && len < (size_t) data->param2) {
3767 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3768 "min_len=%ld)", line, data->name,
3769 (unsigned long) len, (long) data->param2);
3770 return -1;
3771 }
3772
3773 if (data->param3 && len > (size_t) data->param3) {
3774 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3775 "max_len=%ld)", line, data->name,
3776 (unsigned long) len, (long) data->param3);
3777 return -1;
3778 }
3779
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003780 if (has_newline(pos)) {
3781 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
3782 line, data->name);
3783 return -1;
3784 }
3785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 tmp = os_strdup(pos);
3787 if (tmp == NULL)
3788 return -1;
3789
3790 dst = (char **) (((u8 *) config) + (long) data->param1);
3791 os_free(*dst);
3792 *dst = tmp;
3793 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3794
3795 return 0;
3796}
3797
3798
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003799static int wpa_config_process_bgscan(const struct global_parse_data *data,
3800 struct wpa_config *config, int line,
3801 const char *pos)
3802{
3803 size_t len;
3804 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003805 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003806
3807 tmp = wpa_config_parse_string(pos, &len);
3808 if (tmp == NULL) {
3809 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3810 line, data->name);
3811 return -1;
3812 }
3813
Dmitry Shmidt97672262014-02-03 13:02:54 -08003814 res = wpa_global_config_parse_str(data, config, line, tmp);
3815 os_free(tmp);
3816 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003817}
3818
3819
Dmitry Shmidt04949592012-07-19 12:16:46 -07003820static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3821 struct wpa_config *config, int line,
3822 const char *pos)
3823{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003824 struct wpabuf **dst, *tmp;
3825
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003826 tmp = wpabuf_parse_bin(pos);
3827 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003828 return -1;
3829
Dmitry Shmidt04949592012-07-19 12:16:46 -07003830 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3831 wpabuf_free(*dst);
3832 *dst = tmp;
3833 wpa_printf(MSG_DEBUG, "%s", data->name);
3834
3835 return 0;
3836}
3837
3838
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003839static int wpa_config_process_freq_list(const struct global_parse_data *data,
3840 struct wpa_config *config, int line,
3841 const char *value)
3842{
3843 int *freqs;
3844
3845 freqs = wpa_config_parse_int_array(value);
3846 if (freqs == NULL)
3847 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003848 if (freqs[0] == 0) {
3849 os_free(freqs);
3850 freqs = NULL;
3851 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003852 os_free(config->freq_list);
3853 config->freq_list = freqs;
3854 return 0;
3855}
3856
3857
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003858#ifdef CONFIG_P2P
3859static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3860 struct wpa_config *config, int line,
3861 const char *pos)
3862{
3863 u32 *dst;
3864 struct hostapd_ip_addr addr;
3865
3866 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3867 return -1;
3868 if (addr.af != AF_INET)
3869 return -1;
3870
3871 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3872 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3873 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3874 WPA_GET_BE32((u8 *) dst));
3875
3876 return 0;
3877}
3878#endif /* CONFIG_P2P */
3879
3880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881static int wpa_config_process_country(const struct global_parse_data *data,
3882 struct wpa_config *config, int line,
3883 const char *pos)
3884{
3885 if (!pos[0] || !pos[1]) {
3886 wpa_printf(MSG_DEBUG, "Invalid country set");
3887 return -1;
3888 }
3889 config->country[0] = pos[0];
3890 config->country[1] = pos[1];
3891 wpa_printf(MSG_DEBUG, "country='%c%c'",
3892 config->country[0], config->country[1]);
3893 return 0;
3894}
3895
3896
3897static int wpa_config_process_load_dynamic_eap(
3898 const struct global_parse_data *data, struct wpa_config *config,
3899 int line, const char *so)
3900{
3901 int ret;
3902 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3903 ret = eap_peer_method_load(so);
3904 if (ret == -2) {
3905 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3906 "reloading.");
3907 } else if (ret) {
3908 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3909 "method '%s'.", line, so);
3910 return -1;
3911 }
3912
3913 return 0;
3914}
3915
3916
3917#ifdef CONFIG_WPS
3918
3919static int wpa_config_process_uuid(const struct global_parse_data *data,
3920 struct wpa_config *config, int line,
3921 const char *pos)
3922{
3923 char buf[40];
3924 if (uuid_str2bin(pos, config->uuid)) {
3925 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3926 return -1;
3927 }
3928 uuid_bin2str(config->uuid, buf, sizeof(buf));
3929 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3930 return 0;
3931}
3932
3933
3934static int wpa_config_process_device_type(
3935 const struct global_parse_data *data,
3936 struct wpa_config *config, int line, const char *pos)
3937{
3938 return wps_dev_type_str2bin(pos, config->device_type);
3939}
3940
3941
3942static int wpa_config_process_os_version(const struct global_parse_data *data,
3943 struct wpa_config *config, int line,
3944 const char *pos)
3945{
3946 if (hexstr2bin(pos, config->os_version, 4)) {
3947 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3948 return -1;
3949 }
3950 wpa_printf(MSG_DEBUG, "os_version=%08x",
3951 WPA_GET_BE32(config->os_version));
3952 return 0;
3953}
3954
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955
3956static int wpa_config_process_wps_vendor_ext_m1(
3957 const struct global_parse_data *data,
3958 struct wpa_config *config, int line, const char *pos)
3959{
3960 struct wpabuf *tmp;
3961 int len = os_strlen(pos) / 2;
3962 u8 *p;
3963
3964 if (!len) {
3965 wpa_printf(MSG_ERROR, "Line %d: "
3966 "invalid wps_vendor_ext_m1", line);
3967 return -1;
3968 }
3969
3970 tmp = wpabuf_alloc(len);
3971 if (tmp) {
3972 p = wpabuf_put(tmp, len);
3973
3974 if (hexstr2bin(pos, p, len)) {
3975 wpa_printf(MSG_ERROR, "Line %d: "
3976 "invalid wps_vendor_ext_m1", line);
3977 wpabuf_free(tmp);
3978 return -1;
3979 }
3980
3981 wpabuf_free(config->wps_vendor_ext_m1);
3982 config->wps_vendor_ext_m1 = tmp;
3983 } else {
3984 wpa_printf(MSG_ERROR, "Can not allocate "
3985 "memory for wps_vendor_ext_m1");
3986 return -1;
3987 }
3988
3989 return 0;
3990}
3991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992#endif /* CONFIG_WPS */
3993
3994#ifdef CONFIG_P2P
3995static int wpa_config_process_sec_device_type(
3996 const struct global_parse_data *data,
3997 struct wpa_config *config, int line, const char *pos)
3998{
3999 int idx;
4000
4001 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4002 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4003 "items", line);
4004 return -1;
4005 }
4006
4007 idx = config->num_sec_device_types;
4008
4009 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4010 return -1;
4011
4012 config->num_sec_device_types++;
4013 return 0;
4014}
Dmitry Shmidt04949592012-07-19 12:16:46 -07004015
4016
4017static int wpa_config_process_p2p_pref_chan(
4018 const struct global_parse_data *data,
4019 struct wpa_config *config, int line, const char *pos)
4020{
4021 struct p2p_channel *pref = NULL, *n;
4022 unsigned int num = 0;
4023 const char *pos2;
4024 u8 op_class, chan;
4025
4026 /* format: class:chan,class:chan,... */
4027
4028 while (*pos) {
4029 op_class = atoi(pos);
4030 pos2 = os_strchr(pos, ':');
4031 if (pos2 == NULL)
4032 goto fail;
4033 pos2++;
4034 chan = atoi(pos2);
4035
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004036 n = os_realloc_array(pref, num + 1,
4037 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004038 if (n == NULL)
4039 goto fail;
4040 pref = n;
4041 pref[num].op_class = op_class;
4042 pref[num].chan = chan;
4043 num++;
4044
4045 pos = os_strchr(pos2, ',');
4046 if (pos == NULL)
4047 break;
4048 pos++;
4049 }
4050
4051 os_free(config->p2p_pref_chan);
4052 config->p2p_pref_chan = pref;
4053 config->num_p2p_pref_chan = num;
4054 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4055 (u8 *) config->p2p_pref_chan,
4056 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4057
4058 return 0;
4059
4060fail:
4061 os_free(pref);
4062 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4063 return -1;
4064}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004065
4066
4067static int wpa_config_process_p2p_no_go_freq(
4068 const struct global_parse_data *data,
4069 struct wpa_config *config, int line, const char *pos)
4070{
4071 int ret;
4072
4073 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4074 if (ret < 0) {
4075 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4076 return -1;
4077 }
4078
4079 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4080 config->p2p_no_go_freq.num);
4081
4082 return 0;
4083}
4084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085#endif /* CONFIG_P2P */
4086
4087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088static int wpa_config_process_hessid(
4089 const struct global_parse_data *data,
4090 struct wpa_config *config, int line, const char *pos)
4091{
4092 if (hwaddr_aton2(pos, config->hessid) < 0) {
4093 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4094 line, pos);
4095 return -1;
4096 }
4097
4098 return 0;
4099}
4100
4101
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004102static int wpa_config_process_sae_groups(
4103 const struct global_parse_data *data,
4104 struct wpa_config *config, int line, const char *pos)
4105{
4106 int *groups = wpa_config_parse_int_array(pos);
4107 if (groups == NULL) {
4108 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4109 line, pos);
4110 return -1;
4111 }
4112
4113 os_free(config->sae_groups);
4114 config->sae_groups = groups;
4115
4116 return 0;
4117}
4118
4119
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004120static int wpa_config_process_ap_vendor_elements(
4121 const struct global_parse_data *data,
4122 struct wpa_config *config, int line, const char *pos)
4123{
4124 struct wpabuf *tmp;
4125 int len = os_strlen(pos) / 2;
4126 u8 *p;
4127
4128 if (!len) {
4129 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4130 line);
4131 return -1;
4132 }
4133
4134 tmp = wpabuf_alloc(len);
4135 if (tmp) {
4136 p = wpabuf_put(tmp, len);
4137
4138 if (hexstr2bin(pos, p, len)) {
4139 wpa_printf(MSG_ERROR, "Line %d: invalid "
4140 "ap_vendor_elements", line);
4141 wpabuf_free(tmp);
4142 return -1;
4143 }
4144
4145 wpabuf_free(config->ap_vendor_elements);
4146 config->ap_vendor_elements = tmp;
4147 } else {
4148 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4149 "ap_vendor_elements");
4150 return -1;
4151 }
4152
4153 return 0;
4154}
4155
4156
Dmitry Shmidt56052862013-10-04 10:23:25 -07004157#ifdef CONFIG_CTRL_IFACE
4158static int wpa_config_process_no_ctrl_interface(
4159 const struct global_parse_data *data,
4160 struct wpa_config *config, int line, const char *pos)
4161{
4162 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4163 os_free(config->ctrl_interface);
4164 config->ctrl_interface = NULL;
4165 return 0;
4166}
4167#endif /* CONFIG_CTRL_IFACE */
4168
4169
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004170static int wpa_config_get_int(const char *name, struct wpa_config *config,
4171 long offset, char *buf, size_t buflen,
4172 int pretty_print)
4173{
4174 int *val = (int *) (((u8 *) config) + (long) offset);
4175
4176 if (pretty_print)
4177 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4178 return os_snprintf(buf, buflen, "%d", *val);
4179}
4180
4181
4182static int wpa_config_get_str(const char *name, struct wpa_config *config,
4183 long offset, char *buf, size_t buflen,
4184 int pretty_print)
4185{
4186 char **val = (char **) (((u8 *) config) + (long) offset);
4187 int res;
4188
4189 if (pretty_print)
4190 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4191 *val ? *val : "null");
4192 else if (!*val)
4193 return -1;
4194 else
4195 res = os_snprintf(buf, buflen, "%s", *val);
4196 if (os_snprintf_error(buflen, res))
4197 res = -1;
4198
4199 return res;
4200}
4201
4202
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004203#ifdef CONFIG_P2P
4204static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4205 long offset, char *buf, size_t buflen,
4206 int pretty_print)
4207{
4208 void *val = ((u8 *) config) + (long) offset;
4209 int res;
4210 char addr[INET_ADDRSTRLEN];
4211
4212 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4213 return -1;
4214
4215 if (pretty_print)
4216 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4217 else
4218 res = os_snprintf(buf, buflen, "%s", addr);
4219
4220 if (os_snprintf_error(buflen, res))
4221 res = -1;
4222
4223 return res;
4224}
4225#endif /* CONFIG_P2P */
4226
4227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228#ifdef OFFSET
4229#undef OFFSET
4230#endif /* OFFSET */
4231/* OFFSET: Get offset of a variable within the wpa_config structure */
4232#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4233
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004234#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4235#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4236#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237#define INT(f) _INT(f), NULL, NULL
4238#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004239#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240#define STR(f) _STR(f), NULL, NULL
4241#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004242#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004243#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4244 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245
4246static const struct global_parse_data global_fields[] = {
4247#ifdef CONFIG_CTRL_IFACE
4248 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004249 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 { STR(ctrl_interface_group), 0 } /* deprecated */,
4251#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004252#ifdef CONFIG_MACSEC
4253 { INT_RANGE(eapol_version, 1, 3), 0 },
4254#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004256#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004258 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004259#ifdef CONFIG_MESH
4260 { INT(user_mpm), 0 },
4261 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004262 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004263 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004264#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004265 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266 { INT(fast_reauth), 0 },
4267 { STR(opensc_engine_path), 0 },
4268 { STR(pkcs11_engine_path), 0 },
4269 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004270 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 { STR(pcsc_reader), 0 },
4272 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004273 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 { STR(driver_param), 0 },
4275 { INT(dot11RSNAConfigPMKLifetime), 0 },
4276 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4277 { INT(dot11RSNAConfigSATimeout), 0 },
4278#ifndef CONFIG_NO_CONFIG_WRITE
4279 { INT(update_config), 0 },
4280#endif /* CONFIG_NO_CONFIG_WRITE */
4281 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4282#ifdef CONFIG_WPS
4283 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004284 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4285 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4287 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4288 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4289 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4290 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4291 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4292 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4293 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004294 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295#endif /* CONFIG_WPS */
4296#ifdef CONFIG_P2P
4297 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4299 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004300 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4301 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4303 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4304 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4305 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4306 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004307 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004308 { INT_RANGE(p2p_passphrase_len, 8, 63),
4309 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004310 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004311 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4312 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004313 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004314 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004315 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004316 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004317 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004318 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004319 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004320 { IPV4(ip_addr_go), 0 },
4321 { IPV4(ip_addr_mask), 0 },
4322 { IPV4(ip_addr_start), 0 },
4323 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004324 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325#endif /* CONFIG_P2P */
4326 { FUNC(country), CFG_CHANGED_COUNTRY },
4327 { INT(bss_max_count), 0 },
4328 { INT(bss_expiration_age), 0 },
4329 { INT(bss_expiration_scan_count), 0 },
4330 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004331 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004333 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004334#ifdef CONFIG_HS20
4335 { INT_RANGE(hs20, 0, 1), 0 },
4336#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004337 { INT_RANGE(interworking, 0, 1), 0 },
4338 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004339 { INT_RANGE(access_network_type, 0, 15), 0 },
4340 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4341 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004342 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4343 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4344 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4345 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4346 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004347 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4348 { INT(p2p_go_max_inactivity), 0 },
4349 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004350 { INT(okc), 0 },
4351 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004352 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004353 { INT(dtim_period), 0 },
4354 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004355 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004356 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004357 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004358 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004359 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004360 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004361 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004362 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004363 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004364 { INT(mac_addr), 0 },
4365 { INT(rand_addr_lifetime), 0 },
4366 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004367 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004368 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004369 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004370 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004371#ifdef CONFIG_FST
4372 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4373 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4374 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4375#endif /* CONFIG_FST */
4376 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004377 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004378#ifdef CONFIG_MBO
4379 { STR(non_pref_chan), 0 },
4380 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
4381 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
4382#endif /*CONFIG_MBO */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004383 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07004384 { INT_RANGE(ftm_responder, 0, 1), 0 },
4385 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386};
4387
4388#undef FUNC
4389#undef _INT
4390#undef INT
4391#undef INT_RANGE
4392#undef _STR
4393#undef STR
4394#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004395#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004396#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004397#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398
4399
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004400int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4401{
4402 int result = 0;
4403 size_t i;
4404
4405 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4406 const struct global_parse_data *field = &global_fields[i];
4407 int tmp;
4408
4409 if (!field->get)
4410 continue;
4411
4412 tmp = field->get(field->name, config, (long) field->param1,
4413 buf, buflen, 1);
4414 if (tmp < 0)
4415 return -1;
4416 buf += tmp;
4417 buflen -= tmp;
4418 result += tmp;
4419 }
4420 return result;
4421}
4422
4423
4424int wpa_config_get_value(const char *name, struct wpa_config *config,
4425 char *buf, size_t buflen)
4426{
4427 size_t i;
4428
4429 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4430 const struct global_parse_data *field = &global_fields[i];
4431
4432 if (os_strcmp(name, field->name) != 0)
4433 continue;
4434 if (!field->get)
4435 break;
4436 return field->get(name, config, (long) field->param1,
4437 buf, buflen, 0);
4438 }
4439
4440 return -1;
4441}
4442
4443
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004444int wpa_config_get_num_global_field_names(void)
4445{
4446 return NUM_GLOBAL_FIELDS;
4447}
4448
4449
4450const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4451{
4452 if (i >= NUM_GLOBAL_FIELDS)
4453 return NULL;
4454
4455 if (no_var)
4456 *no_var = !global_fields[i].param1;
4457 return global_fields[i].name;
4458}
4459
4460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4462{
4463 size_t i;
4464 int ret = 0;
4465
4466 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4467 const struct global_parse_data *field = &global_fields[i];
4468 size_t flen = os_strlen(field->name);
4469 if (os_strncmp(pos, field->name, flen) != 0 ||
4470 pos[flen] != '=')
4471 continue;
4472
4473 if (field->parser(field, config, line, pos + flen + 1)) {
4474 wpa_printf(MSG_ERROR, "Line %d: failed to "
4475 "parse '%s'.", line, pos);
4476 ret = -1;
4477 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004478 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4479 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 config->changed_parameters |= field->changed_flag;
4481 break;
4482 }
4483 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004484#ifdef CONFIG_AP
4485 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4486 char *tmp = os_strchr(pos, '=');
4487 if (tmp == NULL) {
4488 if (line < 0)
4489 return -1;
4490 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4491 "'%s'", line, pos);
4492 return -1;
4493 }
4494 *tmp++ = '\0';
4495 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4496 tmp)) {
4497 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4498 "AC item", line);
4499 return -1;
4500 }
4501 }
4502#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 if (line < 0)
4504 return -1;
4505 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4506 line, pos);
4507 ret = -1;
4508 }
4509
4510 return ret;
4511}