blob: 0f98f778996ac8ca17365aa8458565843afc2795 [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
Sunil Ravi99c035e2024-07-12 01:42:03 +0000200static int wpa_config_parse_int_impl(const struct parse_data *data,
201 struct wpa_ssid *ssid,
202 int line, const char *value,
203 bool check_range)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700205 int val, *dst;
206 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207
208 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700209 val = strtol(value, &end, 0);
210 if (*end) {
211 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
212 line, value);
213 return -1;
214 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700215
Sunil Ravi99c035e2024-07-12 01:42:03 +0000216 if (check_range && val < (long) data->param3) {
217 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
218 "min_value=%ld)", line, data->name, val,
219 (long) data->param3);
220 return -1;
221 }
222
223 if (check_range && val > (long) data->param4) {
224 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
225 "max_value=%ld)", line, data->name, val,
226 (long) data->param4);
227 return -1;
228 }
229
Dmitry Shmidte4663042016-04-04 10:07:49 -0700230 if (*dst == val)
231 return 1;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000232
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700233 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 return 0;
237}
238
239
Sunil Ravi99c035e2024-07-12 01:42:03 +0000240static int wpa_config_parse_int(const struct parse_data *data,
241 struct wpa_ssid *ssid,
242 int line, const char *value)
243{
244 return wpa_config_parse_int_impl(data, ssid, line, value, false);
245}
246
247
248static int wpa_config_parse_int_range(const struct parse_data *data,
249 struct wpa_ssid *ssid,
250 int line, const char *value)
251{
252 return wpa_config_parse_int_impl(data, ssid, line, value, true);
253}
254
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256#ifndef NO_CONFIG_WRITE
257static char * wpa_config_write_int(const struct parse_data *data,
258 struct wpa_ssid *ssid)
259{
260 int *src, res;
261 char *value;
262
263 src = (int *) (((u8 *) ssid) + (long) data->param1);
264
265 value = os_malloc(20);
266 if (value == NULL)
267 return NULL;
268 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800269 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 os_free(value);
271 return NULL;
272 }
273 value[20 - 1] = '\0';
274 return value;
275}
276#endif /* NO_CONFIG_WRITE */
277
278
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800279static int wpa_config_parse_addr_list(const struct parse_data *data,
280 int line, const char *value,
281 u8 **list, size_t *num, char *name,
282 u8 abort_on_error, u8 masked)
283{
284 const char *pos;
285 u8 *buf, *n, addr[2 * ETH_ALEN];
286 size_t count;
287
288 buf = NULL;
289 count = 0;
290
291 pos = value;
292 while (pos && *pos) {
293 while (*pos == ' ')
294 pos++;
295
296 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
297 if (abort_on_error || count == 0) {
298 wpa_printf(MSG_ERROR,
299 "Line %d: Invalid %s address '%s'",
300 line, name, value);
301 os_free(buf);
302 return -1;
303 }
304 /* continue anyway since this could have been from a
305 * truncated configuration file line */
306 wpa_printf(MSG_INFO,
307 "Line %d: Ignore likely truncated %s address '%s'",
308 line, name, pos);
309 } else {
310 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
311 if (n == NULL) {
312 os_free(buf);
313 return -1;
314 }
315 buf = n;
316 os_memmove(buf + 2 * ETH_ALEN, buf,
317 count * 2 * ETH_ALEN);
318 os_memcpy(buf, addr, 2 * ETH_ALEN);
319 count++;
320 wpa_printf(MSG_MSGDUMP,
321 "%s: addr=" MACSTR " mask=" MACSTR,
322 name, MAC2STR(addr),
323 MAC2STR(&addr[ETH_ALEN]));
324 }
325
326 pos = os_strchr(pos, ' ');
327 }
328
329 os_free(*list);
330 *list = buf;
331 *num = count;
332
333 return 0;
334}
335
336
337#ifndef NO_CONFIG_WRITE
338static char * wpa_config_write_addr_list(const struct parse_data *data,
339 const u8 *list, size_t num, char *name)
340{
341 char *value, *end, *pos;
342 int res;
343 size_t i;
344
345 if (list == NULL || num == 0)
346 return NULL;
347
348 value = os_malloc(2 * 20 * num);
349 if (value == NULL)
350 return NULL;
351 pos = value;
352 end = value + 2 * 20 * num;
353
354 for (i = num; i > 0; i--) {
355 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
356 const u8 *m = a + ETH_ALEN;
357
358 if (i < num)
359 *pos++ = ' ';
360 res = hwaddr_mask_txt(pos, end - pos, a, m);
361 if (res < 0) {
362 os_free(value);
363 return NULL;
364 }
365 pos += res;
366 }
367
368 return value;
369}
370#endif /* NO_CONFIG_WRITE */
371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372static int wpa_config_parse_bssid(const struct parse_data *data,
373 struct wpa_ssid *ssid, int line,
374 const char *value)
375{
376 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
377 os_strcmp(value, "any") == 0) {
378 ssid->bssid_set = 0;
379 wpa_printf(MSG_MSGDUMP, "BSSID any");
380 return 0;
381 }
382 if (hwaddr_aton(value, ssid->bssid)) {
383 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
384 line, value);
385 return -1;
386 }
387 ssid->bssid_set = 1;
388 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
389 return 0;
390}
391
392
393#ifndef NO_CONFIG_WRITE
394static char * wpa_config_write_bssid(const struct parse_data *data,
395 struct wpa_ssid *ssid)
396{
397 char *value;
398 int res;
399
400 if (!ssid->bssid_set)
401 return NULL;
402
403 value = os_malloc(20);
404 if (value == NULL)
405 return NULL;
406 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800407 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 os_free(value);
409 return NULL;
410 }
411 value[20 - 1] = '\0';
412 return value;
413}
414#endif /* NO_CONFIG_WRITE */
415
416
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700417static int wpa_config_parse_bssid_hint(const struct parse_data *data,
418 struct wpa_ssid *ssid, int line,
419 const char *value)
420{
421 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
422 os_strcmp(value, "any") == 0) {
423 ssid->bssid_hint_set = 0;
424 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
425 return 0;
426 }
427 if (hwaddr_aton(value, ssid->bssid_hint)) {
428 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
429 line, value);
430 return -1;
431 }
432 ssid->bssid_hint_set = 1;
433 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
434 return 0;
435}
436
437
438#ifndef NO_CONFIG_WRITE
439static char * wpa_config_write_bssid_hint(const struct parse_data *data,
440 struct wpa_ssid *ssid)
441{
442 char *value;
443 int res;
444
445 if (!ssid->bssid_hint_set)
446 return NULL;
447
448 value = os_malloc(20);
449 if (!value)
450 return NULL;
451 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
452 if (os_snprintf_error(20, res)) {
453 os_free(value);
454 return NULL;
455 }
456 return value;
457}
458#endif /* NO_CONFIG_WRITE */
459
460
Hai Shalom60840252021-02-19 19:02:11 -0800461static int wpa_config_parse_bssid_ignore(const struct parse_data *data,
462 struct wpa_ssid *ssid, int line,
463 const char *value)
464{
465 return wpa_config_parse_addr_list(data, line, value,
466 &ssid->bssid_ignore,
467 &ssid->num_bssid_ignore,
468 "bssid_ignore", 1, 1);
469}
470
471
472/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800473static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
474 struct wpa_ssid *ssid, int line,
475 const char *value)
476{
477 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800478 &ssid->bssid_ignore,
479 &ssid->num_bssid_ignore,
480 "bssid_ignore", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800481}
482
483
484#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800485
486static char * wpa_config_write_bssid_ignore(const struct parse_data *data,
487 struct wpa_ssid *ssid)
488{
489 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
490 ssid->num_bssid_ignore,
491 "bssid_ignore");
492}
493
494
495/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800496static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
497 struct wpa_ssid *ssid)
498{
Hai Shalom60840252021-02-19 19:02:11 -0800499 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
500 ssid->num_bssid_ignore,
501 "bssid_ignore");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800502}
Hai Shalom60840252021-02-19 19:02:11 -0800503
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800504#endif /* NO_CONFIG_WRITE */
505
506
Hai Shalom60840252021-02-19 19:02:11 -0800507static int wpa_config_parse_bssid_accept(const struct parse_data *data,
508 struct wpa_ssid *ssid, int line,
509 const char *value)
510{
511 return wpa_config_parse_addr_list(data, line, value,
512 &ssid->bssid_accept,
513 &ssid->num_bssid_accept,
514 "bssid_accept", 1, 1);
515}
516
517
518/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800519static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
520 struct wpa_ssid *ssid, int line,
521 const char *value)
522{
523 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800524 &ssid->bssid_accept,
525 &ssid->num_bssid_accept,
526 "bssid_accept", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800527}
528
529
530#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800531
532static char * wpa_config_write_bssid_accept(const struct parse_data *data,
533 struct wpa_ssid *ssid)
534{
535 return wpa_config_write_addr_list(data, ssid->bssid_accept,
536 ssid->num_bssid_accept,
537 "bssid_accept");
538}
539
540
541/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800542static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
543 struct wpa_ssid *ssid)
544{
Hai Shalom60840252021-02-19 19:02:11 -0800545 return wpa_config_write_addr_list(data, ssid->bssid_accept,
546 ssid->num_bssid_accept,
547 "bssid_accept");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800548}
Hai Shalom60840252021-02-19 19:02:11 -0800549
550#endif /* NO_CONFIG_WRITE */
551
552
553#ifndef NO_CONFIG_WRITE
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800554#endif /* NO_CONFIG_WRITE */
555
556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557static int wpa_config_parse_psk(const struct parse_data *data,
558 struct wpa_ssid *ssid, int line,
559 const char *value)
560{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700561#ifdef CONFIG_EXT_PASSWORD
562 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700563 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700564 ssid->passphrase = NULL;
565 ssid->psk_set = 0;
566 os_free(ssid->ext_psk);
567 ssid->ext_psk = os_strdup(value + 4);
568 if (ssid->ext_psk == NULL)
569 return -1;
570 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
571 ssid->ext_psk);
572 return 0;
573 }
574#endif /* CONFIG_EXT_PASSWORD */
575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 if (*value == '"') {
577#ifndef CONFIG_NO_PBKDF2
578 const char *pos;
579 size_t len;
580
581 value++;
582 pos = os_strrchr(value, '"');
583 if (pos)
584 len = pos - value;
585 else
586 len = os_strlen(value);
587 if (len < 8 || len > 63) {
588 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
589 "length %lu (expected: 8..63) '%s'.",
590 line, (unsigned long) len, value);
591 return -1;
592 }
593 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
594 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700595 if (has_ctrl_char((u8 *) value, len)) {
596 wpa_printf(MSG_ERROR,
597 "Line %d: Invalid passphrase character",
598 line);
599 return -1;
600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700602 os_memcmp(ssid->passphrase, value, len) == 0) {
603 /* No change to the previously configured value */
604 return 1;
605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700607 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700608 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 if (ssid->passphrase == NULL)
610 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 return 0;
612#else /* CONFIG_NO_PBKDF2 */
613 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
614 "supported.", line);
615 return -1;
616#endif /* CONFIG_NO_PBKDF2 */
617 }
618
619 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
620 value[PMK_LEN * 2] != '\0') {
621 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
622 line, value);
623 return -1;
624 }
625
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700626 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 ssid->passphrase = NULL;
628
629 ssid->psk_set = 1;
630 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
631 return 0;
632}
633
634
635#ifndef NO_CONFIG_WRITE
636static char * wpa_config_write_psk(const struct parse_data *data,
637 struct wpa_ssid *ssid)
638{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639#ifdef CONFIG_EXT_PASSWORD
640 if (ssid->ext_psk) {
641 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
642 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800643 int res;
644
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700645 if (buf == NULL)
646 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800647 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
648 if (os_snprintf_error(len, res)) {
649 os_free(buf);
650 buf = NULL;
651 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652 return buf;
653 }
654#endif /* CONFIG_EXT_PASSWORD */
655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 if (ssid->passphrase)
657 return wpa_config_write_string_ascii(
658 (const u8 *) ssid->passphrase,
659 os_strlen(ssid->passphrase));
660
661 if (ssid->psk_set)
662 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
663
664 return NULL;
665}
666#endif /* NO_CONFIG_WRITE */
667
668
669static int wpa_config_parse_proto(const struct parse_data *data,
670 struct wpa_ssid *ssid, int line,
671 const char *value)
672{
673 int val = 0, last, errors = 0;
674 char *start, *end, *buf;
675
676 buf = os_strdup(value);
677 if (buf == NULL)
678 return -1;
679 start = buf;
680
681 while (*start != '\0') {
682 while (*start == ' ' || *start == '\t')
683 start++;
684 if (*start == '\0')
685 break;
686 end = start;
687 while (*end != ' ' && *end != '\t' && *end != '\0')
688 end++;
689 last = *end == '\0';
690 *end = '\0';
691 if (os_strcmp(start, "WPA") == 0)
692 val |= WPA_PROTO_WPA;
693 else if (os_strcmp(start, "RSN") == 0 ||
694 os_strcmp(start, "WPA2") == 0)
695 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800696 else if (os_strcmp(start, "OSEN") == 0)
697 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 else {
699 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
700 line, start);
701 errors++;
702 }
703
704 if (last)
705 break;
706 start = end + 1;
707 }
708 os_free(buf);
709
710 if (val == 0) {
711 wpa_printf(MSG_ERROR,
712 "Line %d: no proto values configured.", line);
713 errors++;
714 }
715
Dmitry Shmidte4663042016-04-04 10:07:49 -0700716 if (!errors && ssid->proto == val)
717 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
719 ssid->proto = val;
720 return errors ? -1 : 0;
721}
722
723
724#ifndef NO_CONFIG_WRITE
725static char * wpa_config_write_proto(const struct parse_data *data,
726 struct wpa_ssid *ssid)
727{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700728 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 char *buf, *pos, *end;
730
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800731 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 if (buf == NULL)
733 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800734 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735
736 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700737 ret = os_snprintf(pos, end - pos, "%sWPA",
738 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800739 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 return buf;
741 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 }
743
744 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700745 ret = os_snprintf(pos, end - pos, "%sRSN",
746 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800747 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 return buf;
749 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 }
751
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800752 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700753 ret = os_snprintf(pos, end - pos, "%sOSEN",
754 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800755 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800756 return buf;
757 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700758 }
759
760 if (pos == buf) {
761 os_free(buf);
762 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800763 }
764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 return buf;
766}
767#endif /* NO_CONFIG_WRITE */
768
769
770static int wpa_config_parse_key_mgmt(const struct parse_data *data,
771 struct wpa_ssid *ssid, int line,
772 const char *value)
773{
774 int val = 0, last, errors = 0;
775 char *start, *end, *buf;
776
777 buf = os_strdup(value);
778 if (buf == NULL)
779 return -1;
780 start = buf;
781
782 while (*start != '\0') {
783 while (*start == ' ' || *start == '\t')
784 start++;
785 if (*start == '\0')
786 break;
787 end = start;
788 while (*end != ' ' && *end != '\t' && *end != '\0')
789 end++;
790 last = *end == '\0';
791 *end = '\0';
792 if (os_strcmp(start, "WPA-PSK") == 0)
793 val |= WPA_KEY_MGMT_PSK;
794 else if (os_strcmp(start, "WPA-EAP") == 0)
795 val |= WPA_KEY_MGMT_IEEE8021X;
796 else if (os_strcmp(start, "IEEE8021X") == 0)
797 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
798 else if (os_strcmp(start, "NONE") == 0)
799 val |= WPA_KEY_MGMT_NONE;
800 else if (os_strcmp(start, "WPA-NONE") == 0)
801 val |= WPA_KEY_MGMT_WPA_NONE;
802#ifdef CONFIG_IEEE80211R
803 else if (os_strcmp(start, "FT-PSK") == 0)
804 val |= WPA_KEY_MGMT_FT_PSK;
805 else if (os_strcmp(start, "FT-EAP") == 0)
806 val |= WPA_KEY_MGMT_FT_IEEE8021X;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700807#ifdef CONFIG_SHA384
808 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
809 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
810#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811#endif /* CONFIG_IEEE80211R */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000812#ifdef CONFIG_SHA384
813 else if (os_strcmp(start, "WPA-EAP-SHA384") == 0)
814 val |= WPA_KEY_MGMT_IEEE8021X_SHA384;
815#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
817 val |= WPA_KEY_MGMT_PSK_SHA256;
818 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
819 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820#ifdef CONFIG_WPS
821 else if (os_strcmp(start, "WPS") == 0)
822 val |= WPA_KEY_MGMT_WPS;
823#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800824#ifdef CONFIG_SAE
825 else if (os_strcmp(start, "SAE") == 0)
826 val |= WPA_KEY_MGMT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700827 else if (os_strcmp(start, "SAE-EXT-KEY") == 0)
828 val |= WPA_KEY_MGMT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800829 else if (os_strcmp(start, "FT-SAE") == 0)
830 val |= WPA_KEY_MGMT_FT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700831 else if (os_strcmp(start, "FT-SAE-EXT-KEY") == 0)
832 val |= WPA_KEY_MGMT_FT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800833#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800834#ifdef CONFIG_HS20
835 else if (os_strcmp(start, "OSEN") == 0)
836 val |= WPA_KEY_MGMT_OSEN;
837#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800838#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800839 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
840 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800841#endif /* CONFIG_SUITEB */
842#ifdef CONFIG_SUITEB192
843 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
844 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
845#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800846#ifdef CONFIG_FILS
847 else if (os_strcmp(start, "FILS-SHA256") == 0)
848 val |= WPA_KEY_MGMT_FILS_SHA256;
849 else if (os_strcmp(start, "FILS-SHA384") == 0)
850 val |= WPA_KEY_MGMT_FILS_SHA384;
851#ifdef CONFIG_IEEE80211R
852 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
853 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
854 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
855 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
856#endif /* CONFIG_IEEE80211R */
857#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700858#ifdef CONFIG_OWE
859 else if (os_strcmp(start, "OWE") == 0)
860 val |= WPA_KEY_MGMT_OWE;
861#endif /* CONFIG_OWE */
862#ifdef CONFIG_DPP
863 else if (os_strcmp(start, "DPP") == 0)
864 val |= WPA_KEY_MGMT_DPP;
865#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 else {
867 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
868 line, start);
869 errors++;
870 }
871
872 if (last)
873 break;
874 start = end + 1;
875 }
876 os_free(buf);
877
878 if (val == 0) {
879 wpa_printf(MSG_ERROR,
880 "Line %d: no key_mgmt values configured.", line);
881 errors++;
882 }
883
Dmitry Shmidte4663042016-04-04 10:07:49 -0700884 if (!errors && ssid->key_mgmt == val)
885 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
887 ssid->key_mgmt = val;
888 return errors ? -1 : 0;
889}
890
891
892#ifndef NO_CONFIG_WRITE
893static char * wpa_config_write_key_mgmt(const struct parse_data *data,
894 struct wpa_ssid *ssid)
895{
896 char *buf, *pos, *end;
897 int ret;
898
Dmitry Shmidt96571392013-10-14 12:54:46 -0700899 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 if (buf == NULL)
901 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700902 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
904 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
905 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
906 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800907 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 end[-1] = '\0';
909 return buf;
910 }
911 pos += ret;
912 }
913
914 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
915 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
916 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800917 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 end[-1] = '\0';
919 return buf;
920 }
921 pos += ret;
922 }
923
924 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
925 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
926 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800927 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 end[-1] = '\0';
929 return buf;
930 }
931 pos += ret;
932 }
933
934 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
935 ret = os_snprintf(pos, end - pos, "%sNONE",
936 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800937 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 end[-1] = '\0';
939 return buf;
940 }
941 pos += ret;
942 }
943
944 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
945 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
946 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800947 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 end[-1] = '\0';
949 return buf;
950 }
951 pos += ret;
952 }
953
954#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700955 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
956 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
957 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700959 end[-1] = '\0';
960 return buf;
961 }
962 pos += ret;
963 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
Dmitry Shmidt96571392013-10-14 12:54:46 -0700965 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
966 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
967 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800968 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700969 end[-1] = '\0';
970 return buf;
971 }
972 pos += ret;
973 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700974
975#ifdef CONFIG_SHA384
976 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
977 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
978 pos == buf ? "" : " ");
979 if (os_snprintf_error(end - pos, ret)) {
980 end[-1] = '\0';
981 return buf;
982 }
983 pos += ret;
984 }
985#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986#endif /* CONFIG_IEEE80211R */
987
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000988#ifdef CONFIG_SHA384
989 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
990 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA384",
991 pos == buf ? "" : " ");
992 if (os_snprintf_error(end - pos, ret)) {
993 end[-1] = '\0';
994 return buf;
995 }
996 pos += ret;
997 }
998#endif /* CONFIG_SHA384 */
999
Dmitry Shmidt96571392013-10-14 12:54:46 -07001000 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1001 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
1002 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001004 end[-1] = '\0';
1005 return buf;
1006 }
1007 pos += ret;
1008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009
Dmitry Shmidt96571392013-10-14 12:54:46 -07001010 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1011 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
1012 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001013 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001014 end[-1] = '\0';
1015 return buf;
1016 }
1017 pos += ret;
1018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019
1020#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -07001021 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1022 ret = os_snprintf(pos, end - pos, "%sWPS",
1023 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001024 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 end[-1] = '\0';
1026 return buf;
1027 }
1028 pos += ret;
1029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030#endif /* CONFIG_WPS */
1031
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001032#ifdef CONFIG_SAE
1033 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1034 ret = os_snprintf(pos, end - pos, "%sSAE",
1035 pos == buf ? "" : " ");
1036 if (os_snprintf_error(end - pos, ret)) {
1037 end[-1] = '\0';
1038 return buf;
1039 }
1040 pos += ret;
1041 }
1042
Sunil Ravi89eba102022-09-13 21:04:37 -07001043 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1044 ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
1045 pos == buf ? "" : " ");
1046 if (os_snprintf_error(end - pos, ret)) {
1047 end[-1] = '\0';
1048 return buf;
1049 }
1050 pos += ret;
1051 }
1052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1054 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1055 pos == buf ? "" : " ");
1056 if (os_snprintf_error(end - pos, ret)) {
1057 end[-1] = '\0';
1058 return buf;
1059 }
1060 pos += ret;
1061 }
Sunil Ravi89eba102022-09-13 21:04:37 -07001062
1063 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1064 ret = os_snprintf(pos, end - pos, "%sFT-SAE-EXT-KEY",
1065 pos == buf ? "" : " ");
1066 if (os_snprintf_error(end - pos, ret)) {
1067 end[-1] = '\0';
1068 return buf;
1069 }
1070 pos += ret;
1071 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001072#endif /* CONFIG_SAE */
1073
1074#ifdef CONFIG_HS20
1075 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
1076 ret = os_snprintf(pos, end - pos, "%sOSEN",
1077 pos == buf ? "" : " ");
1078 if (os_snprintf_error(end - pos, ret)) {
1079 end[-1] = '\0';
1080 return buf;
1081 }
1082 pos += ret;
1083 }
1084#endif /* CONFIG_HS20 */
1085
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001086#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001087 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1088 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1089 pos == buf ? "" : " ");
1090 if (os_snprintf_error(end - pos, ret)) {
1091 end[-1] = '\0';
1092 return buf;
1093 }
1094 pos += ret;
1095 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001096#endif /* CONFIG_SUITEB */
1097
1098#ifdef CONFIG_SUITEB192
1099 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1100 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1101 pos == buf ? "" : " ");
1102 if (os_snprintf_error(end - pos, ret)) {
1103 end[-1] = '\0';
1104 return buf;
1105 }
1106 pos += ret;
1107 }
1108#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001109
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001110#ifdef CONFIG_FILS
1111 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1112 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1113 pos == buf ? "" : " ");
1114 if (os_snprintf_error(end - pos, ret)) {
1115 end[-1] = '\0';
1116 return buf;
1117 }
1118 pos += ret;
1119 }
1120 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1121 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1122 pos == buf ? "" : " ");
1123 if (os_snprintf_error(end - pos, ret)) {
1124 end[-1] = '\0';
1125 return buf;
1126 }
1127 pos += ret;
1128 }
1129#ifdef CONFIG_IEEE80211R
1130 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1131 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1132 pos == buf ? "" : " ");
1133 if (os_snprintf_error(end - pos, ret)) {
1134 end[-1] = '\0';
1135 return buf;
1136 }
1137 pos += ret;
1138 }
1139 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1140 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1141 pos == buf ? "" : " ");
1142 if (os_snprintf_error(end - pos, ret)) {
1143 end[-1] = '\0';
1144 return buf;
1145 }
1146 pos += ret;
1147 }
1148#endif /* CONFIG_IEEE80211R */
1149#endif /* CONFIG_FILS */
1150
Hai Shalom74f70d42019-02-11 14:42:39 -08001151#ifdef CONFIG_DPP
1152 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1153 ret = os_snprintf(pos, end - pos, "%sDPP",
1154 pos == buf ? "" : " ");
1155 if (os_snprintf_error(end - pos, ret)) {
1156 end[-1] = '\0';
1157 return buf;
1158 }
1159 pos += ret;
1160 }
1161#endif /* CONFIG_DPP */
1162
1163#ifdef CONFIG_OWE
1164 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1165 ret = os_snprintf(pos, end - pos, "%sOWE",
1166 pos == buf ? "" : " ");
1167 if (os_snprintf_error(end - pos, ret)) {
1168 end[-1] = '\0';
1169 return buf;
1170 }
1171 pos += ret;
1172 }
1173#endif /* CONFIG_OWE */
1174
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001175 if (pos == buf) {
1176 os_free(buf);
1177 buf = NULL;
1178 }
1179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 return buf;
1181}
1182#endif /* NO_CONFIG_WRITE */
1183
1184
1185static int wpa_config_parse_cipher(int line, const char *value)
1186{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001187#ifdef CONFIG_NO_WPA
1188 return -1;
1189#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001190 int val = wpa_parse_cipher(value);
1191 if (val < 0) {
1192 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1193 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 if (val == 0) {
1197 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1198 line);
1199 return -1;
1200 }
1201 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001202#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203}
1204
1205
1206#ifndef NO_CONFIG_WRITE
1207static char * wpa_config_write_cipher(int cipher)
1208{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001209#ifdef CONFIG_NO_WPA
1210 return NULL;
1211#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001212 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 if (buf == NULL)
1214 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001216 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1217 os_free(buf);
1218 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 }
1220
1221 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001222#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223}
1224#endif /* NO_CONFIG_WRITE */
1225
1226
1227static int wpa_config_parse_pairwise(const struct parse_data *data,
1228 struct wpa_ssid *ssid, int line,
1229 const char *value)
1230{
1231 int val;
1232 val = wpa_config_parse_cipher(line, value);
1233 if (val == -1)
1234 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001235 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1237 "(0x%x).", line, val);
1238 return -1;
1239 }
1240
Dmitry Shmidte4663042016-04-04 10:07:49 -07001241 if (ssid->pairwise_cipher == val)
1242 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1244 ssid->pairwise_cipher = val;
1245 return 0;
1246}
1247
1248
1249#ifndef NO_CONFIG_WRITE
1250static char * wpa_config_write_pairwise(const struct parse_data *data,
1251 struct wpa_ssid *ssid)
1252{
1253 return wpa_config_write_cipher(ssid->pairwise_cipher);
1254}
1255#endif /* NO_CONFIG_WRITE */
1256
1257
1258static int wpa_config_parse_group(const struct parse_data *data,
1259 struct wpa_ssid *ssid, int line,
1260 const char *value)
1261{
1262 int val;
1263 val = wpa_config_parse_cipher(line, value);
1264 if (val == -1)
1265 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001266
1267 /*
1268 * Backwards compatibility - filter out WEP ciphers that were previously
1269 * allowed.
1270 */
1271 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1272
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001273 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1275 "(0x%x).", line, val);
1276 return -1;
1277 }
1278
Dmitry Shmidte4663042016-04-04 10:07:49 -07001279 if (ssid->group_cipher == val)
1280 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1282 ssid->group_cipher = val;
1283 return 0;
1284}
1285
1286
1287#ifndef NO_CONFIG_WRITE
1288static char * wpa_config_write_group(const struct parse_data *data,
1289 struct wpa_ssid *ssid)
1290{
1291 return wpa_config_write_cipher(ssid->group_cipher);
1292}
1293#endif /* NO_CONFIG_WRITE */
1294
1295
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001296static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1297 struct wpa_ssid *ssid, int line,
1298 const char *value)
1299{
1300 int val;
1301
1302 val = wpa_config_parse_cipher(line, value);
1303 if (val == -1)
1304 return -1;
1305
1306 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1307 wpa_printf(MSG_ERROR,
1308 "Line %d: not allowed group management cipher (0x%x).",
1309 line, val);
1310 return -1;
1311 }
1312
1313 if (ssid->group_mgmt_cipher == val)
1314 return 1;
1315 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1316 ssid->group_mgmt_cipher = val;
1317 return 0;
1318}
1319
1320
1321#ifndef NO_CONFIG_WRITE
1322static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1323 struct wpa_ssid *ssid)
1324{
1325 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1326}
1327#endif /* NO_CONFIG_WRITE */
1328
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330static int wpa_config_parse_auth_alg(const struct parse_data *data,
1331 struct wpa_ssid *ssid, int line,
1332 const char *value)
1333{
1334 int val = 0, last, errors = 0;
1335 char *start, *end, *buf;
1336
1337 buf = os_strdup(value);
1338 if (buf == NULL)
1339 return -1;
1340 start = buf;
1341
1342 while (*start != '\0') {
1343 while (*start == ' ' || *start == '\t')
1344 start++;
1345 if (*start == '\0')
1346 break;
1347 end = start;
1348 while (*end != ' ' && *end != '\t' && *end != '\0')
1349 end++;
1350 last = *end == '\0';
1351 *end = '\0';
1352 if (os_strcmp(start, "OPEN") == 0)
1353 val |= WPA_AUTH_ALG_OPEN;
1354 else if (os_strcmp(start, "SHARED") == 0)
1355 val |= WPA_AUTH_ALG_SHARED;
1356 else if (os_strcmp(start, "LEAP") == 0)
1357 val |= WPA_AUTH_ALG_LEAP;
1358 else {
1359 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1360 line, start);
1361 errors++;
1362 }
1363
1364 if (last)
1365 break;
1366 start = end + 1;
1367 }
1368 os_free(buf);
1369
1370 if (val == 0) {
1371 wpa_printf(MSG_ERROR,
1372 "Line %d: no auth_alg values configured.", line);
1373 errors++;
1374 }
1375
Dmitry Shmidte4663042016-04-04 10:07:49 -07001376 if (!errors && ssid->auth_alg == val)
1377 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1379 ssid->auth_alg = val;
1380 return errors ? -1 : 0;
1381}
1382
1383
1384#ifndef NO_CONFIG_WRITE
1385static char * wpa_config_write_auth_alg(const struct parse_data *data,
1386 struct wpa_ssid *ssid)
1387{
1388 char *buf, *pos, *end;
1389 int ret;
1390
1391 pos = buf = os_zalloc(30);
1392 if (buf == NULL)
1393 return NULL;
1394 end = buf + 30;
1395
1396 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1397 ret = os_snprintf(pos, end - pos, "%sOPEN",
1398 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001399 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 end[-1] = '\0';
1401 return buf;
1402 }
1403 pos += ret;
1404 }
1405
1406 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1407 ret = os_snprintf(pos, end - pos, "%sSHARED",
1408 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001409 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 end[-1] = '\0';
1411 return buf;
1412 }
1413 pos += ret;
1414 }
1415
1416 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1417 ret = os_snprintf(pos, end - pos, "%sLEAP",
1418 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001419 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 end[-1] = '\0';
1421 return buf;
1422 }
1423 pos += ret;
1424 }
1425
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001426 if (pos == buf) {
1427 os_free(buf);
1428 buf = NULL;
1429 }
1430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 return buf;
1432}
1433#endif /* NO_CONFIG_WRITE */
1434
1435
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437{
1438 int *freqs;
1439 size_t used, len;
1440 const char *pos;
1441
1442 used = 0;
1443 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001444 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 if (freqs == NULL)
1446 return NULL;
1447
1448 pos = value;
1449 while (pos) {
1450 while (*pos == ' ')
1451 pos++;
1452 if (used == len) {
1453 int *n;
1454 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001455 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 if (n == NULL) {
1457 os_free(freqs);
1458 return NULL;
1459 }
1460 for (i = len; i <= len * 2; i++)
1461 n[i] = 0;
1462 freqs = n;
1463 len *= 2;
1464 }
1465
1466 freqs[used] = atoi(pos);
1467 if (freqs[used] == 0)
1468 break;
1469 used++;
1470 pos = os_strchr(pos + 1, ' ');
1471 }
1472
1473 return freqs;
1474}
1475
1476
1477static int wpa_config_parse_scan_freq(const struct parse_data *data,
1478 struct wpa_ssid *ssid, int line,
1479 const char *value)
1480{
1481 int *freqs;
1482
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 if (freqs == NULL)
1485 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001486 if (freqs[0] == 0) {
1487 os_free(freqs);
1488 freqs = NULL;
1489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 os_free(ssid->scan_freq);
1491 ssid->scan_freq = freqs;
1492
1493 return 0;
1494}
1495
1496
1497static int wpa_config_parse_freq_list(const struct parse_data *data,
1498 struct wpa_ssid *ssid, int line,
1499 const char *value)
1500{
1501 int *freqs;
1502
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001503 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 if (freqs == NULL)
1505 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001506 if (freqs[0] == 0) {
1507 os_free(freqs);
1508 freqs = NULL;
1509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 os_free(ssid->freq_list);
1511 ssid->freq_list = freqs;
1512
1513 return 0;
1514}
1515
1516
1517#ifndef NO_CONFIG_WRITE
1518static char * wpa_config_write_freqs(const struct parse_data *data,
1519 const int *freqs)
1520{
1521 char *buf, *pos, *end;
1522 int i, ret;
1523 size_t count;
1524
1525 if (freqs == NULL)
1526 return NULL;
1527
1528 count = 0;
1529 for (i = 0; freqs[i]; i++)
1530 count++;
1531
1532 pos = buf = os_zalloc(10 * count + 1);
1533 if (buf == NULL)
1534 return NULL;
1535 end = buf + 10 * count + 1;
1536
1537 for (i = 0; freqs[i]; i++) {
1538 ret = os_snprintf(pos, end - pos, "%s%u",
1539 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001540 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 end[-1] = '\0';
1542 return buf;
1543 }
1544 pos += ret;
1545 }
1546
1547 return buf;
1548}
1549
1550
1551static char * wpa_config_write_scan_freq(const struct parse_data *data,
1552 struct wpa_ssid *ssid)
1553{
1554 return wpa_config_write_freqs(data, ssid->scan_freq);
1555}
1556
1557
1558static char * wpa_config_write_freq_list(const struct parse_data *data,
1559 struct wpa_ssid *ssid)
1560{
1561 return wpa_config_write_freqs(data, ssid->freq_list);
1562}
1563#endif /* NO_CONFIG_WRITE */
1564
1565
1566#ifdef IEEE8021X_EAPOL
1567static int wpa_config_parse_eap(const struct parse_data *data,
1568 struct wpa_ssid *ssid, int line,
1569 const char *value)
1570{
1571 int last, errors = 0;
1572 char *start, *end, *buf;
1573 struct eap_method_type *methods = NULL, *tmp;
1574 size_t num_methods = 0;
1575
1576 buf = os_strdup(value);
1577 if (buf == NULL)
1578 return -1;
1579 start = buf;
1580
1581 while (*start != '\0') {
1582 while (*start == ' ' || *start == '\t')
1583 start++;
1584 if (*start == '\0')
1585 break;
1586 end = start;
1587 while (*end != ' ' && *end != '\t' && *end != '\0')
1588 end++;
1589 last = *end == '\0';
1590 *end = '\0';
1591 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001592 methods = os_realloc_array(methods, num_methods + 1,
1593 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 if (methods == NULL) {
1595 os_free(tmp);
1596 os_free(buf);
1597 return -1;
1598 }
1599 methods[num_methods].method = eap_peer_get_type(
1600 start, &methods[num_methods].vendor);
1601 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1602 methods[num_methods].method == EAP_TYPE_NONE) {
1603 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1604 "'%s'", line, start);
1605 wpa_printf(MSG_ERROR, "You may need to add support for"
1606 " this EAP method during wpa_supplicant\n"
1607 "build time configuration.\n"
1608 "See README for more information.");
1609 errors++;
1610 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1611 methods[num_methods].method == EAP_TYPE_LEAP)
1612 ssid->leap++;
1613 else
1614 ssid->non_leap++;
1615 num_methods++;
1616 if (last)
1617 break;
1618 start = end + 1;
1619 }
1620 os_free(buf);
1621
1622 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (methods == NULL) {
1625 os_free(tmp);
1626 return -1;
1627 }
1628 methods[num_methods].vendor = EAP_VENDOR_IETF;
1629 methods[num_methods].method = EAP_TYPE_NONE;
1630 num_methods++;
1631
Dmitry Shmidte4663042016-04-04 10:07:49 -07001632 if (!errors && ssid->eap.eap_methods) {
1633 struct eap_method_type *prev_m;
1634 size_t i, j, prev_methods, match = 0;
1635
1636 prev_m = ssid->eap.eap_methods;
1637 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1638 prev_m[i].method != EAP_TYPE_NONE; i++) {
1639 /* Count the methods */
1640 }
1641 prev_methods = i + 1;
1642
1643 for (i = 0; prev_methods == num_methods && i < prev_methods;
1644 i++) {
1645 for (j = 0; j < num_methods; j++) {
1646 if (prev_m[i].vendor == methods[j].vendor &&
1647 prev_m[i].method == methods[j].method) {
1648 match++;
1649 break;
1650 }
1651 }
1652 }
1653 if (match == num_methods) {
1654 os_free(methods);
1655 return 1;
1656 }
1657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1659 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001660 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 ssid->eap.eap_methods = methods;
1662 return errors ? -1 : 0;
1663}
1664
1665
Dmitry Shmidt83474442015-04-15 13:47:09 -07001666#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667static char * wpa_config_write_eap(const struct parse_data *data,
1668 struct wpa_ssid *ssid)
1669{
1670 int i, ret;
1671 char *buf, *pos, *end;
1672 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1673 const char *name;
1674
1675 if (eap_methods == NULL)
1676 return NULL;
1677
1678 pos = buf = os_zalloc(100);
1679 if (buf == NULL)
1680 return NULL;
1681 end = buf + 100;
1682
1683 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1684 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1685 name = eap_get_name(eap_methods[i].vendor,
1686 eap_methods[i].method);
1687 if (name) {
1688 ret = os_snprintf(pos, end - pos, "%s%s",
1689 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001690 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 break;
1692 pos += ret;
1693 }
1694 }
1695
1696 end[-1] = '\0';
1697
1698 return buf;
1699}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001700#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701
1702
1703static int wpa_config_parse_password(const struct parse_data *data,
1704 struct wpa_ssid *ssid, int line,
1705 const char *value)
1706{
1707 u8 *hash;
1708
1709 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001710 if (!ssid->eap.password)
1711 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001713 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 ssid->eap.password = NULL;
1715 ssid->eap.password_len = 0;
1716 return 0;
1717 }
1718
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001719#ifdef CONFIG_EXT_PASSWORD
1720 if (os_strncmp(value, "ext:", 4) == 0) {
1721 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001722 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001723 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001724 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001725 ssid->eap.password = (u8 *) name;
1726 ssid->eap.password_len = os_strlen(name);
1727 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1728 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1729 return 0;
1730 }
1731#endif /* CONFIG_EXT_PASSWORD */
1732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 if (os_strncmp(value, "hash:", 5) != 0) {
1734 char *tmp;
1735 size_t res_len;
1736
1737 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001738 if (!tmp) {
1739 wpa_printf(MSG_ERROR,
1740 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 return -1;
1742 }
1743 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1744 (u8 *) tmp, res_len);
1745
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001746 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 ssid->eap.password = (u8 *) tmp;
1748 ssid->eap.password_len = res_len;
1749 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001750 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751
1752 return 0;
1753 }
1754
1755
1756 /* NtPasswordHash: hash:<32 hex digits> */
1757 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001758 wpa_printf(MSG_ERROR,
1759 "Line %d: Invalid password hash length (expected 32 hex digits)",
1760 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 return -1;
1762 }
1763
1764 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001765 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 return -1;
1767
1768 if (hexstr2bin(value + 5, hash, 16)) {
1769 os_free(hash);
1770 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1771 return -1;
1772 }
1773
1774 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1775
Dmitry Shmidte4663042016-04-04 10:07:49 -07001776 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1777 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1778 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1779 bin_clear_free(hash, 16);
1780 return 1;
1781 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001782 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 ssid->eap.password = hash;
1784 ssid->eap.password_len = 16;
1785 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001786 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787
1788 return 0;
1789}
1790
1791
Hai Shalomc3565922019-10-28 11:58:20 -07001792static int wpa_config_parse_machine_password(const struct parse_data *data,
1793 struct wpa_ssid *ssid, int line,
1794 const char *value)
1795{
1796 u8 *hash;
1797
1798 if (os_strcmp(value, "NULL") == 0) {
1799 if (!ssid->eap.machine_password)
1800 return 1; /* Already unset */
1801 wpa_printf(MSG_DEBUG,
1802 "Unset configuration string 'machine_password'");
1803 bin_clear_free(ssid->eap.machine_password,
1804 ssid->eap.machine_password_len);
1805 ssid->eap.machine_password = NULL;
1806 ssid->eap.machine_password_len = 0;
1807 return 0;
1808 }
1809
1810#ifdef CONFIG_EXT_PASSWORD
1811 if (os_strncmp(value, "ext:", 4) == 0) {
1812 char *name = os_strdup(value + 4);
1813
1814 if (!name)
1815 return -1;
1816 bin_clear_free(ssid->eap.machine_password,
1817 ssid->eap.machine_password_len);
1818 ssid->eap.machine_password = (u8 *) name;
1819 ssid->eap.machine_password_len = os_strlen(name);
1820 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1821 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1822 return 0;
1823 }
1824#endif /* CONFIG_EXT_PASSWORD */
1825
1826 if (os_strncmp(value, "hash:", 5) != 0) {
1827 char *tmp;
1828 size_t res_len;
1829
1830 tmp = wpa_config_parse_string(value, &res_len);
1831 if (!tmp) {
1832 wpa_printf(MSG_ERROR,
1833 "Line %d: failed to parse machine_password.",
1834 line);
1835 return -1;
1836 }
1837 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1838 (u8 *) tmp, res_len);
1839
1840 bin_clear_free(ssid->eap.machine_password,
1841 ssid->eap.machine_password_len);
1842 ssid->eap.machine_password = (u8 *) tmp;
1843 ssid->eap.machine_password_len = res_len;
1844 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1845 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1846
1847 return 0;
1848 }
1849
1850
1851 /* NtPasswordHash: hash:<32 hex digits> */
1852 if (os_strlen(value + 5) != 2 * 16) {
1853 wpa_printf(MSG_ERROR,
1854 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1855 line);
1856 return -1;
1857 }
1858
1859 hash = os_malloc(16);
1860 if (!hash)
1861 return -1;
1862
1863 if (hexstr2bin(value + 5, hash, 16)) {
1864 os_free(hash);
1865 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1866 line);
1867 return -1;
1868 }
1869
1870 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1871
1872 if (ssid->eap.machine_password &&
1873 ssid->eap.machine_password_len == 16 &&
1874 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1875 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1876 bin_clear_free(hash, 16);
1877 return 1;
1878 }
1879 bin_clear_free(ssid->eap.machine_password,
1880 ssid->eap.machine_password_len);
1881 ssid->eap.machine_password = hash;
1882 ssid->eap.machine_password_len = 16;
1883 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1884 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1885
1886 return 0;
1887}
1888
1889
Dmitry Shmidt83474442015-04-15 13:47:09 -07001890#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892static char * wpa_config_write_password(const struct parse_data *data,
1893 struct wpa_ssid *ssid)
1894{
1895 char *buf;
1896
Hai Shalomc3565922019-10-28 11:58:20 -07001897 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 return NULL;
1899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900#ifdef CONFIG_EXT_PASSWORD
1901 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1902 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001903 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904 return NULL;
1905 os_memcpy(buf, "ext:", 4);
1906 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1907 return buf;
1908 }
1909#endif /* CONFIG_EXT_PASSWORD */
1910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1912 return wpa_config_write_string(
1913 ssid->eap.password, ssid->eap.password_len);
1914 }
1915
1916 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001917 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 return NULL;
1919
1920 os_memcpy(buf, "hash:", 5);
1921 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1922
1923 return buf;
1924}
Hai Shalomc3565922019-10-28 11:58:20 -07001925
1926
1927static char * wpa_config_write_machine_password(const struct parse_data *data,
1928 struct wpa_ssid *ssid)
1929{
1930 char *buf;
1931
1932 if (!ssid->eap.machine_password)
1933 return NULL;
1934
1935#ifdef CONFIG_EXT_PASSWORD
1936 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1937 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1938 if (!buf)
1939 return NULL;
1940 os_memcpy(buf, "ext:", 4);
1941 os_memcpy(buf + 4, ssid->eap.machine_password,
1942 ssid->eap.machine_password_len);
1943 return buf;
1944 }
1945#endif /* CONFIG_EXT_PASSWORD */
1946
1947 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1948 return wpa_config_write_string(
1949 ssid->eap.machine_password,
1950 ssid->eap.machine_password_len);
1951 }
1952
1953 buf = os_malloc(5 + 32 + 1);
1954 if (!buf)
1955 return NULL;
1956
1957 os_memcpy(buf, "hash:", 5);
1958 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1959
1960 return buf;
1961}
1962
Dmitry Shmidt83474442015-04-15 13:47:09 -07001963#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964#endif /* IEEE8021X_EAPOL */
1965
1966
Hai Shalomfdcde762020-04-02 11:19:20 -07001967#ifdef CONFIG_WEP
1968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1970 const char *value, int idx)
1971{
1972 char *buf, title[20];
1973 int res;
1974
1975 buf = wpa_config_parse_string(value, len);
1976 if (buf == NULL) {
1977 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1978 line, idx, value);
1979 return -1;
1980 }
1981 if (*len > MAX_WEP_KEY_LEN) {
1982 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1983 line, idx, value);
1984 os_free(buf);
1985 return -1;
1986 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001987 if (*len && *len != 5 && *len != 13 && *len != 16) {
1988 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1989 "this network block will be ignored",
1990 line, (unsigned int) *len);
1991 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001993 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001995 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1997 return 0;
1998}
1999
2000
2001static int wpa_config_parse_wep_key0(const struct parse_data *data,
2002 struct wpa_ssid *ssid, int line,
2003 const char *value)
2004{
2005 return wpa_config_parse_wep_key(ssid->wep_key[0],
2006 &ssid->wep_key_len[0], line,
2007 value, 0);
2008}
2009
2010
2011static int wpa_config_parse_wep_key1(const struct parse_data *data,
2012 struct wpa_ssid *ssid, int line,
2013 const char *value)
2014{
2015 return wpa_config_parse_wep_key(ssid->wep_key[1],
2016 &ssid->wep_key_len[1], line,
2017 value, 1);
2018}
2019
2020
2021static int wpa_config_parse_wep_key2(const struct parse_data *data,
2022 struct wpa_ssid *ssid, int line,
2023 const char *value)
2024{
2025 return wpa_config_parse_wep_key(ssid->wep_key[2],
2026 &ssid->wep_key_len[2], line,
2027 value, 2);
2028}
2029
2030
2031static int wpa_config_parse_wep_key3(const struct parse_data *data,
2032 struct wpa_ssid *ssid, int line,
2033 const char *value)
2034{
2035 return wpa_config_parse_wep_key(ssid->wep_key[3],
2036 &ssid->wep_key_len[3], line,
2037 value, 3);
2038}
2039
2040
2041#ifndef NO_CONFIG_WRITE
2042static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2043{
2044 if (ssid->wep_key_len[idx] == 0)
2045 return NULL;
2046 return wpa_config_write_string(ssid->wep_key[idx],
2047 ssid->wep_key_len[idx]);
2048}
2049
2050
2051static char * wpa_config_write_wep_key0(const struct parse_data *data,
2052 struct wpa_ssid *ssid)
2053{
2054 return wpa_config_write_wep_key(ssid, 0);
2055}
2056
2057
2058static char * wpa_config_write_wep_key1(const struct parse_data *data,
2059 struct wpa_ssid *ssid)
2060{
2061 return wpa_config_write_wep_key(ssid, 1);
2062}
2063
2064
2065static char * wpa_config_write_wep_key2(const struct parse_data *data,
2066 struct wpa_ssid *ssid)
2067{
2068 return wpa_config_write_wep_key(ssid, 2);
2069}
2070
2071
2072static char * wpa_config_write_wep_key3(const struct parse_data *data,
2073 struct wpa_ssid *ssid)
2074{
2075 return wpa_config_write_wep_key(ssid, 3);
2076}
2077#endif /* NO_CONFIG_WRITE */
2078
Hai Shalomfdcde762020-04-02 11:19:20 -07002079#endif /* CONFIG_WEP */
2080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082#ifdef CONFIG_P2P
2083
Dmitry Shmidt54605472013-11-08 11:10:19 -08002084static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2085 struct wpa_ssid *ssid, int line,
2086 const char *value)
2087{
2088 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2089 os_strcmp(value, "any") == 0) {
2090 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2091 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2092 return 0;
2093 }
2094 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2095 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2096 line, value);
2097 return -1;
2098 }
2099 ssid->bssid_set = 1;
2100 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2101 MAC2STR(ssid->go_p2p_dev_addr));
2102 return 0;
2103}
2104
2105
2106#ifndef NO_CONFIG_WRITE
2107static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2108 struct wpa_ssid *ssid)
2109{
2110 char *value;
2111 int res;
2112
2113 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2114 return NULL;
2115
2116 value = os_malloc(20);
2117 if (value == NULL)
2118 return NULL;
2119 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002120 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002121 os_free(value);
2122 return NULL;
2123 }
2124 value[20 - 1] = '\0';
2125 return value;
2126}
2127#endif /* NO_CONFIG_WRITE */
2128
2129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2131 struct wpa_ssid *ssid, int line,
2132 const char *value)
2133{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002134 return wpa_config_parse_addr_list(data, line, value,
2135 &ssid->p2p_client_list,
2136 &ssid->num_p2p_clients,
2137 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002138}
2139
2140
2141#ifndef NO_CONFIG_WRITE
2142static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2143 struct wpa_ssid *ssid)
2144{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002145 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2146 ssid->num_p2p_clients,
2147 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002148}
2149#endif /* NO_CONFIG_WRITE */
2150
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002151
2152static int wpa_config_parse_psk_list(const struct parse_data *data,
2153 struct wpa_ssid *ssid, int line,
2154 const char *value)
2155{
2156 struct psk_list_entry *p;
2157 const char *pos;
2158
2159 p = os_zalloc(sizeof(*p));
2160 if (p == NULL)
2161 return -1;
2162
2163 pos = value;
2164 if (os_strncmp(pos, "P2P-", 4) == 0) {
2165 p->p2p = 1;
2166 pos += 4;
2167 }
2168
2169 if (hwaddr_aton(pos, p->addr)) {
2170 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2171 line, pos);
2172 os_free(p);
2173 return -1;
2174 }
2175 pos += 17;
2176 if (*pos != '-') {
2177 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2178 line, pos);
2179 os_free(p);
2180 return -1;
2181 }
2182 pos++;
2183
2184 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2185 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2186 line, pos);
2187 os_free(p);
2188 return -1;
2189 }
2190
2191 dl_list_add(&ssid->psk_list, &p->list);
2192
2193 return 0;
2194}
2195
2196
2197#ifndef NO_CONFIG_WRITE
2198static char * wpa_config_write_psk_list(const struct parse_data *data,
2199 struct wpa_ssid *ssid)
2200{
2201 return NULL;
2202}
2203#endif /* NO_CONFIG_WRITE */
2204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002205#endif /* CONFIG_P2P */
2206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002207
2208#ifdef CONFIG_MESH
2209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2211 struct wpa_ssid *ssid, int line,
2212 const char *value)
2213{
2214 int *rates = wpa_config_parse_int_array(value);
2215
2216 if (rates == NULL) {
2217 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2218 line, value);
2219 return -1;
2220 }
2221 if (rates[0] == 0) {
2222 os_free(rates);
2223 rates = NULL;
2224 }
2225
2226 os_free(ssid->mesh_basic_rates);
2227 ssid->mesh_basic_rates = rates;
2228
2229 return 0;
2230}
2231
2232
2233#ifndef NO_CONFIG_WRITE
2234
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002235static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2236 struct wpa_ssid *ssid)
2237{
2238 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2239}
2240
2241#endif /* NO_CONFIG_WRITE */
2242
2243#endif /* CONFIG_MESH */
2244
2245
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002246#ifdef CONFIG_MACSEC
2247
2248static int wpa_config_parse_mka_cak(const struct parse_data *data,
2249 struct wpa_ssid *ssid, int line,
2250 const char *value)
2251{
Hai Shalom74f70d42019-02-11 14:42:39 -08002252 size_t len;
2253
2254 len = os_strlen(value);
2255 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2256 (len != 2 * 16 && len != 2 * 32) ||
2257 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002258 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2259 line, value);
2260 return -1;
2261 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002262 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002263 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2264
Hai Shalom74f70d42019-02-11 14:42:39 -08002265 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2266 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002267 return 0;
2268}
2269
2270
2271static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2272 struct wpa_ssid *ssid, int line,
2273 const char *value)
2274{
Hai Shalom74f70d42019-02-11 14:42:39 -08002275 size_t len;
2276
2277 len = os_strlen(value);
2278 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2279 len < 2 || /* too short */
2280 len % 2 != 0 /* not an integral number of bytes */) {
2281 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2282 line, value);
2283 return -1;
2284 }
2285 ssid->mka_ckn_len = len / 2;
2286 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002287 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2288 line, value);
2289 return -1;
2290 }
2291
2292 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2293
Hai Shalom74f70d42019-02-11 14:42:39 -08002294 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2295 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002296 return 0;
2297}
2298
2299
2300#ifndef NO_CONFIG_WRITE
2301
2302static char * wpa_config_write_mka_cak(const struct parse_data *data,
2303 struct wpa_ssid *ssid)
2304{
2305 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2306 return NULL;
2307
Hai Shalom74f70d42019-02-11 14:42:39 -08002308 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002309}
2310
2311
2312static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2313 struct wpa_ssid *ssid)
2314{
2315 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2316 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002317 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002318}
2319
2320#endif /* NO_CONFIG_WRITE */
2321
2322#endif /* CONFIG_MACSEC */
2323
2324
Hai Shalom74f70d42019-02-11 14:42:39 -08002325#ifdef CONFIG_OCV
2326
2327static int wpa_config_parse_ocv(const struct parse_data *data,
2328 struct wpa_ssid *ssid, int line,
2329 const char *value)
2330{
2331 char *end;
2332
2333 ssid->ocv = strtol(value, &end, 0);
2334 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2335 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2336 line, value);
2337 return -1;
2338 }
2339 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2340 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2341 return 0;
2342}
2343
2344
2345#ifndef NO_CONFIG_WRITE
2346static char * wpa_config_write_ocv(const struct parse_data *data,
2347 struct wpa_ssid *ssid)
2348{
2349 char *value = os_malloc(20);
2350
2351 if (!value)
2352 return NULL;
2353 os_snprintf(value, 20, "%d", ssid->ocv);
2354 value[20 - 1] = '\0';
2355 return value;
2356}
2357#endif /* NO_CONFIG_WRITE */
2358
2359#endif /* CONFIG_OCV */
2360
2361
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002362static int wpa_config_parse_peerkey(const struct parse_data *data,
2363 struct wpa_ssid *ssid, int line,
2364 const char *value)
2365{
2366 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2367 return 0;
2368}
2369
2370
2371#ifndef NO_CONFIG_WRITE
2372static char * wpa_config_write_peerkey(const struct parse_data *data,
2373 struct wpa_ssid *ssid)
2374{
2375 return NULL;
2376}
2377#endif /* NO_CONFIG_WRITE */
2378
2379
Sunil Ravi77d572f2023-01-17 23:58:31 +00002380static int wpa_config_parse_mac_value(const struct parse_data *data,
2381 struct wpa_ssid *ssid, int line,
2382 const char *value)
2383{
2384 u8 mac_value[ETH_ALEN];
2385
2386 if (hwaddr_aton(value, mac_value) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002387 if (ether_addr_equal(mac_value, ssid->mac_value))
Sunil Ravi77d572f2023-01-17 23:58:31 +00002388 return 1;
2389 os_memcpy(ssid->mac_value, mac_value, ETH_ALEN);
2390 return 0;
2391 }
2392
2393 wpa_printf(MSG_ERROR, "Line %d: Invalid MAC address '%s'",
2394 line, value);
2395 return -1;
2396}
2397
2398
2399#ifndef NO_CONFIG_WRITE
2400static char * wpa_config_write_mac_value(const struct parse_data *data,
2401 struct wpa_ssid *ssid)
2402{
2403 const size_t size = 3 * ETH_ALEN;
2404 char *value;
2405 int res;
2406
2407 if (ssid->mac_addr != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS)
2408 return NULL;
2409
2410 value = os_malloc(size);
2411 if (!value)
2412 return NULL;
2413 res = os_snprintf(value, size, MACSTR, MAC2STR(ssid->mac_value));
2414 if (os_snprintf_error(size, res)) {
2415 os_free(value);
2416 return NULL;
2417 }
2418 value[size - 1] = '\0';
2419 return value;
2420}
2421#endif /* NO_CONFIG_WRITE */
2422
2423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424/* Helper macros for network block parser */
2425
2426#ifdef OFFSET
2427#undef OFFSET
2428#endif /* OFFSET */
2429/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2430#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2431
2432/* STR: Define a string variable for an ASCII string; f = field name */
2433#ifdef NO_CONFIG_WRITE
2434#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002435#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436#else /* NO_CONFIG_WRITE */
2437#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002438#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2439 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440#endif /* NO_CONFIG_WRITE */
2441#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002442#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002444#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445
2446/* STR_LEN: Define a string variable with a separate variable for storing the
2447 * data length. Unlike STR(), this can be used to store arbitrary binary data
2448 * (i.e., even nul termination character). */
2449#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002450#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002452#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2454
2455/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2456 * explicitly specified. */
2457#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2458#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2459#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2460
2461#ifdef NO_CONFIG_WRITE
2462#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002463#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464#else /* NO_CONFIG_WRITE */
2465#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2466 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002467#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2468 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469#endif /* NO_CONFIG_WRITE */
2470
2471/* INT: Define an integer variable */
2472#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002473#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474
2475/* INT_RANGE: Define an integer variable with allowed value range */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002476#ifdef NO_CONFIG_WRITE
2477#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, OFFSET(f), \
2478 (void *) 0, (void *) (min), (void *) (max), 0
2479#else /* NO_CONFIG_WRITE */
2480#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, \
2481 wpa_config_write_int, OFFSET(f), \
2482 (void *) 0, (void *) (min), (void *) (max), 0
2483#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
2485/* FUNC: Define a configuration variable that uses a custom function for
2486 * parsing and writing the value. */
2487#ifdef NO_CONFIG_WRITE
2488#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2489#else /* NO_CONFIG_WRITE */
2490#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2491 NULL, NULL, NULL, NULL
2492#endif /* NO_CONFIG_WRITE */
2493#define FUNC(f) _FUNC(f), 0
2494#define FUNC_KEY(f) _FUNC(f), 1
2495
2496/*
2497 * Table of network configuration variables. This table is used to parse each
2498 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2499 * that is inside a network block.
2500 *
2501 * This table is generated using the helper macros defined above and with
2502 * generous help from the C pre-processor. The field name is stored as a string
2503 * into .name and for STR and INT types, the offset of the target buffer within
2504 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2505 * offset to the field containing the length of the configuration variable.
2506 * .param3 and .param4 can be used to mark the allowed range (length for STR
2507 * and value for INT).
2508 *
2509 * For each configuration line in wpa_supplicant.conf, the parser goes through
2510 * this table and select the entry that matches with the field name. The parser
2511 * function (.parser) is then called to parse the actual value of the field.
2512 *
2513 * This kind of mechanism makes it easy to add new configuration parameters,
2514 * since only one line needs to be added into this table and into the
2515 * struct wpa_ssid definition if the new variable is either a string or
2516 * integer. More complex types will need to use their own parser and writer
2517 * functions.
2518 */
2519static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002520 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 { INT_RANGE(scan_ssid, 0, 1) },
2522 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002523 { FUNC(bssid_hint) },
Hai Shalom60840252021-02-19 19:02:11 -08002524 { FUNC(bssid_ignore) },
2525 { FUNC(bssid_accept) },
2526 { FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2527 { FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002529 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002530 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002531 { STR(sae_password_id) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 { FUNC(proto) },
2533 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002534 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 { FUNC(pairwise) },
2536 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002537 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 { FUNC(auth_alg) },
2539 { FUNC(scan_freq) },
2540 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002541 { INT_RANGE(ht, 0, 1) },
2542 { INT_RANGE(vht, 0, 1) },
Sunil Ravi640215c2023-06-28 23:08:09 +00002543 { INT_RANGE(he, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002544 { INT_RANGE(ht40, -1, 1) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002545 { INT_RANGE(max_oper_chwidth, CONF_OPER_CHWIDTH_USE_HT,
2546 CONF_OPER_CHWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002547 { INT(vht_center_freq1) },
2548 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549#ifdef IEEE8021X_EAPOL
2550 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002551 { STR_LENe(identity, identity) },
2552 { STR_LENe(anonymous_identity, anonymous_identity) },
2553 { STR_LENe(imsi_identity, imsi_identity) },
2554 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002556 { FUNC_KEY(machine_password) },
2557 { STRe(ca_cert, cert.ca_cert) },
2558 { STRe(ca_path, cert.ca_path) },
2559 { STRe(client_cert, cert.client_cert) },
2560 { STRe(private_key, cert.private_key) },
2561 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002562 { STRe(subject_match, cert.subject_match) },
2563 { STRe(check_cert_subject, cert.check_cert_subject) },
2564 { STRe(altsubject_match, cert.altsubject_match) },
2565 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2566 { STRe(domain_match, cert.domain_match) },
2567 { STRe(ca_cert2, phase2_cert.ca_cert) },
2568 { STRe(ca_path2, phase2_cert.ca_path) },
2569 { STRe(client_cert2, phase2_cert.client_cert) },
2570 { STRe(private_key2, phase2_cert.private_key) },
2571 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002572 { STRe(subject_match2, phase2_cert.subject_match) },
2573 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2574 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2575 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2576 { STRe(domain_match2, phase2_cert.domain_match) },
2577 { STRe(phase1, phase1) },
2578 { STRe(phase2, phase2) },
2579 { STRe(machine_phase2, machine_phase2) },
2580 { STRe(pcsc, pcsc) },
2581 { STR_KEYe(pin, cert.pin) },
2582 { STRe(engine_id, cert.engine_id) },
2583 { STRe(key_id, cert.key_id) },
2584 { STRe(cert_id, cert.cert_id) },
2585 { STRe(ca_cert_id, cert.ca_cert_id) },
2586 { STR_KEYe(pin2, phase2_cert.pin) },
2587 { STRe(engine_id2, phase2_cert.engine_id) },
2588 { STRe(key_id2, phase2_cert.key_id) },
2589 { STRe(cert_id2, phase2_cert.cert_id) },
2590 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2591 { INTe(engine, cert.engine) },
2592 { INTe(engine2, phase2_cert.engine) },
2593 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2594 { STRe(machine_ca_path, machine_cert.ca_path) },
2595 { STRe(machine_client_cert, machine_cert.client_cert) },
2596 { STRe(machine_private_key, machine_cert.private_key) },
2597 { STR_KEYe(machine_private_key_passwd,
2598 machine_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002599 { STRe(machine_subject_match, machine_cert.subject_match) },
2600 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2601 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2602 { STRe(machine_domain_suffix_match,
2603 machine_cert.domain_suffix_match) },
2604 { STRe(machine_domain_match, machine_cert.domain_match) },
2605 { STR_KEYe(machine_pin, machine_cert.pin) },
2606 { STRe(machine_engine_id, machine_cert.engine_id) },
2607 { STRe(machine_key_id, machine_cert.key_id) },
2608 { STRe(machine_cert_id, machine_cert.cert_id) },
2609 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2610 { INTe(machine_engine, machine_cert.engine) },
2611 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002613 { INTe(sim_num, sim_num) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002614 { STRe(imsi_privacy_cert, imsi_privacy_cert) },
2615 { STRe(imsi_privacy_attr, imsi_privacy_attr) },
Steven Liu9138d432022-11-23 22:29:05 +00002616 { INTe(strict_conservative_peer_mode, strict_conservative_peer_mode) },
Hai Shalomc3565922019-10-28 11:58:20 -07002617 { STRe(openssl_ciphers, openssl_ciphers) },
2618 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -07002620#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 { FUNC_KEY(wep_key0) },
2622 { FUNC_KEY(wep_key1) },
2623 { FUNC_KEY(wep_key2) },
2624 { FUNC_KEY(wep_key3) },
2625 { INT(wep_tx_keyidx) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002626#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 { INT(priority) },
2628#ifdef IEEE8021X_EAPOL
2629 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002630 { STRe(pac_file, pac_file) },
2631 { INTe(fragment_size, fragment_size) },
2632 { INTe(ocsp, cert.ocsp) },
2633 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002635#ifdef CONFIG_MESH
2636 { INT_RANGE(mode, 0, 5) },
2637 { INT_RANGE(no_auto_peer, 0, 1) },
Hai Shaloma20dcd72022-02-04 13:43:00 -08002638 { INT_RANGE(mesh_fwding, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002639 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002640#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002642#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 { INT_RANGE(proactive_key_caching, 0, 1) },
2644 { INT_RANGE(disabled, 0, 2) },
2645 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002647#ifdef CONFIG_OCV
2648 { FUNC(ocv) },
2649#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002650 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002652 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002653 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002654 { INT_RANGE(enable_edmg, 0, 1) },
2655 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002656#ifdef CONFIG_ACS
2657 { INT_RANGE(acs, 0, 1) },
2658#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002659#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 { FUNC(mesh_basic_rates) },
2661 { INT(dot11MeshMaxRetries) },
2662 { INT(dot11MeshRetryTimeout) },
2663 { INT(dot11MeshConfirmTimeout) },
2664 { INT(dot11MeshHoldingTimeout) },
2665#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 { INT(wpa_ptk_rekey) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002667 { INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002668 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002670 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002672 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002673 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002674 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002675#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002676#ifdef CONFIG_HT_OVERRIDES
2677 { INT_RANGE(disable_ht, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002678 { INT_RANGE(disable_ht40, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002679 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002680 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002681 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002682 { INT_RANGE(tx_stbc, -1, 1) },
2683 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002684 { INT_RANGE(disable_max_amsdu, -1, 1) },
2685 { INT_RANGE(ampdu_factor, -1, 3) },
2686 { INT_RANGE(ampdu_density, -1, 7) },
2687 { STR(ht_mcs) },
2688#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002689#ifdef CONFIG_VHT_OVERRIDES
2690 { INT_RANGE(disable_vht, 0, 1) },
2691 { INT(vht_capa) },
2692 { INT(vht_capa_mask) },
2693 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2694 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2695 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2696 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2697 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2698 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2699 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2700 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2701 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2702 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2703 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2704 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2705 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2706 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2707 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2708 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2709#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07002710#ifdef CONFIG_HE_OVERRIDES
2711 { INT_RANGE(disable_he, 0, 1)},
2712#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002713 { INT(ap_max_inactivity) },
2714 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002715 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002716#ifdef CONFIG_MACSEC
2717 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002718 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002719 { INT_RANGE(macsec_replay_protect, 0, 1) },
2720 { INT(macsec_replay_window) },
Sunil Ravi036cec52023-03-29 11:35:17 -07002721 { INT_RANGE(macsec_offload, 0, 2) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002722 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002723 { INT_RANGE(mka_priority, 0, 255) },
Sunil Ravia04bd252022-05-02 22:54:18 -07002724 { INT_RANGE(macsec_csindex, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002725 { FUNC_KEY(mka_cak) },
2726 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002727#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002728#ifdef CONFIG_HS20
2729 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002730 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002731#endif /* CONFIG_HS20 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002732 { INT_RANGE(mac_addr, 0, 3) },
2733 { FUNC_KEY(mac_value) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002734 { INT_RANGE(pbss, 0, 2) },
2735 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002736 { INT_RANGE(fils_dh_group, 0, 65535) },
2737#ifdef CONFIG_DPP
2738 { STR(dpp_connector) },
2739 { STR_LEN(dpp_netaccesskey) },
2740 { INT(dpp_netaccesskey_expiry) },
2741 { STR_LEN(dpp_csign) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002742 { STR_LEN(dpp_pp_key) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002743 { INT_RANGE(dpp_pfs, 0, 2) },
Sunil Ravi89eba102022-09-13 21:04:37 -07002744 { INT_RANGE(dpp_connector_privacy, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002745#endif /* CONFIG_DPP */
2746 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002747 { INT_RANGE(owe_only, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002748 { INT_RANGE(owe_ptk_workaround, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002749 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002750 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002751 { INT_RANGE(multi_ap_profile, MULTI_AP_PROFILE_1,
2752 MULTI_AP_PROFILE_MAX) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002753 { INT_RANGE(beacon_prot, 0, 1) },
2754 { INT_RANGE(transition_disable, 0, 255) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002755 { INT_RANGE(sae_pk, 0, 2) },
Sunil Ravi77d572f2023-01-17 23:58:31 +00002756 { INT_RANGE(disable_eht, 0, 1)},
Sunil Ravi036cec52023-03-29 11:35:17 -07002757 { INT_RANGE(enable_4addr_mode, 0, 1)},
Sunil Ravi7f769292024-07-23 22:21:32 +00002758 { INT_RANGE(max_idle, 0, 65535)},
2759 { INT_RANGE(ssid_protection, 0, 1)},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760};
2761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762#undef OFFSET
2763#undef _STR
2764#undef STR
2765#undef STR_KEY
2766#undef _STR_LEN
2767#undef STR_LEN
2768#undef STR_LEN_KEY
2769#undef _STR_RANGE
2770#undef STR_RANGE
2771#undef STR_RANGE_KEY
2772#undef _INT
2773#undef INT
2774#undef INT_RANGE
2775#undef _FUNC
2776#undef FUNC
2777#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002778#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779
2780
2781/**
2782 * wpa_config_add_prio_network - Add a network to priority lists
2783 * @config: Configuration data from wpa_config_read()
2784 * @ssid: Pointer to the network configuration to be added to the list
2785 * Returns: 0 on success, -1 on failure
2786 *
2787 * This function is used to add a network block to the priority list of
2788 * networks. This must be called for each network when reading in the full
2789 * configuration. In addition, this can be used indirectly when updating
2790 * priorities by calling wpa_config_update_prio_list().
2791 */
2792int wpa_config_add_prio_network(struct wpa_config *config,
2793 struct wpa_ssid *ssid)
2794{
Hai Shalomfdcde762020-04-02 11:19:20 -07002795 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 struct wpa_ssid *prev, **nlist;
2797
2798 /*
2799 * Add to an existing priority list if one is available for the
2800 * configured priority level for this network.
2801 */
2802 for (prio = 0; prio < config->num_prio; prio++) {
2803 prev = config->pssid[prio];
2804 if (prev->priority == ssid->priority) {
2805 while (prev->pnext)
2806 prev = prev->pnext;
2807 prev->pnext = ssid;
2808 return 0;
2809 }
2810 }
2811
2812 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2814 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815 if (nlist == NULL)
2816 return -1;
2817
2818 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 if (nlist[prio]->priority < ssid->priority) {
2820 os_memmove(&nlist[prio + 1], &nlist[prio],
2821 (config->num_prio - prio) *
2822 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 }
2826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 nlist[prio] = ssid;
2828 config->num_prio++;
2829 config->pssid = nlist;
2830
2831 return 0;
2832}
2833
2834
2835/**
2836 * wpa_config_update_prio_list - Update network priority list
2837 * @config: Configuration data from wpa_config_read()
2838 * Returns: 0 on success, -1 on failure
2839 *
2840 * This function is called to update the priority list of networks in the
2841 * configuration when a network is being added or removed. This is also called
2842 * if a priority for a network is changed.
2843 */
2844int wpa_config_update_prio_list(struct wpa_config *config)
2845{
2846 struct wpa_ssid *ssid;
2847 int ret = 0;
2848
2849 os_free(config->pssid);
2850 config->pssid = NULL;
2851 config->num_prio = 0;
2852
2853 ssid = config->ssid;
2854 while (ssid) {
2855 ssid->pnext = NULL;
2856 if (wpa_config_add_prio_network(config, ssid) < 0)
2857 ret = -1;
2858 ssid = ssid->next;
2859 }
2860
2861 return ret;
2862}
2863
2864
2865#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002866
2867static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2868{
2869 os_free(cert->ca_cert);
2870 os_free(cert->ca_path);
2871 os_free(cert->client_cert);
2872 os_free(cert->private_key);
2873 str_clear_free(cert->private_key_passwd);
Hai Shalomc3565922019-10-28 11:58:20 -07002874 os_free(cert->subject_match);
2875 os_free(cert->check_cert_subject);
2876 os_free(cert->altsubject_match);
2877 os_free(cert->domain_suffix_match);
2878 os_free(cert->domain_match);
2879 str_clear_free(cert->pin);
2880 os_free(cert->engine_id);
2881 os_free(cert->key_id);
2882 os_free(cert->cert_id);
2883 os_free(cert->ca_cert_id);
2884}
2885
2886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887static void eap_peer_config_free(struct eap_peer_config *eap)
2888{
2889 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002890 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002892 os_free(eap->imsi_identity);
Sunil8cd6f4d2022-06-28 18:40:46 +00002893 os_free(eap->imsi_privacy_cert);
2894 os_free(eap->imsi_privacy_attr);
Hai Shalomc3565922019-10-28 11:58:20 -07002895 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002896 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002897 bin_clear_free(eap->machine_password, eap->machine_password_len);
2898 eap_peer_config_free_cert(&eap->cert);
2899 eap_peer_config_free_cert(&eap->phase2_cert);
2900 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 os_free(eap->phase1);
2902 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002903 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 os_free(eap->otp);
2906 os_free(eap->pending_req_otp);
2907 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002908 bin_clear_free(eap->new_password, eap->new_password_len);
2909 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002910 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911}
Hai Shalomc3565922019-10-28 11:58:20 -07002912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913#endif /* IEEE8021X_EAPOL */
2914
2915
2916/**
2917 * wpa_config_free_ssid - Free network/ssid configuration data
2918 * @ssid: Configuration data for the network
2919 *
2920 * This function frees all resources allocated for the network configuration
2921 * data.
2922 */
2923void wpa_config_free_ssid(struct wpa_ssid *ssid)
2924{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002925 struct psk_list_entry *psk;
2926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002928 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002929 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002930 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002931 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932#ifdef IEEE8021X_EAPOL
2933 eap_peer_config_free(&ssid->eap);
2934#endif /* IEEE8021X_EAPOL */
2935 os_free(ssid->id_str);
2936 os_free(ssid->scan_freq);
2937 os_free(ssid->freq_list);
2938 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 os_free(ssid->p2p_client_list);
Hai Shalom60840252021-02-19 19:02:11 -08002940 os_free(ssid->bssid_ignore);
2941 os_free(ssid->bssid_accept);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002942#ifdef CONFIG_HT_OVERRIDES
2943 os_free(ssid->ht_mcs);
2944#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002945#ifdef CONFIG_MESH
2946 os_free(ssid->mesh_basic_rates);
2947#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002948#ifdef CONFIG_HS20
2949 os_free(ssid->roaming_consortium_selection);
2950#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002951 os_free(ssid->dpp_connector);
2952 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2953 os_free(ssid->dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -07002954 os_free(ssid->dpp_pp_key);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002955 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2956 list))) {
2957 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002958 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002959 }
Hai Shalomc3565922019-10-28 11:58:20 -07002960#ifdef CONFIG_SAE
2961 sae_deinit_pt(ssid->pt);
2962#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002963 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964}
2965
2966
Dmitry Shmidt04949592012-07-19 12:16:46 -07002967void wpa_config_free_cred(struct wpa_cred *cred)
2968{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002969 size_t i;
2970
Dmitry Shmidt04949592012-07-19 12:16:46 -07002971 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002972 str_clear_free(cred->username);
2973 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974 os_free(cred->ca_cert);
2975 os_free(cred->client_cert);
2976 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002977 str_clear_free(cred->private_key_passwd);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002978 os_free(cred->engine_id);
2979 os_free(cred->ca_cert_id);
2980 os_free(cred->cert_id);
2981 os_free(cred->key_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002982 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002983 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002984 for (i = 0; i < cred->num_domain; i++)
2985 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002986 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002987 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002988 os_free(cred->eap_method);
2989 os_free(cred->phase1);
2990 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002991 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002992 os_free(cred->roaming_partner);
2993 os_free(cred->provisioning_sp);
2994 for (i = 0; i < cred->num_req_conn_capab; i++)
2995 os_free(cred->req_conn_capab_port[i]);
2996 os_free(cred->req_conn_capab_port);
2997 os_free(cred->req_conn_capab_proto);
Sunil8cd6f4d2022-06-28 18:40:46 +00002998 os_free(cred->imsi_privacy_cert);
2999 os_free(cred->imsi_privacy_attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003000 os_free(cred);
3001}
3002
3003
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003004void wpa_config_flush_blobs(struct wpa_config *config)
3005{
3006#ifndef CONFIG_NO_CONFIG_BLOBS
3007 struct wpa_config_blob *blob, *prev;
3008
3009 blob = config->blobs;
3010 config->blobs = NULL;
3011 while (blob) {
3012 prev = blob;
3013 blob = blob->next;
3014 wpa_config_free_blob(prev);
3015 }
3016#endif /* CONFIG_NO_CONFIG_BLOBS */
3017}
3018
3019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020/**
3021 * wpa_config_free - Free configuration data
3022 * @config: Configuration data from wpa_config_read()
3023 *
3024 * This function frees all resources allocated for the configuration data by
3025 * wpa_config_read().
3026 */
3027void wpa_config_free(struct wpa_config *config)
3028{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003030 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003031 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032
3033 ssid = config->ssid;
3034 while (ssid) {
3035 prev = ssid;
3036 ssid = ssid->next;
3037 wpa_config_free_ssid(prev);
3038 }
3039
Dmitry Shmidt04949592012-07-19 12:16:46 -07003040 cred = config->cred;
3041 while (cred) {
3042 cprev = cred;
3043 cred = cred->next;
3044 wpa_config_free_cred(cprev);
3045 }
3046
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003047 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003050 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
3051 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 os_free(config->ctrl_interface);
3053 os_free(config->ctrl_interface_group);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003054#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 os_free(config->opensc_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003056#endif /* CONFIG_OPENSC_ENGINE_PATH */
3057#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 os_free(config->pkcs11_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003059#endif /* CONFIG_PKCS11_ENGINE_PATH */
3060#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 os_free(config->pkcs11_module_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003062#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003063 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003064 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003065 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 os_free(config->driver_param);
3067 os_free(config->device_name);
3068 os_free(config->manufacturer);
3069 os_free(config->model_name);
3070 os_free(config->model_number);
3071 os_free(config->serial_number);
3072 os_free(config->config_methods);
3073 os_free(config->p2p_ssid_postfix);
3074 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003075 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003076 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003077 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003078 os_free(config->freq_list);
Hai Shalom60840252021-02-19 19:02:11 -08003079 os_free(config->initial_freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080 wpabuf_free(config->wps_nfc_dh_pubkey);
3081 wpabuf_free(config->wps_nfc_dh_privkey);
3082 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003083 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003084 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003085 wpabuf_free(config->ap_vendor_elements);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003086 wpabuf_free(config->ap_assocresp_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003087 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003088 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003089 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003090 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003091 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003092#ifdef CONFIG_MBO
3093 os_free(config->non_pref_chan);
3094#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07003095 os_free(config->dpp_name);
3096 os_free(config->dpp_mud_url);
Sunil Ravi89eba102022-09-13 21:04:37 -07003097 os_free(config->dpp_extra_conf_req_name);
3098 os_free(config->dpp_extra_conf_req_value);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 os_free(config);
3101}
3102
3103
3104/**
3105 * wpa_config_foreach_network - Iterate over each configured network
3106 * @config: Configuration data from wpa_config_read()
3107 * @func: Callback function to process each network
3108 * @arg: Opaque argument to pass to callback function
3109 *
3110 * Iterate over the set of configured networks calling the specified
3111 * function for each item. We guard against callbacks removing the
3112 * supplied network.
3113 */
3114void wpa_config_foreach_network(struct wpa_config *config,
3115 void (*func)(void *, struct wpa_ssid *),
3116 void *arg)
3117{
3118 struct wpa_ssid *ssid, *next;
3119
3120 ssid = config->ssid;
3121 while (ssid) {
3122 next = ssid->next;
3123 func(arg, ssid);
3124 ssid = next;
3125 }
3126}
3127
3128
3129/**
3130 * wpa_config_get_network - Get configured network based on id
3131 * @config: Configuration data from wpa_config_read()
3132 * @id: Unique network id to search for
3133 * Returns: Network configuration or %NULL if not found
3134 */
3135struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3136{
3137 struct wpa_ssid *ssid;
3138
3139 ssid = config->ssid;
3140 while (ssid) {
3141 if (id == ssid->id)
3142 break;
3143 ssid = ssid->next;
3144 }
3145
3146 return ssid;
3147}
3148
3149
3150/**
3151 * wpa_config_add_network - Add a new network with empty configuration
3152 * @config: Configuration data from wpa_config_read()
3153 * Returns: The new network configuration or %NULL if operation failed
3154 */
3155struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3156{
3157 int id;
3158 struct wpa_ssid *ssid, *last = NULL;
3159
3160 id = -1;
3161 ssid = config->ssid;
3162 while (ssid) {
3163 if (ssid->id > id)
3164 id = ssid->id;
3165 last = ssid;
3166 ssid = ssid->next;
3167 }
3168 id++;
3169
3170 ssid = os_zalloc(sizeof(*ssid));
3171 if (ssid == NULL)
3172 return NULL;
3173 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003174 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 if (last)
3176 last->next = ssid;
3177 else
3178 config->ssid = ssid;
3179
3180 wpa_config_update_prio_list(config);
3181
3182 return ssid;
3183}
3184
3185
3186/**
3187 * wpa_config_remove_network - Remove a configured network based on id
3188 * @config: Configuration data from wpa_config_read()
3189 * @id: Unique network id to search for
3190 * Returns: 0 on success, or -1 if the network was not found
3191 */
3192int wpa_config_remove_network(struct wpa_config *config, int id)
3193{
3194 struct wpa_ssid *ssid, *prev = NULL;
3195
3196 ssid = config->ssid;
3197 while (ssid) {
3198 if (id == ssid->id)
3199 break;
3200 prev = ssid;
3201 ssid = ssid->next;
3202 }
3203
3204 if (ssid == NULL)
3205 return -1;
3206
3207 if (prev)
3208 prev->next = ssid->next;
3209 else
3210 config->ssid = ssid->next;
3211
3212 wpa_config_update_prio_list(config);
3213 wpa_config_free_ssid(ssid);
3214 return 0;
3215}
3216
3217
3218/**
3219 * wpa_config_set_network_defaults - Set network default values
3220 * @ssid: Pointer to network configuration data
3221 */
3222void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3223{
3224 ssid->proto = DEFAULT_PROTO;
3225 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3226 ssid->group_cipher = DEFAULT_GROUP;
3227 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Hai Shalomfdcde762020-04-02 11:19:20 -07003228 ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003229 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003230 ssid->ht = 1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003231 ssid->vht = 1;
3232 ssid->he = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233#ifdef IEEE8021X_EAPOL
3234 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3235 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3236 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003237 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003239#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003240 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3241 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3242 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3243 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003244 ssid->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003245 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003246#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003247#ifdef CONFIG_HT_OVERRIDES
3248 ssid->disable_ht = DEFAULT_DISABLE_HT;
3249 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003250 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003251 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003252 ssid->tx_stbc = DEFAULT_TX_STBC;
3253 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003254 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3255 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3256 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3257#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003258#ifdef CONFIG_VHT_OVERRIDES
3259 ssid->vht_rx_mcs_nss_1 = -1;
3260 ssid->vht_rx_mcs_nss_2 = -1;
3261 ssid->vht_rx_mcs_nss_3 = -1;
3262 ssid->vht_rx_mcs_nss_4 = -1;
3263 ssid->vht_rx_mcs_nss_5 = -1;
3264 ssid->vht_rx_mcs_nss_6 = -1;
3265 ssid->vht_rx_mcs_nss_7 = -1;
3266 ssid->vht_rx_mcs_nss_8 = -1;
3267 ssid->vht_tx_mcs_nss_1 = -1;
3268 ssid->vht_tx_mcs_nss_2 = -1;
3269 ssid->vht_tx_mcs_nss_3 = -1;
3270 ssid->vht_tx_mcs_nss_4 = -1;
3271 ssid->vht_tx_mcs_nss_5 = -1;
3272 ssid->vht_tx_mcs_nss_6 = -1;
3273 ssid->vht_tx_mcs_nss_7 = -1;
3274 ssid->vht_tx_mcs_nss_8 = -1;
3275#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003276 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003277 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003278 ssid->sae_pwe = DEFAULT_SAE_PWE;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003279#ifdef CONFIG_MACSEC
3280 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3281#endif /* CONFIG_MACSEC */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003282 ssid->mac_addr = WPAS_MAC_ADDR_STYLE_NOT_SET;
Hai Shalom74f70d42019-02-11 14:42:39 -08003283 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284}
3285
3286
Sunil Ravia04bd252022-05-02 22:54:18 -07003287static const char *removed_fields[] = {
3288 "dh_file",
3289 "dh_file2",
3290 "machine_dh_file",
3291 NULL
3292};
3293
3294static bool removed_field(const char *field)
3295{
3296 int i;
3297
3298 for (i = 0; removed_fields[i]; i++) {
3299 if (os_strcmp(field, removed_fields[i]) == 0)
3300 return true;
3301 }
3302
3303 return false;
3304}
3305
3306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307/**
3308 * wpa_config_set - Set a variable in network configuration
3309 * @ssid: Pointer to network configuration data
3310 * @var: Variable name, e.g., "ssid"
3311 * @value: Variable value
3312 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003313 * Returns: 0 on success with possible change in the value, 1 on success with
3314 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315 *
3316 * This function can be used to set network configuration variables based on
3317 * both the configuration file and management interface input. The value
3318 * parameter must be in the same format as the text-based configuration file is
3319 * using. For example, strings are using double quotation marks.
3320 */
3321int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3322 int line)
3323{
3324 size_t i;
3325 int ret = 0;
3326
3327 if (ssid == NULL || var == NULL || value == NULL)
3328 return -1;
3329
3330 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3331 const struct parse_data *field = &ssid_fields[i];
3332 if (os_strcmp(var, field->name) != 0)
3333 continue;
3334
Dmitry Shmidte4663042016-04-04 10:07:49 -07003335 ret = field->parser(field, ssid, line, value);
3336 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 if (line) {
3338 wpa_printf(MSG_ERROR, "Line %d: failed to "
3339 "parse %s '%s'.", line, var, value);
3340 }
3341 ret = -1;
3342 }
Hai Shalomc3565922019-10-28 11:58:20 -07003343#ifdef CONFIG_SAE
3344 if (os_strcmp(var, "ssid") == 0 ||
3345 os_strcmp(var, "psk") == 0 ||
3346 os_strcmp(var, "sae_password") == 0 ||
3347 os_strcmp(var, "sae_password_id") == 0) {
3348 sae_deinit_pt(ssid->pt);
3349 ssid->pt = NULL;
3350 }
3351#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003352 break;
3353 }
3354 if (i == NUM_SSID_FIELDS) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003355 if (removed_field(var)) {
3356 wpa_printf(MSG_INFO,
3357 "Line %d: Ignore removed configuration field '%s'",
3358 line, var);
3359 return ret;
3360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361 if (line) {
3362 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3363 "'%s'.", line, var);
3364 }
3365 ret = -1;
3366 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003367 ssid->was_recently_reconfigured = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003368
3369 return ret;
3370}
3371
3372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003373int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3374 const char *value)
3375{
3376 size_t len;
3377 char *buf;
3378 int ret;
3379
3380 len = os_strlen(value);
3381 buf = os_malloc(len + 3);
3382 if (buf == NULL)
3383 return -1;
3384 buf[0] = '"';
3385 os_memcpy(buf + 1, value, len);
3386 buf[len + 1] = '"';
3387 buf[len + 2] = '\0';
3388 ret = wpa_config_set(ssid, var, buf, 0);
3389 os_free(buf);
3390 return ret;
3391}
3392
3393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394/**
3395 * wpa_config_get_all - Get all options from network configuration
3396 * @ssid: Pointer to network configuration data
3397 * @get_keys: Determines if keys/passwords will be included in returned list
3398 * (if they may be exported)
3399 * Returns: %NULL terminated list of all set keys and their values in the form
3400 * of [key1, val1, key2, val2, ... , NULL]
3401 *
3402 * This function can be used to get list of all configured network properties.
3403 * The caller is responsible for freeing the returned list and all its
3404 * elements.
3405 */
3406char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3407{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003408#ifdef NO_CONFIG_WRITE
3409 return NULL;
3410#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 const struct parse_data *field;
3412 char *key, *value;
3413 size_t i;
3414 char **props;
3415 int fields_num;
3416
3417 get_keys = get_keys && ssid->export_keys;
3418
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003419 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 if (!props)
3421 return NULL;
3422
3423 fields_num = 0;
3424 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3425 field = &ssid_fields[i];
3426 if (field->key_data && !get_keys)
3427 continue;
3428 value = field->writer(field, ssid);
3429 if (value == NULL)
3430 continue;
3431 if (os_strlen(value) == 0) {
3432 os_free(value);
3433 continue;
3434 }
3435
3436 key = os_strdup(field->name);
3437 if (key == NULL) {
3438 os_free(value);
3439 goto err;
3440 }
3441
3442 props[fields_num * 2] = key;
3443 props[fields_num * 2 + 1] = value;
3444
3445 fields_num++;
3446 }
3447
3448 return props;
3449
3450err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003451 for (i = 0; props[i]; i++)
3452 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 os_free(props);
3454 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003455#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456}
3457
3458
3459#ifndef NO_CONFIG_WRITE
3460/**
3461 * wpa_config_get - Get a variable in network configuration
3462 * @ssid: Pointer to network configuration data
3463 * @var: Variable name, e.g., "ssid"
3464 * Returns: Value of the variable or %NULL on failure
3465 *
3466 * This function can be used to get network configuration variables. The
3467 * returned value is a copy of the configuration variable in text format, i.e,.
3468 * the same format that the text-based configuration file and wpa_config_set()
3469 * are using for the value. The caller is responsible for freeing the returned
3470 * value.
3471 */
3472char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3473{
3474 size_t i;
3475
3476 if (ssid == NULL || var == NULL)
3477 return NULL;
3478
3479 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3480 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003481 if (os_strcmp(var, field->name) == 0) {
3482 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003483
3484 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003485 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003486 "Found newline in value for %s; not returning it",
3487 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003488 os_free(ret);
3489 ret = NULL;
3490 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003491
Paul Stewart85c72c62016-03-03 15:33:47 -08003492 return ret;
3493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 }
3495
3496 return NULL;
3497}
3498
3499
3500/**
3501 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3502 * @ssid: Pointer to network configuration data
3503 * @var: Variable name, e.g., "ssid"
3504 * Returns: Value of the variable or %NULL on failure
3505 *
3506 * This function can be used to get network configuration variable like
3507 * wpa_config_get(). The only difference is that this functions does not expose
3508 * key/password material from the configuration. In case a key/password field
3509 * is requested, the returned value is an empty string or %NULL if the variable
3510 * is not set or "*" if the variable is set (regardless of its value). The
3511 * returned value is a copy of the configuration variable in text format, i.e,.
3512 * the same format that the text-based configuration file and wpa_config_set()
3513 * are using for the value. The caller is responsible for freeing the returned
3514 * value.
3515 */
3516char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3517{
3518 size_t i;
3519
3520 if (ssid == NULL || var == NULL)
3521 return NULL;
3522
3523 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3524 const struct parse_data *field = &ssid_fields[i];
3525 if (os_strcmp(var, field->name) == 0) {
3526 char *res = field->writer(field, ssid);
3527 if (field->key_data) {
3528 if (res && res[0]) {
3529 wpa_printf(MSG_DEBUG, "Do not allow "
3530 "key_data field to be "
3531 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003532 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 return os_strdup("*");
3534 }
3535
3536 os_free(res);
3537 return NULL;
3538 }
3539 return res;
3540 }
3541 }
3542
3543 return NULL;
3544}
3545#endif /* NO_CONFIG_WRITE */
3546
3547
3548/**
3549 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3550 * @ssid: Pointer to network configuration data
3551 *
3552 * This function must be called to update WPA PSK when either SSID or the
3553 * passphrase has changed for the network configuration.
3554 */
3555void wpa_config_update_psk(struct wpa_ssid *ssid)
3556{
3557#ifndef CONFIG_NO_PBKDF2
Sunil Ravia04bd252022-05-02 22:54:18 -07003558 if (pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3559 ssid->psk, PMK_LEN) != 0) {
3560 wpa_printf(MSG_ERROR, "Error in pbkdf2_sha1()");
3561 return;
3562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3564 ssid->psk, PMK_LEN);
3565 ssid->psk_set = 1;
3566#endif /* CONFIG_NO_PBKDF2 */
3567}
3568
3569
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003570static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3571 const char *value)
3572{
3573 u8 *proto;
3574 int **port;
3575 int *ports, *nports;
3576 const char *pos;
3577 unsigned int num_ports;
3578
3579 proto = os_realloc_array(cred->req_conn_capab_proto,
3580 cred->num_req_conn_capab + 1, sizeof(u8));
3581 if (proto == NULL)
3582 return -1;
3583 cred->req_conn_capab_proto = proto;
3584
3585 port = os_realloc_array(cred->req_conn_capab_port,
3586 cred->num_req_conn_capab + 1, sizeof(int *));
3587 if (port == NULL)
3588 return -1;
3589 cred->req_conn_capab_port = port;
3590
3591 proto[cred->num_req_conn_capab] = atoi(value);
3592
3593 pos = os_strchr(value, ':');
3594 if (pos == NULL) {
3595 port[cred->num_req_conn_capab] = NULL;
3596 cred->num_req_conn_capab++;
3597 return 0;
3598 }
3599 pos++;
3600
3601 ports = NULL;
3602 num_ports = 0;
3603
3604 while (*pos) {
3605 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3606 if (nports == NULL) {
3607 os_free(ports);
3608 return -1;
3609 }
3610 ports = nports;
3611 ports[num_ports++] = atoi(pos);
3612
3613 pos = os_strchr(pos, ',');
3614 if (pos == NULL)
3615 break;
3616 pos++;
3617 }
3618
3619 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3620 if (nports == NULL) {
3621 os_free(ports);
3622 return -1;
3623 }
3624 ports = nports;
3625 ports[num_ports] = -1;
3626
3627 port[cred->num_req_conn_capab] = ports;
3628 cred->num_req_conn_capab++;
3629 return 0;
3630}
3631
3632
Sunil Ravi77d572f2023-01-17 23:58:31 +00003633static int
3634wpa_config_set_cred_ois(u8 cred_ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN],
3635 size_t cred_ois_len[MAX_ROAMING_CONS],
3636 unsigned int *cred_num_ois,
3637 const char *value)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003638{
Sunil Ravi77d572f2023-01-17 23:58:31 +00003639 u8 ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3640 size_t ois_len[MAX_ROAMING_CONS];
3641 unsigned int num_ois = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003642 const char *pos, *end;
3643 size_t len;
3644
Sunil Ravi77d572f2023-01-17 23:58:31 +00003645 len = os_strlen(value);
3646 if (len / 2 < 3) {
3647 wpa_printf(MSG_ERROR,
3648 "Invalid organisation identifier (OI) list: %s",
3649 value);
3650 return -1;
3651 }
3652
3653 os_memset(ois, 0, sizeof(ois));
3654 os_memset(ois_len, 0, sizeof(ois_len));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003655
3656 for (pos = value;;) {
3657 end = os_strchr(pos, ',');
3658 len = end ? (size_t) (end - pos) : os_strlen(pos);
3659 if (!end && len == 0)
3660 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003661 if (len / 2 < 3 || (len & 1) != 0 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003662 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3663 hexstr2bin(pos,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003664 ois[num_ois],
Roshan Pius3a1667e2018-07-03 15:17:14 -07003665 len / 2) < 0) {
3666 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003667 "Invalid organisation identifier (OI) entry: %s",
Roshan Pius3a1667e2018-07-03 15:17:14 -07003668 pos);
3669 return -1;
3670 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003671 ois_len[num_ois] = len / 2;
3672 num_ois++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003673
3674 if (!end)
3675 break;
3676
Sunil Ravi77d572f2023-01-17 23:58:31 +00003677 if (num_ois >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003678 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003679 "Too many OIs");
Roshan Pius3a1667e2018-07-03 15:17:14 -07003680 return -1;
3681 }
3682
Roshan Pius3a1667e2018-07-03 15:17:14 -07003683 pos = end + 1;
3684 }
3685
Sunil Ravi77d572f2023-01-17 23:58:31 +00003686 os_memcpy(cred_ois, ois, sizeof(ois));
3687 os_memcpy(cred_ois_len, ois_len, sizeof(ois_len));
3688 *cred_num_ois = num_ois;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003689
3690 return 0;
3691}
3692
3693
Dmitry Shmidt04949592012-07-19 12:16:46 -07003694int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3695 const char *value, int line)
3696{
3697 char *val;
3698 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003699 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003700
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003701 if (os_strcmp(var, "temporary") == 0) {
3702 cred->temporary = atoi(value);
3703 return 0;
3704 }
3705
Dmitry Shmidt04949592012-07-19 12:16:46 -07003706 if (os_strcmp(var, "priority") == 0) {
3707 cred->priority = atoi(value);
3708 return 0;
3709 }
3710
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003711 if (os_strcmp(var, "sp_priority") == 0) {
3712 int prio = atoi(value);
3713 if (prio < 0 || prio > 255)
3714 return -1;
3715 cred->sp_priority = prio;
3716 return 0;
3717 }
3718
Dmitry Shmidt04949592012-07-19 12:16:46 -07003719 if (os_strcmp(var, "pcsc") == 0) {
3720 cred->pcsc = atoi(value);
3721 return 0;
3722 }
3723
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724 if (os_strcmp(var, "eap") == 0) {
3725 struct eap_method_type method;
3726 method.method = eap_peer_get_type(value, &method.vendor);
3727 if (method.vendor == EAP_VENDOR_IETF &&
3728 method.method == EAP_TYPE_NONE) {
3729 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3730 "for a credential", line, value);
3731 return -1;
3732 }
3733 os_free(cred->eap_method);
3734 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3735 if (cred->eap_method == NULL)
3736 return -1;
3737 os_memcpy(cred->eap_method, &method, sizeof(method));
3738 return 0;
3739 }
3740
3741 if (os_strcmp(var, "password") == 0 &&
3742 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003743 if (has_newline(value))
3744 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003745 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003746 cred->password = os_strdup(value);
3747 cred->ext_password = 1;
3748 return 0;
3749 }
3750
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003751 if (os_strcmp(var, "update_identifier") == 0) {
3752 cred->update_identifier = atoi(value);
3753 return 0;
3754 }
3755
3756 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3757 cred->min_dl_bandwidth_home = atoi(value);
3758 return 0;
3759 }
3760
3761 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3762 cred->min_ul_bandwidth_home = atoi(value);
3763 return 0;
3764 }
3765
3766 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3767 cred->min_dl_bandwidth_roaming = atoi(value);
3768 return 0;
3769 }
3770
3771 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3772 cred->min_ul_bandwidth_roaming = atoi(value);
3773 return 0;
3774 }
3775
3776 if (os_strcmp(var, "max_bss_load") == 0) {
3777 cred->max_bss_load = atoi(value);
3778 return 0;
3779 }
3780
3781 if (os_strcmp(var, "req_conn_capab") == 0)
3782 return wpa_config_set_cred_req_conn_capab(cred, value);
3783
3784 if (os_strcmp(var, "ocsp") == 0) {
3785 cred->ocsp = atoi(value);
3786 return 0;
3787 }
3788
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003789 if (os_strcmp(var, "sim_num") == 0) {
3790 cred->sim_num = atoi(value);
3791 return 0;
3792 }
3793
Hai Shaloma20dcd72022-02-04 13:43:00 -08003794 if (os_strcmp(var, "engine") == 0) {
3795 cred->engine = atoi(value);
3796 return 0;
3797 }
3798
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003800 if (val == NULL ||
3801 (os_strcmp(var, "excluded_ssid") != 0 &&
3802 os_strcmp(var, "roaming_consortium") != 0 &&
3803 os_strcmp(var, "required_roaming_consortium") != 0 &&
3804 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003805 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3806 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003807 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808 return -1;
3809 }
3810
3811 if (os_strcmp(var, "realm") == 0) {
3812 os_free(cred->realm);
3813 cred->realm = val;
3814 return 0;
3815 }
3816
3817 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003818 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003819 cred->username = val;
3820 return 0;
3821 }
3822
3823 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003824 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003825 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003826 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003827 return 0;
3828 }
3829
3830 if (os_strcmp(var, "ca_cert") == 0) {
3831 os_free(cred->ca_cert);
3832 cred->ca_cert = val;
3833 return 0;
3834 }
3835
3836 if (os_strcmp(var, "client_cert") == 0) {
3837 os_free(cred->client_cert);
3838 cred->client_cert = val;
3839 return 0;
3840 }
3841
3842 if (os_strcmp(var, "private_key") == 0) {
3843 os_free(cred->private_key);
3844 cred->private_key = val;
3845 return 0;
3846 }
3847
3848 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003849 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003850 cred->private_key_passwd = val;
3851 return 0;
3852 }
3853
Hai Shaloma20dcd72022-02-04 13:43:00 -08003854 if (os_strcmp(var, "engine_id") == 0) {
3855 os_free(cred->engine_id);
3856 cred->engine_id = val;
3857 return 0;
3858 }
3859
3860 if (os_strcmp(var, "ca_cert_id") == 0) {
3861 os_free(cred->ca_cert_id);
3862 cred->ca_cert_id = val;
3863 return 0;
3864 }
3865
3866 if (os_strcmp(var, "cert_id") == 0) {
3867 os_free(cred->cert_id);
3868 cred->cert_id = val;
3869 return 0;
3870 }
3871
3872 if (os_strcmp(var, "key_id") == 0) {
3873 os_free(cred->key_id);
3874 cred->key_id = val;
3875 return 0;
3876 }
3877
Dmitry Shmidt04949592012-07-19 12:16:46 -07003878 if (os_strcmp(var, "imsi") == 0) {
3879 os_free(cred->imsi);
3880 cred->imsi = val;
3881 return 0;
3882 }
3883
3884 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003885 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003886 cred->milenage = val;
3887 return 0;
3888 }
3889
Dmitry Shmidt051af732013-10-22 13:52:46 -07003890 if (os_strcmp(var, "domain_suffix_match") == 0) {
3891 os_free(cred->domain_suffix_match);
3892 cred->domain_suffix_match = val;
3893 return 0;
3894 }
3895
Dmitry Shmidt04949592012-07-19 12:16:46 -07003896 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003897 char **new_domain;
3898 new_domain = os_realloc_array(cred->domain,
3899 cred->num_domain + 1,
3900 sizeof(char *));
3901 if (new_domain == NULL) {
3902 os_free(val);
3903 return -1;
3904 }
3905 new_domain[cred->num_domain++] = val;
3906 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003907 return 0;
3908 }
3909
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003910 if (os_strcmp(var, "phase1") == 0) {
3911 os_free(cred->phase1);
3912 cred->phase1 = val;
3913 return 0;
3914 }
3915
3916 if (os_strcmp(var, "phase2") == 0) {
3917 os_free(cred->phase2);
3918 cred->phase2 = val;
3919 return 0;
3920 }
3921
3922 if (os_strcmp(var, "roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003923 if (len < 3 || len > sizeof(cred->home_ois[0])) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924 wpa_printf(MSG_ERROR, "Line %d: invalid "
3925 "roaming_consortium length %d (3..15 "
3926 "expected)", line, (int) len);
3927 os_free(val);
3928 return -1;
3929 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003930 wpa_printf(MSG_WARNING,
3931 "Line %d: option roaming_consortium is deprecated and will be removed in the future",
3932 line);
3933 os_memcpy(cred->home_ois[0], val, len);
3934 cred->home_ois_len[0] = len;
3935 cred->num_home_ois = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003936 os_free(val);
3937 return 0;
3938 }
3939
Dmitry Shmidt051af732013-10-22 13:52:46 -07003940 if (os_strcmp(var, "required_roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003941 if (len < 3 || len > sizeof(cred->required_home_ois[0])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003942 wpa_printf(MSG_ERROR, "Line %d: invalid "
3943 "required_roaming_consortium length %d "
3944 "(3..15 expected)", line, (int) len);
3945 os_free(val);
3946 return -1;
3947 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003948 wpa_printf(MSG_WARNING,
3949 "Line %d: option required_roaming_consortium is deprecated and will be removed in the future",
3950 line);
3951 os_memcpy(cred->required_home_ois[0], val, len);
3952 cred->required_home_ois_len[0] = len;
3953 cred->num_required_home_ois = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003954 os_free(val);
3955 return 0;
3956 }
3957
Sunil Ravi77d572f2023-01-17 23:58:31 +00003958 if (os_strcmp(var, "home_ois") == 0) {
3959 res = wpa_config_set_cred_ois(cred->home_ois,
3960 cred->home_ois_len,
3961 &cred->num_home_ois,
3962 val);
3963 if (res < 0)
3964 wpa_printf(MSG_ERROR, "Line %d: invalid home_ois",
3965 line);
3966 os_free(val);
3967 return res;
3968 }
3969
3970 if (os_strcmp(var, "required_home_ois") == 0) {
3971 res = wpa_config_set_cred_ois(cred->required_home_ois,
3972 cred->required_home_ois_len,
3973 &cred->num_required_home_ois,
3974 val);
3975 if (res < 0)
3976 wpa_printf(MSG_ERROR,
3977 "Line %d: invalid required_home_ois", line);
3978 os_free(val);
3979 return res;
3980 }
3981
Roshan Pius3a1667e2018-07-03 15:17:14 -07003982 if (os_strcmp(var, "roaming_consortiums") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003983 res = wpa_config_set_cred_ois(cred->roaming_consortiums,
3984 cred->roaming_consortiums_len,
3985 &cred->num_roaming_consortiums,
3986 val);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003987 if (res < 0)
3988 wpa_printf(MSG_ERROR,
3989 "Line %d: invalid roaming_consortiums",
3990 line);
3991 os_free(val);
3992 return res;
3993 }
3994
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003995 if (os_strcmp(var, "excluded_ssid") == 0) {
3996 struct excluded_ssid *e;
3997
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003998 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003999 wpa_printf(MSG_ERROR, "Line %d: invalid "
4000 "excluded_ssid length %d", line, (int) len);
4001 os_free(val);
4002 return -1;
4003 }
4004
4005 e = os_realloc_array(cred->excluded_ssid,
4006 cred->num_excluded_ssid + 1,
4007 sizeof(struct excluded_ssid));
4008 if (e == NULL) {
4009 os_free(val);
4010 return -1;
4011 }
4012 cred->excluded_ssid = e;
4013
4014 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
4015 os_memcpy(e->ssid, val, len);
4016 e->ssid_len = len;
4017
4018 os_free(val);
4019
4020 return 0;
4021 }
4022
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004023 if (os_strcmp(var, "roaming_partner") == 0) {
4024 struct roaming_partner *p;
4025 char *pos;
4026
4027 p = os_realloc_array(cred->roaming_partner,
4028 cred->num_roaming_partner + 1,
4029 sizeof(struct roaming_partner));
4030 if (p == NULL) {
4031 os_free(val);
4032 return -1;
4033 }
4034 cred->roaming_partner = p;
4035
4036 p = &cred->roaming_partner[cred->num_roaming_partner];
4037
4038 pos = os_strchr(val, ',');
4039 if (pos == NULL) {
4040 os_free(val);
4041 return -1;
4042 }
4043 *pos++ = '\0';
4044 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
4045 os_free(val);
4046 return -1;
4047 }
4048 os_memcpy(p->fqdn, val, pos - val);
4049
4050 p->exact_match = atoi(pos);
4051
4052 pos = os_strchr(pos, ',');
4053 if (pos == NULL) {
4054 os_free(val);
4055 return -1;
4056 }
4057 *pos++ = '\0';
4058
4059 p->priority = atoi(pos);
4060
4061 pos = os_strchr(pos, ',');
4062 if (pos == NULL) {
4063 os_free(val);
4064 return -1;
4065 }
4066 *pos++ = '\0';
4067
4068 if (os_strlen(pos) >= sizeof(p->country)) {
4069 os_free(val);
4070 return -1;
4071 }
4072 os_memcpy(p->country, pos, os_strlen(pos) + 1);
4073
4074 cred->num_roaming_partner++;
4075 os_free(val);
4076
4077 return 0;
4078 }
4079
4080 if (os_strcmp(var, "provisioning_sp") == 0) {
4081 os_free(cred->provisioning_sp);
4082 cred->provisioning_sp = val;
4083 return 0;
4084 }
4085
Sunil8cd6f4d2022-06-28 18:40:46 +00004086 if (os_strcmp(var, "imsi_privacy_cert") == 0) {
4087 os_free(cred->imsi_privacy_cert);
4088 cred->imsi_privacy_cert = val;
4089 return 0;
4090 }
4091
4092 if (os_strcmp(var, "imsi_privacy_attr") == 0) {
4093 os_free(cred->imsi_privacy_attr);
4094 cred->imsi_privacy_attr = val;
Sunil Ravia04bd252022-05-02 22:54:18 -07004095 return 0;
4096 }
4097
Steven Liu9138d432022-11-23 22:29:05 +00004098 if (os_strcmp(var, "strict_conservative_peer_mode") == 0) {
4099 cred->strict_conservative_peer_mode = atoi(val);
4100 return 0;
4101 }
4102
Dmitry Shmidt04949592012-07-19 12:16:46 -07004103 if (line) {
4104 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
4105 line, var);
4106 }
4107
4108 os_free(val);
4109
4110 return -1;
4111}
4112
4113
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004114static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004115{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004117 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004118 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004120 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004121 if (buf == NULL)
4122 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004123 res = os_snprintf(buf, bufsize, "%d", val);
4124 if (os_snprintf_error(bufsize, res)) {
4125 os_free(buf);
4126 buf = NULL;
4127 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004128 return buf;
4129}
4130
4131
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004132static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004133{
4134 if (str == NULL)
4135 return NULL;
4136 return os_strdup(str);
4137}
4138
4139
4140char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
4141{
4142 if (os_strcmp(var, "temporary") == 0)
4143 return alloc_int_str(cred->temporary);
4144
4145 if (os_strcmp(var, "priority") == 0)
4146 return alloc_int_str(cred->priority);
4147
4148 if (os_strcmp(var, "sp_priority") == 0)
4149 return alloc_int_str(cred->sp_priority);
4150
4151 if (os_strcmp(var, "pcsc") == 0)
4152 return alloc_int_str(cred->pcsc);
4153
4154 if (os_strcmp(var, "eap") == 0) {
4155 if (!cred->eap_method)
4156 return NULL;
4157 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4158 cred->eap_method[0].method));
4159 }
4160
4161 if (os_strcmp(var, "update_identifier") == 0)
4162 return alloc_int_str(cred->update_identifier);
4163
4164 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4165 return alloc_int_str(cred->min_dl_bandwidth_home);
4166
4167 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4168 return alloc_int_str(cred->min_ul_bandwidth_home);
4169
4170 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4171 return alloc_int_str(cred->min_dl_bandwidth_roaming);
4172
4173 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4174 return alloc_int_str(cred->min_ul_bandwidth_roaming);
4175
4176 if (os_strcmp(var, "max_bss_load") == 0)
4177 return alloc_int_str(cred->max_bss_load);
4178
4179 if (os_strcmp(var, "req_conn_capab") == 0) {
4180 unsigned int i;
4181 char *buf, *end, *pos;
4182 int ret;
4183
4184 if (!cred->num_req_conn_capab)
4185 return NULL;
4186
4187 buf = os_malloc(4000);
4188 if (buf == NULL)
4189 return NULL;
4190 pos = buf;
4191 end = pos + 4000;
4192 for (i = 0; i < cred->num_req_conn_capab; i++) {
4193 int *ports;
4194
4195 ret = os_snprintf(pos, end - pos, "%s%u",
4196 i > 0 ? "\n" : "",
4197 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004198 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004199 return buf;
4200 pos += ret;
4201
4202 ports = cred->req_conn_capab_port[i];
4203 if (ports) {
4204 int j;
4205 for (j = 0; ports[j] != -1; j++) {
4206 ret = os_snprintf(pos, end - pos,
4207 "%s%d",
4208 j > 0 ? "," : ":",
4209 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004210 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004211 return buf;
4212 pos += ret;
4213 }
4214 }
4215 }
4216
4217 return buf;
4218 }
4219
4220 if (os_strcmp(var, "ocsp") == 0)
4221 return alloc_int_str(cred->ocsp);
4222
4223 if (os_strcmp(var, "realm") == 0)
4224 return alloc_strdup(cred->realm);
4225
4226 if (os_strcmp(var, "username") == 0)
4227 return alloc_strdup(cred->username);
4228
4229 if (os_strcmp(var, "password") == 0) {
4230 if (!cred->password)
4231 return NULL;
4232 return alloc_strdup("*");
4233 }
4234
4235 if (os_strcmp(var, "ca_cert") == 0)
4236 return alloc_strdup(cred->ca_cert);
4237
4238 if (os_strcmp(var, "client_cert") == 0)
4239 return alloc_strdup(cred->client_cert);
4240
4241 if (os_strcmp(var, "private_key") == 0)
4242 return alloc_strdup(cred->private_key);
4243
4244 if (os_strcmp(var, "private_key_passwd") == 0) {
4245 if (!cred->private_key_passwd)
4246 return NULL;
4247 return alloc_strdup("*");
4248 }
4249
4250 if (os_strcmp(var, "imsi") == 0)
4251 return alloc_strdup(cred->imsi);
4252
Sunil8cd6f4d2022-06-28 18:40:46 +00004253 if (os_strcmp(var, "imsi_privacy_cert") == 0)
4254 return alloc_strdup(cred->imsi_privacy_cert);
4255
4256 if (os_strcmp(var, "imsi_privacy_attr") == 0)
4257 return alloc_strdup(cred->imsi_privacy_attr);
Sunil Ravia04bd252022-05-02 22:54:18 -07004258
Steven Liu9138d432022-11-23 22:29:05 +00004259 if (os_strcmp(var, "strict_conservative_peer_mode") == 0)
4260 return alloc_int_str(cred->strict_conservative_peer_mode);
4261
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004262 if (os_strcmp(var, "milenage") == 0) {
4263 if (!(cred->milenage))
4264 return NULL;
4265 return alloc_strdup("*");
4266 }
4267
4268 if (os_strcmp(var, "domain_suffix_match") == 0)
4269 return alloc_strdup(cred->domain_suffix_match);
4270
4271 if (os_strcmp(var, "domain") == 0) {
4272 unsigned int i;
4273 char *buf, *end, *pos;
4274 int ret;
4275
4276 if (!cred->num_domain)
4277 return NULL;
4278
4279 buf = os_malloc(4000);
4280 if (buf == NULL)
4281 return NULL;
4282 pos = buf;
4283 end = pos + 4000;
4284
4285 for (i = 0; i < cred->num_domain; i++) {
4286 ret = os_snprintf(pos, end - pos, "%s%s",
4287 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004288 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004289 return buf;
4290 pos += ret;
4291 }
4292
4293 return buf;
4294 }
4295
4296 if (os_strcmp(var, "phase1") == 0)
4297 return alloc_strdup(cred->phase1);
4298
4299 if (os_strcmp(var, "phase2") == 0)
4300 return alloc_strdup(cred->phase2);
4301
4302 if (os_strcmp(var, "roaming_consortium") == 0) {
4303 size_t buflen;
4304 char *buf;
4305
Sunil Ravi77d572f2023-01-17 23:58:31 +00004306 if (!cred->num_home_ois || !cred->home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004307 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004308 buflen = cred->home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004309 buf = os_malloc(buflen);
4310 if (buf == NULL)
4311 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004312 wpa_snprintf_hex(buf, buflen, cred->home_ois[0],
4313 cred->home_ois_len[0]);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004314 return buf;
4315 }
4316
4317 if (os_strcmp(var, "required_roaming_consortium") == 0) {
4318 size_t buflen;
4319 char *buf;
4320
Sunil Ravi77d572f2023-01-17 23:58:31 +00004321 if (!cred->num_required_home_ois ||
4322 !cred->required_home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004323 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004324 buflen = cred->required_home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004325 buf = os_malloc(buflen);
4326 if (buf == NULL)
4327 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004328 wpa_snprintf_hex(buf, buflen, cred->required_home_ois[0],
4329 cred->required_home_ois_len[0]);
4330 return buf;
4331 }
4332
4333 if (os_strcmp(var, "home_ois") == 0) {
4334 size_t buflen;
4335 char *buf, *pos;
4336 size_t i;
4337
4338 if (!cred->num_home_ois)
4339 return NULL;
4340 buflen = cred->num_home_ois * MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4341 buf = os_malloc(buflen);
4342 if (!buf)
4343 return NULL;
4344 pos = buf;
4345 for (i = 0; i < cred->num_home_ois; i++) {
4346 if (i > 0)
4347 *pos++ = ',';
4348 pos += wpa_snprintf_hex(
4349 pos, buf + buflen - pos,
4350 cred->home_ois[i],
4351 cred->home_ois_len[i]);
4352 }
4353 *pos = '\0';
4354 return buf;
4355 }
4356
4357 if (os_strcmp(var, "required_home_ois") == 0) {
4358 size_t buflen;
4359 char *buf, *pos;
4360 size_t i;
4361
4362 if (!cred->num_required_home_ois)
4363 return NULL;
4364 buflen = cred->num_required_home_ois *
4365 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4366 buf = os_malloc(buflen);
4367 if (!buf)
4368 return NULL;
4369 pos = buf;
4370 for (i = 0; i < cred->num_required_home_ois; i++) {
4371 if (i > 0)
4372 *pos++ = ',';
4373 pos += wpa_snprintf_hex(
4374 pos, buf + buflen - pos,
4375 cred->required_home_ois[i],
4376 cred->required_home_ois_len[i]);
4377 }
4378 *pos = '\0';
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004379 return buf;
4380 }
4381
Roshan Pius3a1667e2018-07-03 15:17:14 -07004382 if (os_strcmp(var, "roaming_consortiums") == 0) {
4383 size_t buflen;
4384 char *buf, *pos;
4385 size_t i;
4386
4387 if (!cred->num_roaming_consortiums)
4388 return NULL;
4389 buflen = cred->num_roaming_consortiums *
4390 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4391 buf = os_malloc(buflen);
4392 if (!buf)
4393 return NULL;
4394 pos = buf;
4395 for (i = 0; i < cred->num_roaming_consortiums; i++) {
4396 if (i > 0)
4397 *pos++ = ',';
4398 pos += wpa_snprintf_hex(
4399 pos, buf + buflen - pos,
4400 cred->roaming_consortiums[i],
4401 cred->roaming_consortiums_len[i]);
4402 }
4403 *pos = '\0';
4404 return buf;
4405 }
4406
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004407 if (os_strcmp(var, "excluded_ssid") == 0) {
4408 unsigned int i;
4409 char *buf, *end, *pos;
4410
4411 if (!cred->num_excluded_ssid)
4412 return NULL;
4413
4414 buf = os_malloc(4000);
4415 if (buf == NULL)
4416 return NULL;
4417 pos = buf;
4418 end = pos + 4000;
4419
4420 for (i = 0; i < cred->num_excluded_ssid; i++) {
4421 struct excluded_ssid *e;
4422 int ret;
4423
4424 e = &cred->excluded_ssid[i];
4425 ret = os_snprintf(pos, end - pos, "%s%s",
4426 i > 0 ? "\n" : "",
4427 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004428 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004429 return buf;
4430 pos += ret;
4431 }
4432
4433 return buf;
4434 }
4435
4436 if (os_strcmp(var, "roaming_partner") == 0) {
4437 unsigned int i;
4438 char *buf, *end, *pos;
4439
4440 if (!cred->num_roaming_partner)
4441 return NULL;
4442
4443 buf = os_malloc(4000);
4444 if (buf == NULL)
4445 return NULL;
4446 pos = buf;
4447 end = pos + 4000;
4448
4449 for (i = 0; i < cred->num_roaming_partner; i++) {
4450 struct roaming_partner *p;
4451 int ret;
4452
4453 p = &cred->roaming_partner[i];
4454 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4455 i > 0 ? "\n" : "",
4456 p->fqdn, p->exact_match, p->priority,
4457 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004458 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004459 return buf;
4460 pos += ret;
4461 }
4462
4463 return buf;
4464 }
4465
4466 if (os_strcmp(var, "provisioning_sp") == 0)
4467 return alloc_strdup(cred->provisioning_sp);
4468
4469 return NULL;
4470}
4471
4472
Dmitry Shmidt04949592012-07-19 12:16:46 -07004473struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4474{
4475 struct wpa_cred *cred;
4476
4477 cred = config->cred;
4478 while (cred) {
4479 if (id == cred->id)
4480 break;
4481 cred = cred->next;
4482 }
4483
4484 return cred;
4485}
4486
4487
4488struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4489{
4490 int id;
4491 struct wpa_cred *cred, *last = NULL;
4492
4493 id = -1;
4494 cred = config->cred;
4495 while (cred) {
4496 if (cred->id > id)
4497 id = cred->id;
4498 last = cred;
4499 cred = cred->next;
4500 }
4501 id++;
4502
4503 cred = os_zalloc(sizeof(*cred));
4504 if (cred == NULL)
4505 return NULL;
4506 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004507 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004508 if (last)
4509 last->next = cred;
4510 else
4511 config->cred = cred;
4512
4513 return cred;
4514}
4515
4516
4517int wpa_config_remove_cred(struct wpa_config *config, int id)
4518{
4519 struct wpa_cred *cred, *prev = NULL;
4520
4521 cred = config->cred;
4522 while (cred) {
4523 if (id == cred->id)
4524 break;
4525 prev = cred;
4526 cred = cred->next;
4527 }
4528
4529 if (cred == NULL)
4530 return -1;
4531
4532 if (prev)
4533 prev->next = cred->next;
4534 else
4535 config->cred = cred->next;
4536
4537 wpa_config_free_cred(cred);
4538 return 0;
4539}
4540
4541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542#ifndef CONFIG_NO_CONFIG_BLOBS
4543/**
4544 * wpa_config_get_blob - Get a named configuration blob
4545 * @config: Configuration data from wpa_config_read()
4546 * @name: Name of the blob
4547 * Returns: Pointer to blob data or %NULL if not found
4548 */
4549const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4550 const char *name)
4551{
4552 struct wpa_config_blob *blob = config->blobs;
4553
4554 while (blob) {
4555 if (os_strcmp(blob->name, name) == 0)
4556 return blob;
4557 blob = blob->next;
4558 }
4559 return NULL;
4560}
4561
4562
4563/**
4564 * wpa_config_set_blob - Set or add a named configuration blob
4565 * @config: Configuration data from wpa_config_read()
4566 * @blob: New value for the blob
4567 *
4568 * Adds a new configuration blob or replaces the current value of an existing
4569 * blob.
4570 */
4571void wpa_config_set_blob(struct wpa_config *config,
4572 struct wpa_config_blob *blob)
4573{
4574 wpa_config_remove_blob(config, blob->name);
4575 blob->next = config->blobs;
4576 config->blobs = blob;
4577}
4578
4579
4580/**
4581 * wpa_config_free_blob - Free blob data
4582 * @blob: Pointer to blob to be freed
4583 */
4584void wpa_config_free_blob(struct wpa_config_blob *blob)
4585{
4586 if (blob) {
4587 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004588 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 os_free(blob);
4590 }
4591}
4592
4593
4594/**
4595 * wpa_config_remove_blob - Remove a named configuration blob
4596 * @config: Configuration data from wpa_config_read()
4597 * @name: Name of the blob to remove
4598 * Returns: 0 if blob was removed or -1 if blob was not found
4599 */
4600int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4601{
4602 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4603
4604 while (pos) {
4605 if (os_strcmp(pos->name, name) == 0) {
4606 if (prev)
4607 prev->next = pos->next;
4608 else
4609 config->blobs = pos->next;
4610 wpa_config_free_blob(pos);
4611 return 0;
4612 }
4613 prev = pos;
4614 pos = pos->next;
4615 }
4616
4617 return -1;
4618}
4619#endif /* CONFIG_NO_CONFIG_BLOBS */
4620
4621
4622/**
4623 * wpa_config_alloc_empty - Allocate an empty configuration
4624 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4625 * socket
4626 * @driver_param: Driver parameters
4627 * Returns: Pointer to allocated configuration data or %NULL on failure
4628 */
4629struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4630 const char *driver_param)
4631{
Hai Shalom899fcc72020-10-19 14:38:18 -07004632#define ecw2cw(ecw) ((1 << (ecw)) - 1)
4633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004635 const int aCWmin = 4, aCWmax = 10;
4636 const struct hostapd_wmm_ac_params ac_bk =
4637 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4638 const struct hostapd_wmm_ac_params ac_be =
4639 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4640 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004641 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004642 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004643 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4644 const struct hostapd_tx_queue_params txq_bk =
4645 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4646 const struct hostapd_tx_queue_params txq_be =
4647 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4648 const struct hostapd_tx_queue_params txq_vi =
4649 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4650 const struct hostapd_tx_queue_params txq_vo =
4651 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4652 (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4653
4654#undef ecw2cw
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655
4656 config = os_zalloc(sizeof(*config));
4657 if (config == NULL)
4658 return NULL;
4659 config->eapol_version = DEFAULT_EAPOL_VERSION;
4660 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004661 config->user_mpm = DEFAULT_USER_MPM;
4662 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004663 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004664 config->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004665 config->dot11RSNASAERetransPeriod =
4666 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 config->fast_reauth = DEFAULT_FAST_REAUTH;
4668 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4669 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004670 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004671 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004672 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004673 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4675 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4676 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4677 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004678 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004679 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004680 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Hai Shalom60840252021-02-19 19:02:11 -08004681 config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004682 config->wmm_ac_params[0] = ac_be;
4683 config->wmm_ac_params[1] = ac_bk;
4684 config->wmm_ac_params[2] = ac_vi;
4685 config->wmm_ac_params[3] = ac_vo;
Hai Shalom899fcc72020-10-19 14:38:18 -07004686 config->tx_queue[0] = txq_vo;
4687 config->tx_queue[1] = txq_vi;
4688 config->tx_queue[2] = txq_be;
4689 config->tx_queue[3] = txq_bk;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004690 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004691 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004692 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004693 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004694 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Hai Shalomfdcde762020-04-02 11:19:20 -07004695 config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004697#ifdef CONFIG_MBO
4698 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004699 config->disassoc_imminent_rssi_threshold =
4700 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4701 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004702#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07004703 config->btm_offload = DEFAULT_BTM_OFFLOAD;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 if (ctrl_interface)
4706 config->ctrl_interface = os_strdup(ctrl_interface);
4707 if (driver_param)
4708 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004709 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004711#ifdef CONFIG_TESTING_OPTIONS
4712 config->mld_connect_band_pref = DEFAULT_MLD_CONNECT_BAND_PREF;
4713#endif /* CONFIG_TESTING_OPTIONS */
4714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 return config;
4716}
4717
4718
4719#ifndef CONFIG_NO_STDOUT_DEBUG
4720/**
4721 * wpa_config_debug_dump_networks - Debug dump of configured networks
4722 * @config: Configuration data from wpa_config_read()
4723 */
4724void wpa_config_debug_dump_networks(struct wpa_config *config)
4725{
Hai Shalomfdcde762020-04-02 11:19:20 -07004726 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 struct wpa_ssid *ssid;
4728
4729 for (prio = 0; prio < config->num_prio; prio++) {
4730 ssid = config->pssid[prio];
4731 wpa_printf(MSG_DEBUG, "Priority group %d",
4732 ssid->priority);
4733 while (ssid) {
4734 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4735 ssid->id,
4736 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4737 ssid = ssid->pnext;
4738 }
4739 }
4740}
4741#endif /* CONFIG_NO_STDOUT_DEBUG */
4742
4743
Hai Shalom899fcc72020-10-19 14:38:18 -07004744/**
4745 * Structure for global configuration parsing. This data is used to implement a
4746 * generic parser for the global interface configuration. The table of variables
4747 * is defined below in this file (global_fields[]).
4748 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749struct global_parse_data {
Hai Shalom899fcc72020-10-19 14:38:18 -07004750 /* Configuration variable name */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 char *name;
Hai Shalom899fcc72020-10-19 14:38:18 -07004752
4753 /* Parser function for this variable. The parser functions return 0 or 1
4754 * to indicate success. Value 0 indicates that the parameter value may
4755 * have changed while value 1 means that the value did not change.
4756 * Error cases (failure to parse the string) are indicated by returning
4757 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 int (*parser)(const struct global_parse_data *data,
4759 struct wpa_config *config, int line, const char *value);
Hai Shalom899fcc72020-10-19 14:38:18 -07004760
4761 /* Getter function to print the variable in text format to buf. */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004762 int (*get)(const char *name, struct wpa_config *config, long offset,
4763 char *buf, size_t buflen, int pretty_print);
Hai Shalom899fcc72020-10-19 14:38:18 -07004764
4765 /* Variable specific parameters for the parser. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 void *param1, *param2, *param3;
Hai Shalom899fcc72020-10-19 14:38:18 -07004767
4768 /* Indicates which configuration variable has changed. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 unsigned int changed_flag;
4770};
4771
4772
Sunil Ravi99c035e2024-07-12 01:42:03 +00004773static int
4774wpa_global_config_parse_int_impl(const struct global_parse_data *data,
4775 struct wpa_config *config, int line,
4776 const char *pos, bool check_range)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004778 int val, *dst;
4779 char *end;
Hai Shalom899fcc72020-10-19 14:38:18 -07004780 bool same;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004783 val = strtol(pos, &end, 0);
4784 if (*end) {
4785 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4786 line, pos);
4787 return -1;
4788 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00004789
4790 if (check_range && val < (long) data->param2) {
4791 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4792 "min_value=%ld)", line, data->name, val,
4793 (long) data->param2);
4794 return -1;
4795 }
4796
4797 if (check_range && val > (long) data->param3) {
4798 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4799 "max_value=%ld)", line, data->name, val,
4800 (long) data->param3);
4801 return -1;
4802 }
4803
Hai Shalom899fcc72020-10-19 14:38:18 -07004804 same = *dst == val;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004805 *dst = val;
4806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4808
Hai Shalom899fcc72020-10-19 14:38:18 -07004809 return same;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810}
4811
4812
Sunil Ravi99c035e2024-07-12 01:42:03 +00004813static int wpa_global_config_parse_int(const struct global_parse_data *data,
4814 struct wpa_config *config, int line,
4815 const char *pos)
4816{
4817 return wpa_global_config_parse_int_impl(data, config, line, pos, false);
4818}
4819
4820
4821static int
4822wpa_global_config_parse_int_range(const struct global_parse_data *data,
4823 struct wpa_config *config, int line,
4824 const char *pos)
4825{
4826 return wpa_global_config_parse_int_impl(data, config, line, pos, true);
4827}
4828
4829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830static int wpa_global_config_parse_str(const struct global_parse_data *data,
4831 struct wpa_config *config, int line,
4832 const char *pos)
4833{
Hai Shalom899fcc72020-10-19 14:38:18 -07004834 size_t len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 char **dst, *tmp;
4836
4837 len = os_strlen(pos);
4838 if (data->param2 && len < (size_t) data->param2) {
4839 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4840 "min_len=%ld)", line, data->name,
4841 (unsigned long) len, (long) data->param2);
4842 return -1;
4843 }
4844
4845 if (data->param3 && len > (size_t) data->param3) {
4846 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4847 "max_len=%ld)", line, data->name,
4848 (unsigned long) len, (long) data->param3);
4849 return -1;
4850 }
4851
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004852 if (has_newline(pos)) {
4853 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4854 line, data->name);
4855 return -1;
4856 }
4857
Hai Shalom899fcc72020-10-19 14:38:18 -07004858 dst = (char **) (((u8 *) config) + (long) data->param1);
4859 if (*dst)
4860 prev_len = os_strlen(*dst);
4861 else
4862 prev_len = 0;
4863
4864 /* No change to the previously configured value */
Hai Shalom60840252021-02-19 19:02:11 -08004865 if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
Hai Shalom899fcc72020-10-19 14:38:18 -07004866 return 1;
4867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 tmp = os_strdup(pos);
4869 if (tmp == NULL)
4870 return -1;
4871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 os_free(*dst);
4873 *dst = tmp;
4874 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4875
4876 return 0;
4877}
4878
4879
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004880static int wpa_config_process_bgscan(const struct global_parse_data *data,
4881 struct wpa_config *config, int line,
4882 const char *pos)
4883{
4884 size_t len;
4885 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004886 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004887
4888 tmp = wpa_config_parse_string(pos, &len);
4889 if (tmp == NULL) {
4890 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4891 line, data->name);
4892 return -1;
4893 }
4894
Dmitry Shmidt97672262014-02-03 13:02:54 -08004895 res = wpa_global_config_parse_str(data, config, line, tmp);
4896 os_free(tmp);
4897 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004898}
4899
4900
Dmitry Shmidt04949592012-07-19 12:16:46 -07004901static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4902 struct wpa_config *config, int line,
4903 const char *pos)
4904{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004905 struct wpabuf **dst, *tmp;
4906
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004907 tmp = wpabuf_parse_bin(pos);
4908 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004909 return -1;
4910
Dmitry Shmidt04949592012-07-19 12:16:46 -07004911 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004912 if (wpabuf_cmp(*dst, tmp) == 0) {
4913 wpabuf_free(tmp);
4914 return 1;
4915 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004916 wpabuf_free(*dst);
4917 *dst = tmp;
4918 wpa_printf(MSG_DEBUG, "%s", data->name);
4919
4920 return 0;
4921}
4922
4923
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004924static int wpa_config_process_freq_list(const struct global_parse_data *data,
4925 struct wpa_config *config, int line,
4926 const char *value)
4927{
4928 int *freqs;
4929
4930 freqs = wpa_config_parse_int_array(value);
4931 if (freqs == NULL)
4932 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004933 if (freqs[0] == 0) {
4934 os_free(freqs);
4935 freqs = NULL;
4936 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004937 os_free(config->freq_list);
4938 config->freq_list = freqs;
4939 return 0;
4940}
4941
4942
Hai Shalom60840252021-02-19 19:02:11 -08004943static int
4944wpa_config_process_initial_freq_list(const struct global_parse_data *data,
4945 struct wpa_config *config, int line,
4946 const char *value)
4947{
4948 int *freqs;
4949
4950 freqs = wpa_config_parse_int_array(value);
4951 if (!freqs)
4952 return -1;
4953 if (freqs[0] == 0) {
4954 os_free(freqs);
4955 freqs = NULL;
4956 }
4957 os_free(config->initial_freq_list);
4958 config->initial_freq_list = freqs;
4959 return 0;
4960}
4961
4962
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004963#ifdef CONFIG_P2P
4964static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4965 struct wpa_config *config, int line,
4966 const char *pos)
4967{
4968 u32 *dst;
4969 struct hostapd_ip_addr addr;
4970
4971 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4972 return -1;
4973 if (addr.af != AF_INET)
4974 return -1;
4975
4976 dst = (u32 *) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004977 if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
4978 return 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004979 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4980 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4981 WPA_GET_BE32((u8 *) dst));
4982
4983 return 0;
4984}
4985#endif /* CONFIG_P2P */
4986
4987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988static int wpa_config_process_country(const struct global_parse_data *data,
4989 struct wpa_config *config, int line,
4990 const char *pos)
4991{
4992 if (!pos[0] || !pos[1]) {
4993 wpa_printf(MSG_DEBUG, "Invalid country set");
4994 return -1;
4995 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004996 if (pos[0] == config->country[0] && pos[1] == config->country[1])
4997 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 config->country[0] = pos[0];
4999 config->country[1] = pos[1];
5000 wpa_printf(MSG_DEBUG, "country='%c%c'",
5001 config->country[0], config->country[1]);
5002 return 0;
5003}
5004
5005
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005006#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007static int wpa_config_process_load_dynamic_eap(
5008 const struct global_parse_data *data, struct wpa_config *config,
5009 int line, const char *so)
5010{
5011 int ret;
5012 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
5013 ret = eap_peer_method_load(so);
5014 if (ret == -2) {
5015 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
5016 "reloading.");
5017 } else if (ret) {
5018 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
5019 "method '%s'.", line, so);
5020 return -1;
5021 }
5022
5023 return 0;
5024}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005025#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026
5027
5028#ifdef CONFIG_WPS
5029
5030static int wpa_config_process_uuid(const struct global_parse_data *data,
5031 struct wpa_config *config, int line,
5032 const char *pos)
5033{
5034 char buf[40];
5035 if (uuid_str2bin(pos, config->uuid)) {
5036 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
5037 return -1;
5038 }
5039 uuid_bin2str(config->uuid, buf, sizeof(buf));
5040 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
5041 return 0;
5042}
5043
5044
5045static int wpa_config_process_device_type(
5046 const struct global_parse_data *data,
5047 struct wpa_config *config, int line, const char *pos)
5048{
5049 return wps_dev_type_str2bin(pos, config->device_type);
5050}
5051
5052
5053static int wpa_config_process_os_version(const struct global_parse_data *data,
5054 struct wpa_config *config, int line,
5055 const char *pos)
5056{
5057 if (hexstr2bin(pos, config->os_version, 4)) {
5058 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
5059 return -1;
5060 }
5061 wpa_printf(MSG_DEBUG, "os_version=%08x",
5062 WPA_GET_BE32(config->os_version));
5063 return 0;
5064}
5065
Dmitry Shmidt04949592012-07-19 12:16:46 -07005066
5067static int wpa_config_process_wps_vendor_ext_m1(
5068 const struct global_parse_data *data,
5069 struct wpa_config *config, int line, const char *pos)
5070{
5071 struct wpabuf *tmp;
5072 int len = os_strlen(pos) / 2;
5073 u8 *p;
5074
5075 if (!len) {
5076 wpa_printf(MSG_ERROR, "Line %d: "
5077 "invalid wps_vendor_ext_m1", line);
5078 return -1;
5079 }
5080
5081 tmp = wpabuf_alloc(len);
5082 if (tmp) {
5083 p = wpabuf_put(tmp, len);
5084
5085 if (hexstr2bin(pos, p, len)) {
5086 wpa_printf(MSG_ERROR, "Line %d: "
5087 "invalid wps_vendor_ext_m1", line);
5088 wpabuf_free(tmp);
5089 return -1;
5090 }
5091
5092 wpabuf_free(config->wps_vendor_ext_m1);
5093 config->wps_vendor_ext_m1 = tmp;
5094 } else {
5095 wpa_printf(MSG_ERROR, "Can not allocate "
5096 "memory for wps_vendor_ext_m1");
5097 return -1;
5098 }
5099
5100 return 0;
5101}
5102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005103#endif /* CONFIG_WPS */
5104
5105#ifdef CONFIG_P2P
5106static int wpa_config_process_sec_device_type(
5107 const struct global_parse_data *data,
5108 struct wpa_config *config, int line, const char *pos)
5109{
5110 int idx;
5111
5112 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
5113 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
5114 "items", line);
5115 return -1;
5116 }
5117
5118 idx = config->num_sec_device_types;
5119
5120 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
5121 return -1;
5122
5123 config->num_sec_device_types++;
5124 return 0;
5125}
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126
5127
5128static int wpa_config_process_p2p_pref_chan(
5129 const struct global_parse_data *data,
5130 struct wpa_config *config, int line, const char *pos)
5131{
5132 struct p2p_channel *pref = NULL, *n;
Hai Shalomfdcde762020-04-02 11:19:20 -07005133 size_t num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005134 const char *pos2;
5135 u8 op_class, chan;
5136
5137 /* format: class:chan,class:chan,... */
5138
5139 while (*pos) {
5140 op_class = atoi(pos);
5141 pos2 = os_strchr(pos, ':');
5142 if (pos2 == NULL)
5143 goto fail;
5144 pos2++;
5145 chan = atoi(pos2);
5146
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005147 n = os_realloc_array(pref, num + 1,
5148 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005149 if (n == NULL)
5150 goto fail;
5151 pref = n;
5152 pref[num].op_class = op_class;
5153 pref[num].chan = chan;
5154 num++;
5155
5156 pos = os_strchr(pos2, ',');
5157 if (pos == NULL)
5158 break;
5159 pos++;
5160 }
5161
5162 os_free(config->p2p_pref_chan);
5163 config->p2p_pref_chan = pref;
5164 config->num_p2p_pref_chan = num;
5165 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
5166 (u8 *) config->p2p_pref_chan,
5167 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
5168
5169 return 0;
5170
5171fail:
5172 os_free(pref);
5173 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
5174 return -1;
5175}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005176
5177
5178static int wpa_config_process_p2p_no_go_freq(
5179 const struct global_parse_data *data,
5180 struct wpa_config *config, int line, const char *pos)
5181{
5182 int ret;
5183
5184 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
5185 if (ret < 0) {
5186 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
5187 return -1;
5188 }
5189
5190 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
5191 config->p2p_no_go_freq.num);
5192
5193 return 0;
5194}
5195
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005196static int wpa_config_process_p2p_device_persistent_mac_addr(
5197 const struct global_parse_data *data,
5198 struct wpa_config *config, int line, const char *pos)
5199{
5200 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
5201 wpa_printf(MSG_ERROR,
5202 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
5203 line, pos);
5204 return -1;
5205 }
5206
5207 return 0;
5208}
5209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210#endif /* CONFIG_P2P */
5211
5212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213static int wpa_config_process_hessid(
5214 const struct global_parse_data *data,
5215 struct wpa_config *config, int line, const char *pos)
5216{
5217 if (hwaddr_aton2(pos, config->hessid) < 0) {
5218 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
5219 line, pos);
5220 return -1;
5221 }
5222
5223 return 0;
5224}
5225
5226
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005227static int wpa_config_process_sae_groups(
5228 const struct global_parse_data *data,
5229 struct wpa_config *config, int line, const char *pos)
5230{
5231 int *groups = wpa_config_parse_int_array(pos);
5232 if (groups == NULL) {
5233 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5234 line, pos);
5235 return -1;
5236 }
5237
5238 os_free(config->sae_groups);
5239 config->sae_groups = groups;
5240
5241 return 0;
5242}
5243
5244
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005245static int wpa_config_process_ap_vendor_elements(
5246 const struct global_parse_data *data,
5247 struct wpa_config *config, int line, const char *pos)
5248{
5249 struct wpabuf *tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005250
Hai Shaloma20dcd72022-02-04 13:43:00 -08005251 if (!*pos) {
5252 wpabuf_free(config->ap_vendor_elements);
5253 config->ap_vendor_elements = NULL;
5254 return 0;
5255 }
5256
5257 tmp = wpabuf_parse_bin(pos);
5258 if (!tmp) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005259 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5260 line);
5261 return -1;
5262 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005263 wpabuf_free(config->ap_vendor_elements);
5264 config->ap_vendor_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005265
Hai Shaloma20dcd72022-02-04 13:43:00 -08005266 return 0;
5267}
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005268
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005269
Hai Shaloma20dcd72022-02-04 13:43:00 -08005270static int wpa_config_process_ap_assocresp_elements(
5271 const struct global_parse_data *data,
5272 struct wpa_config *config, int line, const char *pos)
5273{
5274 struct wpabuf *tmp;
5275
5276 if (!*pos) {
5277 wpabuf_free(config->ap_assocresp_elements);
5278 config->ap_assocresp_elements = NULL;
5279 return 0;
5280 }
5281
5282 tmp = wpabuf_parse_bin(pos);
5283 if (!tmp) {
5284 wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5285 line);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005286 return -1;
5287 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005288 wpabuf_free(config->ap_assocresp_elements);
5289 config->ap_assocresp_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005290
5291 return 0;
5292}
5293
5294
Dmitry Shmidt56052862013-10-04 10:23:25 -07005295#ifdef CONFIG_CTRL_IFACE
5296static int wpa_config_process_no_ctrl_interface(
5297 const struct global_parse_data *data,
5298 struct wpa_config *config, int line, const char *pos)
5299{
5300 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5301 os_free(config->ctrl_interface);
5302 config->ctrl_interface = NULL;
5303 return 0;
5304}
5305#endif /* CONFIG_CTRL_IFACE */
5306
5307
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005308static int wpa_config_get_int(const char *name, struct wpa_config *config,
5309 long offset, char *buf, size_t buflen,
5310 int pretty_print)
5311{
5312 int *val = (int *) (((u8 *) config) + (long) offset);
5313
5314 if (pretty_print)
5315 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5316 return os_snprintf(buf, buflen, "%d", *val);
5317}
5318
5319
5320static int wpa_config_get_str(const char *name, struct wpa_config *config,
5321 long offset, char *buf, size_t buflen,
5322 int pretty_print)
5323{
5324 char **val = (char **) (((u8 *) config) + (long) offset);
5325 int res;
5326
5327 if (pretty_print)
5328 res = os_snprintf(buf, buflen, "%s=%s\n", name,
5329 *val ? *val : "null");
5330 else if (!*val)
5331 return -1;
5332 else
5333 res = os_snprintf(buf, buflen, "%s", *val);
5334 if (os_snprintf_error(buflen, res))
5335 res = -1;
5336
5337 return res;
5338}
5339
5340
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005341#ifdef CONFIG_P2P
5342static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5343 long offset, char *buf, size_t buflen,
5344 int pretty_print)
5345{
5346 void *val = ((u8 *) config) + (long) offset;
5347 int res;
5348 char addr[INET_ADDRSTRLEN];
5349
5350 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5351 return -1;
5352
5353 if (pretty_print)
5354 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5355 else
5356 res = os_snprintf(buf, buflen, "%s", addr);
5357
5358 if (os_snprintf_error(buflen, res))
5359 res = -1;
5360
5361 return res;
5362}
5363#endif /* CONFIG_P2P */
5364
5365
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005366#ifdef CONFIG_TESTING_OPTIONS
5367static int wpa_config_process_mld_connect_bssid_pref(
5368 const struct global_parse_data *data,
5369 struct wpa_config *config, int line, const char *pos)
5370{
5371 if (hwaddr_aton2(pos, config->mld_connect_bssid_pref) < 0) {
5372 wpa_printf(MSG_ERROR,
5373 "Line %d: Invalid mld_connect_bssid_pref '%s'",
5374 line, pos);
5375 return -1;
5376 }
5377
5378 return 0;
5379}
5380#endif /* CONFIG_TESTING_OPTIONS */
5381
5382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383#ifdef OFFSET
5384#undef OFFSET
5385#endif /* OFFSET */
5386/* OFFSET: Get offset of a variable within the wpa_config structure */
5387#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5388
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005389#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5390#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5391#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392#define INT(f) _INT(f), NULL, NULL
Sunil Ravi99c035e2024-07-12 01:42:03 +00005393#define INT_RANGE(f, min, max) #f, wpa_global_config_parse_int_range, \
5394 wpa_config_get_int, OFFSET(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005395#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396#define STR(f) _STR(f), NULL, NULL
5397#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005398#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005399#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
5400 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401
5402static const struct global_parse_data global_fields[] = {
5403#ifdef CONFIG_CTRL_IFACE
5404 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005405 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 { STR(ctrl_interface_group), 0 } /* deprecated */,
5407#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005408#ifdef CONFIG_MACSEC
5409 { INT_RANGE(eapol_version, 1, 3), 0 },
5410#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005412#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005413 { INT(ap_scan), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005414 { FUNC(bgscan), CFG_CHANGED_BGSCAN },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005415#ifdef CONFIG_MESH
5416 { INT(user_mpm), 0 },
5417 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005418 { INT(mesh_max_inactivity), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005419 { INT_RANGE(mesh_fwding, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005420 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005421#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 { INT(fast_reauth), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005424#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 { STR(opensc_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005426#endif /* CONFIG_OPENSC_ENGINE_PATH */
5427#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428 { STR(pkcs11_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005429#endif /* CONFIG_PKCS11_ENGINE_PATH */
5430#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 { STR(pkcs11_module_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005432#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005433 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005434 { STR(pcsc_reader), 0 },
5435 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07005436 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 { STR(driver_param), 0 },
5438 { INT(dot11RSNAConfigPMKLifetime), 0 },
5439 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5440 { INT(dot11RSNAConfigSATimeout), 0 },
5441#ifndef CONFIG_NO_CONFIG_WRITE
5442 { INT(update_config), 0 },
5443#endif /* CONFIG_NO_CONFIG_WRITE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005444#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445 { FUNC_NO_VAR(load_dynamic_eap), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005446#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447#ifdef CONFIG_WPS
5448 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005449 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005450 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5451 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5453 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5454 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5455 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5456 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5457 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
5458 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5459 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07005460 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005461 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462#endif /* CONFIG_WPS */
5463#ifdef CONFIG_P2P
5464 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005465 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5466 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005467 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5468 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
5470 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5471 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
5472 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5473 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005474 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005475 { INT_RANGE(p2p_passphrase_len, 8, 63),
5476 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005477 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005478 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5479 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005480 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005481 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005482 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08005483 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005484 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005485 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005486 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005487 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005488 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005489 { IPV4(ip_addr_go), 0 },
5490 { IPV4(ip_addr_mask), 0 },
5491 { IPV4(ip_addr_start), 0 },
5492 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005493 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005494 { INT(p2p_device_random_mac_addr), 0 },
5495 { FUNC(p2p_device_persistent_mac_addr), 0 },
5496 { INT(p2p_interface_random_mac_addr), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005497 { INT(p2p_6ghz_disable), 0 },
Shuibing Dai64a8a892023-03-08 10:26:22 -08005498 { INT(p2p_dfs_chan_enable), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499#endif /* CONFIG_P2P */
5500 { FUNC(country), CFG_CHANGED_COUNTRY },
5501 { INT(bss_max_count), 0 },
5502 { INT(bss_expiration_age), 0 },
5503 { INT(bss_expiration_scan_count), 0 },
5504 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005505 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07005507 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005508 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005509#ifdef CONFIG_HS20
5510 { INT_RANGE(hs20, 0, 1), 0 },
5511#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005512 { INT_RANGE(interworking, 0, 1), 0 },
5513 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005514 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005515 { INT_RANGE(go_interworking, 0, 1), 0 },
5516 { INT_RANGE(go_access_network_type, 0, 15), 0 },
5517 { INT_RANGE(go_internet, 0, 1), 0 },
5518 { INT_RANGE(go_venue_group, 0, 255), 0 },
5519 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005520 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
5521 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005522 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5523 CFG_CHANGED_NFC_PASSWORD_TOKEN },
5524 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5525 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5526 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005527 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5528 { INT(p2p_go_max_inactivity), 0 },
5529 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005530 { INT(okc), 0 },
5531 { INT(pmf), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005532 { INT_RANGE(sae_check_mfp, 0, 1), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005533 { FUNC(sae_groups), 0 },
Hai Shalomfdcde762020-04-02 11:19:20 -07005534 { INT_RANGE(sae_pwe, 0, 3), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005535 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005536 { INT(dtim_period), 0 },
5537 { INT(beacon_int), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005538 { FUNC(ap_assocresp_elements), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005539 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005540 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005541 { FUNC(freq_list), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005542 { FUNC(initial_freq_list), 0},
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005543 { INT(scan_cur_freq), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005544 { INT(scan_res_valid_for_connect), 0},
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005545 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005546 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005547 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005548 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005549 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005550 { INT(p2p_search_delay), 0},
Sunil Ravi77d572f2023-01-17 23:58:31 +00005551 { INT_RANGE(mac_addr, 0, 2), 0 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005552 { INT(rand_addr_lifetime), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005553 { INT_RANGE(preassoc_mac_addr, 0, 2), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005554 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005555 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005556 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005557 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005558#ifdef CONFIG_FST
5559 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5560 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5561 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5562#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005563 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005564 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005565 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005566#ifdef CONFIG_MBO
5567 { STR(non_pref_chan), 0 },
5568 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5569 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005570 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5571 { INT_RANGE(oce, 0, 3), 0 },
5572#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07005573 { INT_RANGE(btm_offload, 0, 1), CFG_CHANGED_DISABLE_BTM_NOTIFY },
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005574 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005575 { INT_RANGE(ftm_responder, 0, 1), 0 },
5576 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005577 { INT(gas_rand_addr_lifetime), 0 },
5578 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005579#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005580 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005581 { STR(dpp_name), 0 },
5582 { STR(dpp_mud_url), 0 },
Sunil Ravi89eba102022-09-13 21:04:37 -07005583 { STR(dpp_extra_conf_req_name), 0 },
5584 { STR(dpp_extra_conf_req_value), 0 },
5585 { INT_RANGE(dpp_connector_privacy_default, 0, 1), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005586#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005587 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005588 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005589#ifdef CONFIG_WNM
5590 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
Hai Shalomfdcde762020-04-02 11:19:20 -07005591 { INT_RANGE(extended_key_id, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005592#endif /* CONFIG_WNM */
Hai Shalom60840252021-02-19 19:02:11 -08005593 { INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
Sunil Ravi7f769292024-07-23 22:21:32 +00005594 { INT_RANGE(rsn_overriding, 0, 2), 0},
Hai Shalom60840252021-02-19 19:02:11 -08005595#ifdef CONFIG_PASN
5596#ifdef CONFIG_TESTING_OPTIONS
5597 { INT_RANGE(force_kdk_derivation, 0, 1), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005598 { INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005599#endif /* CONFIG_TESTING_OPTIONS */
5600#endif /* CONFIG_PASN */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005601#ifdef CONFIG_TESTING_OPTIONS
5602 { INT_RANGE(mld_force_single_link, 0, 1), 0 },
5603 { INT_RANGE(mld_connect_band_pref, 0, MLD_CONNECT_BAND_PREF_MAX), 0 },
5604 { FUNC(mld_connect_bssid_pref), 0 },
5605#endif /* CONFIG_TESTING_OPTIONS */
5606 { INT_RANGE(ft_prepend_pmkid, 0, 1), CFG_CHANGED_FT_PREPEND_PMKID },
5607 /* NOTE: When adding new parameters here, add_interface() in
5608 * wpa_supplicant/dbus_new_introspect.c may need to be modified to
5609 * increase the size of the iface->xml buffer. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610};
5611
5612#undef FUNC
5613#undef _INT
5614#undef INT
5615#undef INT_RANGE
5616#undef _STR
5617#undef STR
5618#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005619#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005620#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005621#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622
5623
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005624int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5625{
5626 int result = 0;
5627 size_t i;
5628
5629 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5630 const struct global_parse_data *field = &global_fields[i];
5631 int tmp;
5632
5633 if (!field->get)
5634 continue;
5635
5636 tmp = field->get(field->name, config, (long) field->param1,
5637 buf, buflen, 1);
5638 if (tmp < 0)
5639 return -1;
5640 buf += tmp;
5641 buflen -= tmp;
5642 result += tmp;
5643 }
5644 return result;
5645}
5646
5647
5648int wpa_config_get_value(const char *name, struct wpa_config *config,
5649 char *buf, size_t buflen)
5650{
5651 size_t i;
5652
5653 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5654 const struct global_parse_data *field = &global_fields[i];
5655
5656 if (os_strcmp(name, field->name) != 0)
5657 continue;
5658 if (!field->get)
5659 break;
5660 return field->get(name, config, (long) field->param1,
5661 buf, buflen, 0);
5662 }
5663
5664 return -1;
5665}
5666
5667
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005668int wpa_config_get_num_global_field_names(void)
5669{
5670 return NUM_GLOBAL_FIELDS;
5671}
5672
5673
5674const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5675{
5676 if (i >= NUM_GLOBAL_FIELDS)
5677 return NULL;
5678
5679 if (no_var)
5680 *no_var = !global_fields[i].param1;
5681 return global_fields[i].name;
5682}
5683
5684
Hai Shalom899fcc72020-10-19 14:38:18 -07005685/**
5686 * wpa_config_process_global - Set a variable in global configuration
5687 * @config: Pointer to global configuration data
5688 * @pos: Name and value in the format "{name}={value}"
5689 * @line: Line number in configuration file or 0 if not used
5690 * Returns: 0 on success with a possible change in value, 1 on success with no
5691 * change to previously configured value, or -1 on failure
5692 *
5693 * This function can be used to set global configuration variables based on
5694 * both the configuration file and management interface input. The value
5695 * parameter must be in the same format as the text-based configuration file is
5696 * using. For example, strings are using double quotation marks.
5697 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5699{
5700 size_t i;
5701 int ret = 0;
5702
5703 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5704 const struct global_parse_data *field = &global_fields[i];
5705 size_t flen = os_strlen(field->name);
5706 if (os_strncmp(pos, field->name, flen) != 0 ||
5707 pos[flen] != '=')
5708 continue;
5709
Hai Shalom899fcc72020-10-19 14:38:18 -07005710 ret = field->parser(field, config, line, pos + flen + 1);
5711 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 wpa_printf(MSG_ERROR, "Line %d: failed to "
5713 "parse '%s'.", line, pos);
5714 ret = -1;
5715 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005716 if (ret == 1)
5717 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005718 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5719 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 config->changed_parameters |= field->changed_flag;
5721 break;
5722 }
5723 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005724#ifdef CONFIG_AP
Hai Shalom899fcc72020-10-19 14:38:18 -07005725 if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5726 char *tmp = os_strchr(pos, '=');
5727
5728 if (!tmp) {
5729 if (line < 0)
5730 wpa_printf(MSG_ERROR,
5731 "Line %d: invalid line %s",
5732 line, pos);
5733 return -1;
5734 }
5735 *tmp++ = '\0';
5736 if (hostapd_config_tx_queue(config->tx_queue, pos,
5737 tmp)) {
5738 wpa_printf(MSG_ERROR,
5739 "Line %d: invalid TX queue item",
5740 line);
5741 return -1;
5742 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005743 return ret;
Hai Shalom899fcc72020-10-19 14:38:18 -07005744 }
5745
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005746 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5747 char *tmp = os_strchr(pos, '=');
5748 if (tmp == NULL) {
5749 if (line < 0)
5750 return -1;
5751 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5752 "'%s'", line, pos);
5753 return -1;
5754 }
5755 *tmp++ = '\0';
5756 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5757 tmp)) {
5758 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5759 "AC item", line);
5760 return -1;
5761 }
Hai Shalomc3565922019-10-28 11:58:20 -07005762 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005763 }
5764#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005765 if (line < 0)
5766 return -1;
5767 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5768 line, pos);
5769 ret = -1;
5770 }
5771
5772 return ret;
5773}