blob: 2d1c126e6a521e5f581604e8824c65260a83c72f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Hai Shalomc3565922019-10-28 11:58:20 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/uuid.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt29333592017-01-09 12:27:11 -080014#include "common/ieee802_1x_defs.h"
Hai Shalomc3565922019-10-28 11:58:20 -070015#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "crypto/sha1.h"
17#include "rsn_supp/wpa.h"
18#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080020#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "config.h"
22
23
24#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25#define NO_CONFIG_WRITE
26#endif
27
28/*
29 * Structure for network configuration parsing. This data is used to implement
30 * a generic parser for each network block variable. The table of configuration
31 * variables is defined below in this file (ssid_fields[]).
32 */
33struct parse_data {
34 /* Configuration variable name */
35 char *name;
36
Dmitry Shmidte4663042016-04-04 10:07:49 -070037 /* Parser function for this variable. The parser functions return 0 or 1
38 * to indicate success. Value 0 indicates that the parameter value may
39 * have changed while value 1 means that the value did not change.
40 * Error cases (failure to parse the string) are indicated by returning
41 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
43 int line, const char *value);
44
45#ifndef NO_CONFIG_WRITE
46 /* Writer function (i.e., to get the variable in text format from
47 * internal presentation). */
48 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
49#endif /* NO_CONFIG_WRITE */
50
51 /* Variable specific parameters for the parser. */
52 void *param1, *param2, *param3, *param4;
53
54 /* 0 = this variable can be included in debug output and ctrl_iface
55 * 1 = this variable contains key/private data and it must not be
56 * included in debug output unless explicitly requested. In
57 * addition, this variable will not be readable through the
58 * ctrl_iface.
59 */
60 int key_data;
61};
62
63
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static int wpa_config_parse_str(const struct parse_data *data,
65 struct wpa_ssid *ssid,
66 int line, const char *value)
67{
Dmitry Shmidte4663042016-04-04 10:07:49 -070068 size_t res_len, *dst_len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 char **dst, *tmp;
70
71 if (os_strcmp(value, "NULL") == 0) {
72 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
73 data->name);
74 tmp = NULL;
75 res_len = 0;
76 goto set;
77 }
78
79 tmp = wpa_config_parse_string(value, &res_len);
80 if (tmp == NULL) {
81 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
82 line, data->name,
83 data->key_data ? "[KEY DATA REMOVED]" : value);
84 return -1;
85 }
86
87 if (data->key_data) {
88 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
89 (u8 *) tmp, res_len);
90 } else {
91 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
92 (u8 *) tmp, res_len);
93 }
94
95 if (data->param3 && res_len < (size_t) data->param3) {
96 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
97 "min_len=%ld)", line, data->name,
98 (unsigned long) res_len, (long) data->param3);
99 os_free(tmp);
100 return -1;
101 }
102
103 if (data->param4 && res_len > (size_t) data->param4) {
104 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
105 "max_len=%ld)", line, data->name,
106 (unsigned long) res_len, (long) data->param4);
107 os_free(tmp);
108 return -1;
109 }
110
111set:
112 dst = (char **) (((u8 *) ssid) + (long) data->param1);
113 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700114
115 if (data->param2)
116 prev_len = *dst_len;
117 else if (*dst)
118 prev_len = os_strlen(*dst);
119 else
120 prev_len = 0;
121 if ((*dst == NULL && tmp == NULL) ||
122 (*dst && tmp && prev_len == res_len &&
123 os_memcmp(*dst, tmp, res_len) == 0)) {
124 /* No change to the previously configured value */
125 os_free(tmp);
126 return 1;
127 }
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 os_free(*dst);
130 *dst = tmp;
131 if (data->param2)
132 *dst_len = res_len;
133
134 return 0;
135}
136
137
138#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
140{
141 char *buf;
142
143 buf = os_malloc(len + 3);
144 if (buf == NULL)
145 return NULL;
146 buf[0] = '"';
147 os_memcpy(buf + 1, value, len);
148 buf[len + 1] = '"';
149 buf[len + 2] = '\0';
150
151 return buf;
152}
153
154
155static char * wpa_config_write_string_hex(const u8 *value, size_t len)
156{
157 char *buf;
158
159 buf = os_zalloc(2 * len + 1);
160 if (buf == NULL)
161 return NULL;
162 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
163
164 return buf;
165}
166
167
168static char * wpa_config_write_string(const u8 *value, size_t len)
169{
170 if (value == NULL)
171 return NULL;
172
173 if (is_hex(value, len))
174 return wpa_config_write_string_hex(value, len);
175 else
176 return wpa_config_write_string_ascii(value, len);
177}
178
179
180static char * wpa_config_write_str(const struct parse_data *data,
181 struct wpa_ssid *ssid)
182{
183 size_t len;
184 char **src;
185
186 src = (char **) (((u8 *) ssid) + (long) data->param1);
187 if (*src == NULL)
188 return NULL;
189
190 if (data->param2)
191 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
192 else
193 len = os_strlen(*src);
194
195 return wpa_config_write_string((const u8 *) *src, len);
196}
197#endif /* NO_CONFIG_WRITE */
198
199
200static int wpa_config_parse_int(const struct parse_data *data,
201 struct wpa_ssid *ssid,
202 int line, const char *value)
203{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700204 int val, *dst;
205 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206
207 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700208 val = strtol(value, &end, 0);
209 if (*end) {
210 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
211 line, value);
212 return -1;
213 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700214
215 if (*dst == val)
216 return 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700217 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
219
220 if (data->param3 && *dst < (long) data->param3) {
221 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
222 "min_value=%ld)", line, data->name, *dst,
223 (long) data->param3);
224 *dst = (long) data->param3;
225 return -1;
226 }
227
228 if (data->param4 && *dst > (long) data->param4) {
229 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
230 "max_value=%ld)", line, data->name, *dst,
231 (long) data->param4);
232 *dst = (long) data->param4;
233 return -1;
234 }
235
236 return 0;
237}
238
239
240#ifndef NO_CONFIG_WRITE
241static char * wpa_config_write_int(const struct parse_data *data,
242 struct wpa_ssid *ssid)
243{
244 int *src, res;
245 char *value;
246
247 src = (int *) (((u8 *) ssid) + (long) data->param1);
248
249 value = os_malloc(20);
250 if (value == NULL)
251 return NULL;
252 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 os_free(value);
255 return NULL;
256 }
257 value[20 - 1] = '\0';
258 return value;
259}
260#endif /* NO_CONFIG_WRITE */
261
262
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800263static int wpa_config_parse_addr_list(const struct parse_data *data,
264 int line, const char *value,
265 u8 **list, size_t *num, char *name,
266 u8 abort_on_error, u8 masked)
267{
268 const char *pos;
269 u8 *buf, *n, addr[2 * ETH_ALEN];
270 size_t count;
271
272 buf = NULL;
273 count = 0;
274
275 pos = value;
276 while (pos && *pos) {
277 while (*pos == ' ')
278 pos++;
279
280 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
281 if (abort_on_error || count == 0) {
282 wpa_printf(MSG_ERROR,
283 "Line %d: Invalid %s address '%s'",
284 line, name, value);
285 os_free(buf);
286 return -1;
287 }
288 /* continue anyway since this could have been from a
289 * truncated configuration file line */
290 wpa_printf(MSG_INFO,
291 "Line %d: Ignore likely truncated %s address '%s'",
292 line, name, pos);
293 } else {
294 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
295 if (n == NULL) {
296 os_free(buf);
297 return -1;
298 }
299 buf = n;
300 os_memmove(buf + 2 * ETH_ALEN, buf,
301 count * 2 * ETH_ALEN);
302 os_memcpy(buf, addr, 2 * ETH_ALEN);
303 count++;
304 wpa_printf(MSG_MSGDUMP,
305 "%s: addr=" MACSTR " mask=" MACSTR,
306 name, MAC2STR(addr),
307 MAC2STR(&addr[ETH_ALEN]));
308 }
309
310 pos = os_strchr(pos, ' ');
311 }
312
313 os_free(*list);
314 *list = buf;
315 *num = count;
316
317 return 0;
318}
319
320
321#ifndef NO_CONFIG_WRITE
322static char * wpa_config_write_addr_list(const struct parse_data *data,
323 const u8 *list, size_t num, char *name)
324{
325 char *value, *end, *pos;
326 int res;
327 size_t i;
328
329 if (list == NULL || num == 0)
330 return NULL;
331
332 value = os_malloc(2 * 20 * num);
333 if (value == NULL)
334 return NULL;
335 pos = value;
336 end = value + 2 * 20 * num;
337
338 for (i = num; i > 0; i--) {
339 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
340 const u8 *m = a + ETH_ALEN;
341
342 if (i < num)
343 *pos++ = ' ';
344 res = hwaddr_mask_txt(pos, end - pos, a, m);
345 if (res < 0) {
346 os_free(value);
347 return NULL;
348 }
349 pos += res;
350 }
351
352 return value;
353}
354#endif /* NO_CONFIG_WRITE */
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356static int wpa_config_parse_bssid(const struct parse_data *data,
357 struct wpa_ssid *ssid, int line,
358 const char *value)
359{
360 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
361 os_strcmp(value, "any") == 0) {
362 ssid->bssid_set = 0;
363 wpa_printf(MSG_MSGDUMP, "BSSID any");
364 return 0;
365 }
366 if (hwaddr_aton(value, ssid->bssid)) {
367 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
368 line, value);
369 return -1;
370 }
371 ssid->bssid_set = 1;
372 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
373 return 0;
374}
375
376
377#ifndef NO_CONFIG_WRITE
378static char * wpa_config_write_bssid(const struct parse_data *data,
379 struct wpa_ssid *ssid)
380{
381 char *value;
382 int res;
383
384 if (!ssid->bssid_set)
385 return NULL;
386
387 value = os_malloc(20);
388 if (value == NULL)
389 return NULL;
390 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800391 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 os_free(value);
393 return NULL;
394 }
395 value[20 - 1] = '\0';
396 return value;
397}
398#endif /* NO_CONFIG_WRITE */
399
400
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700401static int wpa_config_parse_bssid_hint(const struct parse_data *data,
402 struct wpa_ssid *ssid, int line,
403 const char *value)
404{
405 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
406 os_strcmp(value, "any") == 0) {
407 ssid->bssid_hint_set = 0;
408 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
409 return 0;
410 }
411 if (hwaddr_aton(value, ssid->bssid_hint)) {
412 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
413 line, value);
414 return -1;
415 }
416 ssid->bssid_hint_set = 1;
417 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
418 return 0;
419}
420
421
422#ifndef NO_CONFIG_WRITE
423static char * wpa_config_write_bssid_hint(const struct parse_data *data,
424 struct wpa_ssid *ssid)
425{
426 char *value;
427 int res;
428
429 if (!ssid->bssid_hint_set)
430 return NULL;
431
432 value = os_malloc(20);
433 if (!value)
434 return NULL;
435 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
436 if (os_snprintf_error(20, res)) {
437 os_free(value);
438 return NULL;
439 }
440 return value;
441}
442#endif /* NO_CONFIG_WRITE */
443
444
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800445static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
446 struct wpa_ssid *ssid, int line,
447 const char *value)
448{
449 return wpa_config_parse_addr_list(data, line, value,
450 &ssid->bssid_blacklist,
451 &ssid->num_bssid_blacklist,
452 "bssid_blacklist", 1, 1);
453}
454
455
456#ifndef NO_CONFIG_WRITE
457static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
458 struct wpa_ssid *ssid)
459{
460 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
461 ssid->num_bssid_blacklist,
462 "bssid_blacklist");
463}
464#endif /* NO_CONFIG_WRITE */
465
466
467static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
468 struct wpa_ssid *ssid, int line,
469 const char *value)
470{
471 return wpa_config_parse_addr_list(data, line, value,
472 &ssid->bssid_whitelist,
473 &ssid->num_bssid_whitelist,
474 "bssid_whitelist", 1, 1);
475}
476
477
478#ifndef NO_CONFIG_WRITE
479static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
480 struct wpa_ssid *ssid)
481{
482 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
483 ssid->num_bssid_whitelist,
484 "bssid_whitelist");
485}
486#endif /* NO_CONFIG_WRITE */
487
488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489static int wpa_config_parse_psk(const struct parse_data *data,
490 struct wpa_ssid *ssid, int line,
491 const char *value)
492{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700493#ifdef CONFIG_EXT_PASSWORD
494 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700495 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700496 ssid->passphrase = NULL;
497 ssid->psk_set = 0;
498 os_free(ssid->ext_psk);
499 ssid->ext_psk = os_strdup(value + 4);
500 if (ssid->ext_psk == NULL)
501 return -1;
502 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
503 ssid->ext_psk);
504 return 0;
505 }
506#endif /* CONFIG_EXT_PASSWORD */
507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 if (*value == '"') {
509#ifndef CONFIG_NO_PBKDF2
510 const char *pos;
511 size_t len;
512
513 value++;
514 pos = os_strrchr(value, '"');
515 if (pos)
516 len = pos - value;
517 else
518 len = os_strlen(value);
519 if (len < 8 || len > 63) {
520 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
521 "length %lu (expected: 8..63) '%s'.",
522 line, (unsigned long) len, value);
523 return -1;
524 }
525 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
526 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700527 if (has_ctrl_char((u8 *) value, len)) {
528 wpa_printf(MSG_ERROR,
529 "Line %d: Invalid passphrase character",
530 line);
531 return -1;
532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700534 os_memcmp(ssid->passphrase, value, len) == 0) {
535 /* No change to the previously configured value */
536 return 1;
537 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700539 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700540 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 if (ssid->passphrase == NULL)
542 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 return 0;
544#else /* CONFIG_NO_PBKDF2 */
545 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
546 "supported.", line);
547 return -1;
548#endif /* CONFIG_NO_PBKDF2 */
549 }
550
551 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
552 value[PMK_LEN * 2] != '\0') {
553 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
554 line, value);
555 return -1;
556 }
557
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700558 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 ssid->passphrase = NULL;
560
561 ssid->psk_set = 1;
562 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
563 return 0;
564}
565
566
567#ifndef NO_CONFIG_WRITE
568static char * wpa_config_write_psk(const struct parse_data *data,
569 struct wpa_ssid *ssid)
570{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700571#ifdef CONFIG_EXT_PASSWORD
572 if (ssid->ext_psk) {
573 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
574 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800575 int res;
576
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700577 if (buf == NULL)
578 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800579 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
580 if (os_snprintf_error(len, res)) {
581 os_free(buf);
582 buf = NULL;
583 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700584 return buf;
585 }
586#endif /* CONFIG_EXT_PASSWORD */
587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 if (ssid->passphrase)
589 return wpa_config_write_string_ascii(
590 (const u8 *) ssid->passphrase,
591 os_strlen(ssid->passphrase));
592
593 if (ssid->psk_set)
594 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
595
596 return NULL;
597}
598#endif /* NO_CONFIG_WRITE */
599
600
601static int wpa_config_parse_proto(const struct parse_data *data,
602 struct wpa_ssid *ssid, int line,
603 const char *value)
604{
605 int val = 0, last, errors = 0;
606 char *start, *end, *buf;
607
608 buf = os_strdup(value);
609 if (buf == NULL)
610 return -1;
611 start = buf;
612
613 while (*start != '\0') {
614 while (*start == ' ' || *start == '\t')
615 start++;
616 if (*start == '\0')
617 break;
618 end = start;
619 while (*end != ' ' && *end != '\t' && *end != '\0')
620 end++;
621 last = *end == '\0';
622 *end = '\0';
623 if (os_strcmp(start, "WPA") == 0)
624 val |= WPA_PROTO_WPA;
625 else if (os_strcmp(start, "RSN") == 0 ||
626 os_strcmp(start, "WPA2") == 0)
627 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800628 else if (os_strcmp(start, "OSEN") == 0)
629 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 else {
631 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
632 line, start);
633 errors++;
634 }
635
636 if (last)
637 break;
638 start = end + 1;
639 }
640 os_free(buf);
641
642 if (val == 0) {
643 wpa_printf(MSG_ERROR,
644 "Line %d: no proto values configured.", line);
645 errors++;
646 }
647
Dmitry Shmidte4663042016-04-04 10:07:49 -0700648 if (!errors && ssid->proto == val)
649 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
651 ssid->proto = val;
652 return errors ? -1 : 0;
653}
654
655
656#ifndef NO_CONFIG_WRITE
657static char * wpa_config_write_proto(const struct parse_data *data,
658 struct wpa_ssid *ssid)
659{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700660 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 char *buf, *pos, *end;
662
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800663 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 if (buf == NULL)
665 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800666 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667
668 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700669 ret = os_snprintf(pos, end - pos, "%sWPA",
670 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800671 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 return buf;
673 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 }
675
676 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700677 ret = os_snprintf(pos, end - pos, "%sRSN",
678 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800679 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 return buf;
681 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 }
683
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800684 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700685 ret = os_snprintf(pos, end - pos, "%sOSEN",
686 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800687 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800688 return buf;
689 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700690 }
691
692 if (pos == buf) {
693 os_free(buf);
694 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800695 }
696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 return buf;
698}
699#endif /* NO_CONFIG_WRITE */
700
701
702static int wpa_config_parse_key_mgmt(const struct parse_data *data,
703 struct wpa_ssid *ssid, int line,
704 const char *value)
705{
706 int val = 0, last, errors = 0;
707 char *start, *end, *buf;
708
709 buf = os_strdup(value);
710 if (buf == NULL)
711 return -1;
712 start = buf;
713
714 while (*start != '\0') {
715 while (*start == ' ' || *start == '\t')
716 start++;
717 if (*start == '\0')
718 break;
719 end = start;
720 while (*end != ' ' && *end != '\t' && *end != '\0')
721 end++;
722 last = *end == '\0';
723 *end = '\0';
724 if (os_strcmp(start, "WPA-PSK") == 0)
725 val |= WPA_KEY_MGMT_PSK;
726 else if (os_strcmp(start, "WPA-EAP") == 0)
727 val |= WPA_KEY_MGMT_IEEE8021X;
728 else if (os_strcmp(start, "IEEE8021X") == 0)
729 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
730 else if (os_strcmp(start, "NONE") == 0)
731 val |= WPA_KEY_MGMT_NONE;
732 else if (os_strcmp(start, "WPA-NONE") == 0)
733 val |= WPA_KEY_MGMT_WPA_NONE;
734#ifdef CONFIG_IEEE80211R
735 else if (os_strcmp(start, "FT-PSK") == 0)
736 val |= WPA_KEY_MGMT_FT_PSK;
737 else if (os_strcmp(start, "FT-EAP") == 0)
738 val |= WPA_KEY_MGMT_FT_IEEE8021X;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700739#ifdef CONFIG_SHA384
740 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
741 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
742#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
745 val |= WPA_KEY_MGMT_PSK_SHA256;
746 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
747 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748#ifdef CONFIG_WPS
749 else if (os_strcmp(start, "WPS") == 0)
750 val |= WPA_KEY_MGMT_WPS;
751#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800752#ifdef CONFIG_SAE
753 else if (os_strcmp(start, "SAE") == 0)
754 val |= WPA_KEY_MGMT_SAE;
755 else if (os_strcmp(start, "FT-SAE") == 0)
756 val |= WPA_KEY_MGMT_FT_SAE;
757#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800758#ifdef CONFIG_HS20
759 else if (os_strcmp(start, "OSEN") == 0)
760 val |= WPA_KEY_MGMT_OSEN;
761#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800762#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800763 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
764 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800765#endif /* CONFIG_SUITEB */
766#ifdef CONFIG_SUITEB192
767 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
768 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
769#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800770#ifdef CONFIG_FILS
771 else if (os_strcmp(start, "FILS-SHA256") == 0)
772 val |= WPA_KEY_MGMT_FILS_SHA256;
773 else if (os_strcmp(start, "FILS-SHA384") == 0)
774 val |= WPA_KEY_MGMT_FILS_SHA384;
775#ifdef CONFIG_IEEE80211R
776 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
777 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
778 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
779 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
780#endif /* CONFIG_IEEE80211R */
781#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700782#ifdef CONFIG_OWE
783 else if (os_strcmp(start, "OWE") == 0)
784 val |= WPA_KEY_MGMT_OWE;
785#endif /* CONFIG_OWE */
786#ifdef CONFIG_DPP
787 else if (os_strcmp(start, "DPP") == 0)
788 val |= WPA_KEY_MGMT_DPP;
789#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 else {
791 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
792 line, start);
793 errors++;
794 }
795
796 if (last)
797 break;
798 start = end + 1;
799 }
800 os_free(buf);
801
802 if (val == 0) {
803 wpa_printf(MSG_ERROR,
804 "Line %d: no key_mgmt values configured.", line);
805 errors++;
806 }
807
Dmitry Shmidte4663042016-04-04 10:07:49 -0700808 if (!errors && ssid->key_mgmt == val)
809 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
811 ssid->key_mgmt = val;
812 return errors ? -1 : 0;
813}
814
815
816#ifndef NO_CONFIG_WRITE
817static char * wpa_config_write_key_mgmt(const struct parse_data *data,
818 struct wpa_ssid *ssid)
819{
820 char *buf, *pos, *end;
821 int ret;
822
Dmitry Shmidt96571392013-10-14 12:54:46 -0700823 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 if (buf == NULL)
825 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700826 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
829 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
830 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800831 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 end[-1] = '\0';
833 return buf;
834 }
835 pos += ret;
836 }
837
838 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
839 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
840 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800841 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 end[-1] = '\0';
843 return buf;
844 }
845 pos += ret;
846 }
847
848 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
849 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
850 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 end[-1] = '\0';
853 return buf;
854 }
855 pos += ret;
856 }
857
858 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
859 ret = os_snprintf(pos, end - pos, "%sNONE",
860 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800861 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 end[-1] = '\0';
863 return buf;
864 }
865 pos += ret;
866 }
867
868 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
869 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
870 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 end[-1] = '\0';
873 return buf;
874 }
875 pos += ret;
876 }
877
878#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700879 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
880 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
881 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800882 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700883 end[-1] = '\0';
884 return buf;
885 }
886 pos += ret;
887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888
Dmitry Shmidt96571392013-10-14 12:54:46 -0700889 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
890 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
891 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800892 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700893 end[-1] = '\0';
894 return buf;
895 }
896 pos += ret;
897 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700898
899#ifdef CONFIG_SHA384
900 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
901 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
902 pos == buf ? "" : " ");
903 if (os_snprintf_error(end - pos, ret)) {
904 end[-1] = '\0';
905 return buf;
906 }
907 pos += ret;
908 }
909#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910#endif /* CONFIG_IEEE80211R */
911
Dmitry Shmidt96571392013-10-14 12:54:46 -0700912 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
913 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
914 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800915 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700916 end[-1] = '\0';
917 return buf;
918 }
919 pos += ret;
920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
Dmitry Shmidt96571392013-10-14 12:54:46 -0700922 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
923 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
924 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800925 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700926 end[-1] = '\0';
927 return buf;
928 }
929 pos += ret;
930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931
932#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700933 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
934 ret = os_snprintf(pos, end - pos, "%sWPS",
935 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700937 end[-1] = '\0';
938 return buf;
939 }
940 pos += ret;
941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942#endif /* CONFIG_WPS */
943
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800944#ifdef CONFIG_SAE
945 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
946 ret = os_snprintf(pos, end - pos, "%sSAE",
947 pos == buf ? "" : " ");
948 if (os_snprintf_error(end - pos, ret)) {
949 end[-1] = '\0';
950 return buf;
951 }
952 pos += ret;
953 }
954
955 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
956 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
957 pos == buf ? "" : " ");
958 if (os_snprintf_error(end - pos, ret)) {
959 end[-1] = '\0';
960 return buf;
961 }
962 pos += ret;
963 }
964#endif /* CONFIG_SAE */
965
966#ifdef CONFIG_HS20
967 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
968 ret = os_snprintf(pos, end - pos, "%sOSEN",
969 pos == buf ? "" : " ");
970 if (os_snprintf_error(end - pos, ret)) {
971 end[-1] = '\0';
972 return buf;
973 }
974 pos += ret;
975 }
976#endif /* CONFIG_HS20 */
977
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800978#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
980 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
981 pos == buf ? "" : " ");
982 if (os_snprintf_error(end - pos, ret)) {
983 end[-1] = '\0';
984 return buf;
985 }
986 pos += ret;
987 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800988#endif /* CONFIG_SUITEB */
989
990#ifdef CONFIG_SUITEB192
991 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
992 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
993 pos == buf ? "" : " ");
994 if (os_snprintf_error(end - pos, ret)) {
995 end[-1] = '\0';
996 return buf;
997 }
998 pos += ret;
999 }
1000#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001001
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001002#ifdef CONFIG_FILS
1003 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1004 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1005 pos == buf ? "" : " ");
1006 if (os_snprintf_error(end - pos, ret)) {
1007 end[-1] = '\0';
1008 return buf;
1009 }
1010 pos += ret;
1011 }
1012 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1013 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1014 pos == buf ? "" : " ");
1015 if (os_snprintf_error(end - pos, ret)) {
1016 end[-1] = '\0';
1017 return buf;
1018 }
1019 pos += ret;
1020 }
1021#ifdef CONFIG_IEEE80211R
1022 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1023 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1024 pos == buf ? "" : " ");
1025 if (os_snprintf_error(end - pos, ret)) {
1026 end[-1] = '\0';
1027 return buf;
1028 }
1029 pos += ret;
1030 }
1031 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1032 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1033 pos == buf ? "" : " ");
1034 if (os_snprintf_error(end - pos, ret)) {
1035 end[-1] = '\0';
1036 return buf;
1037 }
1038 pos += ret;
1039 }
1040#endif /* CONFIG_IEEE80211R */
1041#endif /* CONFIG_FILS */
1042
Hai Shalom74f70d42019-02-11 14:42:39 -08001043#ifdef CONFIG_DPP
1044 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1045 ret = os_snprintf(pos, end - pos, "%sDPP",
1046 pos == buf ? "" : " ");
1047 if (os_snprintf_error(end - pos, ret)) {
1048 end[-1] = '\0';
1049 return buf;
1050 }
1051 pos += ret;
1052 }
1053#endif /* CONFIG_DPP */
1054
1055#ifdef CONFIG_OWE
1056 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1057 ret = os_snprintf(pos, end - pos, "%sOWE",
1058 pos == buf ? "" : " ");
1059 if (os_snprintf_error(end - pos, ret)) {
1060 end[-1] = '\0';
1061 return buf;
1062 }
1063 pos += ret;
1064 }
1065#endif /* CONFIG_OWE */
1066
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001067 if (pos == buf) {
1068 os_free(buf);
1069 buf = NULL;
1070 }
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 return buf;
1073}
1074#endif /* NO_CONFIG_WRITE */
1075
1076
1077static int wpa_config_parse_cipher(int line, const char *value)
1078{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001079#ifdef CONFIG_NO_WPA
1080 return -1;
1081#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001082 int val = wpa_parse_cipher(value);
1083 if (val < 0) {
1084 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1085 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 if (val == 0) {
1089 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1090 line);
1091 return -1;
1092 }
1093 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001094#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095}
1096
1097
1098#ifndef NO_CONFIG_WRITE
1099static char * wpa_config_write_cipher(int cipher)
1100{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001101#ifdef CONFIG_NO_WPA
1102 return NULL;
1103#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001104 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (buf == NULL)
1106 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001108 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1109 os_free(buf);
1110 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 }
1112
1113 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001114#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115}
1116#endif /* NO_CONFIG_WRITE */
1117
1118
1119static int wpa_config_parse_pairwise(const struct parse_data *data,
1120 struct wpa_ssid *ssid, int line,
1121 const char *value)
1122{
1123 int val;
1124 val = wpa_config_parse_cipher(line, value);
1125 if (val == -1)
1126 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001127 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1129 "(0x%x).", line, val);
1130 return -1;
1131 }
1132
Dmitry Shmidte4663042016-04-04 10:07:49 -07001133 if (ssid->pairwise_cipher == val)
1134 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1136 ssid->pairwise_cipher = val;
1137 return 0;
1138}
1139
1140
1141#ifndef NO_CONFIG_WRITE
1142static char * wpa_config_write_pairwise(const struct parse_data *data,
1143 struct wpa_ssid *ssid)
1144{
1145 return wpa_config_write_cipher(ssid->pairwise_cipher);
1146}
1147#endif /* NO_CONFIG_WRITE */
1148
1149
1150static int wpa_config_parse_group(const struct parse_data *data,
1151 struct wpa_ssid *ssid, int line,
1152 const char *value)
1153{
1154 int val;
1155 val = wpa_config_parse_cipher(line, value);
1156 if (val == -1)
1157 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001158
1159 /*
1160 * Backwards compatibility - filter out WEP ciphers that were previously
1161 * allowed.
1162 */
1163 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1164
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001165 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1167 "(0x%x).", line, val);
1168 return -1;
1169 }
1170
Dmitry Shmidte4663042016-04-04 10:07:49 -07001171 if (ssid->group_cipher == val)
1172 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1174 ssid->group_cipher = val;
1175 return 0;
1176}
1177
1178
1179#ifndef NO_CONFIG_WRITE
1180static char * wpa_config_write_group(const struct parse_data *data,
1181 struct wpa_ssid *ssid)
1182{
1183 return wpa_config_write_cipher(ssid->group_cipher);
1184}
1185#endif /* NO_CONFIG_WRITE */
1186
1187
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001188static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1189 struct wpa_ssid *ssid, int line,
1190 const char *value)
1191{
1192 int val;
1193
1194 val = wpa_config_parse_cipher(line, value);
1195 if (val == -1)
1196 return -1;
1197
1198 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1199 wpa_printf(MSG_ERROR,
1200 "Line %d: not allowed group management cipher (0x%x).",
1201 line, val);
1202 return -1;
1203 }
1204
1205 if (ssid->group_mgmt_cipher == val)
1206 return 1;
1207 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1208 ssid->group_mgmt_cipher = val;
1209 return 0;
1210}
1211
1212
1213#ifndef NO_CONFIG_WRITE
1214static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1215 struct wpa_ssid *ssid)
1216{
1217 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1218}
1219#endif /* NO_CONFIG_WRITE */
1220
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222static int wpa_config_parse_auth_alg(const struct parse_data *data,
1223 struct wpa_ssid *ssid, int line,
1224 const char *value)
1225{
1226 int val = 0, last, errors = 0;
1227 char *start, *end, *buf;
1228
1229 buf = os_strdup(value);
1230 if (buf == NULL)
1231 return -1;
1232 start = buf;
1233
1234 while (*start != '\0') {
1235 while (*start == ' ' || *start == '\t')
1236 start++;
1237 if (*start == '\0')
1238 break;
1239 end = start;
1240 while (*end != ' ' && *end != '\t' && *end != '\0')
1241 end++;
1242 last = *end == '\0';
1243 *end = '\0';
1244 if (os_strcmp(start, "OPEN") == 0)
1245 val |= WPA_AUTH_ALG_OPEN;
1246 else if (os_strcmp(start, "SHARED") == 0)
1247 val |= WPA_AUTH_ALG_SHARED;
1248 else if (os_strcmp(start, "LEAP") == 0)
1249 val |= WPA_AUTH_ALG_LEAP;
1250 else {
1251 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1252 line, start);
1253 errors++;
1254 }
1255
1256 if (last)
1257 break;
1258 start = end + 1;
1259 }
1260 os_free(buf);
1261
1262 if (val == 0) {
1263 wpa_printf(MSG_ERROR,
1264 "Line %d: no auth_alg values configured.", line);
1265 errors++;
1266 }
1267
Dmitry Shmidte4663042016-04-04 10:07:49 -07001268 if (!errors && ssid->auth_alg == val)
1269 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1271 ssid->auth_alg = val;
1272 return errors ? -1 : 0;
1273}
1274
1275
1276#ifndef NO_CONFIG_WRITE
1277static char * wpa_config_write_auth_alg(const struct parse_data *data,
1278 struct wpa_ssid *ssid)
1279{
1280 char *buf, *pos, *end;
1281 int ret;
1282
1283 pos = buf = os_zalloc(30);
1284 if (buf == NULL)
1285 return NULL;
1286 end = buf + 30;
1287
1288 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1289 ret = os_snprintf(pos, end - pos, "%sOPEN",
1290 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001291 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 end[-1] = '\0';
1293 return buf;
1294 }
1295 pos += ret;
1296 }
1297
1298 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1299 ret = os_snprintf(pos, end - pos, "%sSHARED",
1300 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001301 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 end[-1] = '\0';
1303 return buf;
1304 }
1305 pos += ret;
1306 }
1307
1308 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1309 ret = os_snprintf(pos, end - pos, "%sLEAP",
1310 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 end[-1] = '\0';
1313 return buf;
1314 }
1315 pos += ret;
1316 }
1317
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001318 if (pos == buf) {
1319 os_free(buf);
1320 buf = NULL;
1321 }
1322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 return buf;
1324}
1325#endif /* NO_CONFIG_WRITE */
1326
1327
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001328static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329{
1330 int *freqs;
1331 size_t used, len;
1332 const char *pos;
1333
1334 used = 0;
1335 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 if (freqs == NULL)
1338 return NULL;
1339
1340 pos = value;
1341 while (pos) {
1342 while (*pos == ' ')
1343 pos++;
1344 if (used == len) {
1345 int *n;
1346 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001347 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 if (n == NULL) {
1349 os_free(freqs);
1350 return NULL;
1351 }
1352 for (i = len; i <= len * 2; i++)
1353 n[i] = 0;
1354 freqs = n;
1355 len *= 2;
1356 }
1357
1358 freqs[used] = atoi(pos);
1359 if (freqs[used] == 0)
1360 break;
1361 used++;
1362 pos = os_strchr(pos + 1, ' ');
1363 }
1364
1365 return freqs;
1366}
1367
1368
1369static int wpa_config_parse_scan_freq(const struct parse_data *data,
1370 struct wpa_ssid *ssid, int line,
1371 const char *value)
1372{
1373 int *freqs;
1374
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376 if (freqs == NULL)
1377 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001378 if (freqs[0] == 0) {
1379 os_free(freqs);
1380 freqs = NULL;
1381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 os_free(ssid->scan_freq);
1383 ssid->scan_freq = freqs;
1384
1385 return 0;
1386}
1387
1388
1389static int wpa_config_parse_freq_list(const struct parse_data *data,
1390 struct wpa_ssid *ssid, int line,
1391 const char *value)
1392{
1393 int *freqs;
1394
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001395 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (freqs == NULL)
1397 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001398 if (freqs[0] == 0) {
1399 os_free(freqs);
1400 freqs = NULL;
1401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 os_free(ssid->freq_list);
1403 ssid->freq_list = freqs;
1404
1405 return 0;
1406}
1407
1408
1409#ifndef NO_CONFIG_WRITE
1410static char * wpa_config_write_freqs(const struct parse_data *data,
1411 const int *freqs)
1412{
1413 char *buf, *pos, *end;
1414 int i, ret;
1415 size_t count;
1416
1417 if (freqs == NULL)
1418 return NULL;
1419
1420 count = 0;
1421 for (i = 0; freqs[i]; i++)
1422 count++;
1423
1424 pos = buf = os_zalloc(10 * count + 1);
1425 if (buf == NULL)
1426 return NULL;
1427 end = buf + 10 * count + 1;
1428
1429 for (i = 0; freqs[i]; i++) {
1430 ret = os_snprintf(pos, end - pos, "%s%u",
1431 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001432 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 end[-1] = '\0';
1434 return buf;
1435 }
1436 pos += ret;
1437 }
1438
1439 return buf;
1440}
1441
1442
1443static char * wpa_config_write_scan_freq(const struct parse_data *data,
1444 struct wpa_ssid *ssid)
1445{
1446 return wpa_config_write_freqs(data, ssid->scan_freq);
1447}
1448
1449
1450static char * wpa_config_write_freq_list(const struct parse_data *data,
1451 struct wpa_ssid *ssid)
1452{
1453 return wpa_config_write_freqs(data, ssid->freq_list);
1454}
1455#endif /* NO_CONFIG_WRITE */
1456
1457
1458#ifdef IEEE8021X_EAPOL
1459static int wpa_config_parse_eap(const struct parse_data *data,
1460 struct wpa_ssid *ssid, int line,
1461 const char *value)
1462{
1463 int last, errors = 0;
1464 char *start, *end, *buf;
1465 struct eap_method_type *methods = NULL, *tmp;
1466 size_t num_methods = 0;
1467
1468 buf = os_strdup(value);
1469 if (buf == NULL)
1470 return -1;
1471 start = buf;
1472
1473 while (*start != '\0') {
1474 while (*start == ' ' || *start == '\t')
1475 start++;
1476 if (*start == '\0')
1477 break;
1478 end = start;
1479 while (*end != ' ' && *end != '\t' && *end != '\0')
1480 end++;
1481 last = *end == '\0';
1482 *end = '\0';
1483 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001484 methods = os_realloc_array(methods, num_methods + 1,
1485 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 if (methods == NULL) {
1487 os_free(tmp);
1488 os_free(buf);
1489 return -1;
1490 }
1491 methods[num_methods].method = eap_peer_get_type(
1492 start, &methods[num_methods].vendor);
1493 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1494 methods[num_methods].method == EAP_TYPE_NONE) {
1495 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1496 "'%s'", line, start);
1497 wpa_printf(MSG_ERROR, "You may need to add support for"
1498 " this EAP method during wpa_supplicant\n"
1499 "build time configuration.\n"
1500 "See README for more information.");
1501 errors++;
1502 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1503 methods[num_methods].method == EAP_TYPE_LEAP)
1504 ssid->leap++;
1505 else
1506 ssid->non_leap++;
1507 num_methods++;
1508 if (last)
1509 break;
1510 start = end + 1;
1511 }
1512 os_free(buf);
1513
1514 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001515 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 if (methods == NULL) {
1517 os_free(tmp);
1518 return -1;
1519 }
1520 methods[num_methods].vendor = EAP_VENDOR_IETF;
1521 methods[num_methods].method = EAP_TYPE_NONE;
1522 num_methods++;
1523
Dmitry Shmidte4663042016-04-04 10:07:49 -07001524 if (!errors && ssid->eap.eap_methods) {
1525 struct eap_method_type *prev_m;
1526 size_t i, j, prev_methods, match = 0;
1527
1528 prev_m = ssid->eap.eap_methods;
1529 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1530 prev_m[i].method != EAP_TYPE_NONE; i++) {
1531 /* Count the methods */
1532 }
1533 prev_methods = i + 1;
1534
1535 for (i = 0; prev_methods == num_methods && i < prev_methods;
1536 i++) {
1537 for (j = 0; j < num_methods; j++) {
1538 if (prev_m[i].vendor == methods[j].vendor &&
1539 prev_m[i].method == methods[j].method) {
1540 match++;
1541 break;
1542 }
1543 }
1544 }
1545 if (match == num_methods) {
1546 os_free(methods);
1547 return 1;
1548 }
1549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1551 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001552 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 ssid->eap.eap_methods = methods;
1554 return errors ? -1 : 0;
1555}
1556
1557
Dmitry Shmidt83474442015-04-15 13:47:09 -07001558#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559static char * wpa_config_write_eap(const struct parse_data *data,
1560 struct wpa_ssid *ssid)
1561{
1562 int i, ret;
1563 char *buf, *pos, *end;
1564 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1565 const char *name;
1566
1567 if (eap_methods == NULL)
1568 return NULL;
1569
1570 pos = buf = os_zalloc(100);
1571 if (buf == NULL)
1572 return NULL;
1573 end = buf + 100;
1574
1575 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1576 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1577 name = eap_get_name(eap_methods[i].vendor,
1578 eap_methods[i].method);
1579 if (name) {
1580 ret = os_snprintf(pos, end - pos, "%s%s",
1581 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001582 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 break;
1584 pos += ret;
1585 }
1586 }
1587
1588 end[-1] = '\0';
1589
1590 return buf;
1591}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001592#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593
1594
1595static int wpa_config_parse_password(const struct parse_data *data,
1596 struct wpa_ssid *ssid, int line,
1597 const char *value)
1598{
1599 u8 *hash;
1600
1601 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001602 if (!ssid->eap.password)
1603 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001605 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 ssid->eap.password = NULL;
1607 ssid->eap.password_len = 0;
1608 return 0;
1609 }
1610
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001611#ifdef CONFIG_EXT_PASSWORD
1612 if (os_strncmp(value, "ext:", 4) == 0) {
1613 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001614 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001615 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001616 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001617 ssid->eap.password = (u8 *) name;
1618 ssid->eap.password_len = os_strlen(name);
1619 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1620 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1621 return 0;
1622 }
1623#endif /* CONFIG_EXT_PASSWORD */
1624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 if (os_strncmp(value, "hash:", 5) != 0) {
1626 char *tmp;
1627 size_t res_len;
1628
1629 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001630 if (!tmp) {
1631 wpa_printf(MSG_ERROR,
1632 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 return -1;
1634 }
1635 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1636 (u8 *) tmp, res_len);
1637
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001638 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 ssid->eap.password = (u8 *) tmp;
1640 ssid->eap.password_len = res_len;
1641 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001642 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643
1644 return 0;
1645 }
1646
1647
1648 /* NtPasswordHash: hash:<32 hex digits> */
1649 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001650 wpa_printf(MSG_ERROR,
1651 "Line %d: Invalid password hash length (expected 32 hex digits)",
1652 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 return -1;
1654 }
1655
1656 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001657 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 return -1;
1659
1660 if (hexstr2bin(value + 5, hash, 16)) {
1661 os_free(hash);
1662 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1663 return -1;
1664 }
1665
1666 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1667
Dmitry Shmidte4663042016-04-04 10:07:49 -07001668 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1669 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1670 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1671 bin_clear_free(hash, 16);
1672 return 1;
1673 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001674 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 ssid->eap.password = hash;
1676 ssid->eap.password_len = 16;
1677 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001678 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679
1680 return 0;
1681}
1682
1683
Hai Shalomc3565922019-10-28 11:58:20 -07001684static int wpa_config_parse_machine_password(const struct parse_data *data,
1685 struct wpa_ssid *ssid, int line,
1686 const char *value)
1687{
1688 u8 *hash;
1689
1690 if (os_strcmp(value, "NULL") == 0) {
1691 if (!ssid->eap.machine_password)
1692 return 1; /* Already unset */
1693 wpa_printf(MSG_DEBUG,
1694 "Unset configuration string 'machine_password'");
1695 bin_clear_free(ssid->eap.machine_password,
1696 ssid->eap.machine_password_len);
1697 ssid->eap.machine_password = NULL;
1698 ssid->eap.machine_password_len = 0;
1699 return 0;
1700 }
1701
1702#ifdef CONFIG_EXT_PASSWORD
1703 if (os_strncmp(value, "ext:", 4) == 0) {
1704 char *name = os_strdup(value + 4);
1705
1706 if (!name)
1707 return -1;
1708 bin_clear_free(ssid->eap.machine_password,
1709 ssid->eap.machine_password_len);
1710 ssid->eap.machine_password = (u8 *) name;
1711 ssid->eap.machine_password_len = os_strlen(name);
1712 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1713 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1714 return 0;
1715 }
1716#endif /* CONFIG_EXT_PASSWORD */
1717
1718 if (os_strncmp(value, "hash:", 5) != 0) {
1719 char *tmp;
1720 size_t res_len;
1721
1722 tmp = wpa_config_parse_string(value, &res_len);
1723 if (!tmp) {
1724 wpa_printf(MSG_ERROR,
1725 "Line %d: failed to parse machine_password.",
1726 line);
1727 return -1;
1728 }
1729 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1730 (u8 *) tmp, res_len);
1731
1732 bin_clear_free(ssid->eap.machine_password,
1733 ssid->eap.machine_password_len);
1734 ssid->eap.machine_password = (u8 *) tmp;
1735 ssid->eap.machine_password_len = res_len;
1736 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1737 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1738
1739 return 0;
1740 }
1741
1742
1743 /* NtPasswordHash: hash:<32 hex digits> */
1744 if (os_strlen(value + 5) != 2 * 16) {
1745 wpa_printf(MSG_ERROR,
1746 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1747 line);
1748 return -1;
1749 }
1750
1751 hash = os_malloc(16);
1752 if (!hash)
1753 return -1;
1754
1755 if (hexstr2bin(value + 5, hash, 16)) {
1756 os_free(hash);
1757 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1758 line);
1759 return -1;
1760 }
1761
1762 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1763
1764 if (ssid->eap.machine_password &&
1765 ssid->eap.machine_password_len == 16 &&
1766 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1767 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1768 bin_clear_free(hash, 16);
1769 return 1;
1770 }
1771 bin_clear_free(ssid->eap.machine_password,
1772 ssid->eap.machine_password_len);
1773 ssid->eap.machine_password = hash;
1774 ssid->eap.machine_password_len = 16;
1775 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1776 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1777
1778 return 0;
1779}
1780
1781
Dmitry Shmidt83474442015-04-15 13:47:09 -07001782#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784static char * wpa_config_write_password(const struct parse_data *data,
1785 struct wpa_ssid *ssid)
1786{
1787 char *buf;
1788
Hai Shalomc3565922019-10-28 11:58:20 -07001789 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 return NULL;
1791
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001792#ifdef CONFIG_EXT_PASSWORD
1793 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1794 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001795 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001796 return NULL;
1797 os_memcpy(buf, "ext:", 4);
1798 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1799 return buf;
1800 }
1801#endif /* CONFIG_EXT_PASSWORD */
1802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1804 return wpa_config_write_string(
1805 ssid->eap.password, ssid->eap.password_len);
1806 }
1807
1808 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001809 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 return NULL;
1811
1812 os_memcpy(buf, "hash:", 5);
1813 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1814
1815 return buf;
1816}
Hai Shalomc3565922019-10-28 11:58:20 -07001817
1818
1819static char * wpa_config_write_machine_password(const struct parse_data *data,
1820 struct wpa_ssid *ssid)
1821{
1822 char *buf;
1823
1824 if (!ssid->eap.machine_password)
1825 return NULL;
1826
1827#ifdef CONFIG_EXT_PASSWORD
1828 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1829 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1830 if (!buf)
1831 return NULL;
1832 os_memcpy(buf, "ext:", 4);
1833 os_memcpy(buf + 4, ssid->eap.machine_password,
1834 ssid->eap.machine_password_len);
1835 return buf;
1836 }
1837#endif /* CONFIG_EXT_PASSWORD */
1838
1839 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1840 return wpa_config_write_string(
1841 ssid->eap.machine_password,
1842 ssid->eap.machine_password_len);
1843 }
1844
1845 buf = os_malloc(5 + 32 + 1);
1846 if (!buf)
1847 return NULL;
1848
1849 os_memcpy(buf, "hash:", 5);
1850 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1851
1852 return buf;
1853}
1854
Dmitry Shmidt83474442015-04-15 13:47:09 -07001855#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856#endif /* IEEE8021X_EAPOL */
1857
1858
1859static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1860 const char *value, int idx)
1861{
1862 char *buf, title[20];
1863 int res;
1864
1865 buf = wpa_config_parse_string(value, len);
1866 if (buf == NULL) {
1867 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1868 line, idx, value);
1869 return -1;
1870 }
1871 if (*len > MAX_WEP_KEY_LEN) {
1872 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1873 line, idx, value);
1874 os_free(buf);
1875 return -1;
1876 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001877 if (*len && *len != 5 && *len != 13 && *len != 16) {
1878 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1879 "this network block will be ignored",
1880 line, (unsigned int) *len);
1881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001883 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1887 return 0;
1888}
1889
1890
1891static int wpa_config_parse_wep_key0(const struct parse_data *data,
1892 struct wpa_ssid *ssid, int line,
1893 const char *value)
1894{
1895 return wpa_config_parse_wep_key(ssid->wep_key[0],
1896 &ssid->wep_key_len[0], line,
1897 value, 0);
1898}
1899
1900
1901static int wpa_config_parse_wep_key1(const struct parse_data *data,
1902 struct wpa_ssid *ssid, int line,
1903 const char *value)
1904{
1905 return wpa_config_parse_wep_key(ssid->wep_key[1],
1906 &ssid->wep_key_len[1], line,
1907 value, 1);
1908}
1909
1910
1911static int wpa_config_parse_wep_key2(const struct parse_data *data,
1912 struct wpa_ssid *ssid, int line,
1913 const char *value)
1914{
1915 return wpa_config_parse_wep_key(ssid->wep_key[2],
1916 &ssid->wep_key_len[2], line,
1917 value, 2);
1918}
1919
1920
1921static int wpa_config_parse_wep_key3(const struct parse_data *data,
1922 struct wpa_ssid *ssid, int line,
1923 const char *value)
1924{
1925 return wpa_config_parse_wep_key(ssid->wep_key[3],
1926 &ssid->wep_key_len[3], line,
1927 value, 3);
1928}
1929
1930
1931#ifndef NO_CONFIG_WRITE
1932static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1933{
1934 if (ssid->wep_key_len[idx] == 0)
1935 return NULL;
1936 return wpa_config_write_string(ssid->wep_key[idx],
1937 ssid->wep_key_len[idx]);
1938}
1939
1940
1941static char * wpa_config_write_wep_key0(const struct parse_data *data,
1942 struct wpa_ssid *ssid)
1943{
1944 return wpa_config_write_wep_key(ssid, 0);
1945}
1946
1947
1948static char * wpa_config_write_wep_key1(const struct parse_data *data,
1949 struct wpa_ssid *ssid)
1950{
1951 return wpa_config_write_wep_key(ssid, 1);
1952}
1953
1954
1955static char * wpa_config_write_wep_key2(const struct parse_data *data,
1956 struct wpa_ssid *ssid)
1957{
1958 return wpa_config_write_wep_key(ssid, 2);
1959}
1960
1961
1962static char * wpa_config_write_wep_key3(const struct parse_data *data,
1963 struct wpa_ssid *ssid)
1964{
1965 return wpa_config_write_wep_key(ssid, 3);
1966}
1967#endif /* NO_CONFIG_WRITE */
1968
1969
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970#ifdef CONFIG_P2P
1971
Dmitry Shmidt54605472013-11-08 11:10:19 -08001972static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1973 struct wpa_ssid *ssid, int line,
1974 const char *value)
1975{
1976 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1977 os_strcmp(value, "any") == 0) {
1978 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1979 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1980 return 0;
1981 }
1982 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1983 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1984 line, value);
1985 return -1;
1986 }
1987 ssid->bssid_set = 1;
1988 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1989 MAC2STR(ssid->go_p2p_dev_addr));
1990 return 0;
1991}
1992
1993
1994#ifndef NO_CONFIG_WRITE
1995static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1996 struct wpa_ssid *ssid)
1997{
1998 char *value;
1999 int res;
2000
2001 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2002 return NULL;
2003
2004 value = os_malloc(20);
2005 if (value == NULL)
2006 return NULL;
2007 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002008 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002009 os_free(value);
2010 return NULL;
2011 }
2012 value[20 - 1] = '\0';
2013 return value;
2014}
2015#endif /* NO_CONFIG_WRITE */
2016
2017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2019 struct wpa_ssid *ssid, int line,
2020 const char *value)
2021{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002022 return wpa_config_parse_addr_list(data, line, value,
2023 &ssid->p2p_client_list,
2024 &ssid->num_p2p_clients,
2025 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002026}
2027
2028
2029#ifndef NO_CONFIG_WRITE
2030static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2031 struct wpa_ssid *ssid)
2032{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002033 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2034 ssid->num_p2p_clients,
2035 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002036}
2037#endif /* NO_CONFIG_WRITE */
2038
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002039
2040static int wpa_config_parse_psk_list(const struct parse_data *data,
2041 struct wpa_ssid *ssid, int line,
2042 const char *value)
2043{
2044 struct psk_list_entry *p;
2045 const char *pos;
2046
2047 p = os_zalloc(sizeof(*p));
2048 if (p == NULL)
2049 return -1;
2050
2051 pos = value;
2052 if (os_strncmp(pos, "P2P-", 4) == 0) {
2053 p->p2p = 1;
2054 pos += 4;
2055 }
2056
2057 if (hwaddr_aton(pos, p->addr)) {
2058 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2059 line, pos);
2060 os_free(p);
2061 return -1;
2062 }
2063 pos += 17;
2064 if (*pos != '-') {
2065 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2066 line, pos);
2067 os_free(p);
2068 return -1;
2069 }
2070 pos++;
2071
2072 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2073 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2074 line, pos);
2075 os_free(p);
2076 return -1;
2077 }
2078
2079 dl_list_add(&ssid->psk_list, &p->list);
2080
2081 return 0;
2082}
2083
2084
2085#ifndef NO_CONFIG_WRITE
2086static char * wpa_config_write_psk_list(const struct parse_data *data,
2087 struct wpa_ssid *ssid)
2088{
2089 return NULL;
2090}
2091#endif /* NO_CONFIG_WRITE */
2092
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093#endif /* CONFIG_P2P */
2094
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002095
2096#ifdef CONFIG_MESH
2097
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002098static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2099 struct wpa_ssid *ssid, int line,
2100 const char *value)
2101{
2102 int *rates = wpa_config_parse_int_array(value);
2103
2104 if (rates == NULL) {
2105 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2106 line, value);
2107 return -1;
2108 }
2109 if (rates[0] == 0) {
2110 os_free(rates);
2111 rates = NULL;
2112 }
2113
2114 os_free(ssid->mesh_basic_rates);
2115 ssid->mesh_basic_rates = rates;
2116
2117 return 0;
2118}
2119
2120
2121#ifndef NO_CONFIG_WRITE
2122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002123static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2124 struct wpa_ssid *ssid)
2125{
2126 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2127}
2128
2129#endif /* NO_CONFIG_WRITE */
2130
2131#endif /* CONFIG_MESH */
2132
2133
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002134#ifdef CONFIG_MACSEC
2135
2136static int wpa_config_parse_mka_cak(const struct parse_data *data,
2137 struct wpa_ssid *ssid, int line,
2138 const char *value)
2139{
Hai Shalom74f70d42019-02-11 14:42:39 -08002140 size_t len;
2141
2142 len = os_strlen(value);
2143 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2144 (len != 2 * 16 && len != 2 * 32) ||
2145 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002146 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2147 line, value);
2148 return -1;
2149 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002150 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002151 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2152
Hai Shalom74f70d42019-02-11 14:42:39 -08002153 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2154 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002155 return 0;
2156}
2157
2158
2159static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2160 struct wpa_ssid *ssid, int line,
2161 const char *value)
2162{
Hai Shalom74f70d42019-02-11 14:42:39 -08002163 size_t len;
2164
2165 len = os_strlen(value);
2166 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2167 len < 2 || /* too short */
2168 len % 2 != 0 /* not an integral number of bytes */) {
2169 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2170 line, value);
2171 return -1;
2172 }
2173 ssid->mka_ckn_len = len / 2;
2174 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002175 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2176 line, value);
2177 return -1;
2178 }
2179
2180 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2181
Hai Shalom74f70d42019-02-11 14:42:39 -08002182 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2183 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002184 return 0;
2185}
2186
2187
2188#ifndef NO_CONFIG_WRITE
2189
2190static char * wpa_config_write_mka_cak(const struct parse_data *data,
2191 struct wpa_ssid *ssid)
2192{
2193 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2194 return NULL;
2195
Hai Shalom74f70d42019-02-11 14:42:39 -08002196 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002197}
2198
2199
2200static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2201 struct wpa_ssid *ssid)
2202{
2203 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2204 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002205 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002206}
2207
2208#endif /* NO_CONFIG_WRITE */
2209
2210#endif /* CONFIG_MACSEC */
2211
2212
Hai Shalom74f70d42019-02-11 14:42:39 -08002213#ifdef CONFIG_OCV
2214
2215static int wpa_config_parse_ocv(const struct parse_data *data,
2216 struct wpa_ssid *ssid, int line,
2217 const char *value)
2218{
2219 char *end;
2220
2221 ssid->ocv = strtol(value, &end, 0);
2222 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2223 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2224 line, value);
2225 return -1;
2226 }
2227 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2228 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2229 return 0;
2230}
2231
2232
2233#ifndef NO_CONFIG_WRITE
2234static char * wpa_config_write_ocv(const struct parse_data *data,
2235 struct wpa_ssid *ssid)
2236{
2237 char *value = os_malloc(20);
2238
2239 if (!value)
2240 return NULL;
2241 os_snprintf(value, 20, "%d", ssid->ocv);
2242 value[20 - 1] = '\0';
2243 return value;
2244}
2245#endif /* NO_CONFIG_WRITE */
2246
2247#endif /* CONFIG_OCV */
2248
2249
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002250static int wpa_config_parse_peerkey(const struct parse_data *data,
2251 struct wpa_ssid *ssid, int line,
2252 const char *value)
2253{
2254 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2255 return 0;
2256}
2257
2258
2259#ifndef NO_CONFIG_WRITE
2260static char * wpa_config_write_peerkey(const struct parse_data *data,
2261 struct wpa_ssid *ssid)
2262{
2263 return NULL;
2264}
2265#endif /* NO_CONFIG_WRITE */
2266
2267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268/* Helper macros for network block parser */
2269
2270#ifdef OFFSET
2271#undef OFFSET
2272#endif /* OFFSET */
2273/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2274#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2275
2276/* STR: Define a string variable for an ASCII string; f = field name */
2277#ifdef NO_CONFIG_WRITE
2278#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002279#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280#else /* NO_CONFIG_WRITE */
2281#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002282#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2283 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284#endif /* NO_CONFIG_WRITE */
2285#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002286#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002288#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289
2290/* STR_LEN: Define a string variable with a separate variable for storing the
2291 * data length. Unlike STR(), this can be used to store arbitrary binary data
2292 * (i.e., even nul termination character). */
2293#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002294#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002296#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2298
2299/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2300 * explicitly specified. */
2301#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2302#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2303#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2304
2305#ifdef NO_CONFIG_WRITE
2306#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002307#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308#else /* NO_CONFIG_WRITE */
2309#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2310 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002311#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2312 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313#endif /* NO_CONFIG_WRITE */
2314
2315/* INT: Define an integer variable */
2316#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002317#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318
2319/* INT_RANGE: Define an integer variable with allowed value range */
2320#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2321
2322/* FUNC: Define a configuration variable that uses a custom function for
2323 * parsing and writing the value. */
2324#ifdef NO_CONFIG_WRITE
2325#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2326#else /* NO_CONFIG_WRITE */
2327#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2328 NULL, NULL, NULL, NULL
2329#endif /* NO_CONFIG_WRITE */
2330#define FUNC(f) _FUNC(f), 0
2331#define FUNC_KEY(f) _FUNC(f), 1
2332
2333/*
2334 * Table of network configuration variables. This table is used to parse each
2335 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2336 * that is inside a network block.
2337 *
2338 * This table is generated using the helper macros defined above and with
2339 * generous help from the C pre-processor. The field name is stored as a string
2340 * into .name and for STR and INT types, the offset of the target buffer within
2341 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2342 * offset to the field containing the length of the configuration variable.
2343 * .param3 and .param4 can be used to mark the allowed range (length for STR
2344 * and value for INT).
2345 *
2346 * For each configuration line in wpa_supplicant.conf, the parser goes through
2347 * this table and select the entry that matches with the field name. The parser
2348 * function (.parser) is then called to parse the actual value of the field.
2349 *
2350 * This kind of mechanism makes it easy to add new configuration parameters,
2351 * since only one line needs to be added into this table and into the
2352 * struct wpa_ssid definition if the new variable is either a string or
2353 * integer. More complex types will need to use their own parser and writer
2354 * functions.
2355 */
2356static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002357 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 { INT_RANGE(scan_ssid, 0, 1) },
2359 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002360 { FUNC(bssid_hint) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002361 { FUNC(bssid_blacklist) },
2362 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002364 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002365 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002366 { STR(sae_password_id) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367 { FUNC(proto) },
2368 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002369 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 { FUNC(pairwise) },
2371 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002372 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 { FUNC(auth_alg) },
2374 { FUNC(scan_freq) },
2375 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002376 { INT_RANGE(ht, 0, 1) },
2377 { INT_RANGE(vht, 0, 1) },
2378 { INT_RANGE(ht40, -1, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002379 { INT_RANGE(max_oper_chwidth, CHANWIDTH_USE_HT,
2380 CHANWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002381 { INT(vht_center_freq1) },
2382 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383#ifdef IEEE8021X_EAPOL
2384 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002385 { STR_LENe(identity, identity) },
2386 { STR_LENe(anonymous_identity, anonymous_identity) },
2387 { STR_LENe(imsi_identity, imsi_identity) },
2388 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002390 { FUNC_KEY(machine_password) },
2391 { STRe(ca_cert, cert.ca_cert) },
2392 { STRe(ca_path, cert.ca_path) },
2393 { STRe(client_cert, cert.client_cert) },
2394 { STRe(private_key, cert.private_key) },
2395 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
2396 { STRe(dh_file, cert.dh_file) },
2397 { STRe(subject_match, cert.subject_match) },
2398 { STRe(check_cert_subject, cert.check_cert_subject) },
2399 { STRe(altsubject_match, cert.altsubject_match) },
2400 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2401 { STRe(domain_match, cert.domain_match) },
2402 { STRe(ca_cert2, phase2_cert.ca_cert) },
2403 { STRe(ca_path2, phase2_cert.ca_path) },
2404 { STRe(client_cert2, phase2_cert.client_cert) },
2405 { STRe(private_key2, phase2_cert.private_key) },
2406 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
2407 { STRe(dh_file2, phase2_cert.dh_file) },
2408 { STRe(subject_match2, phase2_cert.subject_match) },
2409 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2410 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2411 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2412 { STRe(domain_match2, phase2_cert.domain_match) },
2413 { STRe(phase1, phase1) },
2414 { STRe(phase2, phase2) },
2415 { STRe(machine_phase2, machine_phase2) },
2416 { STRe(pcsc, pcsc) },
2417 { STR_KEYe(pin, cert.pin) },
2418 { STRe(engine_id, cert.engine_id) },
2419 { STRe(key_id, cert.key_id) },
2420 { STRe(cert_id, cert.cert_id) },
2421 { STRe(ca_cert_id, cert.ca_cert_id) },
2422 { STR_KEYe(pin2, phase2_cert.pin) },
2423 { STRe(engine_id2, phase2_cert.engine_id) },
2424 { STRe(key_id2, phase2_cert.key_id) },
2425 { STRe(cert_id2, phase2_cert.cert_id) },
2426 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2427 { INTe(engine, cert.engine) },
2428 { INTe(engine2, phase2_cert.engine) },
2429 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2430 { STRe(machine_ca_path, machine_cert.ca_path) },
2431 { STRe(machine_client_cert, machine_cert.client_cert) },
2432 { STRe(machine_private_key, machine_cert.private_key) },
2433 { STR_KEYe(machine_private_key_passwd,
2434 machine_cert.private_key_passwd) },
2435 { STRe(machine_dh_file, machine_cert.dh_file) },
2436 { STRe(machine_subject_match, machine_cert.subject_match) },
2437 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2438 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2439 { STRe(machine_domain_suffix_match,
2440 machine_cert.domain_suffix_match) },
2441 { STRe(machine_domain_match, machine_cert.domain_match) },
2442 { STR_KEYe(machine_pin, machine_cert.pin) },
2443 { STRe(machine_engine_id, machine_cert.engine_id) },
2444 { STRe(machine_key_id, machine_cert.key_id) },
2445 { STRe(machine_cert_id, machine_cert.cert_id) },
2446 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2447 { INTe(machine_engine, machine_cert.engine) },
2448 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002450 { INTe(sim_num, sim_num) },
2451 { STRe(openssl_ciphers, openssl_ciphers) },
2452 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453#endif /* IEEE8021X_EAPOL */
2454 { FUNC_KEY(wep_key0) },
2455 { FUNC_KEY(wep_key1) },
2456 { FUNC_KEY(wep_key2) },
2457 { FUNC_KEY(wep_key3) },
2458 { INT(wep_tx_keyidx) },
2459 { INT(priority) },
2460#ifdef IEEE8021X_EAPOL
2461 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002462 { STRe(pac_file, pac_file) },
2463 { INTe(fragment_size, fragment_size) },
2464 { INTe(ocsp, cert.ocsp) },
2465 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002467#ifdef CONFIG_MESH
2468 { INT_RANGE(mode, 0, 5) },
2469 { INT_RANGE(no_auto_peer, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002470 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002471#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002473#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 { INT_RANGE(proactive_key_caching, 0, 1) },
2475 { INT_RANGE(disabled, 0, 2) },
2476 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002478#ifdef CONFIG_OCV
2479 { FUNC(ocv) },
2480#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002481 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002483 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002484 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002485 { INT_RANGE(enable_edmg, 0, 1) },
2486 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002487#ifdef CONFIG_ACS
2488 { INT_RANGE(acs, 0, 1) },
2489#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002490#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002491 { FUNC(mesh_basic_rates) },
2492 { INT(dot11MeshMaxRetries) },
2493 { INT(dot11MeshRetryTimeout) },
2494 { INT(dot11MeshConfirmTimeout) },
2495 { INT(dot11MeshHoldingTimeout) },
2496#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 { INT(wpa_ptk_rekey) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002498 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002501#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002502 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002504 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002505#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002506#ifdef CONFIG_HT_OVERRIDES
2507 { INT_RANGE(disable_ht, 0, 1) },
2508 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002509 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002510 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002511 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002512 { INT_RANGE(tx_stbc, -1, 1) },
2513 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002514 { INT_RANGE(disable_max_amsdu, -1, 1) },
2515 { INT_RANGE(ampdu_factor, -1, 3) },
2516 { INT_RANGE(ampdu_density, -1, 7) },
2517 { STR(ht_mcs) },
2518#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002519#ifdef CONFIG_VHT_OVERRIDES
2520 { INT_RANGE(disable_vht, 0, 1) },
2521 { INT(vht_capa) },
2522 { INT(vht_capa_mask) },
2523 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2524 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2525 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2526 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2527 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2528 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2529 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2530 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2531 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2532 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2533 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2534 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2535 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2536 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2537 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2538 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2539#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 { INT(ap_max_inactivity) },
2541 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002542 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002543#ifdef CONFIG_MACSEC
2544 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002545 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002546 { INT_RANGE(macsec_replay_protect, 0, 1) },
2547 { INT(macsec_replay_window) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002548 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002549 { INT_RANGE(mka_priority, 0, 255) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002550 { FUNC_KEY(mka_cak) },
2551 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002552#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002553#ifdef CONFIG_HS20
2554 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002555 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002556#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002557 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002558 { INT_RANGE(pbss, 0, 2) },
2559 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002560 { INT_RANGE(fils_dh_group, 0, 65535) },
2561#ifdef CONFIG_DPP
2562 { STR(dpp_connector) },
2563 { STR_LEN(dpp_netaccesskey) },
2564 { INT(dpp_netaccesskey_expiry) },
2565 { STR_LEN(dpp_csign) },
2566#endif /* CONFIG_DPP */
2567 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002568 { INT_RANGE(owe_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002569 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002570 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571};
2572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573#undef OFFSET
2574#undef _STR
2575#undef STR
2576#undef STR_KEY
2577#undef _STR_LEN
2578#undef STR_LEN
2579#undef STR_LEN_KEY
2580#undef _STR_RANGE
2581#undef STR_RANGE
2582#undef STR_RANGE_KEY
2583#undef _INT
2584#undef INT
2585#undef INT_RANGE
2586#undef _FUNC
2587#undef FUNC
2588#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002589#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
2591
2592/**
2593 * wpa_config_add_prio_network - Add a network to priority lists
2594 * @config: Configuration data from wpa_config_read()
2595 * @ssid: Pointer to the network configuration to be added to the list
2596 * Returns: 0 on success, -1 on failure
2597 *
2598 * This function is used to add a network block to the priority list of
2599 * networks. This must be called for each network when reading in the full
2600 * configuration. In addition, this can be used indirectly when updating
2601 * priorities by calling wpa_config_update_prio_list().
2602 */
2603int wpa_config_add_prio_network(struct wpa_config *config,
2604 struct wpa_ssid *ssid)
2605{
2606 int prio;
2607 struct wpa_ssid *prev, **nlist;
2608
2609 /*
2610 * Add to an existing priority list if one is available for the
2611 * configured priority level for this network.
2612 */
2613 for (prio = 0; prio < config->num_prio; prio++) {
2614 prev = config->pssid[prio];
2615 if (prev->priority == ssid->priority) {
2616 while (prev->pnext)
2617 prev = prev->pnext;
2618 prev->pnext = ssid;
2619 return 0;
2620 }
2621 }
2622
2623 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002624 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2625 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 if (nlist == NULL)
2627 return -1;
2628
2629 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002630 if (nlist[prio]->priority < ssid->priority) {
2631 os_memmove(&nlist[prio + 1], &nlist[prio],
2632 (config->num_prio - prio) *
2633 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 }
2637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 nlist[prio] = ssid;
2639 config->num_prio++;
2640 config->pssid = nlist;
2641
2642 return 0;
2643}
2644
2645
2646/**
2647 * wpa_config_update_prio_list - Update network priority list
2648 * @config: Configuration data from wpa_config_read()
2649 * Returns: 0 on success, -1 on failure
2650 *
2651 * This function is called to update the priority list of networks in the
2652 * configuration when a network is being added or removed. This is also called
2653 * if a priority for a network is changed.
2654 */
2655int wpa_config_update_prio_list(struct wpa_config *config)
2656{
2657 struct wpa_ssid *ssid;
2658 int ret = 0;
2659
2660 os_free(config->pssid);
2661 config->pssid = NULL;
2662 config->num_prio = 0;
2663
2664 ssid = config->ssid;
2665 while (ssid) {
2666 ssid->pnext = NULL;
2667 if (wpa_config_add_prio_network(config, ssid) < 0)
2668 ret = -1;
2669 ssid = ssid->next;
2670 }
2671
2672 return ret;
2673}
2674
2675
2676#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002677
2678static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2679{
2680 os_free(cert->ca_cert);
2681 os_free(cert->ca_path);
2682 os_free(cert->client_cert);
2683 os_free(cert->private_key);
2684 str_clear_free(cert->private_key_passwd);
2685 os_free(cert->dh_file);
2686 os_free(cert->subject_match);
2687 os_free(cert->check_cert_subject);
2688 os_free(cert->altsubject_match);
2689 os_free(cert->domain_suffix_match);
2690 os_free(cert->domain_match);
2691 str_clear_free(cert->pin);
2692 os_free(cert->engine_id);
2693 os_free(cert->key_id);
2694 os_free(cert->cert_id);
2695 os_free(cert->ca_cert_id);
2696}
2697
2698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699static void eap_peer_config_free(struct eap_peer_config *eap)
2700{
2701 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002702 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002704 os_free(eap->imsi_identity);
Hai Shalomc3565922019-10-28 11:58:20 -07002705 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002706 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002707 bin_clear_free(eap->machine_password, eap->machine_password_len);
2708 eap_peer_config_free_cert(&eap->cert);
2709 eap_peer_config_free_cert(&eap->phase2_cert);
2710 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 os_free(eap->phase1);
2712 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002713 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 os_free(eap->otp);
2716 os_free(eap->pending_req_otp);
2717 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002718 bin_clear_free(eap->new_password, eap->new_password_len);
2719 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002720 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721}
Hai Shalomc3565922019-10-28 11:58:20 -07002722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723#endif /* IEEE8021X_EAPOL */
2724
2725
2726/**
2727 * wpa_config_free_ssid - Free network/ssid configuration data
2728 * @ssid: Configuration data for the network
2729 *
2730 * This function frees all resources allocated for the network configuration
2731 * data.
2732 */
2733void wpa_config_free_ssid(struct wpa_ssid *ssid)
2734{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002735 struct psk_list_entry *psk;
2736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002738 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002739 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002740 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002741 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742#ifdef IEEE8021X_EAPOL
2743 eap_peer_config_free(&ssid->eap);
2744#endif /* IEEE8021X_EAPOL */
2745 os_free(ssid->id_str);
2746 os_free(ssid->scan_freq);
2747 os_free(ssid->freq_list);
2748 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002749 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002750 os_free(ssid->bssid_blacklist);
2751 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002752#ifdef CONFIG_HT_OVERRIDES
2753 os_free(ssid->ht_mcs);
2754#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002755#ifdef CONFIG_MESH
2756 os_free(ssid->mesh_basic_rates);
2757#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002758#ifdef CONFIG_HS20
2759 os_free(ssid->roaming_consortium_selection);
2760#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002761 os_free(ssid->dpp_connector);
2762 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2763 os_free(ssid->dpp_csign);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002764 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2765 list))) {
2766 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002767 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002768 }
Hai Shalomc3565922019-10-28 11:58:20 -07002769#ifdef CONFIG_SAE
2770 sae_deinit_pt(ssid->pt);
2771#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002772 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773}
2774
2775
Dmitry Shmidt04949592012-07-19 12:16:46 -07002776void wpa_config_free_cred(struct wpa_cred *cred)
2777{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002778 size_t i;
2779
Dmitry Shmidt04949592012-07-19 12:16:46 -07002780 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002781 str_clear_free(cred->username);
2782 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002783 os_free(cred->ca_cert);
2784 os_free(cred->client_cert);
2785 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002786 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002787 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002788 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002789 for (i = 0; i < cred->num_domain; i++)
2790 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002791 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002792 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002793 os_free(cred->eap_method);
2794 os_free(cred->phase1);
2795 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002796 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002797 os_free(cred->roaming_partner);
2798 os_free(cred->provisioning_sp);
2799 for (i = 0; i < cred->num_req_conn_capab; i++)
2800 os_free(cred->req_conn_capab_port[i]);
2801 os_free(cred->req_conn_capab_port);
2802 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002803 os_free(cred);
2804}
2805
2806
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002807void wpa_config_flush_blobs(struct wpa_config *config)
2808{
2809#ifndef CONFIG_NO_CONFIG_BLOBS
2810 struct wpa_config_blob *blob, *prev;
2811
2812 blob = config->blobs;
2813 config->blobs = NULL;
2814 while (blob) {
2815 prev = blob;
2816 blob = blob->next;
2817 wpa_config_free_blob(prev);
2818 }
2819#endif /* CONFIG_NO_CONFIG_BLOBS */
2820}
2821
2822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823/**
2824 * wpa_config_free - Free configuration data
2825 * @config: Configuration data from wpa_config_read()
2826 *
2827 * This function frees all resources allocated for the configuration data by
2828 * wpa_config_read().
2829 */
2830void wpa_config_free(struct wpa_config *config)
2831{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002833 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002834 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835
2836 ssid = config->ssid;
2837 while (ssid) {
2838 prev = ssid;
2839 ssid = ssid->next;
2840 wpa_config_free_ssid(prev);
2841 }
2842
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843 cred = config->cred;
2844 while (cred) {
2845 cprev = cred;
2846 cred = cred->next;
2847 wpa_config_free_cred(cprev);
2848 }
2849
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002850 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851
Dmitry Shmidt04949592012-07-19 12:16:46 -07002852 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002853 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2854 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 os_free(config->ctrl_interface);
2856 os_free(config->ctrl_interface_group);
2857 os_free(config->opensc_engine_path);
2858 os_free(config->pkcs11_engine_path);
2859 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002860 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002862 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 os_free(config->driver_param);
2864 os_free(config->device_name);
2865 os_free(config->manufacturer);
2866 os_free(config->model_name);
2867 os_free(config->model_number);
2868 os_free(config->serial_number);
2869 os_free(config->config_methods);
2870 os_free(config->p2p_ssid_postfix);
2871 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002873 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002874 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002875 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002876 wpabuf_free(config->wps_nfc_dh_pubkey);
2877 wpabuf_free(config->wps_nfc_dh_privkey);
2878 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002879 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002880 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002881 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002882 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002883 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002884 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002885 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002886 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002887#ifdef CONFIG_MBO
2888 os_free(config->non_pref_chan);
2889#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07002890 os_free(config->dpp_name);
2891 os_free(config->dpp_mud_url);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 os_free(config);
2894}
2895
2896
2897/**
2898 * wpa_config_foreach_network - Iterate over each configured network
2899 * @config: Configuration data from wpa_config_read()
2900 * @func: Callback function to process each network
2901 * @arg: Opaque argument to pass to callback function
2902 *
2903 * Iterate over the set of configured networks calling the specified
2904 * function for each item. We guard against callbacks removing the
2905 * supplied network.
2906 */
2907void wpa_config_foreach_network(struct wpa_config *config,
2908 void (*func)(void *, struct wpa_ssid *),
2909 void *arg)
2910{
2911 struct wpa_ssid *ssid, *next;
2912
2913 ssid = config->ssid;
2914 while (ssid) {
2915 next = ssid->next;
2916 func(arg, ssid);
2917 ssid = next;
2918 }
2919}
2920
2921
2922/**
2923 * wpa_config_get_network - Get configured network based on id
2924 * @config: Configuration data from wpa_config_read()
2925 * @id: Unique network id to search for
2926 * Returns: Network configuration or %NULL if not found
2927 */
2928struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2929{
2930 struct wpa_ssid *ssid;
2931
2932 ssid = config->ssid;
2933 while (ssid) {
2934 if (id == ssid->id)
2935 break;
2936 ssid = ssid->next;
2937 }
2938
2939 return ssid;
2940}
2941
2942
2943/**
2944 * wpa_config_add_network - Add a new network with empty configuration
2945 * @config: Configuration data from wpa_config_read()
2946 * Returns: The new network configuration or %NULL if operation failed
2947 */
2948struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2949{
2950 int id;
2951 struct wpa_ssid *ssid, *last = NULL;
2952
2953 id = -1;
2954 ssid = config->ssid;
2955 while (ssid) {
2956 if (ssid->id > id)
2957 id = ssid->id;
2958 last = ssid;
2959 ssid = ssid->next;
2960 }
2961 id++;
2962
2963 ssid = os_zalloc(sizeof(*ssid));
2964 if (ssid == NULL)
2965 return NULL;
2966 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002967 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968 if (last)
2969 last->next = ssid;
2970 else
2971 config->ssid = ssid;
2972
2973 wpa_config_update_prio_list(config);
2974
2975 return ssid;
2976}
2977
2978
2979/**
2980 * wpa_config_remove_network - Remove a configured network based on id
2981 * @config: Configuration data from wpa_config_read()
2982 * @id: Unique network id to search for
2983 * Returns: 0 on success, or -1 if the network was not found
2984 */
2985int wpa_config_remove_network(struct wpa_config *config, int id)
2986{
2987 struct wpa_ssid *ssid, *prev = NULL;
2988
2989 ssid = config->ssid;
2990 while (ssid) {
2991 if (id == ssid->id)
2992 break;
2993 prev = ssid;
2994 ssid = ssid->next;
2995 }
2996
2997 if (ssid == NULL)
2998 return -1;
2999
3000 if (prev)
3001 prev->next = ssid->next;
3002 else
3003 config->ssid = ssid->next;
3004
3005 wpa_config_update_prio_list(config);
3006 wpa_config_free_ssid(ssid);
3007 return 0;
3008}
3009
3010
3011/**
3012 * wpa_config_set_network_defaults - Set network default values
3013 * @ssid: Pointer to network configuration data
3014 */
3015void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3016{
3017 ssid->proto = DEFAULT_PROTO;
3018 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3019 ssid->group_cipher = DEFAULT_GROUP;
3020 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003021 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003022 ssid->ht = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023#ifdef IEEE8021X_EAPOL
3024 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3025 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3026 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003027 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003029#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3031 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3032 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3033 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003034 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003035#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003036#ifdef CONFIG_HT_OVERRIDES
3037 ssid->disable_ht = DEFAULT_DISABLE_HT;
3038 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003039 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003040 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003041 ssid->tx_stbc = DEFAULT_TX_STBC;
3042 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003043 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3044 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3045 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3046#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003047#ifdef CONFIG_VHT_OVERRIDES
3048 ssid->vht_rx_mcs_nss_1 = -1;
3049 ssid->vht_rx_mcs_nss_2 = -1;
3050 ssid->vht_rx_mcs_nss_3 = -1;
3051 ssid->vht_rx_mcs_nss_4 = -1;
3052 ssid->vht_rx_mcs_nss_5 = -1;
3053 ssid->vht_rx_mcs_nss_6 = -1;
3054 ssid->vht_rx_mcs_nss_7 = -1;
3055 ssid->vht_rx_mcs_nss_8 = -1;
3056 ssid->vht_tx_mcs_nss_1 = -1;
3057 ssid->vht_tx_mcs_nss_2 = -1;
3058 ssid->vht_tx_mcs_nss_3 = -1;
3059 ssid->vht_tx_mcs_nss_4 = -1;
3060 ssid->vht_tx_mcs_nss_5 = -1;
3061 ssid->vht_tx_mcs_nss_6 = -1;
3062 ssid->vht_tx_mcs_nss_7 = -1;
3063 ssid->vht_tx_mcs_nss_8 = -1;
3064#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003065 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003066 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003067#ifdef CONFIG_MACSEC
3068 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3069#endif /* CONFIG_MACSEC */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003070 ssid->mac_addr = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003071 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072}
3073
3074
3075/**
3076 * wpa_config_set - Set a variable in network configuration
3077 * @ssid: Pointer to network configuration data
3078 * @var: Variable name, e.g., "ssid"
3079 * @value: Variable value
3080 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003081 * Returns: 0 on success with possible change in the value, 1 on success with
3082 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 *
3084 * This function can be used to set network configuration variables based on
3085 * both the configuration file and management interface input. The value
3086 * parameter must be in the same format as the text-based configuration file is
3087 * using. For example, strings are using double quotation marks.
3088 */
3089int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3090 int line)
3091{
3092 size_t i;
3093 int ret = 0;
3094
3095 if (ssid == NULL || var == NULL || value == NULL)
3096 return -1;
3097
3098 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3099 const struct parse_data *field = &ssid_fields[i];
3100 if (os_strcmp(var, field->name) != 0)
3101 continue;
3102
Dmitry Shmidte4663042016-04-04 10:07:49 -07003103 ret = field->parser(field, ssid, line, value);
3104 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 if (line) {
3106 wpa_printf(MSG_ERROR, "Line %d: failed to "
3107 "parse %s '%s'.", line, var, value);
3108 }
3109 ret = -1;
3110 }
Hai Shalomc3565922019-10-28 11:58:20 -07003111#ifdef CONFIG_SAE
3112 if (os_strcmp(var, "ssid") == 0 ||
3113 os_strcmp(var, "psk") == 0 ||
3114 os_strcmp(var, "sae_password") == 0 ||
3115 os_strcmp(var, "sae_password_id") == 0) {
3116 sae_deinit_pt(ssid->pt);
3117 ssid->pt = NULL;
3118 }
3119#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 break;
3121 }
3122 if (i == NUM_SSID_FIELDS) {
3123 if (line) {
3124 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3125 "'%s'.", line, var);
3126 }
3127 ret = -1;
3128 }
3129
3130 return ret;
3131}
3132
3133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003134int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3135 const char *value)
3136{
3137 size_t len;
3138 char *buf;
3139 int ret;
3140
3141 len = os_strlen(value);
3142 buf = os_malloc(len + 3);
3143 if (buf == NULL)
3144 return -1;
3145 buf[0] = '"';
3146 os_memcpy(buf + 1, value, len);
3147 buf[len + 1] = '"';
3148 buf[len + 2] = '\0';
3149 ret = wpa_config_set(ssid, var, buf, 0);
3150 os_free(buf);
3151 return ret;
3152}
3153
3154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155/**
3156 * wpa_config_get_all - Get all options from network configuration
3157 * @ssid: Pointer to network configuration data
3158 * @get_keys: Determines if keys/passwords will be included in returned list
3159 * (if they may be exported)
3160 * Returns: %NULL terminated list of all set keys and their values in the form
3161 * of [key1, val1, key2, val2, ... , NULL]
3162 *
3163 * This function can be used to get list of all configured network properties.
3164 * The caller is responsible for freeing the returned list and all its
3165 * elements.
3166 */
3167char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3168{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003169#ifdef NO_CONFIG_WRITE
3170 return NULL;
3171#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172 const struct parse_data *field;
3173 char *key, *value;
3174 size_t i;
3175 char **props;
3176 int fields_num;
3177
3178 get_keys = get_keys && ssid->export_keys;
3179
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003180 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 if (!props)
3182 return NULL;
3183
3184 fields_num = 0;
3185 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3186 field = &ssid_fields[i];
3187 if (field->key_data && !get_keys)
3188 continue;
3189 value = field->writer(field, ssid);
3190 if (value == NULL)
3191 continue;
3192 if (os_strlen(value) == 0) {
3193 os_free(value);
3194 continue;
3195 }
3196
3197 key = os_strdup(field->name);
3198 if (key == NULL) {
3199 os_free(value);
3200 goto err;
3201 }
3202
3203 props[fields_num * 2] = key;
3204 props[fields_num * 2 + 1] = value;
3205
3206 fields_num++;
3207 }
3208
3209 return props;
3210
3211err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003212 for (i = 0; props[i]; i++)
3213 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 os_free(props);
3215 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003216#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217}
3218
3219
3220#ifndef NO_CONFIG_WRITE
3221/**
3222 * wpa_config_get - Get a variable in network configuration
3223 * @ssid: Pointer to network configuration data
3224 * @var: Variable name, e.g., "ssid"
3225 * Returns: Value of the variable or %NULL on failure
3226 *
3227 * This function can be used to get network configuration variables. The
3228 * returned value is a copy of the configuration variable in text format, i.e,.
3229 * the same format that the text-based configuration file and wpa_config_set()
3230 * are using for the value. The caller is responsible for freeing the returned
3231 * value.
3232 */
3233char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3234{
3235 size_t i;
3236
3237 if (ssid == NULL || var == NULL)
3238 return NULL;
3239
3240 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3241 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003242 if (os_strcmp(var, field->name) == 0) {
3243 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003244
3245 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003246 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003247 "Found newline in value for %s; not returning it",
3248 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003249 os_free(ret);
3250 ret = NULL;
3251 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003252
Paul Stewart85c72c62016-03-03 15:33:47 -08003253 return ret;
3254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 }
3256
3257 return NULL;
3258}
3259
3260
3261/**
3262 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3263 * @ssid: Pointer to network configuration data
3264 * @var: Variable name, e.g., "ssid"
3265 * Returns: Value of the variable or %NULL on failure
3266 *
3267 * This function can be used to get network configuration variable like
3268 * wpa_config_get(). The only difference is that this functions does not expose
3269 * key/password material from the configuration. In case a key/password field
3270 * is requested, the returned value is an empty string or %NULL if the variable
3271 * is not set or "*" if the variable is set (regardless of its value). The
3272 * returned value is a copy of the configuration variable in text format, i.e,.
3273 * the same format that the text-based configuration file and wpa_config_set()
3274 * are using for the value. The caller is responsible for freeing the returned
3275 * value.
3276 */
3277char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3278{
3279 size_t i;
3280
3281 if (ssid == NULL || var == NULL)
3282 return NULL;
3283
3284 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3285 const struct parse_data *field = &ssid_fields[i];
3286 if (os_strcmp(var, field->name) == 0) {
3287 char *res = field->writer(field, ssid);
3288 if (field->key_data) {
3289 if (res && res[0]) {
3290 wpa_printf(MSG_DEBUG, "Do not allow "
3291 "key_data field to be "
3292 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003293 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 return os_strdup("*");
3295 }
3296
3297 os_free(res);
3298 return NULL;
3299 }
3300 return res;
3301 }
3302 }
3303
3304 return NULL;
3305}
3306#endif /* NO_CONFIG_WRITE */
3307
3308
3309/**
3310 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3311 * @ssid: Pointer to network configuration data
3312 *
3313 * This function must be called to update WPA PSK when either SSID or the
3314 * passphrase has changed for the network configuration.
3315 */
3316void wpa_config_update_psk(struct wpa_ssid *ssid)
3317{
3318#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003319 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 ssid->psk, PMK_LEN);
3321 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3322 ssid->psk, PMK_LEN);
3323 ssid->psk_set = 1;
3324#endif /* CONFIG_NO_PBKDF2 */
3325}
3326
3327
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003328static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3329 const char *value)
3330{
3331 u8 *proto;
3332 int **port;
3333 int *ports, *nports;
3334 const char *pos;
3335 unsigned int num_ports;
3336
3337 proto = os_realloc_array(cred->req_conn_capab_proto,
3338 cred->num_req_conn_capab + 1, sizeof(u8));
3339 if (proto == NULL)
3340 return -1;
3341 cred->req_conn_capab_proto = proto;
3342
3343 port = os_realloc_array(cred->req_conn_capab_port,
3344 cred->num_req_conn_capab + 1, sizeof(int *));
3345 if (port == NULL)
3346 return -1;
3347 cred->req_conn_capab_port = port;
3348
3349 proto[cred->num_req_conn_capab] = atoi(value);
3350
3351 pos = os_strchr(value, ':');
3352 if (pos == NULL) {
3353 port[cred->num_req_conn_capab] = NULL;
3354 cred->num_req_conn_capab++;
3355 return 0;
3356 }
3357 pos++;
3358
3359 ports = NULL;
3360 num_ports = 0;
3361
3362 while (*pos) {
3363 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3364 if (nports == NULL) {
3365 os_free(ports);
3366 return -1;
3367 }
3368 ports = nports;
3369 ports[num_ports++] = atoi(pos);
3370
3371 pos = os_strchr(pos, ',');
3372 if (pos == NULL)
3373 break;
3374 pos++;
3375 }
3376
3377 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3378 if (nports == NULL) {
3379 os_free(ports);
3380 return -1;
3381 }
3382 ports = nports;
3383 ports[num_ports] = -1;
3384
3385 port[cred->num_req_conn_capab] = ports;
3386 cred->num_req_conn_capab++;
3387 return 0;
3388}
3389
3390
Roshan Pius3a1667e2018-07-03 15:17:14 -07003391static int wpa_config_set_cred_roaming_consortiums(struct wpa_cred *cred,
3392 const char *value)
3393{
3394 u8 roaming_consortiums[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3395 size_t roaming_consortiums_len[MAX_ROAMING_CONS];
3396 unsigned int num_roaming_consortiums = 0;
3397 const char *pos, *end;
3398 size_t len;
3399
3400 os_memset(roaming_consortiums, 0, sizeof(roaming_consortiums));
3401 os_memset(roaming_consortiums_len, 0, sizeof(roaming_consortiums_len));
3402
3403 for (pos = value;;) {
3404 end = os_strchr(pos, ',');
3405 len = end ? (size_t) (end - pos) : os_strlen(pos);
3406 if (!end && len == 0)
3407 break;
3408 if (len == 0 || (len & 1) != 0 ||
3409 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3410 hexstr2bin(pos,
3411 roaming_consortiums[num_roaming_consortiums],
3412 len / 2) < 0) {
3413 wpa_printf(MSG_INFO,
3414 "Invalid roaming_consortiums entry: %s",
3415 pos);
3416 return -1;
3417 }
3418 roaming_consortiums_len[num_roaming_consortiums] = len / 2;
3419 num_roaming_consortiums++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003420
3421 if (!end)
3422 break;
3423
3424 if (num_roaming_consortiums >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003425 wpa_printf(MSG_INFO,
3426 "Too many roaming_consortiums OIs");
3427 return -1;
3428 }
3429
Roshan Pius3a1667e2018-07-03 15:17:14 -07003430 pos = end + 1;
3431 }
3432
3433 os_memcpy(cred->roaming_consortiums, roaming_consortiums,
3434 sizeof(roaming_consortiums));
3435 os_memcpy(cred->roaming_consortiums_len, roaming_consortiums_len,
3436 sizeof(roaming_consortiums_len));
3437 cred->num_roaming_consortiums = num_roaming_consortiums;
3438
3439 return 0;
3440}
3441
3442
Dmitry Shmidt04949592012-07-19 12:16:46 -07003443int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3444 const char *value, int line)
3445{
3446 char *val;
3447 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003448 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003450 if (os_strcmp(var, "temporary") == 0) {
3451 cred->temporary = atoi(value);
3452 return 0;
3453 }
3454
Dmitry Shmidt04949592012-07-19 12:16:46 -07003455 if (os_strcmp(var, "priority") == 0) {
3456 cred->priority = atoi(value);
3457 return 0;
3458 }
3459
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003460 if (os_strcmp(var, "sp_priority") == 0) {
3461 int prio = atoi(value);
3462 if (prio < 0 || prio > 255)
3463 return -1;
3464 cred->sp_priority = prio;
3465 return 0;
3466 }
3467
Dmitry Shmidt04949592012-07-19 12:16:46 -07003468 if (os_strcmp(var, "pcsc") == 0) {
3469 cred->pcsc = atoi(value);
3470 return 0;
3471 }
3472
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003473 if (os_strcmp(var, "eap") == 0) {
3474 struct eap_method_type method;
3475 method.method = eap_peer_get_type(value, &method.vendor);
3476 if (method.vendor == EAP_VENDOR_IETF &&
3477 method.method == EAP_TYPE_NONE) {
3478 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3479 "for a credential", line, value);
3480 return -1;
3481 }
3482 os_free(cred->eap_method);
3483 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3484 if (cred->eap_method == NULL)
3485 return -1;
3486 os_memcpy(cred->eap_method, &method, sizeof(method));
3487 return 0;
3488 }
3489
3490 if (os_strcmp(var, "password") == 0 &&
3491 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003492 if (has_newline(value))
3493 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003494 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003495 cred->password = os_strdup(value);
3496 cred->ext_password = 1;
3497 return 0;
3498 }
3499
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003500 if (os_strcmp(var, "update_identifier") == 0) {
3501 cred->update_identifier = atoi(value);
3502 return 0;
3503 }
3504
3505 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3506 cred->min_dl_bandwidth_home = atoi(value);
3507 return 0;
3508 }
3509
3510 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3511 cred->min_ul_bandwidth_home = atoi(value);
3512 return 0;
3513 }
3514
3515 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3516 cred->min_dl_bandwidth_roaming = atoi(value);
3517 return 0;
3518 }
3519
3520 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3521 cred->min_ul_bandwidth_roaming = atoi(value);
3522 return 0;
3523 }
3524
3525 if (os_strcmp(var, "max_bss_load") == 0) {
3526 cred->max_bss_load = atoi(value);
3527 return 0;
3528 }
3529
3530 if (os_strcmp(var, "req_conn_capab") == 0)
3531 return wpa_config_set_cred_req_conn_capab(cred, value);
3532
3533 if (os_strcmp(var, "ocsp") == 0) {
3534 cred->ocsp = atoi(value);
3535 return 0;
3536 }
3537
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003538 if (os_strcmp(var, "sim_num") == 0) {
3539 cred->sim_num = atoi(value);
3540 return 0;
3541 }
3542
Dmitry Shmidt04949592012-07-19 12:16:46 -07003543 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003544 if (val == NULL ||
3545 (os_strcmp(var, "excluded_ssid") != 0 &&
3546 os_strcmp(var, "roaming_consortium") != 0 &&
3547 os_strcmp(var, "required_roaming_consortium") != 0 &&
3548 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003549 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3550 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003551 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003552 return -1;
3553 }
3554
3555 if (os_strcmp(var, "realm") == 0) {
3556 os_free(cred->realm);
3557 cred->realm = val;
3558 return 0;
3559 }
3560
3561 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003562 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003563 cred->username = val;
3564 return 0;
3565 }
3566
3567 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003568 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003569 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003570 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003571 return 0;
3572 }
3573
3574 if (os_strcmp(var, "ca_cert") == 0) {
3575 os_free(cred->ca_cert);
3576 cred->ca_cert = val;
3577 return 0;
3578 }
3579
3580 if (os_strcmp(var, "client_cert") == 0) {
3581 os_free(cred->client_cert);
3582 cred->client_cert = val;
3583 return 0;
3584 }
3585
3586 if (os_strcmp(var, "private_key") == 0) {
3587 os_free(cred->private_key);
3588 cred->private_key = val;
3589 return 0;
3590 }
3591
3592 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003593 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003594 cred->private_key_passwd = val;
3595 return 0;
3596 }
3597
3598 if (os_strcmp(var, "imsi") == 0) {
3599 os_free(cred->imsi);
3600 cred->imsi = val;
3601 return 0;
3602 }
3603
3604 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003605 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003606 cred->milenage = val;
3607 return 0;
3608 }
3609
Dmitry Shmidt051af732013-10-22 13:52:46 -07003610 if (os_strcmp(var, "domain_suffix_match") == 0) {
3611 os_free(cred->domain_suffix_match);
3612 cred->domain_suffix_match = val;
3613 return 0;
3614 }
3615
Dmitry Shmidt04949592012-07-19 12:16:46 -07003616 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003617 char **new_domain;
3618 new_domain = os_realloc_array(cred->domain,
3619 cred->num_domain + 1,
3620 sizeof(char *));
3621 if (new_domain == NULL) {
3622 os_free(val);
3623 return -1;
3624 }
3625 new_domain[cred->num_domain++] = val;
3626 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003627 return 0;
3628 }
3629
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630 if (os_strcmp(var, "phase1") == 0) {
3631 os_free(cred->phase1);
3632 cred->phase1 = val;
3633 return 0;
3634 }
3635
3636 if (os_strcmp(var, "phase2") == 0) {
3637 os_free(cred->phase2);
3638 cred->phase2 = val;
3639 return 0;
3640 }
3641
3642 if (os_strcmp(var, "roaming_consortium") == 0) {
3643 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3644 wpa_printf(MSG_ERROR, "Line %d: invalid "
3645 "roaming_consortium length %d (3..15 "
3646 "expected)", line, (int) len);
3647 os_free(val);
3648 return -1;
3649 }
3650 os_memcpy(cred->roaming_consortium, val, len);
3651 cred->roaming_consortium_len = len;
3652 os_free(val);
3653 return 0;
3654 }
3655
Dmitry Shmidt051af732013-10-22 13:52:46 -07003656 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3657 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3658 {
3659 wpa_printf(MSG_ERROR, "Line %d: invalid "
3660 "required_roaming_consortium length %d "
3661 "(3..15 expected)", line, (int) len);
3662 os_free(val);
3663 return -1;
3664 }
3665 os_memcpy(cred->required_roaming_consortium, val, len);
3666 cred->required_roaming_consortium_len = len;
3667 os_free(val);
3668 return 0;
3669 }
3670
Roshan Pius3a1667e2018-07-03 15:17:14 -07003671 if (os_strcmp(var, "roaming_consortiums") == 0) {
3672 res = wpa_config_set_cred_roaming_consortiums(cred, val);
3673 if (res < 0)
3674 wpa_printf(MSG_ERROR,
3675 "Line %d: invalid roaming_consortiums",
3676 line);
3677 os_free(val);
3678 return res;
3679 }
3680
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003681 if (os_strcmp(var, "excluded_ssid") == 0) {
3682 struct excluded_ssid *e;
3683
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003684 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003685 wpa_printf(MSG_ERROR, "Line %d: invalid "
3686 "excluded_ssid length %d", line, (int) len);
3687 os_free(val);
3688 return -1;
3689 }
3690
3691 e = os_realloc_array(cred->excluded_ssid,
3692 cred->num_excluded_ssid + 1,
3693 sizeof(struct excluded_ssid));
3694 if (e == NULL) {
3695 os_free(val);
3696 return -1;
3697 }
3698 cred->excluded_ssid = e;
3699
3700 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3701 os_memcpy(e->ssid, val, len);
3702 e->ssid_len = len;
3703
3704 os_free(val);
3705
3706 return 0;
3707 }
3708
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003709 if (os_strcmp(var, "roaming_partner") == 0) {
3710 struct roaming_partner *p;
3711 char *pos;
3712
3713 p = os_realloc_array(cred->roaming_partner,
3714 cred->num_roaming_partner + 1,
3715 sizeof(struct roaming_partner));
3716 if (p == NULL) {
3717 os_free(val);
3718 return -1;
3719 }
3720 cred->roaming_partner = p;
3721
3722 p = &cred->roaming_partner[cred->num_roaming_partner];
3723
3724 pos = os_strchr(val, ',');
3725 if (pos == NULL) {
3726 os_free(val);
3727 return -1;
3728 }
3729 *pos++ = '\0';
3730 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3731 os_free(val);
3732 return -1;
3733 }
3734 os_memcpy(p->fqdn, val, pos - val);
3735
3736 p->exact_match = atoi(pos);
3737
3738 pos = os_strchr(pos, ',');
3739 if (pos == NULL) {
3740 os_free(val);
3741 return -1;
3742 }
3743 *pos++ = '\0';
3744
3745 p->priority = atoi(pos);
3746
3747 pos = os_strchr(pos, ',');
3748 if (pos == NULL) {
3749 os_free(val);
3750 return -1;
3751 }
3752 *pos++ = '\0';
3753
3754 if (os_strlen(pos) >= sizeof(p->country)) {
3755 os_free(val);
3756 return -1;
3757 }
3758 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3759
3760 cred->num_roaming_partner++;
3761 os_free(val);
3762
3763 return 0;
3764 }
3765
3766 if (os_strcmp(var, "provisioning_sp") == 0) {
3767 os_free(cred->provisioning_sp);
3768 cred->provisioning_sp = val;
3769 return 0;
3770 }
3771
Dmitry Shmidt04949592012-07-19 12:16:46 -07003772 if (line) {
3773 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3774 line, var);
3775 }
3776
3777 os_free(val);
3778
3779 return -1;
3780}
3781
3782
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003783static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003784{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003785 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003786 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003787 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003789 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003790 if (buf == NULL)
3791 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003792 res = os_snprintf(buf, bufsize, "%d", val);
3793 if (os_snprintf_error(bufsize, res)) {
3794 os_free(buf);
3795 buf = NULL;
3796 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003797 return buf;
3798}
3799
3800
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003801static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003802{
3803 if (str == NULL)
3804 return NULL;
3805 return os_strdup(str);
3806}
3807
3808
3809char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3810{
3811 if (os_strcmp(var, "temporary") == 0)
3812 return alloc_int_str(cred->temporary);
3813
3814 if (os_strcmp(var, "priority") == 0)
3815 return alloc_int_str(cred->priority);
3816
3817 if (os_strcmp(var, "sp_priority") == 0)
3818 return alloc_int_str(cred->sp_priority);
3819
3820 if (os_strcmp(var, "pcsc") == 0)
3821 return alloc_int_str(cred->pcsc);
3822
3823 if (os_strcmp(var, "eap") == 0) {
3824 if (!cred->eap_method)
3825 return NULL;
3826 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3827 cred->eap_method[0].method));
3828 }
3829
3830 if (os_strcmp(var, "update_identifier") == 0)
3831 return alloc_int_str(cred->update_identifier);
3832
3833 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3834 return alloc_int_str(cred->min_dl_bandwidth_home);
3835
3836 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3837 return alloc_int_str(cred->min_ul_bandwidth_home);
3838
3839 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3840 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3841
3842 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3843 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3844
3845 if (os_strcmp(var, "max_bss_load") == 0)
3846 return alloc_int_str(cred->max_bss_load);
3847
3848 if (os_strcmp(var, "req_conn_capab") == 0) {
3849 unsigned int i;
3850 char *buf, *end, *pos;
3851 int ret;
3852
3853 if (!cred->num_req_conn_capab)
3854 return NULL;
3855
3856 buf = os_malloc(4000);
3857 if (buf == NULL)
3858 return NULL;
3859 pos = buf;
3860 end = pos + 4000;
3861 for (i = 0; i < cred->num_req_conn_capab; i++) {
3862 int *ports;
3863
3864 ret = os_snprintf(pos, end - pos, "%s%u",
3865 i > 0 ? "\n" : "",
3866 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003867 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003868 return buf;
3869 pos += ret;
3870
3871 ports = cred->req_conn_capab_port[i];
3872 if (ports) {
3873 int j;
3874 for (j = 0; ports[j] != -1; j++) {
3875 ret = os_snprintf(pos, end - pos,
3876 "%s%d",
3877 j > 0 ? "," : ":",
3878 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003879 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003880 return buf;
3881 pos += ret;
3882 }
3883 }
3884 }
3885
3886 return buf;
3887 }
3888
3889 if (os_strcmp(var, "ocsp") == 0)
3890 return alloc_int_str(cred->ocsp);
3891
3892 if (os_strcmp(var, "realm") == 0)
3893 return alloc_strdup(cred->realm);
3894
3895 if (os_strcmp(var, "username") == 0)
3896 return alloc_strdup(cred->username);
3897
3898 if (os_strcmp(var, "password") == 0) {
3899 if (!cred->password)
3900 return NULL;
3901 return alloc_strdup("*");
3902 }
3903
3904 if (os_strcmp(var, "ca_cert") == 0)
3905 return alloc_strdup(cred->ca_cert);
3906
3907 if (os_strcmp(var, "client_cert") == 0)
3908 return alloc_strdup(cred->client_cert);
3909
3910 if (os_strcmp(var, "private_key") == 0)
3911 return alloc_strdup(cred->private_key);
3912
3913 if (os_strcmp(var, "private_key_passwd") == 0) {
3914 if (!cred->private_key_passwd)
3915 return NULL;
3916 return alloc_strdup("*");
3917 }
3918
3919 if (os_strcmp(var, "imsi") == 0)
3920 return alloc_strdup(cred->imsi);
3921
3922 if (os_strcmp(var, "milenage") == 0) {
3923 if (!(cred->milenage))
3924 return NULL;
3925 return alloc_strdup("*");
3926 }
3927
3928 if (os_strcmp(var, "domain_suffix_match") == 0)
3929 return alloc_strdup(cred->domain_suffix_match);
3930
3931 if (os_strcmp(var, "domain") == 0) {
3932 unsigned int i;
3933 char *buf, *end, *pos;
3934 int ret;
3935
3936 if (!cred->num_domain)
3937 return NULL;
3938
3939 buf = os_malloc(4000);
3940 if (buf == NULL)
3941 return NULL;
3942 pos = buf;
3943 end = pos + 4000;
3944
3945 for (i = 0; i < cred->num_domain; i++) {
3946 ret = os_snprintf(pos, end - pos, "%s%s",
3947 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003948 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003949 return buf;
3950 pos += ret;
3951 }
3952
3953 return buf;
3954 }
3955
3956 if (os_strcmp(var, "phase1") == 0)
3957 return alloc_strdup(cred->phase1);
3958
3959 if (os_strcmp(var, "phase2") == 0)
3960 return alloc_strdup(cred->phase2);
3961
3962 if (os_strcmp(var, "roaming_consortium") == 0) {
3963 size_t buflen;
3964 char *buf;
3965
3966 if (!cred->roaming_consortium_len)
3967 return NULL;
3968 buflen = cred->roaming_consortium_len * 2 + 1;
3969 buf = os_malloc(buflen);
3970 if (buf == NULL)
3971 return NULL;
3972 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3973 cred->roaming_consortium_len);
3974 return buf;
3975 }
3976
3977 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3978 size_t buflen;
3979 char *buf;
3980
3981 if (!cred->required_roaming_consortium_len)
3982 return NULL;
3983 buflen = cred->required_roaming_consortium_len * 2 + 1;
3984 buf = os_malloc(buflen);
3985 if (buf == NULL)
3986 return NULL;
3987 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3988 cred->required_roaming_consortium_len);
3989 return buf;
3990 }
3991
Roshan Pius3a1667e2018-07-03 15:17:14 -07003992 if (os_strcmp(var, "roaming_consortiums") == 0) {
3993 size_t buflen;
3994 char *buf, *pos;
3995 size_t i;
3996
3997 if (!cred->num_roaming_consortiums)
3998 return NULL;
3999 buflen = cred->num_roaming_consortiums *
4000 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4001 buf = os_malloc(buflen);
4002 if (!buf)
4003 return NULL;
4004 pos = buf;
4005 for (i = 0; i < cred->num_roaming_consortiums; i++) {
4006 if (i > 0)
4007 *pos++ = ',';
4008 pos += wpa_snprintf_hex(
4009 pos, buf + buflen - pos,
4010 cred->roaming_consortiums[i],
4011 cred->roaming_consortiums_len[i]);
4012 }
4013 *pos = '\0';
4014 return buf;
4015 }
4016
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004017 if (os_strcmp(var, "excluded_ssid") == 0) {
4018 unsigned int i;
4019 char *buf, *end, *pos;
4020
4021 if (!cred->num_excluded_ssid)
4022 return NULL;
4023
4024 buf = os_malloc(4000);
4025 if (buf == NULL)
4026 return NULL;
4027 pos = buf;
4028 end = pos + 4000;
4029
4030 for (i = 0; i < cred->num_excluded_ssid; i++) {
4031 struct excluded_ssid *e;
4032 int ret;
4033
4034 e = &cred->excluded_ssid[i];
4035 ret = os_snprintf(pos, end - pos, "%s%s",
4036 i > 0 ? "\n" : "",
4037 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004038 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004039 return buf;
4040 pos += ret;
4041 }
4042
4043 return buf;
4044 }
4045
4046 if (os_strcmp(var, "roaming_partner") == 0) {
4047 unsigned int i;
4048 char *buf, *end, *pos;
4049
4050 if (!cred->num_roaming_partner)
4051 return NULL;
4052
4053 buf = os_malloc(4000);
4054 if (buf == NULL)
4055 return NULL;
4056 pos = buf;
4057 end = pos + 4000;
4058
4059 for (i = 0; i < cred->num_roaming_partner; i++) {
4060 struct roaming_partner *p;
4061 int ret;
4062
4063 p = &cred->roaming_partner[i];
4064 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4065 i > 0 ? "\n" : "",
4066 p->fqdn, p->exact_match, p->priority,
4067 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004068 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004069 return buf;
4070 pos += ret;
4071 }
4072
4073 return buf;
4074 }
4075
4076 if (os_strcmp(var, "provisioning_sp") == 0)
4077 return alloc_strdup(cred->provisioning_sp);
4078
4079 return NULL;
4080}
4081
4082
Dmitry Shmidt04949592012-07-19 12:16:46 -07004083struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4084{
4085 struct wpa_cred *cred;
4086
4087 cred = config->cred;
4088 while (cred) {
4089 if (id == cred->id)
4090 break;
4091 cred = cred->next;
4092 }
4093
4094 return cred;
4095}
4096
4097
4098struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4099{
4100 int id;
4101 struct wpa_cred *cred, *last = NULL;
4102
4103 id = -1;
4104 cred = config->cred;
4105 while (cred) {
4106 if (cred->id > id)
4107 id = cred->id;
4108 last = cred;
4109 cred = cred->next;
4110 }
4111 id++;
4112
4113 cred = os_zalloc(sizeof(*cred));
4114 if (cred == NULL)
4115 return NULL;
4116 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004117 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004118 if (last)
4119 last->next = cred;
4120 else
4121 config->cred = cred;
4122
4123 return cred;
4124}
4125
4126
4127int wpa_config_remove_cred(struct wpa_config *config, int id)
4128{
4129 struct wpa_cred *cred, *prev = NULL;
4130
4131 cred = config->cred;
4132 while (cred) {
4133 if (id == cred->id)
4134 break;
4135 prev = cred;
4136 cred = cred->next;
4137 }
4138
4139 if (cred == NULL)
4140 return -1;
4141
4142 if (prev)
4143 prev->next = cred->next;
4144 else
4145 config->cred = cred->next;
4146
4147 wpa_config_free_cred(cred);
4148 return 0;
4149}
4150
4151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152#ifndef CONFIG_NO_CONFIG_BLOBS
4153/**
4154 * wpa_config_get_blob - Get a named configuration blob
4155 * @config: Configuration data from wpa_config_read()
4156 * @name: Name of the blob
4157 * Returns: Pointer to blob data or %NULL if not found
4158 */
4159const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4160 const char *name)
4161{
4162 struct wpa_config_blob *blob = config->blobs;
4163
4164 while (blob) {
4165 if (os_strcmp(blob->name, name) == 0)
4166 return blob;
4167 blob = blob->next;
4168 }
4169 return NULL;
4170}
4171
4172
4173/**
4174 * wpa_config_set_blob - Set or add a named configuration blob
4175 * @config: Configuration data from wpa_config_read()
4176 * @blob: New value for the blob
4177 *
4178 * Adds a new configuration blob or replaces the current value of an existing
4179 * blob.
4180 */
4181void wpa_config_set_blob(struct wpa_config *config,
4182 struct wpa_config_blob *blob)
4183{
4184 wpa_config_remove_blob(config, blob->name);
4185 blob->next = config->blobs;
4186 config->blobs = blob;
4187}
4188
4189
4190/**
4191 * wpa_config_free_blob - Free blob data
4192 * @blob: Pointer to blob to be freed
4193 */
4194void wpa_config_free_blob(struct wpa_config_blob *blob)
4195{
4196 if (blob) {
4197 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004198 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 os_free(blob);
4200 }
4201}
4202
4203
4204/**
4205 * wpa_config_remove_blob - Remove a named configuration blob
4206 * @config: Configuration data from wpa_config_read()
4207 * @name: Name of the blob to remove
4208 * Returns: 0 if blob was removed or -1 if blob was not found
4209 */
4210int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4211{
4212 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4213
4214 while (pos) {
4215 if (os_strcmp(pos->name, name) == 0) {
4216 if (prev)
4217 prev->next = pos->next;
4218 else
4219 config->blobs = pos->next;
4220 wpa_config_free_blob(pos);
4221 return 0;
4222 }
4223 prev = pos;
4224 pos = pos->next;
4225 }
4226
4227 return -1;
4228}
4229#endif /* CONFIG_NO_CONFIG_BLOBS */
4230
4231
4232/**
4233 * wpa_config_alloc_empty - Allocate an empty configuration
4234 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4235 * socket
4236 * @driver_param: Driver parameters
4237 * Returns: Pointer to allocated configuration data or %NULL on failure
4238 */
4239struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4240 const char *driver_param)
4241{
4242 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004243 const int aCWmin = 4, aCWmax = 10;
4244 const struct hostapd_wmm_ac_params ac_bk =
4245 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4246 const struct hostapd_wmm_ac_params ac_be =
4247 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4248 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
4249 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
4250 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
4251 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252
4253 config = os_zalloc(sizeof(*config));
4254 if (config == NULL)
4255 return NULL;
4256 config->eapol_version = DEFAULT_EAPOL_VERSION;
4257 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004258 config->user_mpm = DEFAULT_USER_MPM;
4259 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004260 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004261 config->dot11RSNASAERetransPeriod =
4262 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 config->fast_reauth = DEFAULT_FAST_REAUTH;
4264 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4265 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004266 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004267 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004268 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004269 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4271 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4272 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4273 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004274 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004275 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004276 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004277 config->wmm_ac_params[0] = ac_be;
4278 config->wmm_ac_params[1] = ac_bk;
4279 config->wmm_ac_params[2] = ac_vi;
4280 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004281 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004282 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004283 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004284 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004285 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004287#ifdef CONFIG_MBO
4288 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004289 config->disassoc_imminent_rssi_threshold =
4290 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4291 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004292#endif /* CONFIG_MBO */
4293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 if (ctrl_interface)
4295 config->ctrl_interface = os_strdup(ctrl_interface);
4296 if (driver_param)
4297 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004298 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299
4300 return config;
4301}
4302
4303
4304#ifndef CONFIG_NO_STDOUT_DEBUG
4305/**
4306 * wpa_config_debug_dump_networks - Debug dump of configured networks
4307 * @config: Configuration data from wpa_config_read()
4308 */
4309void wpa_config_debug_dump_networks(struct wpa_config *config)
4310{
4311 int prio;
4312 struct wpa_ssid *ssid;
4313
4314 for (prio = 0; prio < config->num_prio; prio++) {
4315 ssid = config->pssid[prio];
4316 wpa_printf(MSG_DEBUG, "Priority group %d",
4317 ssid->priority);
4318 while (ssid) {
4319 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4320 ssid->id,
4321 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4322 ssid = ssid->pnext;
4323 }
4324 }
4325}
4326#endif /* CONFIG_NO_STDOUT_DEBUG */
4327
4328
4329struct global_parse_data {
4330 char *name;
4331 int (*parser)(const struct global_parse_data *data,
4332 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004333 int (*get)(const char *name, struct wpa_config *config, long offset,
4334 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 void *param1, *param2, *param3;
4336 unsigned int changed_flag;
4337};
4338
4339
4340static int wpa_global_config_parse_int(const struct global_parse_data *data,
4341 struct wpa_config *config, int line,
4342 const char *pos)
4343{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004344 int val, *dst;
4345 char *end;
4346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004348 val = strtol(pos, &end, 0);
4349 if (*end) {
4350 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4351 line, pos);
4352 return -1;
4353 }
4354 *dst = val;
4355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4357
4358 if (data->param2 && *dst < (long) data->param2) {
4359 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4360 "min_value=%ld)", line, data->name, *dst,
4361 (long) data->param2);
4362 *dst = (long) data->param2;
4363 return -1;
4364 }
4365
4366 if (data->param3 && *dst > (long) data->param3) {
4367 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4368 "max_value=%ld)", line, data->name, *dst,
4369 (long) data->param3);
4370 *dst = (long) data->param3;
4371 return -1;
4372 }
4373
4374 return 0;
4375}
4376
4377
4378static int wpa_global_config_parse_str(const struct global_parse_data *data,
4379 struct wpa_config *config, int line,
4380 const char *pos)
4381{
4382 size_t len;
4383 char **dst, *tmp;
4384
4385 len = os_strlen(pos);
4386 if (data->param2 && len < (size_t) data->param2) {
4387 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4388 "min_len=%ld)", line, data->name,
4389 (unsigned long) len, (long) data->param2);
4390 return -1;
4391 }
4392
4393 if (data->param3 && len > (size_t) data->param3) {
4394 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4395 "max_len=%ld)", line, data->name,
4396 (unsigned long) len, (long) data->param3);
4397 return -1;
4398 }
4399
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004400 if (has_newline(pos)) {
4401 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4402 line, data->name);
4403 return -1;
4404 }
4405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 tmp = os_strdup(pos);
4407 if (tmp == NULL)
4408 return -1;
4409
4410 dst = (char **) (((u8 *) config) + (long) data->param1);
4411 os_free(*dst);
4412 *dst = tmp;
4413 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4414
4415 return 0;
4416}
4417
4418
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004419static int wpa_config_process_bgscan(const struct global_parse_data *data,
4420 struct wpa_config *config, int line,
4421 const char *pos)
4422{
4423 size_t len;
4424 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004425 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004426
4427 tmp = wpa_config_parse_string(pos, &len);
4428 if (tmp == NULL) {
4429 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4430 line, data->name);
4431 return -1;
4432 }
4433
Dmitry Shmidt97672262014-02-03 13:02:54 -08004434 res = wpa_global_config_parse_str(data, config, line, tmp);
4435 os_free(tmp);
4436 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004437}
4438
4439
Dmitry Shmidt04949592012-07-19 12:16:46 -07004440static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4441 struct wpa_config *config, int line,
4442 const char *pos)
4443{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004444 struct wpabuf **dst, *tmp;
4445
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004446 tmp = wpabuf_parse_bin(pos);
4447 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004448 return -1;
4449
Dmitry Shmidt04949592012-07-19 12:16:46 -07004450 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
4451 wpabuf_free(*dst);
4452 *dst = tmp;
4453 wpa_printf(MSG_DEBUG, "%s", data->name);
4454
4455 return 0;
4456}
4457
4458
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004459static int wpa_config_process_freq_list(const struct global_parse_data *data,
4460 struct wpa_config *config, int line,
4461 const char *value)
4462{
4463 int *freqs;
4464
4465 freqs = wpa_config_parse_int_array(value);
4466 if (freqs == NULL)
4467 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004468 if (freqs[0] == 0) {
4469 os_free(freqs);
4470 freqs = NULL;
4471 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004472 os_free(config->freq_list);
4473 config->freq_list = freqs;
4474 return 0;
4475}
4476
4477
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004478#ifdef CONFIG_P2P
4479static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4480 struct wpa_config *config, int line,
4481 const char *pos)
4482{
4483 u32 *dst;
4484 struct hostapd_ip_addr addr;
4485
4486 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4487 return -1;
4488 if (addr.af != AF_INET)
4489 return -1;
4490
4491 dst = (u32 *) (((u8 *) config) + (long) data->param1);
4492 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4493 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4494 WPA_GET_BE32((u8 *) dst));
4495
4496 return 0;
4497}
4498#endif /* CONFIG_P2P */
4499
4500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501static int wpa_config_process_country(const struct global_parse_data *data,
4502 struct wpa_config *config, int line,
4503 const char *pos)
4504{
4505 if (!pos[0] || !pos[1]) {
4506 wpa_printf(MSG_DEBUG, "Invalid country set");
4507 return -1;
4508 }
4509 config->country[0] = pos[0];
4510 config->country[1] = pos[1];
4511 wpa_printf(MSG_DEBUG, "country='%c%c'",
4512 config->country[0], config->country[1]);
4513 return 0;
4514}
4515
4516
4517static int wpa_config_process_load_dynamic_eap(
4518 const struct global_parse_data *data, struct wpa_config *config,
4519 int line, const char *so)
4520{
4521 int ret;
4522 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4523 ret = eap_peer_method_load(so);
4524 if (ret == -2) {
4525 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4526 "reloading.");
4527 } else if (ret) {
4528 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4529 "method '%s'.", line, so);
4530 return -1;
4531 }
4532
4533 return 0;
4534}
4535
4536
4537#ifdef CONFIG_WPS
4538
4539static int wpa_config_process_uuid(const struct global_parse_data *data,
4540 struct wpa_config *config, int line,
4541 const char *pos)
4542{
4543 char buf[40];
4544 if (uuid_str2bin(pos, config->uuid)) {
4545 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4546 return -1;
4547 }
4548 uuid_bin2str(config->uuid, buf, sizeof(buf));
4549 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4550 return 0;
4551}
4552
4553
4554static int wpa_config_process_device_type(
4555 const struct global_parse_data *data,
4556 struct wpa_config *config, int line, const char *pos)
4557{
4558 return wps_dev_type_str2bin(pos, config->device_type);
4559}
4560
4561
4562static int wpa_config_process_os_version(const struct global_parse_data *data,
4563 struct wpa_config *config, int line,
4564 const char *pos)
4565{
4566 if (hexstr2bin(pos, config->os_version, 4)) {
4567 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4568 return -1;
4569 }
4570 wpa_printf(MSG_DEBUG, "os_version=%08x",
4571 WPA_GET_BE32(config->os_version));
4572 return 0;
4573}
4574
Dmitry Shmidt04949592012-07-19 12:16:46 -07004575
4576static int wpa_config_process_wps_vendor_ext_m1(
4577 const struct global_parse_data *data,
4578 struct wpa_config *config, int line, const char *pos)
4579{
4580 struct wpabuf *tmp;
4581 int len = os_strlen(pos) / 2;
4582 u8 *p;
4583
4584 if (!len) {
4585 wpa_printf(MSG_ERROR, "Line %d: "
4586 "invalid wps_vendor_ext_m1", line);
4587 return -1;
4588 }
4589
4590 tmp = wpabuf_alloc(len);
4591 if (tmp) {
4592 p = wpabuf_put(tmp, len);
4593
4594 if (hexstr2bin(pos, p, len)) {
4595 wpa_printf(MSG_ERROR, "Line %d: "
4596 "invalid wps_vendor_ext_m1", line);
4597 wpabuf_free(tmp);
4598 return -1;
4599 }
4600
4601 wpabuf_free(config->wps_vendor_ext_m1);
4602 config->wps_vendor_ext_m1 = tmp;
4603 } else {
4604 wpa_printf(MSG_ERROR, "Can not allocate "
4605 "memory for wps_vendor_ext_m1");
4606 return -1;
4607 }
4608
4609 return 0;
4610}
4611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612#endif /* CONFIG_WPS */
4613
4614#ifdef CONFIG_P2P
4615static int wpa_config_process_sec_device_type(
4616 const struct global_parse_data *data,
4617 struct wpa_config *config, int line, const char *pos)
4618{
4619 int idx;
4620
4621 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4622 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4623 "items", line);
4624 return -1;
4625 }
4626
4627 idx = config->num_sec_device_types;
4628
4629 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4630 return -1;
4631
4632 config->num_sec_device_types++;
4633 return 0;
4634}
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635
4636
4637static int wpa_config_process_p2p_pref_chan(
4638 const struct global_parse_data *data,
4639 struct wpa_config *config, int line, const char *pos)
4640{
4641 struct p2p_channel *pref = NULL, *n;
4642 unsigned int num = 0;
4643 const char *pos2;
4644 u8 op_class, chan;
4645
4646 /* format: class:chan,class:chan,... */
4647
4648 while (*pos) {
4649 op_class = atoi(pos);
4650 pos2 = os_strchr(pos, ':');
4651 if (pos2 == NULL)
4652 goto fail;
4653 pos2++;
4654 chan = atoi(pos2);
4655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004656 n = os_realloc_array(pref, num + 1,
4657 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004658 if (n == NULL)
4659 goto fail;
4660 pref = n;
4661 pref[num].op_class = op_class;
4662 pref[num].chan = chan;
4663 num++;
4664
4665 pos = os_strchr(pos2, ',');
4666 if (pos == NULL)
4667 break;
4668 pos++;
4669 }
4670
4671 os_free(config->p2p_pref_chan);
4672 config->p2p_pref_chan = pref;
4673 config->num_p2p_pref_chan = num;
4674 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4675 (u8 *) config->p2p_pref_chan,
4676 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4677
4678 return 0;
4679
4680fail:
4681 os_free(pref);
4682 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4683 return -1;
4684}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004685
4686
4687static int wpa_config_process_p2p_no_go_freq(
4688 const struct global_parse_data *data,
4689 struct wpa_config *config, int line, const char *pos)
4690{
4691 int ret;
4692
4693 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4694 if (ret < 0) {
4695 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4696 return -1;
4697 }
4698
4699 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4700 config->p2p_no_go_freq.num);
4701
4702 return 0;
4703}
4704
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004705static int wpa_config_process_p2p_device_persistent_mac_addr(
4706 const struct global_parse_data *data,
4707 struct wpa_config *config, int line, const char *pos)
4708{
4709 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
4710 wpa_printf(MSG_ERROR,
4711 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
4712 line, pos);
4713 return -1;
4714 }
4715
4716 return 0;
4717}
4718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719#endif /* CONFIG_P2P */
4720
4721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004722static int wpa_config_process_hessid(
4723 const struct global_parse_data *data,
4724 struct wpa_config *config, int line, const char *pos)
4725{
4726 if (hwaddr_aton2(pos, config->hessid) < 0) {
4727 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4728 line, pos);
4729 return -1;
4730 }
4731
4732 return 0;
4733}
4734
4735
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004736static int wpa_config_process_sae_groups(
4737 const struct global_parse_data *data,
4738 struct wpa_config *config, int line, const char *pos)
4739{
4740 int *groups = wpa_config_parse_int_array(pos);
4741 if (groups == NULL) {
4742 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4743 line, pos);
4744 return -1;
4745 }
4746
4747 os_free(config->sae_groups);
4748 config->sae_groups = groups;
4749
4750 return 0;
4751}
4752
4753
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004754static int wpa_config_process_ap_vendor_elements(
4755 const struct global_parse_data *data,
4756 struct wpa_config *config, int line, const char *pos)
4757{
4758 struct wpabuf *tmp;
4759 int len = os_strlen(pos) / 2;
4760 u8 *p;
4761
4762 if (!len) {
4763 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4764 line);
4765 return -1;
4766 }
4767
4768 tmp = wpabuf_alloc(len);
4769 if (tmp) {
4770 p = wpabuf_put(tmp, len);
4771
4772 if (hexstr2bin(pos, p, len)) {
4773 wpa_printf(MSG_ERROR, "Line %d: invalid "
4774 "ap_vendor_elements", line);
4775 wpabuf_free(tmp);
4776 return -1;
4777 }
4778
4779 wpabuf_free(config->ap_vendor_elements);
4780 config->ap_vendor_elements = tmp;
4781 } else {
4782 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4783 "ap_vendor_elements");
4784 return -1;
4785 }
4786
4787 return 0;
4788}
4789
4790
Dmitry Shmidt56052862013-10-04 10:23:25 -07004791#ifdef CONFIG_CTRL_IFACE
4792static int wpa_config_process_no_ctrl_interface(
4793 const struct global_parse_data *data,
4794 struct wpa_config *config, int line, const char *pos)
4795{
4796 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4797 os_free(config->ctrl_interface);
4798 config->ctrl_interface = NULL;
4799 return 0;
4800}
4801#endif /* CONFIG_CTRL_IFACE */
4802
4803
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004804static int wpa_config_get_int(const char *name, struct wpa_config *config,
4805 long offset, char *buf, size_t buflen,
4806 int pretty_print)
4807{
4808 int *val = (int *) (((u8 *) config) + (long) offset);
4809
4810 if (pretty_print)
4811 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4812 return os_snprintf(buf, buflen, "%d", *val);
4813}
4814
4815
4816static int wpa_config_get_str(const char *name, struct wpa_config *config,
4817 long offset, char *buf, size_t buflen,
4818 int pretty_print)
4819{
4820 char **val = (char **) (((u8 *) config) + (long) offset);
4821 int res;
4822
4823 if (pretty_print)
4824 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4825 *val ? *val : "null");
4826 else if (!*val)
4827 return -1;
4828 else
4829 res = os_snprintf(buf, buflen, "%s", *val);
4830 if (os_snprintf_error(buflen, res))
4831 res = -1;
4832
4833 return res;
4834}
4835
4836
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004837#ifdef CONFIG_P2P
4838static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4839 long offset, char *buf, size_t buflen,
4840 int pretty_print)
4841{
4842 void *val = ((u8 *) config) + (long) offset;
4843 int res;
4844 char addr[INET_ADDRSTRLEN];
4845
4846 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4847 return -1;
4848
4849 if (pretty_print)
4850 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4851 else
4852 res = os_snprintf(buf, buflen, "%s", addr);
4853
4854 if (os_snprintf_error(buflen, res))
4855 res = -1;
4856
4857 return res;
4858}
4859#endif /* CONFIG_P2P */
4860
4861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862#ifdef OFFSET
4863#undef OFFSET
4864#endif /* OFFSET */
4865/* OFFSET: Get offset of a variable within the wpa_config structure */
4866#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4867
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004868#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4869#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4870#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871#define INT(f) _INT(f), NULL, NULL
4872#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004873#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874#define STR(f) _STR(f), NULL, NULL
4875#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004876#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004877#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4878 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879
4880static const struct global_parse_data global_fields[] = {
4881#ifdef CONFIG_CTRL_IFACE
4882 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004883 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 { STR(ctrl_interface_group), 0 } /* deprecated */,
4885#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004886#ifdef CONFIG_MACSEC
4887 { INT_RANGE(eapol_version, 1, 3), 0 },
4888#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004890#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004892 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004893#ifdef CONFIG_MESH
4894 { INT(user_mpm), 0 },
4895 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004896 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004897 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004898#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 { INT(fast_reauth), 0 },
4901 { STR(opensc_engine_path), 0 },
4902 { STR(pkcs11_engine_path), 0 },
4903 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004904 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004905 { STR(pcsc_reader), 0 },
4906 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004907 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 { STR(driver_param), 0 },
4909 { INT(dot11RSNAConfigPMKLifetime), 0 },
4910 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4911 { INT(dot11RSNAConfigSATimeout), 0 },
4912#ifndef CONFIG_NO_CONFIG_WRITE
4913 { INT(update_config), 0 },
4914#endif /* CONFIG_NO_CONFIG_WRITE */
4915 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4916#ifdef CONFIG_WPS
4917 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004918 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004919 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4920 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4922 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4923 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4924 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4925 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4926 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4927 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4928 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07004929 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004930 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931#endif /* CONFIG_WPS */
4932#ifdef CONFIG_P2P
4933 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004934 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4935 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004936 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4937 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4939 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4940 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4941 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4942 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004943 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004944 { INT_RANGE(p2p_passphrase_len, 8, 63),
4945 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004946 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004947 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4948 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004949 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004950 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004951 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08004952 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004953 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004954 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004955 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004956 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004957 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004958 { IPV4(ip_addr_go), 0 },
4959 { IPV4(ip_addr_mask), 0 },
4960 { IPV4(ip_addr_start), 0 },
4961 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004962 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004963 { INT(p2p_device_random_mac_addr), 0 },
4964 { FUNC(p2p_device_persistent_mac_addr), 0 },
4965 { INT(p2p_interface_random_mac_addr), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966#endif /* CONFIG_P2P */
4967 { FUNC(country), CFG_CHANGED_COUNTRY },
4968 { INT(bss_max_count), 0 },
4969 { INT(bss_expiration_age), 0 },
4970 { INT(bss_expiration_scan_count), 0 },
4971 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004972 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07004974 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004975 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004976#ifdef CONFIG_HS20
4977 { INT_RANGE(hs20, 0, 1), 0 },
4978#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004979 { INT_RANGE(interworking, 0, 1), 0 },
4980 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004981 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004982 { INT_RANGE(go_interworking, 0, 1), 0 },
4983 { INT_RANGE(go_access_network_type, 0, 15), 0 },
4984 { INT_RANGE(go_internet, 0, 1), 0 },
4985 { INT_RANGE(go_venue_group, 0, 255), 0 },
4986 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004987 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4988 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004989 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4990 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4991 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4992 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4993 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004994 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4995 { INT(p2p_go_max_inactivity), 0 },
4996 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004997 { INT(okc), 0 },
4998 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004999 { FUNC(sae_groups), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005000 { INT_RANGE(sae_pwe, 0, 2), 0 },
5001 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005002 { INT(dtim_period), 0 },
5003 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005004 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005005 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005006 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005007 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005008 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005009 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005010 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005011 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005012 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005013 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005014 { INT(mac_addr), 0 },
5015 { INT(rand_addr_lifetime), 0 },
5016 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005017 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005018 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005019 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005020 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005021#ifdef CONFIG_FST
5022 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5023 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5024 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5025#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005026 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005027 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005028 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005029#ifdef CONFIG_MBO
5030 { STR(non_pref_chan), 0 },
5031 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5032 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005033 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5034 { INT_RANGE(oce, 0, 3), 0 },
5035#endif /* CONFIG_MBO */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005036 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005037 { INT_RANGE(ftm_responder, 0, 1), 0 },
5038 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005039 { INT(gas_rand_addr_lifetime), 0 },
5040 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005041#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005042 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005043 { STR(dpp_name), 0 },
5044 { STR(dpp_mud_url), 0 },
5045#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005046 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005047 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005048#ifdef CONFIG_WNM
5049 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
5050#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051};
5052
5053#undef FUNC
5054#undef _INT
5055#undef INT
5056#undef INT_RANGE
5057#undef _STR
5058#undef STR
5059#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005060#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005061#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005062#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063
5064
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005065int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5066{
5067 int result = 0;
5068 size_t i;
5069
5070 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5071 const struct global_parse_data *field = &global_fields[i];
5072 int tmp;
5073
5074 if (!field->get)
5075 continue;
5076
5077 tmp = field->get(field->name, config, (long) field->param1,
5078 buf, buflen, 1);
5079 if (tmp < 0)
5080 return -1;
5081 buf += tmp;
5082 buflen -= tmp;
5083 result += tmp;
5084 }
5085 return result;
5086}
5087
5088
5089int wpa_config_get_value(const char *name, struct wpa_config *config,
5090 char *buf, size_t buflen)
5091{
5092 size_t i;
5093
5094 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5095 const struct global_parse_data *field = &global_fields[i];
5096
5097 if (os_strcmp(name, field->name) != 0)
5098 continue;
5099 if (!field->get)
5100 break;
5101 return field->get(name, config, (long) field->param1,
5102 buf, buflen, 0);
5103 }
5104
5105 return -1;
5106}
5107
5108
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005109int wpa_config_get_num_global_field_names(void)
5110{
5111 return NUM_GLOBAL_FIELDS;
5112}
5113
5114
5115const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5116{
5117 if (i >= NUM_GLOBAL_FIELDS)
5118 return NULL;
5119
5120 if (no_var)
5121 *no_var = !global_fields[i].param1;
5122 return global_fields[i].name;
5123}
5124
5125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5127{
5128 size_t i;
5129 int ret = 0;
5130
5131 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5132 const struct global_parse_data *field = &global_fields[i];
5133 size_t flen = os_strlen(field->name);
5134 if (os_strncmp(pos, field->name, flen) != 0 ||
5135 pos[flen] != '=')
5136 continue;
5137
5138 if (field->parser(field, config, line, pos + flen + 1)) {
5139 wpa_printf(MSG_ERROR, "Line %d: failed to "
5140 "parse '%s'.", line, pos);
5141 ret = -1;
5142 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005143 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5144 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 config->changed_parameters |= field->changed_flag;
5146 break;
5147 }
5148 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005149#ifdef CONFIG_AP
5150 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5151 char *tmp = os_strchr(pos, '=');
5152 if (tmp == NULL) {
5153 if (line < 0)
5154 return -1;
5155 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5156 "'%s'", line, pos);
5157 return -1;
5158 }
5159 *tmp++ = '\0';
5160 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5161 tmp)) {
5162 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5163 "AC item", line);
5164 return -1;
5165 }
Hai Shalomc3565922019-10-28 11:58:20 -07005166 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005167 }
5168#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169 if (line < 0)
5170 return -1;
5171 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5172 line, pos);
5173 ret = -1;
5174 }
5175
5176 return ret;
5177}