blob: 2120a6ec67d7ed73d0c29957eec9217936917ebf [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 Shmidtabb90a32016-12-05 15:34:39 -08001831#ifdef CONFIG_MACSEC
1832
1833static int wpa_config_parse_mka_cak(const struct parse_data *data,
1834 struct wpa_ssid *ssid, int line,
1835 const char *value)
1836{
1837 if (hexstr2bin(value, ssid->mka_cak, MACSEC_CAK_LEN) ||
1838 value[MACSEC_CAK_LEN * 2] != '\0') {
1839 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
1840 line, value);
1841 return -1;
1842 }
1843
1844 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
1845
1846 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak, MACSEC_CAK_LEN);
1847 return 0;
1848}
1849
1850
1851static int wpa_config_parse_mka_ckn(const struct parse_data *data,
1852 struct wpa_ssid *ssid, int line,
1853 const char *value)
1854{
1855 if (hexstr2bin(value, ssid->mka_ckn, MACSEC_CKN_LEN) ||
1856 value[MACSEC_CKN_LEN * 2] != '\0') {
1857 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
1858 line, value);
1859 return -1;
1860 }
1861
1862 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
1863
1864 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn, MACSEC_CKN_LEN);
1865 return 0;
1866}
1867
1868
1869#ifndef NO_CONFIG_WRITE
1870
1871static char * wpa_config_write_mka_cak(const struct parse_data *data,
1872 struct wpa_ssid *ssid)
1873{
1874 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
1875 return NULL;
1876
1877 return wpa_config_write_string_hex(ssid->mka_cak, MACSEC_CAK_LEN);
1878}
1879
1880
1881static char * wpa_config_write_mka_ckn(const struct parse_data *data,
1882 struct wpa_ssid *ssid)
1883{
1884 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
1885 return NULL;
1886 return wpa_config_write_string_hex(ssid->mka_ckn, MACSEC_CKN_LEN);
1887}
1888
1889#endif /* NO_CONFIG_WRITE */
1890
1891#endif /* CONFIG_MACSEC */
1892
1893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894/* Helper macros for network block parser */
1895
1896#ifdef OFFSET
1897#undef OFFSET
1898#endif /* OFFSET */
1899/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1900#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1901
1902/* STR: Define a string variable for an ASCII string; f = field name */
1903#ifdef NO_CONFIG_WRITE
1904#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1905#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1906#else /* NO_CONFIG_WRITE */
1907#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1908#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1909#endif /* NO_CONFIG_WRITE */
1910#define STR(f) _STR(f), NULL, NULL, NULL, 0
1911#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1912#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1913#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1914
1915/* STR_LEN: Define a string variable with a separate variable for storing the
1916 * data length. Unlike STR(), this can be used to store arbitrary binary data
1917 * (i.e., even nul termination character). */
1918#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1919#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1920#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1921#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1922#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1923
1924/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1925 * explicitly specified. */
1926#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1927#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1928#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1929
1930#ifdef NO_CONFIG_WRITE
1931#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1932#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1933#else /* NO_CONFIG_WRITE */
1934#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1935 OFFSET(f), (void *) 0
1936#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1937 OFFSET(eap.f), (void *) 0
1938#endif /* NO_CONFIG_WRITE */
1939
1940/* INT: Define an integer variable */
1941#define INT(f) _INT(f), NULL, NULL, 0
1942#define INTe(f) _INTe(f), NULL, NULL, 0
1943
1944/* INT_RANGE: Define an integer variable with allowed value range */
1945#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1946
1947/* FUNC: Define a configuration variable that uses a custom function for
1948 * parsing and writing the value. */
1949#ifdef NO_CONFIG_WRITE
1950#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1951#else /* NO_CONFIG_WRITE */
1952#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1953 NULL, NULL, NULL, NULL
1954#endif /* NO_CONFIG_WRITE */
1955#define FUNC(f) _FUNC(f), 0
1956#define FUNC_KEY(f) _FUNC(f), 1
1957
1958/*
1959 * Table of network configuration variables. This table is used to parse each
1960 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1961 * that is inside a network block.
1962 *
1963 * This table is generated using the helper macros defined above and with
1964 * generous help from the C pre-processor. The field name is stored as a string
1965 * into .name and for STR and INT types, the offset of the target buffer within
1966 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1967 * offset to the field containing the length of the configuration variable.
1968 * .param3 and .param4 can be used to mark the allowed range (length for STR
1969 * and value for INT).
1970 *
1971 * For each configuration line in wpa_supplicant.conf, the parser goes through
1972 * this table and select the entry that matches with the field name. The parser
1973 * function (.parser) is then called to parse the actual value of the field.
1974 *
1975 * This kind of mechanism makes it easy to add new configuration parameters,
1976 * since only one line needs to be added into this table and into the
1977 * struct wpa_ssid definition if the new variable is either a string or
1978 * integer. More complex types will need to use their own parser and writer
1979 * functions.
1980 */
1981static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001982 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 { INT_RANGE(scan_ssid, 0, 1) },
1984 { FUNC(bssid) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001985 { FUNC(bssid_blacklist) },
1986 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001988 { INT(mem_only_psk) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 { FUNC(proto) },
1990 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001991 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 { FUNC(pairwise) },
1993 { FUNC(group) },
1994 { FUNC(auth_alg) },
1995 { FUNC(scan_freq) },
1996 { FUNC(freq_list) },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001997 { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
1998 VHT_CHANWIDTH_80P80MHZ) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999#ifdef IEEE8021X_EAPOL
2000 { FUNC(eap) },
2001 { STR_LENe(identity) },
2002 { STR_LENe(anonymous_identity) },
2003 { FUNC_KEY(password) },
2004 { STRe(ca_cert) },
2005 { STRe(ca_path) },
2006 { STRe(client_cert) },
2007 { STRe(private_key) },
2008 { STR_KEYe(private_key_passwd) },
2009 { STRe(dh_file) },
2010 { STRe(subject_match) },
2011 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07002012 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002013 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 { STRe(ca_cert2) },
2015 { STRe(ca_path2) },
2016 { STRe(client_cert2) },
2017 { STRe(private_key2) },
2018 { STR_KEYe(private_key2_passwd) },
2019 { STRe(dh_file2) },
2020 { STRe(subject_match2) },
2021 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07002022 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002023 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 { STRe(phase1) },
2025 { STRe(phase2) },
2026 { STRe(pcsc) },
2027 { STR_KEYe(pin) },
2028 { STRe(engine_id) },
2029 { STRe(key_id) },
2030 { STRe(cert_id) },
2031 { STRe(ca_cert_id) },
2032 { STR_KEYe(pin2) },
2033 { STRe(engine2_id) },
2034 { STRe(key2_id) },
2035 { STRe(cert2_id) },
2036 { STRe(ca_cert2_id) },
2037 { INTe(engine) },
2038 { INTe(engine2) },
2039 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002040 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002041 { STRe(openssl_ciphers) },
2042 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043#endif /* IEEE8021X_EAPOL */
2044 { FUNC_KEY(wep_key0) },
2045 { FUNC_KEY(wep_key1) },
2046 { FUNC_KEY(wep_key2) },
2047 { FUNC_KEY(wep_key3) },
2048 { INT(wep_tx_keyidx) },
2049 { INT(priority) },
2050#ifdef IEEE8021X_EAPOL
2051 { INT(eap_workaround) },
2052 { STRe(pac_file) },
2053 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002054 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002056#ifdef CONFIG_MESH
2057 { INT_RANGE(mode, 0, 5) },
2058 { INT_RANGE(no_auto_peer, 0, 1) },
2059#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002061#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 { INT_RANGE(proactive_key_caching, 0, 1) },
2063 { INT_RANGE(disabled, 0, 2) },
2064 { STR(id_str) },
2065#ifdef CONFIG_IEEE80211W
2066 { INT_RANGE(ieee80211w, 0, 2) },
2067#endif /* CONFIG_IEEE80211W */
2068 { INT_RANGE(peerkey, 0, 1) },
2069 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002070 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002071 { INT_RANGE(fixed_freq, 0, 1) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002072#ifdef CONFIG_ACS
2073 { INT_RANGE(acs, 0, 1) },
2074#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002075#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002076 { FUNC(mesh_basic_rates) },
2077 { INT(dot11MeshMaxRetries) },
2078 { INT(dot11MeshRetryTimeout) },
2079 { INT(dot11MeshConfirmTimeout) },
2080 { INT(dot11MeshHoldingTimeout) },
2081#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 { INT(wpa_ptk_rekey) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002083 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002085 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002086#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002087 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002088 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002089 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002091#ifdef CONFIG_HT_OVERRIDES
2092 { INT_RANGE(disable_ht, 0, 1) },
2093 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002094 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002095 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002096 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002097 { INT_RANGE(disable_max_amsdu, -1, 1) },
2098 { INT_RANGE(ampdu_factor, -1, 3) },
2099 { INT_RANGE(ampdu_density, -1, 7) },
2100 { STR(ht_mcs) },
2101#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002102#ifdef CONFIG_VHT_OVERRIDES
2103 { INT_RANGE(disable_vht, 0, 1) },
2104 { INT(vht_capa) },
2105 { INT(vht_capa_mask) },
2106 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2107 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2108 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2109 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2110 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2111 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2112 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2113 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2114 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2115 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2116 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2117 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2118 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2119 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2120 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2121 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2122#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002123 { INT(ap_max_inactivity) },
2124 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002125 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002126#ifdef CONFIG_MACSEC
2127 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002128 { INT_RANGE(macsec_integ_only, 0, 1) },
2129 { INT_RANGE(macsec_port, 1, 65534) },
2130 { FUNC_KEY(mka_cak) },
2131 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002132#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002133#ifdef CONFIG_HS20
2134 { INT(update_identifier) },
2135#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002136 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002137 { INT_RANGE(pbss, 0, 2) },
2138 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139};
2140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141#undef OFFSET
2142#undef _STR
2143#undef STR
2144#undef STR_KEY
2145#undef _STR_LEN
2146#undef STR_LEN
2147#undef STR_LEN_KEY
2148#undef _STR_RANGE
2149#undef STR_RANGE
2150#undef STR_RANGE_KEY
2151#undef _INT
2152#undef INT
2153#undef INT_RANGE
2154#undef _FUNC
2155#undef FUNC
2156#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002157#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158
2159
2160/**
2161 * wpa_config_add_prio_network - Add a network to priority lists
2162 * @config: Configuration data from wpa_config_read()
2163 * @ssid: Pointer to the network configuration to be added to the list
2164 * Returns: 0 on success, -1 on failure
2165 *
2166 * This function is used to add a network block to the priority list of
2167 * networks. This must be called for each network when reading in the full
2168 * configuration. In addition, this can be used indirectly when updating
2169 * priorities by calling wpa_config_update_prio_list().
2170 */
2171int wpa_config_add_prio_network(struct wpa_config *config,
2172 struct wpa_ssid *ssid)
2173{
2174 int prio;
2175 struct wpa_ssid *prev, **nlist;
2176
2177 /*
2178 * Add to an existing priority list if one is available for the
2179 * configured priority level for this network.
2180 */
2181 for (prio = 0; prio < config->num_prio; prio++) {
2182 prev = config->pssid[prio];
2183 if (prev->priority == ssid->priority) {
2184 while (prev->pnext)
2185 prev = prev->pnext;
2186 prev->pnext = ssid;
2187 return 0;
2188 }
2189 }
2190
2191 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002192 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2193 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 if (nlist == NULL)
2195 return -1;
2196
2197 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002198 if (nlist[prio]->priority < ssid->priority) {
2199 os_memmove(&nlist[prio + 1], &nlist[prio],
2200 (config->num_prio - prio) *
2201 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002203 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 }
2205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 nlist[prio] = ssid;
2207 config->num_prio++;
2208 config->pssid = nlist;
2209
2210 return 0;
2211}
2212
2213
2214/**
2215 * wpa_config_update_prio_list - Update network priority list
2216 * @config: Configuration data from wpa_config_read()
2217 * Returns: 0 on success, -1 on failure
2218 *
2219 * This function is called to update the priority list of networks in the
2220 * configuration when a network is being added or removed. This is also called
2221 * if a priority for a network is changed.
2222 */
2223int wpa_config_update_prio_list(struct wpa_config *config)
2224{
2225 struct wpa_ssid *ssid;
2226 int ret = 0;
2227
2228 os_free(config->pssid);
2229 config->pssid = NULL;
2230 config->num_prio = 0;
2231
2232 ssid = config->ssid;
2233 while (ssid) {
2234 ssid->pnext = NULL;
2235 if (wpa_config_add_prio_network(config, ssid) < 0)
2236 ret = -1;
2237 ssid = ssid->next;
2238 }
2239
2240 return ret;
2241}
2242
2243
2244#ifdef IEEE8021X_EAPOL
2245static void eap_peer_config_free(struct eap_peer_config *eap)
2246{
2247 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002248 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002250 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 os_free(eap->ca_cert);
2252 os_free(eap->ca_path);
2253 os_free(eap->client_cert);
2254 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002255 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 os_free(eap->dh_file);
2257 os_free(eap->subject_match);
2258 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002259 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002260 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 os_free(eap->ca_cert2);
2262 os_free(eap->ca_path2);
2263 os_free(eap->client_cert2);
2264 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002265 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 os_free(eap->dh_file2);
2267 os_free(eap->subject_match2);
2268 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002269 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002270 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 os_free(eap->phase1);
2272 os_free(eap->phase2);
2273 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002274 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 os_free(eap->engine_id);
2276 os_free(eap->key_id);
2277 os_free(eap->cert_id);
2278 os_free(eap->ca_cert_id);
2279 os_free(eap->key2_id);
2280 os_free(eap->cert2_id);
2281 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002282 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 os_free(eap->engine2_id);
2284 os_free(eap->otp);
2285 os_free(eap->pending_req_otp);
2286 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002287 bin_clear_free(eap->new_password, eap->new_password_len);
2288 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002289 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290}
2291#endif /* IEEE8021X_EAPOL */
2292
2293
2294/**
2295 * wpa_config_free_ssid - Free network/ssid configuration data
2296 * @ssid: Configuration data for the network
2297 *
2298 * This function frees all resources allocated for the network configuration
2299 * data.
2300 */
2301void wpa_config_free_ssid(struct wpa_ssid *ssid)
2302{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002303 struct psk_list_entry *psk;
2304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002306 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002307 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308#ifdef IEEE8021X_EAPOL
2309 eap_peer_config_free(&ssid->eap);
2310#endif /* IEEE8021X_EAPOL */
2311 os_free(ssid->id_str);
2312 os_free(ssid->scan_freq);
2313 os_free(ssid->freq_list);
2314 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002316 os_free(ssid->bssid_blacklist);
2317 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002318#ifdef CONFIG_HT_OVERRIDES
2319 os_free(ssid->ht_mcs);
2320#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002321#ifdef CONFIG_MESH
2322 os_free(ssid->mesh_basic_rates);
2323#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002324 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2325 list))) {
2326 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002327 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002328 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002329 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330}
2331
2332
Dmitry Shmidt04949592012-07-19 12:16:46 -07002333void wpa_config_free_cred(struct wpa_cred *cred)
2334{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002335 size_t i;
2336
Dmitry Shmidt04949592012-07-19 12:16:46 -07002337 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002338 str_clear_free(cred->username);
2339 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002340 os_free(cred->ca_cert);
2341 os_free(cred->client_cert);
2342 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002343 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002344 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002345 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002346 for (i = 0; i < cred->num_domain; i++)
2347 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002348 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002349 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002350 os_free(cred->eap_method);
2351 os_free(cred->phase1);
2352 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002353 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002354 os_free(cred->roaming_partner);
2355 os_free(cred->provisioning_sp);
2356 for (i = 0; i < cred->num_req_conn_capab; i++)
2357 os_free(cred->req_conn_capab_port[i]);
2358 os_free(cred->req_conn_capab_port);
2359 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002360 os_free(cred);
2361}
2362
2363
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002364void wpa_config_flush_blobs(struct wpa_config *config)
2365{
2366#ifndef CONFIG_NO_CONFIG_BLOBS
2367 struct wpa_config_blob *blob, *prev;
2368
2369 blob = config->blobs;
2370 config->blobs = NULL;
2371 while (blob) {
2372 prev = blob;
2373 blob = blob->next;
2374 wpa_config_free_blob(prev);
2375 }
2376#endif /* CONFIG_NO_CONFIG_BLOBS */
2377}
2378
2379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380/**
2381 * wpa_config_free - Free configuration data
2382 * @config: Configuration data from wpa_config_read()
2383 *
2384 * This function frees all resources allocated for the configuration data by
2385 * wpa_config_read().
2386 */
2387void wpa_config_free(struct wpa_config *config)
2388{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002390 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002391 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392
2393 ssid = config->ssid;
2394 while (ssid) {
2395 prev = ssid;
2396 ssid = ssid->next;
2397 wpa_config_free_ssid(prev);
2398 }
2399
Dmitry Shmidt04949592012-07-19 12:16:46 -07002400 cred = config->cred;
2401 while (cred) {
2402 cprev = cred;
2403 cred = cred->next;
2404 wpa_config_free_cred(cprev);
2405 }
2406
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002407 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408
Dmitry Shmidt04949592012-07-19 12:16:46 -07002409 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002410 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2411 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 os_free(config->ctrl_interface);
2413 os_free(config->ctrl_interface_group);
2414 os_free(config->opensc_engine_path);
2415 os_free(config->pkcs11_engine_path);
2416 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002417 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002418 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002419 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 os_free(config->driver_param);
2421 os_free(config->device_name);
2422 os_free(config->manufacturer);
2423 os_free(config->model_name);
2424 os_free(config->model_number);
2425 os_free(config->serial_number);
2426 os_free(config->config_methods);
2427 os_free(config->p2p_ssid_postfix);
2428 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002429 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002430 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002431 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002432 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002433 wpabuf_free(config->wps_nfc_dh_pubkey);
2434 wpabuf_free(config->wps_nfc_dh_privkey);
2435 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002436 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002437 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002438 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002439 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002440 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002441 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002442 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002443 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002444#ifdef CONFIG_MBO
2445 os_free(config->non_pref_chan);
2446#endif /* CONFIG_MBO */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 os_free(config);
2449}
2450
2451
2452/**
2453 * wpa_config_foreach_network - Iterate over each configured network
2454 * @config: Configuration data from wpa_config_read()
2455 * @func: Callback function to process each network
2456 * @arg: Opaque argument to pass to callback function
2457 *
2458 * Iterate over the set of configured networks calling the specified
2459 * function for each item. We guard against callbacks removing the
2460 * supplied network.
2461 */
2462void wpa_config_foreach_network(struct wpa_config *config,
2463 void (*func)(void *, struct wpa_ssid *),
2464 void *arg)
2465{
2466 struct wpa_ssid *ssid, *next;
2467
2468 ssid = config->ssid;
2469 while (ssid) {
2470 next = ssid->next;
2471 func(arg, ssid);
2472 ssid = next;
2473 }
2474}
2475
2476
2477/**
2478 * wpa_config_get_network - Get configured network based on id
2479 * @config: Configuration data from wpa_config_read()
2480 * @id: Unique network id to search for
2481 * Returns: Network configuration or %NULL if not found
2482 */
2483struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2484{
2485 struct wpa_ssid *ssid;
2486
2487 ssid = config->ssid;
2488 while (ssid) {
2489 if (id == ssid->id)
2490 break;
2491 ssid = ssid->next;
2492 }
2493
2494 return ssid;
2495}
2496
2497
2498/**
2499 * wpa_config_add_network - Add a new network with empty configuration
2500 * @config: Configuration data from wpa_config_read()
2501 * Returns: The new network configuration or %NULL if operation failed
2502 */
2503struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2504{
2505 int id;
2506 struct wpa_ssid *ssid, *last = NULL;
2507
2508 id = -1;
2509 ssid = config->ssid;
2510 while (ssid) {
2511 if (ssid->id > id)
2512 id = ssid->id;
2513 last = ssid;
2514 ssid = ssid->next;
2515 }
2516 id++;
2517
2518 ssid = os_zalloc(sizeof(*ssid));
2519 if (ssid == NULL)
2520 return NULL;
2521 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002522 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 if (last)
2524 last->next = ssid;
2525 else
2526 config->ssid = ssid;
2527
2528 wpa_config_update_prio_list(config);
2529
2530 return ssid;
2531}
2532
2533
2534/**
2535 * wpa_config_remove_network - Remove a configured network based on id
2536 * @config: Configuration data from wpa_config_read()
2537 * @id: Unique network id to search for
2538 * Returns: 0 on success, or -1 if the network was not found
2539 */
2540int wpa_config_remove_network(struct wpa_config *config, int id)
2541{
2542 struct wpa_ssid *ssid, *prev = NULL;
2543
2544 ssid = config->ssid;
2545 while (ssid) {
2546 if (id == ssid->id)
2547 break;
2548 prev = ssid;
2549 ssid = ssid->next;
2550 }
2551
2552 if (ssid == NULL)
2553 return -1;
2554
2555 if (prev)
2556 prev->next = ssid->next;
2557 else
2558 config->ssid = ssid->next;
2559
2560 wpa_config_update_prio_list(config);
2561 wpa_config_free_ssid(ssid);
2562 return 0;
2563}
2564
2565
2566/**
2567 * wpa_config_set_network_defaults - Set network default values
2568 * @ssid: Pointer to network configuration data
2569 */
2570void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2571{
2572 ssid->proto = DEFAULT_PROTO;
2573 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2574 ssid->group_cipher = DEFAULT_GROUP;
2575 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002576 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577#ifdef IEEE8021X_EAPOL
2578 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2579 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2580 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002581 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002583#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002584 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2585 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2586 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2587 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2588#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002589#ifdef CONFIG_HT_OVERRIDES
2590 ssid->disable_ht = DEFAULT_DISABLE_HT;
2591 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002592 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002593 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002594 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2595 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2596 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2597#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002598#ifdef CONFIG_VHT_OVERRIDES
2599 ssid->vht_rx_mcs_nss_1 = -1;
2600 ssid->vht_rx_mcs_nss_2 = -1;
2601 ssid->vht_rx_mcs_nss_3 = -1;
2602 ssid->vht_rx_mcs_nss_4 = -1;
2603 ssid->vht_rx_mcs_nss_5 = -1;
2604 ssid->vht_rx_mcs_nss_6 = -1;
2605 ssid->vht_rx_mcs_nss_7 = -1;
2606 ssid->vht_rx_mcs_nss_8 = -1;
2607 ssid->vht_tx_mcs_nss_1 = -1;
2608 ssid->vht_tx_mcs_nss_2 = -1;
2609 ssid->vht_tx_mcs_nss_3 = -1;
2610 ssid->vht_tx_mcs_nss_4 = -1;
2611 ssid->vht_tx_mcs_nss_5 = -1;
2612 ssid->vht_tx_mcs_nss_6 = -1;
2613 ssid->vht_tx_mcs_nss_7 = -1;
2614 ssid->vht_tx_mcs_nss_8 = -1;
2615#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002616 ssid->proactive_key_caching = -1;
2617#ifdef CONFIG_IEEE80211W
2618 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2619#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002620 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621}
2622
2623
2624/**
2625 * wpa_config_set - Set a variable in network configuration
2626 * @ssid: Pointer to network configuration data
2627 * @var: Variable name, e.g., "ssid"
2628 * @value: Variable value
2629 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07002630 * Returns: 0 on success with possible change in the value, 1 on success with
2631 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 *
2633 * This function can be used to set network configuration variables based on
2634 * both the configuration file and management interface input. The value
2635 * parameter must be in the same format as the text-based configuration file is
2636 * using. For example, strings are using double quotation marks.
2637 */
2638int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2639 int line)
2640{
2641 size_t i;
2642 int ret = 0;
2643
2644 if (ssid == NULL || var == NULL || value == NULL)
2645 return -1;
2646
2647 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2648 const struct parse_data *field = &ssid_fields[i];
2649 if (os_strcmp(var, field->name) != 0)
2650 continue;
2651
Dmitry Shmidte4663042016-04-04 10:07:49 -07002652 ret = field->parser(field, ssid, line, value);
2653 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 if (line) {
2655 wpa_printf(MSG_ERROR, "Line %d: failed to "
2656 "parse %s '%s'.", line, var, value);
2657 }
2658 ret = -1;
2659 }
2660 break;
2661 }
2662 if (i == NUM_SSID_FIELDS) {
2663 if (line) {
2664 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2665 "'%s'.", line, var);
2666 }
2667 ret = -1;
2668 }
2669
2670 return ret;
2671}
2672
2673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002674int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2675 const char *value)
2676{
2677 size_t len;
2678 char *buf;
2679 int ret;
2680
2681 len = os_strlen(value);
2682 buf = os_malloc(len + 3);
2683 if (buf == NULL)
2684 return -1;
2685 buf[0] = '"';
2686 os_memcpy(buf + 1, value, len);
2687 buf[len + 1] = '"';
2688 buf[len + 2] = '\0';
2689 ret = wpa_config_set(ssid, var, buf, 0);
2690 os_free(buf);
2691 return ret;
2692}
2693
2694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695/**
2696 * wpa_config_get_all - Get all options from network configuration
2697 * @ssid: Pointer to network configuration data
2698 * @get_keys: Determines if keys/passwords will be included in returned list
2699 * (if they may be exported)
2700 * Returns: %NULL terminated list of all set keys and their values in the form
2701 * of [key1, val1, key2, val2, ... , NULL]
2702 *
2703 * This function can be used to get list of all configured network properties.
2704 * The caller is responsible for freeing the returned list and all its
2705 * elements.
2706 */
2707char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2708{
Dmitry Shmidt83474442015-04-15 13:47:09 -07002709#ifdef NO_CONFIG_WRITE
2710 return NULL;
2711#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 const struct parse_data *field;
2713 char *key, *value;
2714 size_t i;
2715 char **props;
2716 int fields_num;
2717
2718 get_keys = get_keys && ssid->export_keys;
2719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 if (!props)
2722 return NULL;
2723
2724 fields_num = 0;
2725 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2726 field = &ssid_fields[i];
2727 if (field->key_data && !get_keys)
2728 continue;
2729 value = field->writer(field, ssid);
2730 if (value == NULL)
2731 continue;
2732 if (os_strlen(value) == 0) {
2733 os_free(value);
2734 continue;
2735 }
2736
2737 key = os_strdup(field->name);
2738 if (key == NULL) {
2739 os_free(value);
2740 goto err;
2741 }
2742
2743 props[fields_num * 2] = key;
2744 props[fields_num * 2 + 1] = value;
2745
2746 fields_num++;
2747 }
2748
2749 return props;
2750
2751err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002752 for (i = 0; props[i]; i++)
2753 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 os_free(props);
2755 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07002756#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757}
2758
2759
2760#ifndef NO_CONFIG_WRITE
2761/**
2762 * wpa_config_get - Get a variable in network configuration
2763 * @ssid: Pointer to network configuration data
2764 * @var: Variable name, e.g., "ssid"
2765 * Returns: Value of the variable or %NULL on failure
2766 *
2767 * This function can be used to get network configuration variables. The
2768 * returned value is a copy of the configuration variable in text format, i.e,.
2769 * the same format that the text-based configuration file and wpa_config_set()
2770 * are using for the value. The caller is responsible for freeing the returned
2771 * value.
2772 */
2773char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2774{
2775 size_t i;
2776
2777 if (ssid == NULL || var == NULL)
2778 return NULL;
2779
2780 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2781 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08002782 if (os_strcmp(var, field->name) == 0) {
2783 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002784
2785 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07002786 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002787 "Found newline in value for %s; not returning it",
2788 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08002789 os_free(ret);
2790 ret = NULL;
2791 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002792
Paul Stewart85c72c62016-03-03 15:33:47 -08002793 return ret;
2794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 }
2796
2797 return NULL;
2798}
2799
2800
2801/**
2802 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2803 * @ssid: Pointer to network configuration data
2804 * @var: Variable name, e.g., "ssid"
2805 * Returns: Value of the variable or %NULL on failure
2806 *
2807 * This function can be used to get network configuration variable like
2808 * wpa_config_get(). The only difference is that this functions does not expose
2809 * key/password material from the configuration. In case a key/password field
2810 * is requested, the returned value is an empty string or %NULL if the variable
2811 * is not set or "*" if the variable is set (regardless of its value). The
2812 * returned value is a copy of the configuration variable in text format, i.e,.
2813 * the same format that the text-based configuration file and wpa_config_set()
2814 * are using for the value. The caller is responsible for freeing the returned
2815 * value.
2816 */
2817char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2818{
2819 size_t i;
2820
2821 if (ssid == NULL || var == NULL)
2822 return NULL;
2823
2824 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2825 const struct parse_data *field = &ssid_fields[i];
2826 if (os_strcmp(var, field->name) == 0) {
2827 char *res = field->writer(field, ssid);
2828 if (field->key_data) {
2829 if (res && res[0]) {
2830 wpa_printf(MSG_DEBUG, "Do not allow "
2831 "key_data field to be "
2832 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002833 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 return os_strdup("*");
2835 }
2836
2837 os_free(res);
2838 return NULL;
2839 }
2840 return res;
2841 }
2842 }
2843
2844 return NULL;
2845}
2846#endif /* NO_CONFIG_WRITE */
2847
2848
2849/**
2850 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2851 * @ssid: Pointer to network configuration data
2852 *
2853 * This function must be called to update WPA PSK when either SSID or the
2854 * passphrase has changed for the network configuration.
2855 */
2856void wpa_config_update_psk(struct wpa_ssid *ssid)
2857{
2858#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002859 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 ssid->psk, PMK_LEN);
2861 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2862 ssid->psk, PMK_LEN);
2863 ssid->psk_set = 1;
2864#endif /* CONFIG_NO_PBKDF2 */
2865}
2866
2867
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002868static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2869 const char *value)
2870{
2871 u8 *proto;
2872 int **port;
2873 int *ports, *nports;
2874 const char *pos;
2875 unsigned int num_ports;
2876
2877 proto = os_realloc_array(cred->req_conn_capab_proto,
2878 cred->num_req_conn_capab + 1, sizeof(u8));
2879 if (proto == NULL)
2880 return -1;
2881 cred->req_conn_capab_proto = proto;
2882
2883 port = os_realloc_array(cred->req_conn_capab_port,
2884 cred->num_req_conn_capab + 1, sizeof(int *));
2885 if (port == NULL)
2886 return -1;
2887 cred->req_conn_capab_port = port;
2888
2889 proto[cred->num_req_conn_capab] = atoi(value);
2890
2891 pos = os_strchr(value, ':');
2892 if (pos == NULL) {
2893 port[cred->num_req_conn_capab] = NULL;
2894 cred->num_req_conn_capab++;
2895 return 0;
2896 }
2897 pos++;
2898
2899 ports = NULL;
2900 num_ports = 0;
2901
2902 while (*pos) {
2903 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2904 if (nports == NULL) {
2905 os_free(ports);
2906 return -1;
2907 }
2908 ports = nports;
2909 ports[num_ports++] = atoi(pos);
2910
2911 pos = os_strchr(pos, ',');
2912 if (pos == NULL)
2913 break;
2914 pos++;
2915 }
2916
2917 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2918 if (nports == NULL) {
2919 os_free(ports);
2920 return -1;
2921 }
2922 ports = nports;
2923 ports[num_ports] = -1;
2924
2925 port[cred->num_req_conn_capab] = ports;
2926 cred->num_req_conn_capab++;
2927 return 0;
2928}
2929
2930
Dmitry Shmidt04949592012-07-19 12:16:46 -07002931int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2932 const char *value, int line)
2933{
2934 char *val;
2935 size_t len;
2936
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002937 if (os_strcmp(var, "temporary") == 0) {
2938 cred->temporary = atoi(value);
2939 return 0;
2940 }
2941
Dmitry Shmidt04949592012-07-19 12:16:46 -07002942 if (os_strcmp(var, "priority") == 0) {
2943 cred->priority = atoi(value);
2944 return 0;
2945 }
2946
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002947 if (os_strcmp(var, "sp_priority") == 0) {
2948 int prio = atoi(value);
2949 if (prio < 0 || prio > 255)
2950 return -1;
2951 cred->sp_priority = prio;
2952 return 0;
2953 }
2954
Dmitry Shmidt04949592012-07-19 12:16:46 -07002955 if (os_strcmp(var, "pcsc") == 0) {
2956 cred->pcsc = atoi(value);
2957 return 0;
2958 }
2959
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960 if (os_strcmp(var, "eap") == 0) {
2961 struct eap_method_type method;
2962 method.method = eap_peer_get_type(value, &method.vendor);
2963 if (method.vendor == EAP_VENDOR_IETF &&
2964 method.method == EAP_TYPE_NONE) {
2965 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2966 "for a credential", line, value);
2967 return -1;
2968 }
2969 os_free(cred->eap_method);
2970 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2971 if (cred->eap_method == NULL)
2972 return -1;
2973 os_memcpy(cred->eap_method, &method, sizeof(method));
2974 return 0;
2975 }
2976
2977 if (os_strcmp(var, "password") == 0 &&
2978 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002979 if (has_newline(value))
2980 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002981 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002982 cred->password = os_strdup(value);
2983 cred->ext_password = 1;
2984 return 0;
2985 }
2986
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002987 if (os_strcmp(var, "update_identifier") == 0) {
2988 cred->update_identifier = atoi(value);
2989 return 0;
2990 }
2991
2992 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2993 cred->min_dl_bandwidth_home = atoi(value);
2994 return 0;
2995 }
2996
2997 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2998 cred->min_ul_bandwidth_home = atoi(value);
2999 return 0;
3000 }
3001
3002 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3003 cred->min_dl_bandwidth_roaming = atoi(value);
3004 return 0;
3005 }
3006
3007 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3008 cred->min_ul_bandwidth_roaming = atoi(value);
3009 return 0;
3010 }
3011
3012 if (os_strcmp(var, "max_bss_load") == 0) {
3013 cred->max_bss_load = atoi(value);
3014 return 0;
3015 }
3016
3017 if (os_strcmp(var, "req_conn_capab") == 0)
3018 return wpa_config_set_cred_req_conn_capab(cred, value);
3019
3020 if (os_strcmp(var, "ocsp") == 0) {
3021 cred->ocsp = atoi(value);
3022 return 0;
3023 }
3024
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003025 if (os_strcmp(var, "sim_num") == 0) {
3026 cred->sim_num = atoi(value);
3027 return 0;
3028 }
3029
Dmitry Shmidt04949592012-07-19 12:16:46 -07003030 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003031 if (val == NULL ||
3032 (os_strcmp(var, "excluded_ssid") != 0 &&
3033 os_strcmp(var, "roaming_consortium") != 0 &&
3034 os_strcmp(var, "required_roaming_consortium") != 0 &&
3035 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003036 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3037 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003038 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003039 return -1;
3040 }
3041
3042 if (os_strcmp(var, "realm") == 0) {
3043 os_free(cred->realm);
3044 cred->realm = val;
3045 return 0;
3046 }
3047
3048 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003049 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003050 cred->username = val;
3051 return 0;
3052 }
3053
3054 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003055 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003056 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003057 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003058 return 0;
3059 }
3060
3061 if (os_strcmp(var, "ca_cert") == 0) {
3062 os_free(cred->ca_cert);
3063 cred->ca_cert = val;
3064 return 0;
3065 }
3066
3067 if (os_strcmp(var, "client_cert") == 0) {
3068 os_free(cred->client_cert);
3069 cred->client_cert = val;
3070 return 0;
3071 }
3072
3073 if (os_strcmp(var, "private_key") == 0) {
3074 os_free(cred->private_key);
3075 cred->private_key = val;
3076 return 0;
3077 }
3078
3079 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003080 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003081 cred->private_key_passwd = val;
3082 return 0;
3083 }
3084
3085 if (os_strcmp(var, "imsi") == 0) {
3086 os_free(cred->imsi);
3087 cred->imsi = val;
3088 return 0;
3089 }
3090
3091 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003092 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003093 cred->milenage = val;
3094 return 0;
3095 }
3096
Dmitry Shmidt051af732013-10-22 13:52:46 -07003097 if (os_strcmp(var, "domain_suffix_match") == 0) {
3098 os_free(cred->domain_suffix_match);
3099 cred->domain_suffix_match = val;
3100 return 0;
3101 }
3102
Dmitry Shmidt04949592012-07-19 12:16:46 -07003103 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003104 char **new_domain;
3105 new_domain = os_realloc_array(cred->domain,
3106 cred->num_domain + 1,
3107 sizeof(char *));
3108 if (new_domain == NULL) {
3109 os_free(val);
3110 return -1;
3111 }
3112 new_domain[cred->num_domain++] = val;
3113 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003114 return 0;
3115 }
3116
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003117 if (os_strcmp(var, "phase1") == 0) {
3118 os_free(cred->phase1);
3119 cred->phase1 = val;
3120 return 0;
3121 }
3122
3123 if (os_strcmp(var, "phase2") == 0) {
3124 os_free(cred->phase2);
3125 cred->phase2 = val;
3126 return 0;
3127 }
3128
3129 if (os_strcmp(var, "roaming_consortium") == 0) {
3130 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3131 wpa_printf(MSG_ERROR, "Line %d: invalid "
3132 "roaming_consortium length %d (3..15 "
3133 "expected)", line, (int) len);
3134 os_free(val);
3135 return -1;
3136 }
3137 os_memcpy(cred->roaming_consortium, val, len);
3138 cred->roaming_consortium_len = len;
3139 os_free(val);
3140 return 0;
3141 }
3142
Dmitry Shmidt051af732013-10-22 13:52:46 -07003143 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3144 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3145 {
3146 wpa_printf(MSG_ERROR, "Line %d: invalid "
3147 "required_roaming_consortium length %d "
3148 "(3..15 expected)", line, (int) len);
3149 os_free(val);
3150 return -1;
3151 }
3152 os_memcpy(cred->required_roaming_consortium, val, len);
3153 cred->required_roaming_consortium_len = len;
3154 os_free(val);
3155 return 0;
3156 }
3157
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003158 if (os_strcmp(var, "excluded_ssid") == 0) {
3159 struct excluded_ssid *e;
3160
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003161 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003162 wpa_printf(MSG_ERROR, "Line %d: invalid "
3163 "excluded_ssid length %d", line, (int) len);
3164 os_free(val);
3165 return -1;
3166 }
3167
3168 e = os_realloc_array(cred->excluded_ssid,
3169 cred->num_excluded_ssid + 1,
3170 sizeof(struct excluded_ssid));
3171 if (e == NULL) {
3172 os_free(val);
3173 return -1;
3174 }
3175 cred->excluded_ssid = e;
3176
3177 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3178 os_memcpy(e->ssid, val, len);
3179 e->ssid_len = len;
3180
3181 os_free(val);
3182
3183 return 0;
3184 }
3185
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003186 if (os_strcmp(var, "roaming_partner") == 0) {
3187 struct roaming_partner *p;
3188 char *pos;
3189
3190 p = os_realloc_array(cred->roaming_partner,
3191 cred->num_roaming_partner + 1,
3192 sizeof(struct roaming_partner));
3193 if (p == NULL) {
3194 os_free(val);
3195 return -1;
3196 }
3197 cred->roaming_partner = p;
3198
3199 p = &cred->roaming_partner[cred->num_roaming_partner];
3200
3201 pos = os_strchr(val, ',');
3202 if (pos == NULL) {
3203 os_free(val);
3204 return -1;
3205 }
3206 *pos++ = '\0';
3207 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3208 os_free(val);
3209 return -1;
3210 }
3211 os_memcpy(p->fqdn, val, pos - val);
3212
3213 p->exact_match = atoi(pos);
3214
3215 pos = os_strchr(pos, ',');
3216 if (pos == NULL) {
3217 os_free(val);
3218 return -1;
3219 }
3220 *pos++ = '\0';
3221
3222 p->priority = atoi(pos);
3223
3224 pos = os_strchr(pos, ',');
3225 if (pos == NULL) {
3226 os_free(val);
3227 return -1;
3228 }
3229 *pos++ = '\0';
3230
3231 if (os_strlen(pos) >= sizeof(p->country)) {
3232 os_free(val);
3233 return -1;
3234 }
3235 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3236
3237 cred->num_roaming_partner++;
3238 os_free(val);
3239
3240 return 0;
3241 }
3242
3243 if (os_strcmp(var, "provisioning_sp") == 0) {
3244 os_free(cred->provisioning_sp);
3245 cred->provisioning_sp = val;
3246 return 0;
3247 }
3248
Dmitry Shmidt04949592012-07-19 12:16:46 -07003249 if (line) {
3250 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3251 line, var);
3252 }
3253
3254 os_free(val);
3255
3256 return -1;
3257}
3258
3259
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003260static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003261{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003262 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003263 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003264 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003265
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003266 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003267 if (buf == NULL)
3268 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003269 res = os_snprintf(buf, bufsize, "%d", val);
3270 if (os_snprintf_error(bufsize, res)) {
3271 os_free(buf);
3272 buf = NULL;
3273 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003274 return buf;
3275}
3276
3277
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003278static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003279{
3280 if (str == NULL)
3281 return NULL;
3282 return os_strdup(str);
3283}
3284
3285
3286char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3287{
3288 if (os_strcmp(var, "temporary") == 0)
3289 return alloc_int_str(cred->temporary);
3290
3291 if (os_strcmp(var, "priority") == 0)
3292 return alloc_int_str(cred->priority);
3293
3294 if (os_strcmp(var, "sp_priority") == 0)
3295 return alloc_int_str(cred->sp_priority);
3296
3297 if (os_strcmp(var, "pcsc") == 0)
3298 return alloc_int_str(cred->pcsc);
3299
3300 if (os_strcmp(var, "eap") == 0) {
3301 if (!cred->eap_method)
3302 return NULL;
3303 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3304 cred->eap_method[0].method));
3305 }
3306
3307 if (os_strcmp(var, "update_identifier") == 0)
3308 return alloc_int_str(cred->update_identifier);
3309
3310 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3311 return alloc_int_str(cred->min_dl_bandwidth_home);
3312
3313 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3314 return alloc_int_str(cred->min_ul_bandwidth_home);
3315
3316 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3317 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3318
3319 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3320 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3321
3322 if (os_strcmp(var, "max_bss_load") == 0)
3323 return alloc_int_str(cred->max_bss_load);
3324
3325 if (os_strcmp(var, "req_conn_capab") == 0) {
3326 unsigned int i;
3327 char *buf, *end, *pos;
3328 int ret;
3329
3330 if (!cred->num_req_conn_capab)
3331 return NULL;
3332
3333 buf = os_malloc(4000);
3334 if (buf == NULL)
3335 return NULL;
3336 pos = buf;
3337 end = pos + 4000;
3338 for (i = 0; i < cred->num_req_conn_capab; i++) {
3339 int *ports;
3340
3341 ret = os_snprintf(pos, end - pos, "%s%u",
3342 i > 0 ? "\n" : "",
3343 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003344 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003345 return buf;
3346 pos += ret;
3347
3348 ports = cred->req_conn_capab_port[i];
3349 if (ports) {
3350 int j;
3351 for (j = 0; ports[j] != -1; j++) {
3352 ret = os_snprintf(pos, end - pos,
3353 "%s%d",
3354 j > 0 ? "," : ":",
3355 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003357 return buf;
3358 pos += ret;
3359 }
3360 }
3361 }
3362
3363 return buf;
3364 }
3365
3366 if (os_strcmp(var, "ocsp") == 0)
3367 return alloc_int_str(cred->ocsp);
3368
3369 if (os_strcmp(var, "realm") == 0)
3370 return alloc_strdup(cred->realm);
3371
3372 if (os_strcmp(var, "username") == 0)
3373 return alloc_strdup(cred->username);
3374
3375 if (os_strcmp(var, "password") == 0) {
3376 if (!cred->password)
3377 return NULL;
3378 return alloc_strdup("*");
3379 }
3380
3381 if (os_strcmp(var, "ca_cert") == 0)
3382 return alloc_strdup(cred->ca_cert);
3383
3384 if (os_strcmp(var, "client_cert") == 0)
3385 return alloc_strdup(cred->client_cert);
3386
3387 if (os_strcmp(var, "private_key") == 0)
3388 return alloc_strdup(cred->private_key);
3389
3390 if (os_strcmp(var, "private_key_passwd") == 0) {
3391 if (!cred->private_key_passwd)
3392 return NULL;
3393 return alloc_strdup("*");
3394 }
3395
3396 if (os_strcmp(var, "imsi") == 0)
3397 return alloc_strdup(cred->imsi);
3398
3399 if (os_strcmp(var, "milenage") == 0) {
3400 if (!(cred->milenage))
3401 return NULL;
3402 return alloc_strdup("*");
3403 }
3404
3405 if (os_strcmp(var, "domain_suffix_match") == 0)
3406 return alloc_strdup(cred->domain_suffix_match);
3407
3408 if (os_strcmp(var, "domain") == 0) {
3409 unsigned int i;
3410 char *buf, *end, *pos;
3411 int ret;
3412
3413 if (!cred->num_domain)
3414 return NULL;
3415
3416 buf = os_malloc(4000);
3417 if (buf == NULL)
3418 return NULL;
3419 pos = buf;
3420 end = pos + 4000;
3421
3422 for (i = 0; i < cred->num_domain; i++) {
3423 ret = os_snprintf(pos, end - pos, "%s%s",
3424 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003425 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003426 return buf;
3427 pos += ret;
3428 }
3429
3430 return buf;
3431 }
3432
3433 if (os_strcmp(var, "phase1") == 0)
3434 return alloc_strdup(cred->phase1);
3435
3436 if (os_strcmp(var, "phase2") == 0)
3437 return alloc_strdup(cred->phase2);
3438
3439 if (os_strcmp(var, "roaming_consortium") == 0) {
3440 size_t buflen;
3441 char *buf;
3442
3443 if (!cred->roaming_consortium_len)
3444 return NULL;
3445 buflen = cred->roaming_consortium_len * 2 + 1;
3446 buf = os_malloc(buflen);
3447 if (buf == NULL)
3448 return NULL;
3449 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3450 cred->roaming_consortium_len);
3451 return buf;
3452 }
3453
3454 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3455 size_t buflen;
3456 char *buf;
3457
3458 if (!cred->required_roaming_consortium_len)
3459 return NULL;
3460 buflen = cred->required_roaming_consortium_len * 2 + 1;
3461 buf = os_malloc(buflen);
3462 if (buf == NULL)
3463 return NULL;
3464 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3465 cred->required_roaming_consortium_len);
3466 return buf;
3467 }
3468
3469 if (os_strcmp(var, "excluded_ssid") == 0) {
3470 unsigned int i;
3471 char *buf, *end, *pos;
3472
3473 if (!cred->num_excluded_ssid)
3474 return NULL;
3475
3476 buf = os_malloc(4000);
3477 if (buf == NULL)
3478 return NULL;
3479 pos = buf;
3480 end = pos + 4000;
3481
3482 for (i = 0; i < cred->num_excluded_ssid; i++) {
3483 struct excluded_ssid *e;
3484 int ret;
3485
3486 e = &cred->excluded_ssid[i];
3487 ret = os_snprintf(pos, end - pos, "%s%s",
3488 i > 0 ? "\n" : "",
3489 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003490 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003491 return buf;
3492 pos += ret;
3493 }
3494
3495 return buf;
3496 }
3497
3498 if (os_strcmp(var, "roaming_partner") == 0) {
3499 unsigned int i;
3500 char *buf, *end, *pos;
3501
3502 if (!cred->num_roaming_partner)
3503 return NULL;
3504
3505 buf = os_malloc(4000);
3506 if (buf == NULL)
3507 return NULL;
3508 pos = buf;
3509 end = pos + 4000;
3510
3511 for (i = 0; i < cred->num_roaming_partner; i++) {
3512 struct roaming_partner *p;
3513 int ret;
3514
3515 p = &cred->roaming_partner[i];
3516 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3517 i > 0 ? "\n" : "",
3518 p->fqdn, p->exact_match, p->priority,
3519 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003520 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003521 return buf;
3522 pos += ret;
3523 }
3524
3525 return buf;
3526 }
3527
3528 if (os_strcmp(var, "provisioning_sp") == 0)
3529 return alloc_strdup(cred->provisioning_sp);
3530
3531 return NULL;
3532}
3533
3534
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3536{
3537 struct wpa_cred *cred;
3538
3539 cred = config->cred;
3540 while (cred) {
3541 if (id == cred->id)
3542 break;
3543 cred = cred->next;
3544 }
3545
3546 return cred;
3547}
3548
3549
3550struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3551{
3552 int id;
3553 struct wpa_cred *cred, *last = NULL;
3554
3555 id = -1;
3556 cred = config->cred;
3557 while (cred) {
3558 if (cred->id > id)
3559 id = cred->id;
3560 last = cred;
3561 cred = cred->next;
3562 }
3563 id++;
3564
3565 cred = os_zalloc(sizeof(*cred));
3566 if (cred == NULL)
3567 return NULL;
3568 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003569 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003570 if (last)
3571 last->next = cred;
3572 else
3573 config->cred = cred;
3574
3575 return cred;
3576}
3577
3578
3579int wpa_config_remove_cred(struct wpa_config *config, int id)
3580{
3581 struct wpa_cred *cred, *prev = NULL;
3582
3583 cred = config->cred;
3584 while (cred) {
3585 if (id == cred->id)
3586 break;
3587 prev = cred;
3588 cred = cred->next;
3589 }
3590
3591 if (cred == NULL)
3592 return -1;
3593
3594 if (prev)
3595 prev->next = cred->next;
3596 else
3597 config->cred = cred->next;
3598
3599 wpa_config_free_cred(cred);
3600 return 0;
3601}
3602
3603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604#ifndef CONFIG_NO_CONFIG_BLOBS
3605/**
3606 * wpa_config_get_blob - Get a named configuration blob
3607 * @config: Configuration data from wpa_config_read()
3608 * @name: Name of the blob
3609 * Returns: Pointer to blob data or %NULL if not found
3610 */
3611const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3612 const char *name)
3613{
3614 struct wpa_config_blob *blob = config->blobs;
3615
3616 while (blob) {
3617 if (os_strcmp(blob->name, name) == 0)
3618 return blob;
3619 blob = blob->next;
3620 }
3621 return NULL;
3622}
3623
3624
3625/**
3626 * wpa_config_set_blob - Set or add a named configuration blob
3627 * @config: Configuration data from wpa_config_read()
3628 * @blob: New value for the blob
3629 *
3630 * Adds a new configuration blob or replaces the current value of an existing
3631 * blob.
3632 */
3633void wpa_config_set_blob(struct wpa_config *config,
3634 struct wpa_config_blob *blob)
3635{
3636 wpa_config_remove_blob(config, blob->name);
3637 blob->next = config->blobs;
3638 config->blobs = blob;
3639}
3640
3641
3642/**
3643 * wpa_config_free_blob - Free blob data
3644 * @blob: Pointer to blob to be freed
3645 */
3646void wpa_config_free_blob(struct wpa_config_blob *blob)
3647{
3648 if (blob) {
3649 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003650 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 os_free(blob);
3652 }
3653}
3654
3655
3656/**
3657 * wpa_config_remove_blob - Remove a named configuration blob
3658 * @config: Configuration data from wpa_config_read()
3659 * @name: Name of the blob to remove
3660 * Returns: 0 if blob was removed or -1 if blob was not found
3661 */
3662int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3663{
3664 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3665
3666 while (pos) {
3667 if (os_strcmp(pos->name, name) == 0) {
3668 if (prev)
3669 prev->next = pos->next;
3670 else
3671 config->blobs = pos->next;
3672 wpa_config_free_blob(pos);
3673 return 0;
3674 }
3675 prev = pos;
3676 pos = pos->next;
3677 }
3678
3679 return -1;
3680}
3681#endif /* CONFIG_NO_CONFIG_BLOBS */
3682
3683
3684/**
3685 * wpa_config_alloc_empty - Allocate an empty configuration
3686 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3687 * socket
3688 * @driver_param: Driver parameters
3689 * Returns: Pointer to allocated configuration data or %NULL on failure
3690 */
3691struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3692 const char *driver_param)
3693{
3694 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003695 const int aCWmin = 4, aCWmax = 10;
3696 const struct hostapd_wmm_ac_params ac_bk =
3697 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3698 const struct hostapd_wmm_ac_params ac_be =
3699 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3700 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3701 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3702 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3703 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704
3705 config = os_zalloc(sizeof(*config));
3706 if (config == NULL)
3707 return NULL;
3708 config->eapol_version = DEFAULT_EAPOL_VERSION;
3709 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003710 config->user_mpm = DEFAULT_USER_MPM;
3711 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003712 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003713 config->dot11RSNASAERetransPeriod =
3714 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 config->fast_reauth = DEFAULT_FAST_REAUTH;
3716 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3717 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003718 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003719 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003720 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003721 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3723 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3724 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3725 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003726 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003727 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003728 config->wmm_ac_params[0] = ac_be;
3729 config->wmm_ac_params[1] = ac_bk;
3730 config->wmm_ac_params[2] = ac_vi;
3731 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003732 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003733 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003734 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003735 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003736 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003738#ifdef CONFIG_MBO
3739 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
3740#endif /* CONFIG_MBO */
3741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 if (ctrl_interface)
3743 config->ctrl_interface = os_strdup(ctrl_interface);
3744 if (driver_param)
3745 config->driver_param = os_strdup(driver_param);
3746
3747 return config;
3748}
3749
3750
3751#ifndef CONFIG_NO_STDOUT_DEBUG
3752/**
3753 * wpa_config_debug_dump_networks - Debug dump of configured networks
3754 * @config: Configuration data from wpa_config_read()
3755 */
3756void wpa_config_debug_dump_networks(struct wpa_config *config)
3757{
3758 int prio;
3759 struct wpa_ssid *ssid;
3760
3761 for (prio = 0; prio < config->num_prio; prio++) {
3762 ssid = config->pssid[prio];
3763 wpa_printf(MSG_DEBUG, "Priority group %d",
3764 ssid->priority);
3765 while (ssid) {
3766 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3767 ssid->id,
3768 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3769 ssid = ssid->pnext;
3770 }
3771 }
3772}
3773#endif /* CONFIG_NO_STDOUT_DEBUG */
3774
3775
3776struct global_parse_data {
3777 char *name;
3778 int (*parser)(const struct global_parse_data *data,
3779 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003780 int (*get)(const char *name, struct wpa_config *config, long offset,
3781 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 void *param1, *param2, *param3;
3783 unsigned int changed_flag;
3784};
3785
3786
3787static int wpa_global_config_parse_int(const struct global_parse_data *data,
3788 struct wpa_config *config, int line,
3789 const char *pos)
3790{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003791 int val, *dst;
3792 char *end;
3793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003795 val = strtol(pos, &end, 0);
3796 if (*end) {
3797 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3798 line, pos);
3799 return -1;
3800 }
3801 *dst = val;
3802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3804
3805 if (data->param2 && *dst < (long) data->param2) {
3806 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3807 "min_value=%ld)", line, data->name, *dst,
3808 (long) data->param2);
3809 *dst = (long) data->param2;
3810 return -1;
3811 }
3812
3813 if (data->param3 && *dst > (long) data->param3) {
3814 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3815 "max_value=%ld)", line, data->name, *dst,
3816 (long) data->param3);
3817 *dst = (long) data->param3;
3818 return -1;
3819 }
3820
3821 return 0;
3822}
3823
3824
3825static int wpa_global_config_parse_str(const struct global_parse_data *data,
3826 struct wpa_config *config, int line,
3827 const char *pos)
3828{
3829 size_t len;
3830 char **dst, *tmp;
3831
3832 len = os_strlen(pos);
3833 if (data->param2 && len < (size_t) data->param2) {
3834 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3835 "min_len=%ld)", line, data->name,
3836 (unsigned long) len, (long) data->param2);
3837 return -1;
3838 }
3839
3840 if (data->param3 && len > (size_t) data->param3) {
3841 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3842 "max_len=%ld)", line, data->name,
3843 (unsigned long) len, (long) data->param3);
3844 return -1;
3845 }
3846
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003847 if (has_newline(pos)) {
3848 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
3849 line, data->name);
3850 return -1;
3851 }
3852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853 tmp = os_strdup(pos);
3854 if (tmp == NULL)
3855 return -1;
3856
3857 dst = (char **) (((u8 *) config) + (long) data->param1);
3858 os_free(*dst);
3859 *dst = tmp;
3860 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3861
3862 return 0;
3863}
3864
3865
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003866static int wpa_config_process_bgscan(const struct global_parse_data *data,
3867 struct wpa_config *config, int line,
3868 const char *pos)
3869{
3870 size_t len;
3871 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003872 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003873
3874 tmp = wpa_config_parse_string(pos, &len);
3875 if (tmp == NULL) {
3876 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3877 line, data->name);
3878 return -1;
3879 }
3880
Dmitry Shmidt97672262014-02-03 13:02:54 -08003881 res = wpa_global_config_parse_str(data, config, line, tmp);
3882 os_free(tmp);
3883 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003884}
3885
3886
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3888 struct wpa_config *config, int line,
3889 const char *pos)
3890{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003891 struct wpabuf **dst, *tmp;
3892
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003893 tmp = wpabuf_parse_bin(pos);
3894 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895 return -1;
3896
Dmitry Shmidt04949592012-07-19 12:16:46 -07003897 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3898 wpabuf_free(*dst);
3899 *dst = tmp;
3900 wpa_printf(MSG_DEBUG, "%s", data->name);
3901
3902 return 0;
3903}
3904
3905
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003906static int wpa_config_process_freq_list(const struct global_parse_data *data,
3907 struct wpa_config *config, int line,
3908 const char *value)
3909{
3910 int *freqs;
3911
3912 freqs = wpa_config_parse_int_array(value);
3913 if (freqs == NULL)
3914 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003915 if (freqs[0] == 0) {
3916 os_free(freqs);
3917 freqs = NULL;
3918 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003919 os_free(config->freq_list);
3920 config->freq_list = freqs;
3921 return 0;
3922}
3923
3924
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003925#ifdef CONFIG_P2P
3926static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3927 struct wpa_config *config, int line,
3928 const char *pos)
3929{
3930 u32 *dst;
3931 struct hostapd_ip_addr addr;
3932
3933 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3934 return -1;
3935 if (addr.af != AF_INET)
3936 return -1;
3937
3938 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3939 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3940 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3941 WPA_GET_BE32((u8 *) dst));
3942
3943 return 0;
3944}
3945#endif /* CONFIG_P2P */
3946
3947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948static int wpa_config_process_country(const struct global_parse_data *data,
3949 struct wpa_config *config, int line,
3950 const char *pos)
3951{
3952 if (!pos[0] || !pos[1]) {
3953 wpa_printf(MSG_DEBUG, "Invalid country set");
3954 return -1;
3955 }
3956 config->country[0] = pos[0];
3957 config->country[1] = pos[1];
3958 wpa_printf(MSG_DEBUG, "country='%c%c'",
3959 config->country[0], config->country[1]);
3960 return 0;
3961}
3962
3963
3964static int wpa_config_process_load_dynamic_eap(
3965 const struct global_parse_data *data, struct wpa_config *config,
3966 int line, const char *so)
3967{
3968 int ret;
3969 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3970 ret = eap_peer_method_load(so);
3971 if (ret == -2) {
3972 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3973 "reloading.");
3974 } else if (ret) {
3975 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3976 "method '%s'.", line, so);
3977 return -1;
3978 }
3979
3980 return 0;
3981}
3982
3983
3984#ifdef CONFIG_WPS
3985
3986static int wpa_config_process_uuid(const struct global_parse_data *data,
3987 struct wpa_config *config, int line,
3988 const char *pos)
3989{
3990 char buf[40];
3991 if (uuid_str2bin(pos, config->uuid)) {
3992 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3993 return -1;
3994 }
3995 uuid_bin2str(config->uuid, buf, sizeof(buf));
3996 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3997 return 0;
3998}
3999
4000
4001static int wpa_config_process_device_type(
4002 const struct global_parse_data *data,
4003 struct wpa_config *config, int line, const char *pos)
4004{
4005 return wps_dev_type_str2bin(pos, config->device_type);
4006}
4007
4008
4009static int wpa_config_process_os_version(const struct global_parse_data *data,
4010 struct wpa_config *config, int line,
4011 const char *pos)
4012{
4013 if (hexstr2bin(pos, config->os_version, 4)) {
4014 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4015 return -1;
4016 }
4017 wpa_printf(MSG_DEBUG, "os_version=%08x",
4018 WPA_GET_BE32(config->os_version));
4019 return 0;
4020}
4021
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022
4023static int wpa_config_process_wps_vendor_ext_m1(
4024 const struct global_parse_data *data,
4025 struct wpa_config *config, int line, const char *pos)
4026{
4027 struct wpabuf *tmp;
4028 int len = os_strlen(pos) / 2;
4029 u8 *p;
4030
4031 if (!len) {
4032 wpa_printf(MSG_ERROR, "Line %d: "
4033 "invalid wps_vendor_ext_m1", line);
4034 return -1;
4035 }
4036
4037 tmp = wpabuf_alloc(len);
4038 if (tmp) {
4039 p = wpabuf_put(tmp, len);
4040
4041 if (hexstr2bin(pos, p, len)) {
4042 wpa_printf(MSG_ERROR, "Line %d: "
4043 "invalid wps_vendor_ext_m1", line);
4044 wpabuf_free(tmp);
4045 return -1;
4046 }
4047
4048 wpabuf_free(config->wps_vendor_ext_m1);
4049 config->wps_vendor_ext_m1 = tmp;
4050 } else {
4051 wpa_printf(MSG_ERROR, "Can not allocate "
4052 "memory for wps_vendor_ext_m1");
4053 return -1;
4054 }
4055
4056 return 0;
4057}
4058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059#endif /* CONFIG_WPS */
4060
4061#ifdef CONFIG_P2P
4062static int wpa_config_process_sec_device_type(
4063 const struct global_parse_data *data,
4064 struct wpa_config *config, int line, const char *pos)
4065{
4066 int idx;
4067
4068 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4069 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4070 "items", line);
4071 return -1;
4072 }
4073
4074 idx = config->num_sec_device_types;
4075
4076 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4077 return -1;
4078
4079 config->num_sec_device_types++;
4080 return 0;
4081}
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082
4083
4084static int wpa_config_process_p2p_pref_chan(
4085 const struct global_parse_data *data,
4086 struct wpa_config *config, int line, const char *pos)
4087{
4088 struct p2p_channel *pref = NULL, *n;
4089 unsigned int num = 0;
4090 const char *pos2;
4091 u8 op_class, chan;
4092
4093 /* format: class:chan,class:chan,... */
4094
4095 while (*pos) {
4096 op_class = atoi(pos);
4097 pos2 = os_strchr(pos, ':');
4098 if (pos2 == NULL)
4099 goto fail;
4100 pos2++;
4101 chan = atoi(pos2);
4102
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004103 n = os_realloc_array(pref, num + 1,
4104 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004105 if (n == NULL)
4106 goto fail;
4107 pref = n;
4108 pref[num].op_class = op_class;
4109 pref[num].chan = chan;
4110 num++;
4111
4112 pos = os_strchr(pos2, ',');
4113 if (pos == NULL)
4114 break;
4115 pos++;
4116 }
4117
4118 os_free(config->p2p_pref_chan);
4119 config->p2p_pref_chan = pref;
4120 config->num_p2p_pref_chan = num;
4121 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4122 (u8 *) config->p2p_pref_chan,
4123 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4124
4125 return 0;
4126
4127fail:
4128 os_free(pref);
4129 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4130 return -1;
4131}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004132
4133
4134static int wpa_config_process_p2p_no_go_freq(
4135 const struct global_parse_data *data,
4136 struct wpa_config *config, int line, const char *pos)
4137{
4138 int ret;
4139
4140 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4141 if (ret < 0) {
4142 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4143 return -1;
4144 }
4145
4146 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4147 config->p2p_no_go_freq.num);
4148
4149 return 0;
4150}
4151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152#endif /* CONFIG_P2P */
4153
4154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004155static int wpa_config_process_hessid(
4156 const struct global_parse_data *data,
4157 struct wpa_config *config, int line, const char *pos)
4158{
4159 if (hwaddr_aton2(pos, config->hessid) < 0) {
4160 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4161 line, pos);
4162 return -1;
4163 }
4164
4165 return 0;
4166}
4167
4168
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004169static int wpa_config_process_sae_groups(
4170 const struct global_parse_data *data,
4171 struct wpa_config *config, int line, const char *pos)
4172{
4173 int *groups = wpa_config_parse_int_array(pos);
4174 if (groups == NULL) {
4175 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4176 line, pos);
4177 return -1;
4178 }
4179
4180 os_free(config->sae_groups);
4181 config->sae_groups = groups;
4182
4183 return 0;
4184}
4185
4186
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004187static int wpa_config_process_ap_vendor_elements(
4188 const struct global_parse_data *data,
4189 struct wpa_config *config, int line, const char *pos)
4190{
4191 struct wpabuf *tmp;
4192 int len = os_strlen(pos) / 2;
4193 u8 *p;
4194
4195 if (!len) {
4196 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4197 line);
4198 return -1;
4199 }
4200
4201 tmp = wpabuf_alloc(len);
4202 if (tmp) {
4203 p = wpabuf_put(tmp, len);
4204
4205 if (hexstr2bin(pos, p, len)) {
4206 wpa_printf(MSG_ERROR, "Line %d: invalid "
4207 "ap_vendor_elements", line);
4208 wpabuf_free(tmp);
4209 return -1;
4210 }
4211
4212 wpabuf_free(config->ap_vendor_elements);
4213 config->ap_vendor_elements = tmp;
4214 } else {
4215 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4216 "ap_vendor_elements");
4217 return -1;
4218 }
4219
4220 return 0;
4221}
4222
4223
Dmitry Shmidt56052862013-10-04 10:23:25 -07004224#ifdef CONFIG_CTRL_IFACE
4225static int wpa_config_process_no_ctrl_interface(
4226 const struct global_parse_data *data,
4227 struct wpa_config *config, int line, const char *pos)
4228{
4229 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4230 os_free(config->ctrl_interface);
4231 config->ctrl_interface = NULL;
4232 return 0;
4233}
4234#endif /* CONFIG_CTRL_IFACE */
4235
4236
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004237static int wpa_config_get_int(const char *name, struct wpa_config *config,
4238 long offset, char *buf, size_t buflen,
4239 int pretty_print)
4240{
4241 int *val = (int *) (((u8 *) config) + (long) offset);
4242
4243 if (pretty_print)
4244 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4245 return os_snprintf(buf, buflen, "%d", *val);
4246}
4247
4248
4249static int wpa_config_get_str(const char *name, struct wpa_config *config,
4250 long offset, char *buf, size_t buflen,
4251 int pretty_print)
4252{
4253 char **val = (char **) (((u8 *) config) + (long) offset);
4254 int res;
4255
4256 if (pretty_print)
4257 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4258 *val ? *val : "null");
4259 else if (!*val)
4260 return -1;
4261 else
4262 res = os_snprintf(buf, buflen, "%s", *val);
4263 if (os_snprintf_error(buflen, res))
4264 res = -1;
4265
4266 return res;
4267}
4268
4269
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004270#ifdef CONFIG_P2P
4271static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4272 long offset, char *buf, size_t buflen,
4273 int pretty_print)
4274{
4275 void *val = ((u8 *) config) + (long) offset;
4276 int res;
4277 char addr[INET_ADDRSTRLEN];
4278
4279 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4280 return -1;
4281
4282 if (pretty_print)
4283 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4284 else
4285 res = os_snprintf(buf, buflen, "%s", addr);
4286
4287 if (os_snprintf_error(buflen, res))
4288 res = -1;
4289
4290 return res;
4291}
4292#endif /* CONFIG_P2P */
4293
4294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295#ifdef OFFSET
4296#undef OFFSET
4297#endif /* OFFSET */
4298/* OFFSET: Get offset of a variable within the wpa_config structure */
4299#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4300
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004301#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4302#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4303#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304#define INT(f) _INT(f), NULL, NULL
4305#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004306#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307#define STR(f) _STR(f), NULL, NULL
4308#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004309#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004310#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4311 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312
4313static const struct global_parse_data global_fields[] = {
4314#ifdef CONFIG_CTRL_IFACE
4315 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004316 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 { STR(ctrl_interface_group), 0 } /* deprecated */,
4318#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004319#ifdef CONFIG_MACSEC
4320 { INT_RANGE(eapol_version, 1, 3), 0 },
4321#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004323#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004325 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004326#ifdef CONFIG_MESH
4327 { INT(user_mpm), 0 },
4328 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004329 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004330 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004331#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004332 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333 { INT(fast_reauth), 0 },
4334 { STR(opensc_engine_path), 0 },
4335 { STR(pkcs11_engine_path), 0 },
4336 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004337 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004338 { STR(pcsc_reader), 0 },
4339 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004340 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 { STR(driver_param), 0 },
4342 { INT(dot11RSNAConfigPMKLifetime), 0 },
4343 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4344 { INT(dot11RSNAConfigSATimeout), 0 },
4345#ifndef CONFIG_NO_CONFIG_WRITE
4346 { INT(update_config), 0 },
4347#endif /* CONFIG_NO_CONFIG_WRITE */
4348 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4349#ifdef CONFIG_WPS
4350 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004351 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4352 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4354 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4355 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4356 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4357 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4358 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4359 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4360 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004361 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362#endif /* CONFIG_WPS */
4363#ifdef CONFIG_P2P
4364 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004365 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4366 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004367 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4368 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4370 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4371 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4372 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4373 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004374 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004375 { INT_RANGE(p2p_passphrase_len, 8, 63),
4376 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004377 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004378 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4379 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004380 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004381 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004382 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004383 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004384 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004385 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004386 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004387 { IPV4(ip_addr_go), 0 },
4388 { IPV4(ip_addr_mask), 0 },
4389 { IPV4(ip_addr_start), 0 },
4390 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004391 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392#endif /* CONFIG_P2P */
4393 { FUNC(country), CFG_CHANGED_COUNTRY },
4394 { INT(bss_max_count), 0 },
4395 { INT(bss_expiration_age), 0 },
4396 { INT(bss_expiration_scan_count), 0 },
4397 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004398 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004400 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004401#ifdef CONFIG_HS20
4402 { INT_RANGE(hs20, 0, 1), 0 },
4403#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004404 { INT_RANGE(interworking, 0, 1), 0 },
4405 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004406 { INT_RANGE(access_network_type, 0, 15), 0 },
4407 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4408 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004409 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4410 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4411 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4412 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4413 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004414 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4415 { INT(p2p_go_max_inactivity), 0 },
4416 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004417 { INT(okc), 0 },
4418 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004419 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004420 { INT(dtim_period), 0 },
4421 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004422 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004423 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004424 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004425 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004426 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004427 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004428 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004429 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004430 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004431 { INT(mac_addr), 0 },
4432 { INT(rand_addr_lifetime), 0 },
4433 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004435 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004436 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004437 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004438#ifdef CONFIG_FST
4439 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4440 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4441 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4442#endif /* CONFIG_FST */
4443 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004444 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004445#ifdef CONFIG_MBO
4446 { STR(non_pref_chan), 0 },
4447 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
4448 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
4449#endif /*CONFIG_MBO */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004450 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07004451 { INT_RANGE(ftm_responder, 0, 1), 0 },
4452 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453};
4454
4455#undef FUNC
4456#undef _INT
4457#undef INT
4458#undef INT_RANGE
4459#undef _STR
4460#undef STR
4461#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004462#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004463#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004464#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465
4466
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004467int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4468{
4469 int result = 0;
4470 size_t i;
4471
4472 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4473 const struct global_parse_data *field = &global_fields[i];
4474 int tmp;
4475
4476 if (!field->get)
4477 continue;
4478
4479 tmp = field->get(field->name, config, (long) field->param1,
4480 buf, buflen, 1);
4481 if (tmp < 0)
4482 return -1;
4483 buf += tmp;
4484 buflen -= tmp;
4485 result += tmp;
4486 }
4487 return result;
4488}
4489
4490
4491int wpa_config_get_value(const char *name, struct wpa_config *config,
4492 char *buf, size_t buflen)
4493{
4494 size_t i;
4495
4496 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4497 const struct global_parse_data *field = &global_fields[i];
4498
4499 if (os_strcmp(name, field->name) != 0)
4500 continue;
4501 if (!field->get)
4502 break;
4503 return field->get(name, config, (long) field->param1,
4504 buf, buflen, 0);
4505 }
4506
4507 return -1;
4508}
4509
4510
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004511int wpa_config_get_num_global_field_names(void)
4512{
4513 return NUM_GLOBAL_FIELDS;
4514}
4515
4516
4517const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4518{
4519 if (i >= NUM_GLOBAL_FIELDS)
4520 return NULL;
4521
4522 if (no_var)
4523 *no_var = !global_fields[i].param1;
4524 return global_fields[i].name;
4525}
4526
4527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4529{
4530 size_t i;
4531 int ret = 0;
4532
4533 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4534 const struct global_parse_data *field = &global_fields[i];
4535 size_t flen = os_strlen(field->name);
4536 if (os_strncmp(pos, field->name, flen) != 0 ||
4537 pos[flen] != '=')
4538 continue;
4539
4540 if (field->parser(field, config, line, pos + flen + 1)) {
4541 wpa_printf(MSG_ERROR, "Line %d: failed to "
4542 "parse '%s'.", line, pos);
4543 ret = -1;
4544 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004545 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4546 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 config->changed_parameters |= field->changed_flag;
4548 break;
4549 }
4550 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004551#ifdef CONFIG_AP
4552 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4553 char *tmp = os_strchr(pos, '=');
4554 if (tmp == NULL) {
4555 if (line < 0)
4556 return -1;
4557 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4558 "'%s'", line, pos);
4559 return -1;
4560 }
4561 *tmp++ = '\0';
4562 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4563 tmp)) {
4564 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4565 "AC item", line);
4566 return -1;
4567 }
4568 }
4569#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 if (line < 0)
4571 return -1;
4572 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4573 line, pos);
4574 ret = -1;
4575 }
4576
4577 return ret;
4578}