blob: 47d9e6dbb5349dc70478d89452c25431f36d5e6a [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;
696 else {
697 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
698 line, start);
699 errors++;
700 }
701
702 if (last)
703 break;
704 start = end + 1;
705 }
706 os_free(buf);
707
708 if (val == 0) {
709 wpa_printf(MSG_ERROR,
710 "Line %d: no proto values configured.", line);
711 errors++;
712 }
713
Dmitry Shmidte4663042016-04-04 10:07:49 -0700714 if (!errors && ssid->proto == val)
715 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
717 ssid->proto = val;
718 return errors ? -1 : 0;
719}
720
721
722#ifndef NO_CONFIG_WRITE
723static char * wpa_config_write_proto(const struct parse_data *data,
724 struct wpa_ssid *ssid)
725{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700726 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 char *buf, *pos, *end;
728
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800729 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 if (buf == NULL)
731 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800732 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733
734 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700735 ret = os_snprintf(pos, end - pos, "%sWPA",
736 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800737 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 return buf;
739 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 }
741
742 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700743 ret = os_snprintf(pos, end - pos, "%sRSN",
744 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800745 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746 return buf;
747 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 }
749
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700750 if (pos == buf) {
751 os_free(buf);
752 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800753 }
754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 return buf;
756}
757#endif /* NO_CONFIG_WRITE */
758
759
760static int wpa_config_parse_key_mgmt(const struct parse_data *data,
761 struct wpa_ssid *ssid, int line,
762 const char *value)
763{
764 int val = 0, last, errors = 0;
765 char *start, *end, *buf;
766
767 buf = os_strdup(value);
768 if (buf == NULL)
769 return -1;
770 start = buf;
771
772 while (*start != '\0') {
773 while (*start == ' ' || *start == '\t')
774 start++;
775 if (*start == '\0')
776 break;
777 end = start;
778 while (*end != ' ' && *end != '\t' && *end != '\0')
779 end++;
780 last = *end == '\0';
781 *end = '\0';
782 if (os_strcmp(start, "WPA-PSK") == 0)
783 val |= WPA_KEY_MGMT_PSK;
784 else if (os_strcmp(start, "WPA-EAP") == 0)
785 val |= WPA_KEY_MGMT_IEEE8021X;
786 else if (os_strcmp(start, "IEEE8021X") == 0)
787 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
788 else if (os_strcmp(start, "NONE") == 0)
789 val |= WPA_KEY_MGMT_NONE;
790 else if (os_strcmp(start, "WPA-NONE") == 0)
791 val |= WPA_KEY_MGMT_WPA_NONE;
792#ifdef CONFIG_IEEE80211R
793 else if (os_strcmp(start, "FT-PSK") == 0)
794 val |= WPA_KEY_MGMT_FT_PSK;
795 else if (os_strcmp(start, "FT-EAP") == 0)
796 val |= WPA_KEY_MGMT_FT_IEEE8021X;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700797#ifdef CONFIG_SHA384
798 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
799 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
800#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801#endif /* CONFIG_IEEE80211R */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000802#ifdef CONFIG_SHA384
803 else if (os_strcmp(start, "WPA-EAP-SHA384") == 0)
804 val |= WPA_KEY_MGMT_IEEE8021X_SHA384;
805#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
807 val |= WPA_KEY_MGMT_PSK_SHA256;
808 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
809 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810#ifdef CONFIG_WPS
811 else if (os_strcmp(start, "WPS") == 0)
812 val |= WPA_KEY_MGMT_WPS;
813#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800814#ifdef CONFIG_SAE
815 else if (os_strcmp(start, "SAE") == 0)
816 val |= WPA_KEY_MGMT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700817 else if (os_strcmp(start, "SAE-EXT-KEY") == 0)
818 val |= WPA_KEY_MGMT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800819 else if (os_strcmp(start, "FT-SAE") == 0)
820 val |= WPA_KEY_MGMT_FT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700821 else if (os_strcmp(start, "FT-SAE-EXT-KEY") == 0)
822 val |= WPA_KEY_MGMT_FT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800823#endif /* CONFIG_SAE */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800824#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800825 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
826 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800827#endif /* CONFIG_SUITEB */
828#ifdef CONFIG_SUITEB192
829 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
830 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
831#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800832#ifdef CONFIG_FILS
833 else if (os_strcmp(start, "FILS-SHA256") == 0)
834 val |= WPA_KEY_MGMT_FILS_SHA256;
835 else if (os_strcmp(start, "FILS-SHA384") == 0)
836 val |= WPA_KEY_MGMT_FILS_SHA384;
837#ifdef CONFIG_IEEE80211R
838 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
839 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
840 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
841 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
842#endif /* CONFIG_IEEE80211R */
843#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700844#ifdef CONFIG_OWE
845 else if (os_strcmp(start, "OWE") == 0)
846 val |= WPA_KEY_MGMT_OWE;
847#endif /* CONFIG_OWE */
848#ifdef CONFIG_DPP
849 else if (os_strcmp(start, "DPP") == 0)
850 val |= WPA_KEY_MGMT_DPP;
851#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 else {
853 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
854 line, start);
855 errors++;
856 }
857
858 if (last)
859 break;
860 start = end + 1;
861 }
862 os_free(buf);
863
864 if (val == 0) {
865 wpa_printf(MSG_ERROR,
866 "Line %d: no key_mgmt values configured.", line);
867 errors++;
868 }
869
Dmitry Shmidte4663042016-04-04 10:07:49 -0700870 if (!errors && ssid->key_mgmt == val)
871 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
873 ssid->key_mgmt = val;
874 return errors ? -1 : 0;
875}
876
877
878#ifndef NO_CONFIG_WRITE
879static char * wpa_config_write_key_mgmt(const struct parse_data *data,
880 struct wpa_ssid *ssid)
881{
882 char *buf, *pos, *end;
883 int ret;
884
Dmitry Shmidt96571392013-10-14 12:54:46 -0700885 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 if (buf == NULL)
887 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700888 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
890 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
891 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
892 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800893 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 end[-1] = '\0';
895 return buf;
896 }
897 pos += ret;
898 }
899
900 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
901 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
902 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800903 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 end[-1] = '\0';
905 return buf;
906 }
907 pos += ret;
908 }
909
910 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
911 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
912 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800913 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 end[-1] = '\0';
915 return buf;
916 }
917 pos += ret;
918 }
919
920 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
921 ret = os_snprintf(pos, end - pos, "%sNONE",
922 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800923 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 end[-1] = '\0';
925 return buf;
926 }
927 pos += ret;
928 }
929
930 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
931 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
932 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800933 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 end[-1] = '\0';
935 return buf;
936 }
937 pos += ret;
938 }
939
940#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700941 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
942 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
943 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800944 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700945 end[-1] = '\0';
946 return buf;
947 }
948 pos += ret;
949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
Dmitry Shmidt96571392013-10-14 12:54:46 -0700951 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
952 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
953 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800954 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700955 end[-1] = '\0';
956 return buf;
957 }
958 pos += ret;
959 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700960
961#ifdef CONFIG_SHA384
962 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
963 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
964 pos == buf ? "" : " ");
965 if (os_snprintf_error(end - pos, ret)) {
966 end[-1] = '\0';
967 return buf;
968 }
969 pos += ret;
970 }
971#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972#endif /* CONFIG_IEEE80211R */
973
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000974#ifdef CONFIG_SHA384
975 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
976 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA384",
977 pos == buf ? "" : " ");
978 if (os_snprintf_error(end - pos, ret)) {
979 end[-1] = '\0';
980 return buf;
981 }
982 pos += ret;
983 }
984#endif /* CONFIG_SHA384 */
985
Dmitry Shmidt96571392013-10-14 12:54:46 -0700986 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
987 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
988 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800989 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700990 end[-1] = '\0';
991 return buf;
992 }
993 pos += ret;
994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995
Dmitry Shmidt96571392013-10-14 12:54:46 -0700996 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
997 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
998 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800999 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001000 end[-1] = '\0';
1001 return buf;
1002 }
1003 pos += ret;
1004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005
1006#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -07001007 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1008 ret = os_snprintf(pos, end - pos, "%sWPS",
1009 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001010 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001011 end[-1] = '\0';
1012 return buf;
1013 }
1014 pos += ret;
1015 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016#endif /* CONFIG_WPS */
1017
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001018#ifdef CONFIG_SAE
1019 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1020 ret = os_snprintf(pos, end - pos, "%sSAE",
1021 pos == buf ? "" : " ");
1022 if (os_snprintf_error(end - pos, ret)) {
1023 end[-1] = '\0';
1024 return buf;
1025 }
1026 pos += ret;
1027 }
1028
Sunil Ravi89eba102022-09-13 21:04:37 -07001029 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1030 ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
1031 pos == buf ? "" : " ");
1032 if (os_snprintf_error(end - pos, ret)) {
1033 end[-1] = '\0';
1034 return buf;
1035 }
1036 pos += ret;
1037 }
1038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001039 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1040 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1041 pos == buf ? "" : " ");
1042 if (os_snprintf_error(end - pos, ret)) {
1043 end[-1] = '\0';
1044 return buf;
1045 }
1046 pos += ret;
1047 }
Sunil Ravi89eba102022-09-13 21:04:37 -07001048
1049 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1050 ret = os_snprintf(pos, end - pos, "%sFT-SAE-EXT-KEY",
1051 pos == buf ? "" : " ");
1052 if (os_snprintf_error(end - pos, ret)) {
1053 end[-1] = '\0';
1054 return buf;
1055 }
1056 pos += ret;
1057 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001058#endif /* CONFIG_SAE */
1059
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001060#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001061 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1062 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1063 pos == buf ? "" : " ");
1064 if (os_snprintf_error(end - pos, ret)) {
1065 end[-1] = '\0';
1066 return buf;
1067 }
1068 pos += ret;
1069 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001070#endif /* CONFIG_SUITEB */
1071
1072#ifdef CONFIG_SUITEB192
1073 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1074 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1075 pos == buf ? "" : " ");
1076 if (os_snprintf_error(end - pos, ret)) {
1077 end[-1] = '\0';
1078 return buf;
1079 }
1080 pos += ret;
1081 }
1082#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001083
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001084#ifdef CONFIG_FILS
1085 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1086 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1087 pos == buf ? "" : " ");
1088 if (os_snprintf_error(end - pos, ret)) {
1089 end[-1] = '\0';
1090 return buf;
1091 }
1092 pos += ret;
1093 }
1094 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1095 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1096 pos == buf ? "" : " ");
1097 if (os_snprintf_error(end - pos, ret)) {
1098 end[-1] = '\0';
1099 return buf;
1100 }
1101 pos += ret;
1102 }
1103#ifdef CONFIG_IEEE80211R
1104 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1105 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1106 pos == buf ? "" : " ");
1107 if (os_snprintf_error(end - pos, ret)) {
1108 end[-1] = '\0';
1109 return buf;
1110 }
1111 pos += ret;
1112 }
1113 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1114 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1115 pos == buf ? "" : " ");
1116 if (os_snprintf_error(end - pos, ret)) {
1117 end[-1] = '\0';
1118 return buf;
1119 }
1120 pos += ret;
1121 }
1122#endif /* CONFIG_IEEE80211R */
1123#endif /* CONFIG_FILS */
1124
Hai Shalom74f70d42019-02-11 14:42:39 -08001125#ifdef CONFIG_DPP
1126 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1127 ret = os_snprintf(pos, end - pos, "%sDPP",
1128 pos == buf ? "" : " ");
1129 if (os_snprintf_error(end - pos, ret)) {
1130 end[-1] = '\0';
1131 return buf;
1132 }
1133 pos += ret;
1134 }
1135#endif /* CONFIG_DPP */
1136
1137#ifdef CONFIG_OWE
1138 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1139 ret = os_snprintf(pos, end - pos, "%sOWE",
1140 pos == buf ? "" : " ");
1141 if (os_snprintf_error(end - pos, ret)) {
1142 end[-1] = '\0';
1143 return buf;
1144 }
1145 pos += ret;
1146 }
1147#endif /* CONFIG_OWE */
1148
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001149 if (pos == buf) {
1150 os_free(buf);
1151 buf = NULL;
1152 }
1153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 return buf;
1155}
1156#endif /* NO_CONFIG_WRITE */
1157
1158
1159static int wpa_config_parse_cipher(int line, const char *value)
1160{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001161#ifdef CONFIG_NO_WPA
1162 return -1;
1163#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001164 int val = wpa_parse_cipher(value);
1165 if (val < 0) {
1166 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1167 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 if (val == 0) {
1171 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1172 line);
1173 return -1;
1174 }
1175 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001176#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177}
1178
1179
1180#ifndef NO_CONFIG_WRITE
1181static char * wpa_config_write_cipher(int cipher)
1182{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001183#ifdef CONFIG_NO_WPA
1184 return NULL;
1185#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001186 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 if (buf == NULL)
1188 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001190 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1191 os_free(buf);
1192 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 }
1194
1195 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001196#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197}
1198#endif /* NO_CONFIG_WRITE */
1199
1200
1201static int wpa_config_parse_pairwise(const struct parse_data *data,
1202 struct wpa_ssid *ssid, int line,
1203 const char *value)
1204{
1205 int val;
1206 val = wpa_config_parse_cipher(line, value);
1207 if (val == -1)
1208 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001209 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1211 "(0x%x).", line, val);
1212 return -1;
1213 }
1214
Dmitry Shmidte4663042016-04-04 10:07:49 -07001215 if (ssid->pairwise_cipher == val)
1216 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1218 ssid->pairwise_cipher = val;
1219 return 0;
1220}
1221
1222
1223#ifndef NO_CONFIG_WRITE
1224static char * wpa_config_write_pairwise(const struct parse_data *data,
1225 struct wpa_ssid *ssid)
1226{
1227 return wpa_config_write_cipher(ssid->pairwise_cipher);
1228}
1229#endif /* NO_CONFIG_WRITE */
1230
1231
1232static int wpa_config_parse_group(const struct parse_data *data,
1233 struct wpa_ssid *ssid, int line,
1234 const char *value)
1235{
1236 int val;
1237 val = wpa_config_parse_cipher(line, value);
1238 if (val == -1)
1239 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001240
1241 /*
1242 * Backwards compatibility - filter out WEP ciphers that were previously
1243 * allowed.
1244 */
1245 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1246
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001247 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1249 "(0x%x).", line, val);
1250 return -1;
1251 }
1252
Dmitry Shmidte4663042016-04-04 10:07:49 -07001253 if (ssid->group_cipher == val)
1254 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1256 ssid->group_cipher = val;
1257 return 0;
1258}
1259
1260
1261#ifndef NO_CONFIG_WRITE
1262static char * wpa_config_write_group(const struct parse_data *data,
1263 struct wpa_ssid *ssid)
1264{
1265 return wpa_config_write_cipher(ssid->group_cipher);
1266}
1267#endif /* NO_CONFIG_WRITE */
1268
1269
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001270static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1271 struct wpa_ssid *ssid, int line,
1272 const char *value)
1273{
1274 int val;
1275
1276 val = wpa_config_parse_cipher(line, value);
1277 if (val == -1)
1278 return -1;
1279
1280 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1281 wpa_printf(MSG_ERROR,
1282 "Line %d: not allowed group management cipher (0x%x).",
1283 line, val);
1284 return -1;
1285 }
1286
1287 if (ssid->group_mgmt_cipher == val)
1288 return 1;
1289 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1290 ssid->group_mgmt_cipher = val;
1291 return 0;
1292}
1293
1294
1295#ifndef NO_CONFIG_WRITE
1296static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1297 struct wpa_ssid *ssid)
1298{
1299 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1300}
1301#endif /* NO_CONFIG_WRITE */
1302
1303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304static int wpa_config_parse_auth_alg(const struct parse_data *data,
1305 struct wpa_ssid *ssid, int line,
1306 const char *value)
1307{
1308 int val = 0, last, errors = 0;
1309 char *start, *end, *buf;
1310
1311 buf = os_strdup(value);
1312 if (buf == NULL)
1313 return -1;
1314 start = buf;
1315
1316 while (*start != '\0') {
1317 while (*start == ' ' || *start == '\t')
1318 start++;
1319 if (*start == '\0')
1320 break;
1321 end = start;
1322 while (*end != ' ' && *end != '\t' && *end != '\0')
1323 end++;
1324 last = *end == '\0';
1325 *end = '\0';
1326 if (os_strcmp(start, "OPEN") == 0)
1327 val |= WPA_AUTH_ALG_OPEN;
1328 else if (os_strcmp(start, "SHARED") == 0)
1329 val |= WPA_AUTH_ALG_SHARED;
1330 else if (os_strcmp(start, "LEAP") == 0)
1331 val |= WPA_AUTH_ALG_LEAP;
1332 else {
1333 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1334 line, start);
1335 errors++;
1336 }
1337
1338 if (last)
1339 break;
1340 start = end + 1;
1341 }
1342 os_free(buf);
1343
1344 if (val == 0) {
1345 wpa_printf(MSG_ERROR,
1346 "Line %d: no auth_alg values configured.", line);
1347 errors++;
1348 }
1349
Dmitry Shmidte4663042016-04-04 10:07:49 -07001350 if (!errors && ssid->auth_alg == val)
1351 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1353 ssid->auth_alg = val;
1354 return errors ? -1 : 0;
1355}
1356
1357
1358#ifndef NO_CONFIG_WRITE
1359static char * wpa_config_write_auth_alg(const struct parse_data *data,
1360 struct wpa_ssid *ssid)
1361{
1362 char *buf, *pos, *end;
1363 int ret;
1364
1365 pos = buf = os_zalloc(30);
1366 if (buf == NULL)
1367 return NULL;
1368 end = buf + 30;
1369
1370 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1371 ret = os_snprintf(pos, end - pos, "%sOPEN",
1372 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001373 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 end[-1] = '\0';
1375 return buf;
1376 }
1377 pos += ret;
1378 }
1379
1380 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1381 ret = os_snprintf(pos, end - pos, "%sSHARED",
1382 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001383 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 end[-1] = '\0';
1385 return buf;
1386 }
1387 pos += ret;
1388 }
1389
1390 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1391 ret = os_snprintf(pos, end - pos, "%sLEAP",
1392 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001393 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 end[-1] = '\0';
1395 return buf;
1396 }
1397 pos += ret;
1398 }
1399
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001400 if (pos == buf) {
1401 os_free(buf);
1402 buf = NULL;
1403 }
1404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 return buf;
1406}
1407#endif /* NO_CONFIG_WRITE */
1408
1409
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001410static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411{
1412 int *freqs;
1413 size_t used, len;
1414 const char *pos;
1415
1416 used = 0;
1417 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001418 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 if (freqs == NULL)
1420 return NULL;
1421
1422 pos = value;
1423 while (pos) {
1424 while (*pos == ' ')
1425 pos++;
1426 if (used == len) {
1427 int *n;
1428 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001429 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 if (n == NULL) {
1431 os_free(freqs);
1432 return NULL;
1433 }
1434 for (i = len; i <= len * 2; i++)
1435 n[i] = 0;
1436 freqs = n;
1437 len *= 2;
1438 }
1439
1440 freqs[used] = atoi(pos);
1441 if (freqs[used] == 0)
1442 break;
1443 used++;
1444 pos = os_strchr(pos + 1, ' ');
1445 }
1446
1447 return freqs;
1448}
1449
1450
1451static int wpa_config_parse_scan_freq(const struct parse_data *data,
1452 struct wpa_ssid *ssid, int line,
1453 const char *value)
1454{
1455 int *freqs;
1456
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001457 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 if (freqs == NULL)
1459 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001460 if (freqs[0] == 0) {
1461 os_free(freqs);
1462 freqs = NULL;
1463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 os_free(ssid->scan_freq);
1465 ssid->scan_freq = freqs;
1466
1467 return 0;
1468}
1469
1470
1471static int wpa_config_parse_freq_list(const struct parse_data *data,
1472 struct wpa_ssid *ssid, int line,
1473 const char *value)
1474{
1475 int *freqs;
1476
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001477 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (freqs == NULL)
1479 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001480 if (freqs[0] == 0) {
1481 os_free(freqs);
1482 freqs = NULL;
1483 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 os_free(ssid->freq_list);
1485 ssid->freq_list = freqs;
1486
1487 return 0;
1488}
1489
1490
1491#ifndef NO_CONFIG_WRITE
1492static char * wpa_config_write_freqs(const struct parse_data *data,
1493 const int *freqs)
1494{
1495 char *buf, *pos, *end;
1496 int i, ret;
1497 size_t count;
1498
1499 if (freqs == NULL)
1500 return NULL;
1501
1502 count = 0;
1503 for (i = 0; freqs[i]; i++)
1504 count++;
1505
1506 pos = buf = os_zalloc(10 * count + 1);
1507 if (buf == NULL)
1508 return NULL;
1509 end = buf + 10 * count + 1;
1510
1511 for (i = 0; freqs[i]; i++) {
1512 ret = os_snprintf(pos, end - pos, "%s%u",
1513 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001514 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 end[-1] = '\0';
1516 return buf;
1517 }
1518 pos += ret;
1519 }
1520
1521 return buf;
1522}
1523
1524
1525static char * wpa_config_write_scan_freq(const struct parse_data *data,
1526 struct wpa_ssid *ssid)
1527{
1528 return wpa_config_write_freqs(data, ssid->scan_freq);
1529}
1530
1531
1532static char * wpa_config_write_freq_list(const struct parse_data *data,
1533 struct wpa_ssid *ssid)
1534{
1535 return wpa_config_write_freqs(data, ssid->freq_list);
1536}
1537#endif /* NO_CONFIG_WRITE */
1538
1539
1540#ifdef IEEE8021X_EAPOL
1541static int wpa_config_parse_eap(const struct parse_data *data,
1542 struct wpa_ssid *ssid, int line,
1543 const char *value)
1544{
1545 int last, errors = 0;
1546 char *start, *end, *buf;
1547 struct eap_method_type *methods = NULL, *tmp;
1548 size_t num_methods = 0;
1549
1550 buf = os_strdup(value);
1551 if (buf == NULL)
1552 return -1;
1553 start = buf;
1554
1555 while (*start != '\0') {
1556 while (*start == ' ' || *start == '\t')
1557 start++;
1558 if (*start == '\0')
1559 break;
1560 end = start;
1561 while (*end != ' ' && *end != '\t' && *end != '\0')
1562 end++;
1563 last = *end == '\0';
1564 *end = '\0';
1565 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001566 methods = os_realloc_array(methods, num_methods + 1,
1567 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 if (methods == NULL) {
1569 os_free(tmp);
1570 os_free(buf);
1571 return -1;
1572 }
1573 methods[num_methods].method = eap_peer_get_type(
1574 start, &methods[num_methods].vendor);
1575 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1576 methods[num_methods].method == EAP_TYPE_NONE) {
1577 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1578 "'%s'", line, start);
1579 wpa_printf(MSG_ERROR, "You may need to add support for"
1580 " this EAP method during wpa_supplicant\n"
1581 "build time configuration.\n"
1582 "See README for more information.");
1583 errors++;
1584 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1585 methods[num_methods].method == EAP_TYPE_LEAP)
1586 ssid->leap++;
1587 else
1588 ssid->non_leap++;
1589 num_methods++;
1590 if (last)
1591 break;
1592 start = end + 1;
1593 }
1594 os_free(buf);
1595
1596 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001597 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 if (methods == NULL) {
1599 os_free(tmp);
1600 return -1;
1601 }
1602 methods[num_methods].vendor = EAP_VENDOR_IETF;
1603 methods[num_methods].method = EAP_TYPE_NONE;
1604 num_methods++;
1605
Dmitry Shmidte4663042016-04-04 10:07:49 -07001606 if (!errors && ssid->eap.eap_methods) {
1607 struct eap_method_type *prev_m;
1608 size_t i, j, prev_methods, match = 0;
1609
1610 prev_m = ssid->eap.eap_methods;
1611 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1612 prev_m[i].method != EAP_TYPE_NONE; i++) {
1613 /* Count the methods */
1614 }
1615 prev_methods = i + 1;
1616
1617 for (i = 0; prev_methods == num_methods && i < prev_methods;
1618 i++) {
1619 for (j = 0; j < num_methods; j++) {
1620 if (prev_m[i].vendor == methods[j].vendor &&
1621 prev_m[i].method == methods[j].method) {
1622 match++;
1623 break;
1624 }
1625 }
1626 }
1627 if (match == num_methods) {
1628 os_free(methods);
1629 return 1;
1630 }
1631 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1633 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001634 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 ssid->eap.eap_methods = methods;
1636 return errors ? -1 : 0;
1637}
1638
1639
Dmitry Shmidt83474442015-04-15 13:47:09 -07001640#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641static char * wpa_config_write_eap(const struct parse_data *data,
1642 struct wpa_ssid *ssid)
1643{
1644 int i, ret;
1645 char *buf, *pos, *end;
1646 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1647 const char *name;
1648
1649 if (eap_methods == NULL)
1650 return NULL;
1651
1652 pos = buf = os_zalloc(100);
1653 if (buf == NULL)
1654 return NULL;
1655 end = buf + 100;
1656
1657 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1658 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1659 name = eap_get_name(eap_methods[i].vendor,
1660 eap_methods[i].method);
1661 if (name) {
1662 ret = os_snprintf(pos, end - pos, "%s%s",
1663 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001664 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 break;
1666 pos += ret;
1667 }
1668 }
1669
1670 end[-1] = '\0';
1671
1672 return buf;
1673}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001674#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675
1676
1677static int wpa_config_parse_password(const struct parse_data *data,
1678 struct wpa_ssid *ssid, int line,
1679 const char *value)
1680{
1681 u8 *hash;
1682
1683 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001684 if (!ssid->eap.password)
1685 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001687 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 ssid->eap.password = NULL;
1689 ssid->eap.password_len = 0;
1690 return 0;
1691 }
1692
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001693#ifdef CONFIG_EXT_PASSWORD
1694 if (os_strncmp(value, "ext:", 4) == 0) {
1695 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001696 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001697 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001698 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001699 ssid->eap.password = (u8 *) name;
1700 ssid->eap.password_len = os_strlen(name);
1701 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1702 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1703 return 0;
1704 }
1705#endif /* CONFIG_EXT_PASSWORD */
1706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 if (os_strncmp(value, "hash:", 5) != 0) {
1708 char *tmp;
1709 size_t res_len;
1710
1711 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001712 if (!tmp) {
1713 wpa_printf(MSG_ERROR,
1714 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 return -1;
1716 }
1717 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1718 (u8 *) tmp, res_len);
1719
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001720 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 ssid->eap.password = (u8 *) tmp;
1722 ssid->eap.password_len = res_len;
1723 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001724 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725
1726 return 0;
1727 }
1728
1729
1730 /* NtPasswordHash: hash:<32 hex digits> */
1731 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001732 wpa_printf(MSG_ERROR,
1733 "Line %d: Invalid password hash length (expected 32 hex digits)",
1734 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 return -1;
1736 }
1737
1738 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001739 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 return -1;
1741
1742 if (hexstr2bin(value + 5, hash, 16)) {
1743 os_free(hash);
1744 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1745 return -1;
1746 }
1747
1748 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1749
Dmitry Shmidte4663042016-04-04 10:07:49 -07001750 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1751 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1752 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1753 bin_clear_free(hash, 16);
1754 return 1;
1755 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001756 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 ssid->eap.password = hash;
1758 ssid->eap.password_len = 16;
1759 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001760 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761
1762 return 0;
1763}
1764
1765
Hai Shalomc3565922019-10-28 11:58:20 -07001766static int wpa_config_parse_machine_password(const struct parse_data *data,
1767 struct wpa_ssid *ssid, int line,
1768 const char *value)
1769{
1770 u8 *hash;
1771
1772 if (os_strcmp(value, "NULL") == 0) {
1773 if (!ssid->eap.machine_password)
1774 return 1; /* Already unset */
1775 wpa_printf(MSG_DEBUG,
1776 "Unset configuration string 'machine_password'");
1777 bin_clear_free(ssid->eap.machine_password,
1778 ssid->eap.machine_password_len);
1779 ssid->eap.machine_password = NULL;
1780 ssid->eap.machine_password_len = 0;
1781 return 0;
1782 }
1783
1784#ifdef CONFIG_EXT_PASSWORD
1785 if (os_strncmp(value, "ext:", 4) == 0) {
1786 char *name = os_strdup(value + 4);
1787
1788 if (!name)
1789 return -1;
1790 bin_clear_free(ssid->eap.machine_password,
1791 ssid->eap.machine_password_len);
1792 ssid->eap.machine_password = (u8 *) name;
1793 ssid->eap.machine_password_len = os_strlen(name);
1794 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1795 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1796 return 0;
1797 }
1798#endif /* CONFIG_EXT_PASSWORD */
1799
1800 if (os_strncmp(value, "hash:", 5) != 0) {
1801 char *tmp;
1802 size_t res_len;
1803
1804 tmp = wpa_config_parse_string(value, &res_len);
1805 if (!tmp) {
1806 wpa_printf(MSG_ERROR,
1807 "Line %d: failed to parse machine_password.",
1808 line);
1809 return -1;
1810 }
1811 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1812 (u8 *) tmp, res_len);
1813
1814 bin_clear_free(ssid->eap.machine_password,
1815 ssid->eap.machine_password_len);
1816 ssid->eap.machine_password = (u8 *) tmp;
1817 ssid->eap.machine_password_len = res_len;
1818 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1819 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1820
1821 return 0;
1822 }
1823
1824
1825 /* NtPasswordHash: hash:<32 hex digits> */
1826 if (os_strlen(value + 5) != 2 * 16) {
1827 wpa_printf(MSG_ERROR,
1828 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1829 line);
1830 return -1;
1831 }
1832
1833 hash = os_malloc(16);
1834 if (!hash)
1835 return -1;
1836
1837 if (hexstr2bin(value + 5, hash, 16)) {
1838 os_free(hash);
1839 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1840 line);
1841 return -1;
1842 }
1843
1844 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1845
1846 if (ssid->eap.machine_password &&
1847 ssid->eap.machine_password_len == 16 &&
1848 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1849 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1850 bin_clear_free(hash, 16);
1851 return 1;
1852 }
1853 bin_clear_free(ssid->eap.machine_password,
1854 ssid->eap.machine_password_len);
1855 ssid->eap.machine_password = hash;
1856 ssid->eap.machine_password_len = 16;
1857 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1858 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1859
1860 return 0;
1861}
1862
1863
Dmitry Shmidt83474442015-04-15 13:47:09 -07001864#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866static char * wpa_config_write_password(const struct parse_data *data,
1867 struct wpa_ssid *ssid)
1868{
1869 char *buf;
1870
Hai Shalomc3565922019-10-28 11:58:20 -07001871 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 return NULL;
1873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001874#ifdef CONFIG_EXT_PASSWORD
1875 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1876 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001877 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001878 return NULL;
1879 os_memcpy(buf, "ext:", 4);
1880 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1881 return buf;
1882 }
1883#endif /* CONFIG_EXT_PASSWORD */
1884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1886 return wpa_config_write_string(
1887 ssid->eap.password, ssid->eap.password_len);
1888 }
1889
1890 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001891 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892 return NULL;
1893
1894 os_memcpy(buf, "hash:", 5);
1895 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1896
1897 return buf;
1898}
Hai Shalomc3565922019-10-28 11:58:20 -07001899
1900
1901static char * wpa_config_write_machine_password(const struct parse_data *data,
1902 struct wpa_ssid *ssid)
1903{
1904 char *buf;
1905
1906 if (!ssid->eap.machine_password)
1907 return NULL;
1908
1909#ifdef CONFIG_EXT_PASSWORD
1910 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1911 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1912 if (!buf)
1913 return NULL;
1914 os_memcpy(buf, "ext:", 4);
1915 os_memcpy(buf + 4, ssid->eap.machine_password,
1916 ssid->eap.machine_password_len);
1917 return buf;
1918 }
1919#endif /* CONFIG_EXT_PASSWORD */
1920
1921 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1922 return wpa_config_write_string(
1923 ssid->eap.machine_password,
1924 ssid->eap.machine_password_len);
1925 }
1926
1927 buf = os_malloc(5 + 32 + 1);
1928 if (!buf)
1929 return NULL;
1930
1931 os_memcpy(buf, "hash:", 5);
1932 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1933
1934 return buf;
1935}
1936
Dmitry Shmidt83474442015-04-15 13:47:09 -07001937#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938#endif /* IEEE8021X_EAPOL */
1939
1940
Hai Shalomfdcde762020-04-02 11:19:20 -07001941#ifdef CONFIG_WEP
1942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1944 const char *value, int idx)
1945{
1946 char *buf, title[20];
1947 int res;
1948
1949 buf = wpa_config_parse_string(value, len);
1950 if (buf == NULL) {
1951 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1952 line, idx, value);
1953 return -1;
1954 }
1955 if (*len > MAX_WEP_KEY_LEN) {
1956 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1957 line, idx, value);
1958 os_free(buf);
1959 return -1;
1960 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001961 if (*len && *len != 5 && *len != 13 && *len != 16) {
1962 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1963 "this network block will be ignored",
1964 line, (unsigned int) *len);
1965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001967 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001969 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1971 return 0;
1972}
1973
1974
1975static int wpa_config_parse_wep_key0(const struct parse_data *data,
1976 struct wpa_ssid *ssid, int line,
1977 const char *value)
1978{
1979 return wpa_config_parse_wep_key(ssid->wep_key[0],
1980 &ssid->wep_key_len[0], line,
1981 value, 0);
1982}
1983
1984
1985static int wpa_config_parse_wep_key1(const struct parse_data *data,
1986 struct wpa_ssid *ssid, int line,
1987 const char *value)
1988{
1989 return wpa_config_parse_wep_key(ssid->wep_key[1],
1990 &ssid->wep_key_len[1], line,
1991 value, 1);
1992}
1993
1994
1995static int wpa_config_parse_wep_key2(const struct parse_data *data,
1996 struct wpa_ssid *ssid, int line,
1997 const char *value)
1998{
1999 return wpa_config_parse_wep_key(ssid->wep_key[2],
2000 &ssid->wep_key_len[2], line,
2001 value, 2);
2002}
2003
2004
2005static int wpa_config_parse_wep_key3(const struct parse_data *data,
2006 struct wpa_ssid *ssid, int line,
2007 const char *value)
2008{
2009 return wpa_config_parse_wep_key(ssid->wep_key[3],
2010 &ssid->wep_key_len[3], line,
2011 value, 3);
2012}
2013
2014
2015#ifndef NO_CONFIG_WRITE
2016static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2017{
2018 if (ssid->wep_key_len[idx] == 0)
2019 return NULL;
2020 return wpa_config_write_string(ssid->wep_key[idx],
2021 ssid->wep_key_len[idx]);
2022}
2023
2024
2025static char * wpa_config_write_wep_key0(const struct parse_data *data,
2026 struct wpa_ssid *ssid)
2027{
2028 return wpa_config_write_wep_key(ssid, 0);
2029}
2030
2031
2032static char * wpa_config_write_wep_key1(const struct parse_data *data,
2033 struct wpa_ssid *ssid)
2034{
2035 return wpa_config_write_wep_key(ssid, 1);
2036}
2037
2038
2039static char * wpa_config_write_wep_key2(const struct parse_data *data,
2040 struct wpa_ssid *ssid)
2041{
2042 return wpa_config_write_wep_key(ssid, 2);
2043}
2044
2045
2046static char * wpa_config_write_wep_key3(const struct parse_data *data,
2047 struct wpa_ssid *ssid)
2048{
2049 return wpa_config_write_wep_key(ssid, 3);
2050}
2051#endif /* NO_CONFIG_WRITE */
2052
Hai Shalomfdcde762020-04-02 11:19:20 -07002053#endif /* CONFIG_WEP */
2054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002056#ifdef CONFIG_P2P
2057
Dmitry Shmidt54605472013-11-08 11:10:19 -08002058static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2059 struct wpa_ssid *ssid, int line,
2060 const char *value)
2061{
2062 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2063 os_strcmp(value, "any") == 0) {
2064 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2065 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2066 return 0;
2067 }
2068 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2069 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2070 line, value);
2071 return -1;
2072 }
2073 ssid->bssid_set = 1;
2074 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2075 MAC2STR(ssid->go_p2p_dev_addr));
2076 return 0;
2077}
2078
2079
2080#ifndef NO_CONFIG_WRITE
2081static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2082 struct wpa_ssid *ssid)
2083{
2084 char *value;
2085 int res;
2086
2087 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2088 return NULL;
2089
2090 value = os_malloc(20);
2091 if (value == NULL)
2092 return NULL;
2093 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002094 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002095 os_free(value);
2096 return NULL;
2097 }
2098 value[20 - 1] = '\0';
2099 return value;
2100}
2101#endif /* NO_CONFIG_WRITE */
2102
2103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002104static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2105 struct wpa_ssid *ssid, int line,
2106 const char *value)
2107{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002108 return wpa_config_parse_addr_list(data, line, value,
2109 &ssid->p2p_client_list,
2110 &ssid->num_p2p_clients,
2111 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112}
2113
2114
Sunil Ravi876a49b2025-02-03 19:18:32 +00002115static int wpa_config_parse_p2p2_client_list(const struct parse_data *data,
2116 struct wpa_ssid *ssid, int line,
2117 const char *value)
2118{
2119 int *ids = wpa_config_parse_int_array(value);
2120
2121 if (!ids) {
2122 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p2_client_list '%s'",
2123 line, value);
2124 return -1;
2125 }
2126
2127 os_free(ssid->p2p2_client_list);
2128 ssid->p2p2_client_list = ids;
2129
2130 return 0;
2131}
2132
2133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002134#ifndef NO_CONFIG_WRITE
Sunil Ravi876a49b2025-02-03 19:18:32 +00002135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2137 struct wpa_ssid *ssid)
2138{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002139 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2140 ssid->num_p2p_clients,
2141 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002142}
Sunil Ravi876a49b2025-02-03 19:18:32 +00002143
2144
2145static char * wpa_config_write_p2p2_client_list(const struct parse_data *data,
2146 struct wpa_ssid *ssid)
2147{
2148 return wpa_config_write_freqs(data, ssid->p2p2_client_list);
2149}
2150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002151#endif /* NO_CONFIG_WRITE */
2152
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002153
2154static int wpa_config_parse_psk_list(const struct parse_data *data,
2155 struct wpa_ssid *ssid, int line,
2156 const char *value)
2157{
2158 struct psk_list_entry *p;
2159 const char *pos;
2160
2161 p = os_zalloc(sizeof(*p));
2162 if (p == NULL)
2163 return -1;
2164
2165 pos = value;
2166 if (os_strncmp(pos, "P2P-", 4) == 0) {
2167 p->p2p = 1;
2168 pos += 4;
2169 }
2170
2171 if (hwaddr_aton(pos, p->addr)) {
2172 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2173 line, pos);
2174 os_free(p);
2175 return -1;
2176 }
2177 pos += 17;
2178 if (*pos != '-') {
2179 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2180 line, pos);
2181 os_free(p);
2182 return -1;
2183 }
2184 pos++;
2185
2186 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2187 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2188 line, pos);
2189 os_free(p);
2190 return -1;
2191 }
2192
2193 dl_list_add(&ssid->psk_list, &p->list);
2194
2195 return 0;
2196}
2197
2198
2199#ifndef NO_CONFIG_WRITE
2200static char * wpa_config_write_psk_list(const struct parse_data *data,
2201 struct wpa_ssid *ssid)
2202{
2203 return NULL;
2204}
2205#endif /* NO_CONFIG_WRITE */
2206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002207#endif /* CONFIG_P2P */
2208
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002209
2210#ifdef CONFIG_MESH
2211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002212static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2213 struct wpa_ssid *ssid, int line,
2214 const char *value)
2215{
2216 int *rates = wpa_config_parse_int_array(value);
2217
2218 if (rates == NULL) {
2219 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2220 line, value);
2221 return -1;
2222 }
2223 if (rates[0] == 0) {
2224 os_free(rates);
2225 rates = NULL;
2226 }
2227
2228 os_free(ssid->mesh_basic_rates);
2229 ssid->mesh_basic_rates = rates;
2230
2231 return 0;
2232}
2233
2234
2235#ifndef NO_CONFIG_WRITE
2236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002237static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2238 struct wpa_ssid *ssid)
2239{
2240 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2241}
2242
2243#endif /* NO_CONFIG_WRITE */
2244
2245#endif /* CONFIG_MESH */
2246
2247
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002248#ifdef CONFIG_MACSEC
2249
2250static int wpa_config_parse_mka_cak(const struct parse_data *data,
2251 struct wpa_ssid *ssid, int line,
2252 const char *value)
2253{
Hai Shalom74f70d42019-02-11 14:42:39 -08002254 size_t len;
2255
2256 len = os_strlen(value);
2257 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2258 (len != 2 * 16 && len != 2 * 32) ||
2259 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002260 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2261 line, value);
2262 return -1;
2263 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002264 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002265 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2266
Hai Shalom74f70d42019-02-11 14:42:39 -08002267 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2268 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002269 return 0;
2270}
2271
2272
2273static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2274 struct wpa_ssid *ssid, int line,
2275 const char *value)
2276{
Hai Shalom74f70d42019-02-11 14:42:39 -08002277 size_t len;
2278
2279 len = os_strlen(value);
2280 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2281 len < 2 || /* too short */
2282 len % 2 != 0 /* not an integral number of bytes */) {
2283 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2284 line, value);
2285 return -1;
2286 }
2287 ssid->mka_ckn_len = len / 2;
2288 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002289 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2290 line, value);
2291 return -1;
2292 }
2293
2294 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2295
Hai Shalom74f70d42019-02-11 14:42:39 -08002296 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2297 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002298 return 0;
2299}
2300
2301
2302#ifndef NO_CONFIG_WRITE
2303
2304static char * wpa_config_write_mka_cak(const struct parse_data *data,
2305 struct wpa_ssid *ssid)
2306{
2307 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2308 return NULL;
2309
Hai Shalom74f70d42019-02-11 14:42:39 -08002310 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002311}
2312
2313
2314static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2315 struct wpa_ssid *ssid)
2316{
2317 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2318 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002319 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002320}
2321
2322#endif /* NO_CONFIG_WRITE */
2323
2324#endif /* CONFIG_MACSEC */
2325
2326
Hai Shalom74f70d42019-02-11 14:42:39 -08002327#ifdef CONFIG_OCV
2328
2329static int wpa_config_parse_ocv(const struct parse_data *data,
2330 struct wpa_ssid *ssid, int line,
2331 const char *value)
2332{
2333 char *end;
2334
2335 ssid->ocv = strtol(value, &end, 0);
2336 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2337 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2338 line, value);
2339 return -1;
2340 }
2341 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2342 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2343 return 0;
2344}
2345
2346
2347#ifndef NO_CONFIG_WRITE
2348static char * wpa_config_write_ocv(const struct parse_data *data,
2349 struct wpa_ssid *ssid)
2350{
2351 char *value = os_malloc(20);
2352
2353 if (!value)
2354 return NULL;
2355 os_snprintf(value, 20, "%d", ssid->ocv);
2356 value[20 - 1] = '\0';
2357 return value;
2358}
2359#endif /* NO_CONFIG_WRITE */
2360
2361#endif /* CONFIG_OCV */
2362
2363
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002364static int wpa_config_parse_peerkey(const struct parse_data *data,
2365 struct wpa_ssid *ssid, int line,
2366 const char *value)
2367{
2368 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2369 return 0;
2370}
2371
2372
2373#ifndef NO_CONFIG_WRITE
2374static char * wpa_config_write_peerkey(const struct parse_data *data,
2375 struct wpa_ssid *ssid)
2376{
2377 return NULL;
2378}
2379#endif /* NO_CONFIG_WRITE */
2380
2381
Sunil Ravi77d572f2023-01-17 23:58:31 +00002382static int wpa_config_parse_mac_value(const struct parse_data *data,
2383 struct wpa_ssid *ssid, int line,
2384 const char *value)
2385{
2386 u8 mac_value[ETH_ALEN];
2387
2388 if (hwaddr_aton(value, mac_value) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002389 if (ether_addr_equal(mac_value, ssid->mac_value))
Sunil Ravi77d572f2023-01-17 23:58:31 +00002390 return 1;
2391 os_memcpy(ssid->mac_value, mac_value, ETH_ALEN);
2392 return 0;
2393 }
2394
2395 wpa_printf(MSG_ERROR, "Line %d: Invalid MAC address '%s'",
2396 line, value);
2397 return -1;
2398}
2399
2400
2401#ifndef NO_CONFIG_WRITE
2402static char * wpa_config_write_mac_value(const struct parse_data *data,
2403 struct wpa_ssid *ssid)
2404{
2405 const size_t size = 3 * ETH_ALEN;
2406 char *value;
2407 int res;
2408
2409 if (ssid->mac_addr != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS)
2410 return NULL;
2411
2412 value = os_malloc(size);
2413 if (!value)
2414 return NULL;
2415 res = os_snprintf(value, size, MACSTR, MAC2STR(ssid->mac_value));
2416 if (os_snprintf_error(size, res)) {
2417 os_free(value);
2418 return NULL;
2419 }
2420 value[size - 1] = '\0';
2421 return value;
2422}
2423#endif /* NO_CONFIG_WRITE */
2424
2425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426/* Helper macros for network block parser */
2427
2428#ifdef OFFSET
2429#undef OFFSET
2430#endif /* OFFSET */
2431/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2432#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2433
2434/* STR: Define a string variable for an ASCII string; f = field name */
2435#ifdef NO_CONFIG_WRITE
2436#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002437#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438#else /* NO_CONFIG_WRITE */
2439#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002440#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2441 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442#endif /* NO_CONFIG_WRITE */
2443#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002444#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002446#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447
2448/* STR_LEN: Define a string variable with a separate variable for storing the
2449 * data length. Unlike STR(), this can be used to store arbitrary binary data
2450 * (i.e., even nul termination character). */
2451#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002452#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002454#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2456
2457/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2458 * explicitly specified. */
2459#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2460#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2461#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2462
2463#ifdef NO_CONFIG_WRITE
2464#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002465#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466#else /* NO_CONFIG_WRITE */
2467#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2468 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002469#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2470 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471#endif /* NO_CONFIG_WRITE */
2472
2473/* INT: Define an integer variable */
2474#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002475#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476
2477/* INT_RANGE: Define an integer variable with allowed value range */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002478#ifdef NO_CONFIG_WRITE
2479#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, OFFSET(f), \
2480 (void *) 0, (void *) (min), (void *) (max), 0
2481#else /* NO_CONFIG_WRITE */
2482#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, \
2483 wpa_config_write_int, OFFSET(f), \
2484 (void *) 0, (void *) (min), (void *) (max), 0
2485#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486
2487/* FUNC: Define a configuration variable that uses a custom function for
2488 * parsing and writing the value. */
2489#ifdef NO_CONFIG_WRITE
2490#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2491#else /* NO_CONFIG_WRITE */
2492#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2493 NULL, NULL, NULL, NULL
2494#endif /* NO_CONFIG_WRITE */
2495#define FUNC(f) _FUNC(f), 0
2496#define FUNC_KEY(f) _FUNC(f), 1
2497
2498/*
2499 * Table of network configuration variables. This table is used to parse each
2500 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2501 * that is inside a network block.
2502 *
2503 * This table is generated using the helper macros defined above and with
2504 * generous help from the C pre-processor. The field name is stored as a string
2505 * into .name and for STR and INT types, the offset of the target buffer within
2506 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2507 * offset to the field containing the length of the configuration variable.
2508 * .param3 and .param4 can be used to mark the allowed range (length for STR
2509 * and value for INT).
2510 *
2511 * For each configuration line in wpa_supplicant.conf, the parser goes through
2512 * this table and select the entry that matches with the field name. The parser
2513 * function (.parser) is then called to parse the actual value of the field.
2514 *
2515 * This kind of mechanism makes it easy to add new configuration parameters,
2516 * since only one line needs to be added into this table and into the
2517 * struct wpa_ssid definition if the new variable is either a string or
2518 * integer. More complex types will need to use their own parser and writer
2519 * functions.
2520 */
2521static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002522 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 { INT_RANGE(scan_ssid, 0, 1) },
2524 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002525 { FUNC(bssid_hint) },
Hai Shalom60840252021-02-19 19:02:11 -08002526 { FUNC(bssid_ignore) },
2527 { FUNC(bssid_accept) },
2528 { FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2529 { FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002531 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002532 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002533 { STR(sae_password_id) },
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002534 { INT(sae_pwe) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 { FUNC(proto) },
2536 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002537 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 { FUNC(pairwise) },
2539 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002540 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 { FUNC(auth_alg) },
2542 { FUNC(scan_freq) },
2543 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002544 { INT_RANGE(ht, 0, 1) },
2545 { INT_RANGE(vht, 0, 1) },
Sunil Ravi640215c2023-06-28 23:08:09 +00002546 { INT_RANGE(he, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002547 { INT_RANGE(ht40, -1, 1) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002548 { INT_RANGE(max_oper_chwidth, CONF_OPER_CHWIDTH_USE_HT,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002549 CONF_OPER_CHWIDTH_320MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002550 { INT(vht_center_freq1) },
2551 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552#ifdef IEEE8021X_EAPOL
2553 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002554 { STR_LENe(identity, identity) },
2555 { STR_LENe(anonymous_identity, anonymous_identity) },
2556 { STR_LENe(imsi_identity, imsi_identity) },
2557 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002559 { FUNC_KEY(machine_password) },
2560 { STRe(ca_cert, cert.ca_cert) },
2561 { STRe(ca_path, cert.ca_path) },
2562 { STRe(client_cert, cert.client_cert) },
2563 { STRe(private_key, cert.private_key) },
2564 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002565 { STRe(subject_match, cert.subject_match) },
2566 { STRe(check_cert_subject, cert.check_cert_subject) },
2567 { STRe(altsubject_match, cert.altsubject_match) },
2568 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2569 { STRe(domain_match, cert.domain_match) },
2570 { STRe(ca_cert2, phase2_cert.ca_cert) },
2571 { STRe(ca_path2, phase2_cert.ca_path) },
2572 { STRe(client_cert2, phase2_cert.client_cert) },
2573 { STRe(private_key2, phase2_cert.private_key) },
2574 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002575 { STRe(subject_match2, phase2_cert.subject_match) },
2576 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2577 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2578 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2579 { STRe(domain_match2, phase2_cert.domain_match) },
2580 { STRe(phase1, phase1) },
2581 { STRe(phase2, phase2) },
2582 { STRe(machine_phase2, machine_phase2) },
2583 { STRe(pcsc, pcsc) },
2584 { STR_KEYe(pin, cert.pin) },
2585 { STRe(engine_id, cert.engine_id) },
2586 { STRe(key_id, cert.key_id) },
2587 { STRe(cert_id, cert.cert_id) },
2588 { STRe(ca_cert_id, cert.ca_cert_id) },
2589 { STR_KEYe(pin2, phase2_cert.pin) },
2590 { STRe(engine_id2, phase2_cert.engine_id) },
2591 { STRe(key_id2, phase2_cert.key_id) },
2592 { STRe(cert_id2, phase2_cert.cert_id) },
2593 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2594 { INTe(engine, cert.engine) },
2595 { INTe(engine2, phase2_cert.engine) },
2596 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2597 { STRe(machine_ca_path, machine_cert.ca_path) },
2598 { STRe(machine_client_cert, machine_cert.client_cert) },
2599 { STRe(machine_private_key, machine_cert.private_key) },
2600 { STR_KEYe(machine_private_key_passwd,
2601 machine_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002602 { STRe(machine_subject_match, machine_cert.subject_match) },
2603 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2604 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2605 { STRe(machine_domain_suffix_match,
2606 machine_cert.domain_suffix_match) },
2607 { STRe(machine_domain_match, machine_cert.domain_match) },
2608 { STR_KEYe(machine_pin, machine_cert.pin) },
2609 { STRe(machine_engine_id, machine_cert.engine_id) },
2610 { STRe(machine_key_id, machine_cert.key_id) },
2611 { STRe(machine_cert_id, machine_cert.cert_id) },
2612 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2613 { INTe(machine_engine, machine_cert.engine) },
2614 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002616 { INTe(sim_num, sim_num) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002617 { STRe(imsi_privacy_cert, imsi_privacy_cert) },
2618 { STRe(imsi_privacy_attr, imsi_privacy_attr) },
Steven Liu9138d432022-11-23 22:29:05 +00002619 { INTe(strict_conservative_peer_mode, strict_conservative_peer_mode) },
Hai Shalomc3565922019-10-28 11:58:20 -07002620 { STRe(openssl_ciphers, openssl_ciphers) },
2621 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -07002623#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 { FUNC_KEY(wep_key0) },
2625 { FUNC_KEY(wep_key1) },
2626 { FUNC_KEY(wep_key2) },
2627 { FUNC_KEY(wep_key3) },
2628 { INT(wep_tx_keyidx) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002629#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 { INT(priority) },
2631#ifdef IEEE8021X_EAPOL
2632 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002633 { STRe(pac_file, pac_file) },
2634 { INTe(fragment_size, fragment_size) },
2635 { INTe(ocsp, cert.ocsp) },
2636 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002638#ifdef CONFIG_MESH
2639 { INT_RANGE(mode, 0, 5) },
2640 { INT_RANGE(no_auto_peer, 0, 1) },
Hai Shaloma20dcd72022-02-04 13:43:00 -08002641 { INT_RANGE(mesh_fwding, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002642 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002643#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002645#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 { INT_RANGE(proactive_key_caching, 0, 1) },
2647 { INT_RANGE(disabled, 0, 2) },
2648 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002650#ifdef CONFIG_OCV
2651 { FUNC(ocv) },
2652#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002653 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002655 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002656 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002657 { INT_RANGE(enable_edmg, 0, 1) },
2658 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002659#ifdef CONFIG_ACS
2660 { INT_RANGE(acs, 0, 1) },
2661#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002662#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002663 { FUNC(mesh_basic_rates) },
2664 { INT(dot11MeshMaxRetries) },
2665 { INT(dot11MeshRetryTimeout) },
2666 { INT(dot11MeshConfirmTimeout) },
2667 { INT(dot11MeshHoldingTimeout) },
2668#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 { INT(wpa_ptk_rekey) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002670 { INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002671 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002673 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002674#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002675 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676 { FUNC(p2p_client_list) },
Sunil Ravi876a49b2025-02-03 19:18:32 +00002677 { FUNC(p2p2_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002678 { FUNC(psk_list) },
Sunil Ravi876a49b2025-02-03 19:18:32 +00002679 { INT(go_dik_id) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002680#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002681#ifdef CONFIG_HT_OVERRIDES
2682 { INT_RANGE(disable_ht, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002683 { INT_RANGE(disable_ht40, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002684 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002685 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002686 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002687 { INT_RANGE(tx_stbc, -1, 1) },
2688 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002689 { INT_RANGE(disable_max_amsdu, -1, 1) },
2690 { INT_RANGE(ampdu_factor, -1, 3) },
2691 { INT_RANGE(ampdu_density, -1, 7) },
2692 { STR(ht_mcs) },
2693#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002694#ifdef CONFIG_VHT_OVERRIDES
2695 { INT_RANGE(disable_vht, 0, 1) },
2696 { INT(vht_capa) },
2697 { INT(vht_capa_mask) },
2698 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2699 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2700 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2701 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2702 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2703 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2704 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2705 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2706 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2707 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2708 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2709 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2710 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2711 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2712 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2713 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2714#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07002715#ifdef CONFIG_HE_OVERRIDES
2716 { INT_RANGE(disable_he, 0, 1)},
2717#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002718 { INT(ap_max_inactivity) },
2719 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002720 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002721#ifdef CONFIG_MACSEC
2722 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002723 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002724 { INT_RANGE(macsec_replay_protect, 0, 1) },
2725 { INT(macsec_replay_window) },
Sunil Ravi036cec52023-03-29 11:35:17 -07002726 { INT_RANGE(macsec_offload, 0, 2) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002727 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002728 { INT_RANGE(mka_priority, 0, 255) },
Sunil Ravia04bd252022-05-02 22:54:18 -07002729 { INT_RANGE(macsec_csindex, 0, 1) },
Sunil Ravi876a49b2025-02-03 19:18:32 +00002730 { INT_RANGE(macsec_icv_indicator, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002731 { FUNC_KEY(mka_cak) },
2732 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002733#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002734#ifdef CONFIG_HS20
2735 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002736 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002737#endif /* CONFIG_HS20 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002738 { INT_RANGE(mac_addr, 0, 3) },
2739 { FUNC_KEY(mac_value) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002740 { INT_RANGE(pbss, 0, 2) },
2741 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002742 { INT_RANGE(fils_dh_group, 0, 65535) },
2743#ifdef CONFIG_DPP
2744 { STR(dpp_connector) },
2745 { STR_LEN(dpp_netaccesskey) },
2746 { INT(dpp_netaccesskey_expiry) },
2747 { STR_LEN(dpp_csign) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002748 { STR_LEN(dpp_pp_key) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002749 { INT_RANGE(dpp_pfs, 0, 2) },
Sunil Ravi89eba102022-09-13 21:04:37 -07002750 { INT_RANGE(dpp_connector_privacy, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002751#endif /* CONFIG_DPP */
2752 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002753 { INT_RANGE(owe_only, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002754 { INT_RANGE(owe_ptk_workaround, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002755 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002756 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002757 { INT_RANGE(multi_ap_profile, MULTI_AP_PROFILE_1,
2758 MULTI_AP_PROFILE_MAX) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002759 { INT_RANGE(beacon_prot, 0, 1) },
2760 { INT_RANGE(transition_disable, 0, 255) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002761 { INT_RANGE(sae_pk, 0, 2) },
Sunil Ravi77d572f2023-01-17 23:58:31 +00002762 { INT_RANGE(disable_eht, 0, 1)},
Sunil Ravi036cec52023-03-29 11:35:17 -07002763 { INT_RANGE(enable_4addr_mode, 0, 1)},
Sunil Ravi7f769292024-07-23 22:21:32 +00002764 { INT_RANGE(max_idle, 0, 65535)},
2765 { INT_RANGE(ssid_protection, 0, 1)},
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002766 { INT_RANGE(rsn_overriding, 0, 2)},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767};
2768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769#undef OFFSET
2770#undef _STR
2771#undef STR
2772#undef STR_KEY
2773#undef _STR_LEN
2774#undef STR_LEN
2775#undef STR_LEN_KEY
2776#undef _STR_RANGE
2777#undef STR_RANGE
2778#undef STR_RANGE_KEY
2779#undef _INT
2780#undef INT
2781#undef INT_RANGE
2782#undef _FUNC
2783#undef FUNC
2784#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002785#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786
2787
2788/**
2789 * wpa_config_add_prio_network - Add a network to priority lists
2790 * @config: Configuration data from wpa_config_read()
2791 * @ssid: Pointer to the network configuration to be added to the list
2792 * Returns: 0 on success, -1 on failure
2793 *
2794 * This function is used to add a network block to the priority list of
2795 * networks. This must be called for each network when reading in the full
2796 * configuration. In addition, this can be used indirectly when updating
2797 * priorities by calling wpa_config_update_prio_list().
2798 */
2799int wpa_config_add_prio_network(struct wpa_config *config,
2800 struct wpa_ssid *ssid)
2801{
Hai Shalomfdcde762020-04-02 11:19:20 -07002802 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 struct wpa_ssid *prev, **nlist;
2804
2805 /*
2806 * Add to an existing priority list if one is available for the
2807 * configured priority level for this network.
2808 */
2809 for (prio = 0; prio < config->num_prio; prio++) {
2810 prev = config->pssid[prio];
2811 if (prev->priority == ssid->priority) {
2812 while (prev->pnext)
2813 prev = prev->pnext;
2814 prev->pnext = ssid;
2815 return 0;
2816 }
2817 }
2818
2819 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002820 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2821 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 if (nlist == NULL)
2823 return -1;
2824
2825 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002826 if (nlist[prio]->priority < ssid->priority) {
2827 os_memmove(&nlist[prio + 1], &nlist[prio],
2828 (config->num_prio - prio) *
2829 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832 }
2833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 nlist[prio] = ssid;
2835 config->num_prio++;
2836 config->pssid = nlist;
2837
2838 return 0;
2839}
2840
2841
2842/**
2843 * wpa_config_update_prio_list - Update network priority list
2844 * @config: Configuration data from wpa_config_read()
2845 * Returns: 0 on success, -1 on failure
2846 *
2847 * This function is called to update the priority list of networks in the
2848 * configuration when a network is being added or removed. This is also called
2849 * if a priority for a network is changed.
2850 */
2851int wpa_config_update_prio_list(struct wpa_config *config)
2852{
2853 struct wpa_ssid *ssid;
2854 int ret = 0;
2855
2856 os_free(config->pssid);
2857 config->pssid = NULL;
2858 config->num_prio = 0;
2859
2860 ssid = config->ssid;
2861 while (ssid) {
2862 ssid->pnext = NULL;
2863 if (wpa_config_add_prio_network(config, ssid) < 0)
2864 ret = -1;
2865 ssid = ssid->next;
2866 }
2867
2868 return ret;
2869}
2870
2871
2872#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002873
2874static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2875{
2876 os_free(cert->ca_cert);
2877 os_free(cert->ca_path);
2878 os_free(cert->client_cert);
2879 os_free(cert->private_key);
2880 str_clear_free(cert->private_key_passwd);
Hai Shalomc3565922019-10-28 11:58:20 -07002881 os_free(cert->subject_match);
2882 os_free(cert->check_cert_subject);
2883 os_free(cert->altsubject_match);
2884 os_free(cert->domain_suffix_match);
2885 os_free(cert->domain_match);
2886 str_clear_free(cert->pin);
2887 os_free(cert->engine_id);
2888 os_free(cert->key_id);
2889 os_free(cert->cert_id);
2890 os_free(cert->ca_cert_id);
2891}
2892
2893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894static void eap_peer_config_free(struct eap_peer_config *eap)
2895{
2896 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002897 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002899 os_free(eap->imsi_identity);
Sunil8cd6f4d2022-06-28 18:40:46 +00002900 os_free(eap->imsi_privacy_cert);
2901 os_free(eap->imsi_privacy_attr);
Hai Shalomc3565922019-10-28 11:58:20 -07002902 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002903 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002904 bin_clear_free(eap->machine_password, eap->machine_password_len);
2905 eap_peer_config_free_cert(&eap->cert);
2906 eap_peer_config_free_cert(&eap->phase2_cert);
2907 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 os_free(eap->phase1);
2909 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002910 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 os_free(eap->otp);
2913 os_free(eap->pending_req_otp);
2914 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002915 bin_clear_free(eap->new_password, eap->new_password_len);
2916 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918}
Hai Shalomc3565922019-10-28 11:58:20 -07002919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920#endif /* IEEE8021X_EAPOL */
2921
2922
2923/**
2924 * wpa_config_free_ssid - Free network/ssid configuration data
2925 * @ssid: Configuration data for the network
2926 *
2927 * This function frees all resources allocated for the network configuration
2928 * data.
2929 */
2930void wpa_config_free_ssid(struct wpa_ssid *ssid)
2931{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002932 struct psk_list_entry *psk;
2933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002935 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002936 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002937 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002938 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939#ifdef IEEE8021X_EAPOL
2940 eap_peer_config_free(&ssid->eap);
2941#endif /* IEEE8021X_EAPOL */
2942 os_free(ssid->id_str);
2943 os_free(ssid->scan_freq);
2944 os_free(ssid->freq_list);
2945 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002946 os_free(ssid->p2p_client_list);
Sunil Ravi876a49b2025-02-03 19:18:32 +00002947 os_free(ssid->p2p2_client_list);
Hai Shalom60840252021-02-19 19:02:11 -08002948 os_free(ssid->bssid_ignore);
2949 os_free(ssid->bssid_accept);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002950#ifdef CONFIG_HT_OVERRIDES
2951 os_free(ssid->ht_mcs);
2952#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002953#ifdef CONFIG_MESH
2954 os_free(ssid->mesh_basic_rates);
2955#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002956#ifdef CONFIG_HS20
2957 os_free(ssid->roaming_consortium_selection);
2958#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002959 os_free(ssid->dpp_connector);
2960 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2961 os_free(ssid->dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -07002962 os_free(ssid->dpp_pp_key);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002963 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2964 list))) {
2965 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002966 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002967 }
Hai Shalomc3565922019-10-28 11:58:20 -07002968#ifdef CONFIG_SAE
2969 sae_deinit_pt(ssid->pt);
2970#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002971 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972}
2973
2974
Dmitry Shmidt04949592012-07-19 12:16:46 -07002975void wpa_config_free_cred(struct wpa_cred *cred)
2976{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002977 size_t i;
2978
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002980 str_clear_free(cred->username);
2981 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002982 os_free(cred->ca_cert);
2983 os_free(cred->client_cert);
2984 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002985 str_clear_free(cred->private_key_passwd);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002986 os_free(cred->engine_id);
2987 os_free(cred->ca_cert_id);
2988 os_free(cred->cert_id);
2989 os_free(cred->key_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002990 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002991 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002992 for (i = 0; i < cred->num_domain; i++)
2993 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002994 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002995 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002996 os_free(cred->eap_method);
2997 os_free(cred->phase1);
2998 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002999 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003000 os_free(cred->roaming_partner);
3001 os_free(cred->provisioning_sp);
3002 for (i = 0; i < cred->num_req_conn_capab; i++)
3003 os_free(cred->req_conn_capab_port[i]);
3004 os_free(cred->req_conn_capab_port);
3005 os_free(cred->req_conn_capab_proto);
Sunil8cd6f4d2022-06-28 18:40:46 +00003006 os_free(cred->imsi_privacy_cert);
3007 os_free(cred->imsi_privacy_attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003008 os_free(cred);
3009}
3010
3011
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003012void wpa_config_flush_blobs(struct wpa_config *config)
3013{
3014#ifndef CONFIG_NO_CONFIG_BLOBS
3015 struct wpa_config_blob *blob, *prev;
3016
3017 blob = config->blobs;
3018 config->blobs = NULL;
3019 while (blob) {
3020 prev = blob;
3021 blob = blob->next;
3022 wpa_config_free_blob(prev);
3023 }
3024#endif /* CONFIG_NO_CONFIG_BLOBS */
3025}
3026
3027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028/**
3029 * wpa_config_free - Free configuration data
3030 * @config: Configuration data from wpa_config_read()
3031 *
3032 * This function frees all resources allocated for the configuration data by
3033 * wpa_config_read().
3034 */
3035void wpa_config_free(struct wpa_config *config)
3036{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003038 struct wpa_cred *cred, *cprev;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003039 struct wpa_dev_ik *identity, *iprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003040 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041
3042 ssid = config->ssid;
3043 while (ssid) {
3044 prev = ssid;
3045 ssid = ssid->next;
3046 wpa_config_free_ssid(prev);
3047 }
3048
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049 cred = config->cred;
3050 while (cred) {
3051 cprev = cred;
3052 cred = cred->next;
3053 wpa_config_free_cred(cprev);
3054 }
3055
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003056 identity = config->identity;
3057 while (identity) {
3058 iprev = identity;
3059 identity = identity->next;
3060 wpa_config_free_identity(iprev);
3061 }
3062
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003063 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064
Dmitry Shmidt04949592012-07-19 12:16:46 -07003065 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003066 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
3067 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 os_free(config->ctrl_interface);
3069 os_free(config->ctrl_interface_group);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003070#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 os_free(config->opensc_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003072#endif /* CONFIG_OPENSC_ENGINE_PATH */
3073#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 os_free(config->pkcs11_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003075#endif /* CONFIG_PKCS11_ENGINE_PATH */
3076#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 os_free(config->pkcs11_module_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003078#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003079 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003081 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 os_free(config->driver_param);
3083 os_free(config->device_name);
3084 os_free(config->manufacturer);
3085 os_free(config->model_name);
3086 os_free(config->model_number);
3087 os_free(config->serial_number);
3088 os_free(config->config_methods);
3089 os_free(config->p2p_ssid_postfix);
3090 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003091 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003092 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003093 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003094 os_free(config->freq_list);
Hai Shalom60840252021-02-19 19:02:11 -08003095 os_free(config->initial_freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003096 wpabuf_free(config->wps_nfc_dh_pubkey);
3097 wpabuf_free(config->wps_nfc_dh_privkey);
3098 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003099 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003100 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003101 wpabuf_free(config->ap_vendor_elements);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003102 wpabuf_free(config->ap_assocresp_elements);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003103 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003104 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003105 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003106 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003107#ifdef CONFIG_MBO
3108 os_free(config->non_pref_chan);
3109#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07003110 os_free(config->dpp_name);
3111 os_free(config->dpp_mud_url);
Sunil Ravi89eba102022-09-13 21:04:37 -07003112 os_free(config->dpp_extra_conf_req_name);
3113 os_free(config->dpp_extra_conf_req_value);
Sunil Ravic0f5d412024-09-11 22:12:49 +00003114 wpabuf_free(config->dik);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003115 wpabuf_free(config->wfa_gen_capa_supp);
3116 wpabuf_free(config->wfa_gen_capa_cert);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 os_free(config);
3119}
3120
3121
3122/**
3123 * wpa_config_foreach_network - Iterate over each configured network
3124 * @config: Configuration data from wpa_config_read()
3125 * @func: Callback function to process each network
3126 * @arg: Opaque argument to pass to callback function
3127 *
3128 * Iterate over the set of configured networks calling the specified
3129 * function for each item. We guard against callbacks removing the
3130 * supplied network.
3131 */
3132void wpa_config_foreach_network(struct wpa_config *config,
3133 void (*func)(void *, struct wpa_ssid *),
3134 void *arg)
3135{
3136 struct wpa_ssid *ssid, *next;
3137
3138 ssid = config->ssid;
3139 while (ssid) {
3140 next = ssid->next;
3141 func(arg, ssid);
3142 ssid = next;
3143 }
3144}
3145
3146
3147/**
3148 * wpa_config_get_network - Get configured network based on id
3149 * @config: Configuration data from wpa_config_read()
3150 * @id: Unique network id to search for
3151 * Returns: Network configuration or %NULL if not found
3152 */
3153struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3154{
3155 struct wpa_ssid *ssid;
3156
3157 ssid = config->ssid;
3158 while (ssid) {
3159 if (id == ssid->id)
3160 break;
3161 ssid = ssid->next;
3162 }
3163
3164 return ssid;
3165}
3166
3167
Sunil Ravi876a49b2025-02-03 19:18:32 +00003168#ifdef CONFIG_P2P
3169/**
3170 * wpa_config_get_network_with_dik_id - Get configured network based on ID of
3171 * device identity block
3172 * @config: Configuration data from wpa_config_read()
3173 * @dik_id: DIK ID to search for
3174 * Returns: Network configuration or %NULL if not found
3175 */
3176struct wpa_ssid * wpa_config_get_network_with_dik_id(struct wpa_config *config,
3177 int dik_id)
3178{
3179 struct wpa_ssid *ssid;
3180
3181 for (ssid = config->ssid; ssid; ssid = ssid->next) {
3182 if (ssid->disabled != 2)
3183 continue;
3184
3185 if (ssid->go_dik_id == dik_id)
3186 return ssid;
3187
3188 if (int_array_includes(ssid->p2p2_client_list, dik_id))
3189 return ssid;
3190 }
3191
3192 return NULL;
3193}
3194#endif /* CONFIG_P2P */
3195
3196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197/**
3198 * wpa_config_add_network - Add a new network with empty configuration
3199 * @config: Configuration data from wpa_config_read()
3200 * Returns: The new network configuration or %NULL if operation failed
3201 */
3202struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3203{
3204 int id;
3205 struct wpa_ssid *ssid, *last = NULL;
3206
3207 id = -1;
3208 ssid = config->ssid;
3209 while (ssid) {
3210 if (ssid->id > id)
3211 id = ssid->id;
3212 last = ssid;
3213 ssid = ssid->next;
3214 }
3215 id++;
3216
3217 ssid = os_zalloc(sizeof(*ssid));
3218 if (ssid == NULL)
3219 return NULL;
3220 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003221 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 if (last)
3223 last->next = ssid;
3224 else
3225 config->ssid = ssid;
3226
3227 wpa_config_update_prio_list(config);
3228
3229 return ssid;
3230}
3231
3232
3233/**
3234 * wpa_config_remove_network - Remove a configured network based on id
3235 * @config: Configuration data from wpa_config_read()
3236 * @id: Unique network id to search for
3237 * Returns: 0 on success, or -1 if the network was not found
3238 */
3239int wpa_config_remove_network(struct wpa_config *config, int id)
3240{
3241 struct wpa_ssid *ssid, *prev = NULL;
3242
3243 ssid = config->ssid;
3244 while (ssid) {
3245 if (id == ssid->id)
3246 break;
3247 prev = ssid;
3248 ssid = ssid->next;
3249 }
3250
3251 if (ssid == NULL)
3252 return -1;
3253
3254 if (prev)
3255 prev->next = ssid->next;
3256 else
3257 config->ssid = ssid->next;
3258
3259 wpa_config_update_prio_list(config);
3260 wpa_config_free_ssid(ssid);
3261 return 0;
3262}
3263
3264
3265/**
3266 * wpa_config_set_network_defaults - Set network default values
3267 * @ssid: Pointer to network configuration data
3268 */
3269void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3270{
3271 ssid->proto = DEFAULT_PROTO;
3272 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3273 ssid->group_cipher = DEFAULT_GROUP;
3274 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Hai Shalomfdcde762020-04-02 11:19:20 -07003275 ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003276 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003277 ssid->ht = 1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003278 ssid->vht = 1;
3279 ssid->he = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280#ifdef IEEE8021X_EAPOL
3281 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3282 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3283 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003284 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003286#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003287 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3288 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3289 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3290 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003291 ssid->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003292 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003293#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003294#ifdef CONFIG_HT_OVERRIDES
3295 ssid->disable_ht = DEFAULT_DISABLE_HT;
3296 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003297 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003298 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003299 ssid->tx_stbc = DEFAULT_TX_STBC;
3300 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003301 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3302 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3303 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3304#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003305#ifdef CONFIG_VHT_OVERRIDES
3306 ssid->vht_rx_mcs_nss_1 = -1;
3307 ssid->vht_rx_mcs_nss_2 = -1;
3308 ssid->vht_rx_mcs_nss_3 = -1;
3309 ssid->vht_rx_mcs_nss_4 = -1;
3310 ssid->vht_rx_mcs_nss_5 = -1;
3311 ssid->vht_rx_mcs_nss_6 = -1;
3312 ssid->vht_rx_mcs_nss_7 = -1;
3313 ssid->vht_rx_mcs_nss_8 = -1;
3314 ssid->vht_tx_mcs_nss_1 = -1;
3315 ssid->vht_tx_mcs_nss_2 = -1;
3316 ssid->vht_tx_mcs_nss_3 = -1;
3317 ssid->vht_tx_mcs_nss_4 = -1;
3318 ssid->vht_tx_mcs_nss_5 = -1;
3319 ssid->vht_tx_mcs_nss_6 = -1;
3320 ssid->vht_tx_mcs_nss_7 = -1;
3321 ssid->vht_tx_mcs_nss_8 = -1;
3322#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003323 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003324 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003325 ssid->sae_pwe = DEFAULT_SAE_PWE;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003326#ifdef CONFIG_MACSEC
3327 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3328#endif /* CONFIG_MACSEC */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003329 ssid->mac_addr = WPAS_MAC_ADDR_STYLE_NOT_SET;
Hai Shalom74f70d42019-02-11 14:42:39 -08003330 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003331 ssid->rsn_overriding = RSN_OVERRIDING_NOT_SET;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332}
3333
3334
Sunil Ravia04bd252022-05-02 22:54:18 -07003335static const char *removed_fields[] = {
3336 "dh_file",
3337 "dh_file2",
3338 "machine_dh_file",
3339 NULL
3340};
3341
3342static bool removed_field(const char *field)
3343{
3344 int i;
3345
3346 for (i = 0; removed_fields[i]; i++) {
3347 if (os_strcmp(field, removed_fields[i]) == 0)
3348 return true;
3349 }
3350
3351 return false;
3352}
3353
3354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355/**
3356 * wpa_config_set - Set a variable in network configuration
3357 * @ssid: Pointer to network configuration data
3358 * @var: Variable name, e.g., "ssid"
3359 * @value: Variable value
3360 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003361 * Returns: 0 on success with possible change in the value, 1 on success with
3362 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 *
3364 * This function can be used to set network configuration variables based on
3365 * both the configuration file and management interface input. The value
3366 * parameter must be in the same format as the text-based configuration file is
3367 * using. For example, strings are using double quotation marks.
3368 */
3369int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3370 int line)
3371{
3372 size_t i;
3373 int ret = 0;
3374
3375 if (ssid == NULL || var == NULL || value == NULL)
3376 return -1;
3377
3378 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3379 const struct parse_data *field = &ssid_fields[i];
3380 if (os_strcmp(var, field->name) != 0)
3381 continue;
3382
Dmitry Shmidte4663042016-04-04 10:07:49 -07003383 ret = field->parser(field, ssid, line, value);
3384 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385 if (line) {
3386 wpa_printf(MSG_ERROR, "Line %d: failed to "
3387 "parse %s '%s'.", line, var, value);
3388 }
3389 ret = -1;
3390 }
Hai Shalomc3565922019-10-28 11:58:20 -07003391#ifdef CONFIG_SAE
3392 if (os_strcmp(var, "ssid") == 0 ||
3393 os_strcmp(var, "psk") == 0 ||
3394 os_strcmp(var, "sae_password") == 0 ||
3395 os_strcmp(var, "sae_password_id") == 0) {
3396 sae_deinit_pt(ssid->pt);
3397 ssid->pt = NULL;
3398 }
3399#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400 break;
3401 }
3402 if (i == NUM_SSID_FIELDS) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003403 if (removed_field(var)) {
3404 wpa_printf(MSG_INFO,
3405 "Line %d: Ignore removed configuration field '%s'",
3406 line, var);
3407 return ret;
3408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 if (line) {
3410 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3411 "'%s'.", line, var);
3412 }
3413 ret = -1;
3414 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003415 ssid->was_recently_reconfigured = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416
3417 return ret;
3418}
3419
3420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003421int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3422 const char *value)
3423{
3424 size_t len;
3425 char *buf;
3426 int ret;
3427
3428 len = os_strlen(value);
3429 buf = os_malloc(len + 3);
3430 if (buf == NULL)
3431 return -1;
3432 buf[0] = '"';
3433 os_memcpy(buf + 1, value, len);
3434 buf[len + 1] = '"';
3435 buf[len + 2] = '\0';
3436 ret = wpa_config_set(ssid, var, buf, 0);
3437 os_free(buf);
3438 return ret;
3439}
3440
3441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442/**
3443 * wpa_config_get_all - Get all options from network configuration
3444 * @ssid: Pointer to network configuration data
3445 * @get_keys: Determines if keys/passwords will be included in returned list
3446 * (if they may be exported)
3447 * Returns: %NULL terminated list of all set keys and their values in the form
3448 * of [key1, val1, key2, val2, ... , NULL]
3449 *
3450 * This function can be used to get list of all configured network properties.
3451 * The caller is responsible for freeing the returned list and all its
3452 * elements.
3453 */
3454char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3455{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003456#ifdef NO_CONFIG_WRITE
3457 return NULL;
3458#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459 const struct parse_data *field;
3460 char *key, *value;
3461 size_t i;
3462 char **props;
3463 int fields_num;
3464
3465 get_keys = get_keys && ssid->export_keys;
3466
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003467 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 if (!props)
3469 return NULL;
3470
3471 fields_num = 0;
3472 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3473 field = &ssid_fields[i];
3474 if (field->key_data && !get_keys)
3475 continue;
3476 value = field->writer(field, ssid);
3477 if (value == NULL)
3478 continue;
3479 if (os_strlen(value) == 0) {
3480 os_free(value);
3481 continue;
3482 }
3483
3484 key = os_strdup(field->name);
3485 if (key == NULL) {
3486 os_free(value);
3487 goto err;
3488 }
3489
3490 props[fields_num * 2] = key;
3491 props[fields_num * 2 + 1] = value;
3492
3493 fields_num++;
3494 }
3495
3496 return props;
3497
3498err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003499 for (i = 0; props[i]; i++)
3500 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 os_free(props);
3502 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003503#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504}
3505
3506
3507#ifndef NO_CONFIG_WRITE
3508/**
3509 * wpa_config_get - Get a variable in network configuration
3510 * @ssid: Pointer to network configuration data
3511 * @var: Variable name, e.g., "ssid"
3512 * Returns: Value of the variable or %NULL on failure
3513 *
3514 * This function can be used to get network configuration variables. The
3515 * returned value is a copy of the configuration variable in text format, i.e,.
3516 * the same format that the text-based configuration file and wpa_config_set()
3517 * are using for the value. The caller is responsible for freeing the returned
3518 * value.
3519 */
3520char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3521{
3522 size_t i;
3523
3524 if (ssid == NULL || var == NULL)
3525 return NULL;
3526
3527 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3528 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003529 if (os_strcmp(var, field->name) == 0) {
3530 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003531
3532 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003533 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003534 "Found newline in value for %s; not returning it",
3535 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003536 os_free(ret);
3537 ret = NULL;
3538 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003539
Paul Stewart85c72c62016-03-03 15:33:47 -08003540 return ret;
3541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 }
3543
3544 return NULL;
3545}
3546
3547
3548/**
3549 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3550 * @ssid: Pointer to network configuration data
3551 * @var: Variable name, e.g., "ssid"
3552 * Returns: Value of the variable or %NULL on failure
3553 *
3554 * This function can be used to get network configuration variable like
3555 * wpa_config_get(). The only difference is that this functions does not expose
3556 * key/password material from the configuration. In case a key/password field
3557 * is requested, the returned value is an empty string or %NULL if the variable
3558 * is not set or "*" if the variable is set (regardless of its value). The
3559 * returned value is a copy of the configuration variable in text format, i.e,.
3560 * the same format that the text-based configuration file and wpa_config_set()
3561 * are using for the value. The caller is responsible for freeing the returned
3562 * value.
3563 */
3564char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3565{
3566 size_t i;
3567
3568 if (ssid == NULL || var == NULL)
3569 return NULL;
3570
3571 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3572 const struct parse_data *field = &ssid_fields[i];
3573 if (os_strcmp(var, field->name) == 0) {
3574 char *res = field->writer(field, ssid);
3575 if (field->key_data) {
3576 if (res && res[0]) {
3577 wpa_printf(MSG_DEBUG, "Do not allow "
3578 "key_data field to be "
3579 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003580 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 return os_strdup("*");
3582 }
3583
3584 os_free(res);
3585 return NULL;
3586 }
3587 return res;
3588 }
3589 }
3590
3591 return NULL;
3592}
3593#endif /* NO_CONFIG_WRITE */
3594
3595
3596/**
3597 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3598 * @ssid: Pointer to network configuration data
3599 *
3600 * This function must be called to update WPA PSK when either SSID or the
3601 * passphrase has changed for the network configuration.
3602 */
3603void wpa_config_update_psk(struct wpa_ssid *ssid)
3604{
3605#ifndef CONFIG_NO_PBKDF2
Sunil Ravia04bd252022-05-02 22:54:18 -07003606 if (pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3607 ssid->psk, PMK_LEN) != 0) {
3608 wpa_printf(MSG_ERROR, "Error in pbkdf2_sha1()");
3609 return;
3610 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3612 ssid->psk, PMK_LEN);
3613 ssid->psk_set = 1;
3614#endif /* CONFIG_NO_PBKDF2 */
3615}
3616
3617
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003618static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3619 const char *value)
3620{
3621 u8 *proto;
3622 int **port;
3623 int *ports, *nports;
3624 const char *pos;
3625 unsigned int num_ports;
3626
3627 proto = os_realloc_array(cred->req_conn_capab_proto,
3628 cred->num_req_conn_capab + 1, sizeof(u8));
3629 if (proto == NULL)
3630 return -1;
3631 cred->req_conn_capab_proto = proto;
3632
3633 port = os_realloc_array(cred->req_conn_capab_port,
3634 cred->num_req_conn_capab + 1, sizeof(int *));
3635 if (port == NULL)
3636 return -1;
3637 cred->req_conn_capab_port = port;
3638
3639 proto[cred->num_req_conn_capab] = atoi(value);
3640
3641 pos = os_strchr(value, ':');
3642 if (pos == NULL) {
3643 port[cred->num_req_conn_capab] = NULL;
3644 cred->num_req_conn_capab++;
3645 return 0;
3646 }
3647 pos++;
3648
3649 ports = NULL;
3650 num_ports = 0;
3651
3652 while (*pos) {
3653 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3654 if (nports == NULL) {
3655 os_free(ports);
3656 return -1;
3657 }
3658 ports = nports;
3659 ports[num_ports++] = atoi(pos);
3660
3661 pos = os_strchr(pos, ',');
3662 if (pos == NULL)
3663 break;
3664 pos++;
3665 }
3666
3667 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3668 if (nports == NULL) {
3669 os_free(ports);
3670 return -1;
3671 }
3672 ports = nports;
3673 ports[num_ports] = -1;
3674
3675 port[cred->num_req_conn_capab] = ports;
3676 cred->num_req_conn_capab++;
3677 return 0;
3678}
3679
3680
Sunil Ravi77d572f2023-01-17 23:58:31 +00003681static int
3682wpa_config_set_cred_ois(u8 cred_ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN],
3683 size_t cred_ois_len[MAX_ROAMING_CONS],
3684 unsigned int *cred_num_ois,
3685 const char *value)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003686{
Sunil Ravi77d572f2023-01-17 23:58:31 +00003687 u8 ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3688 size_t ois_len[MAX_ROAMING_CONS];
3689 unsigned int num_ois = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003690 const char *pos, *end;
3691 size_t len;
3692
Sunil Ravi77d572f2023-01-17 23:58:31 +00003693 len = os_strlen(value);
3694 if (len / 2 < 3) {
3695 wpa_printf(MSG_ERROR,
3696 "Invalid organisation identifier (OI) list: %s",
3697 value);
3698 return -1;
3699 }
3700
3701 os_memset(ois, 0, sizeof(ois));
3702 os_memset(ois_len, 0, sizeof(ois_len));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003703
3704 for (pos = value;;) {
3705 end = os_strchr(pos, ',');
3706 len = end ? (size_t) (end - pos) : os_strlen(pos);
3707 if (!end && len == 0)
3708 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003709 if (len / 2 < 3 || (len & 1) != 0 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003710 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3711 hexstr2bin(pos,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003712 ois[num_ois],
Roshan Pius3a1667e2018-07-03 15:17:14 -07003713 len / 2) < 0) {
3714 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003715 "Invalid organisation identifier (OI) entry: %s",
Roshan Pius3a1667e2018-07-03 15:17:14 -07003716 pos);
3717 return -1;
3718 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003719 ois_len[num_ois] = len / 2;
3720 num_ois++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003721
3722 if (!end)
3723 break;
3724
Sunil Ravi77d572f2023-01-17 23:58:31 +00003725 if (num_ois >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003726 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003727 "Too many OIs");
Roshan Pius3a1667e2018-07-03 15:17:14 -07003728 return -1;
3729 }
3730
Roshan Pius3a1667e2018-07-03 15:17:14 -07003731 pos = end + 1;
3732 }
3733
Sunil Ravi77d572f2023-01-17 23:58:31 +00003734 os_memcpy(cred_ois, ois, sizeof(ois));
3735 os_memcpy(cred_ois_len, ois_len, sizeof(ois_len));
3736 *cred_num_ois = num_ois;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003737
3738 return 0;
3739}
3740
3741
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3743 const char *value, int line)
3744{
3745 char *val;
3746 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003747 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003748
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003749 if (os_strcmp(var, "temporary") == 0) {
3750 cred->temporary = atoi(value);
3751 return 0;
3752 }
3753
Dmitry Shmidt04949592012-07-19 12:16:46 -07003754 if (os_strcmp(var, "priority") == 0) {
3755 cred->priority = atoi(value);
3756 return 0;
3757 }
3758
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003759 if (os_strcmp(var, "sp_priority") == 0) {
3760 int prio = atoi(value);
3761 if (prio < 0 || prio > 255)
3762 return -1;
3763 cred->sp_priority = prio;
3764 return 0;
3765 }
3766
Dmitry Shmidt04949592012-07-19 12:16:46 -07003767 if (os_strcmp(var, "pcsc") == 0) {
3768 cred->pcsc = atoi(value);
3769 return 0;
3770 }
3771
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003772 if (os_strcmp(var, "eap") == 0) {
3773 struct eap_method_type method;
3774 method.method = eap_peer_get_type(value, &method.vendor);
3775 if (method.vendor == EAP_VENDOR_IETF &&
3776 method.method == EAP_TYPE_NONE) {
3777 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3778 "for a credential", line, value);
3779 return -1;
3780 }
3781 os_free(cred->eap_method);
3782 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3783 if (cred->eap_method == NULL)
3784 return -1;
3785 os_memcpy(cred->eap_method, &method, sizeof(method));
3786 return 0;
3787 }
3788
3789 if (os_strcmp(var, "password") == 0 &&
3790 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003791 if (has_newline(value))
3792 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003793 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003794 cred->password = os_strdup(value);
3795 cred->ext_password = 1;
3796 return 0;
3797 }
3798
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003799 if (os_strcmp(var, "update_identifier") == 0) {
3800 cred->update_identifier = atoi(value);
3801 return 0;
3802 }
3803
3804 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3805 cred->min_dl_bandwidth_home = atoi(value);
3806 return 0;
3807 }
3808
3809 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3810 cred->min_ul_bandwidth_home = atoi(value);
3811 return 0;
3812 }
3813
3814 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3815 cred->min_dl_bandwidth_roaming = atoi(value);
3816 return 0;
3817 }
3818
3819 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3820 cred->min_ul_bandwidth_roaming = atoi(value);
3821 return 0;
3822 }
3823
3824 if (os_strcmp(var, "max_bss_load") == 0) {
3825 cred->max_bss_load = atoi(value);
3826 return 0;
3827 }
3828
3829 if (os_strcmp(var, "req_conn_capab") == 0)
3830 return wpa_config_set_cred_req_conn_capab(cred, value);
3831
3832 if (os_strcmp(var, "ocsp") == 0) {
3833 cred->ocsp = atoi(value);
3834 return 0;
3835 }
3836
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003837 if (os_strcmp(var, "sim_num") == 0) {
3838 cred->sim_num = atoi(value);
3839 return 0;
3840 }
3841
Hai Shaloma20dcd72022-02-04 13:43:00 -08003842 if (os_strcmp(var, "engine") == 0) {
3843 cred->engine = atoi(value);
3844 return 0;
3845 }
3846
Dmitry Shmidt04949592012-07-19 12:16:46 -07003847 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003848 if (val == NULL ||
3849 (os_strcmp(var, "excluded_ssid") != 0 &&
3850 os_strcmp(var, "roaming_consortium") != 0 &&
3851 os_strcmp(var, "required_roaming_consortium") != 0 &&
3852 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003853 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3854 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003855 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003856 return -1;
3857 }
3858
3859 if (os_strcmp(var, "realm") == 0) {
3860 os_free(cred->realm);
3861 cred->realm = val;
3862 return 0;
3863 }
3864
3865 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003866 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003867 cred->username = val;
3868 return 0;
3869 }
3870
3871 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003872 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003873 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003875 return 0;
3876 }
3877
3878 if (os_strcmp(var, "ca_cert") == 0) {
3879 os_free(cred->ca_cert);
3880 cred->ca_cert = val;
3881 return 0;
3882 }
3883
3884 if (os_strcmp(var, "client_cert") == 0) {
3885 os_free(cred->client_cert);
3886 cred->client_cert = val;
3887 return 0;
3888 }
3889
3890 if (os_strcmp(var, "private_key") == 0) {
3891 os_free(cred->private_key);
3892 cred->private_key = val;
3893 return 0;
3894 }
3895
3896 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003897 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003898 cred->private_key_passwd = val;
3899 return 0;
3900 }
3901
Hai Shaloma20dcd72022-02-04 13:43:00 -08003902 if (os_strcmp(var, "engine_id") == 0) {
3903 os_free(cred->engine_id);
3904 cred->engine_id = val;
3905 return 0;
3906 }
3907
3908 if (os_strcmp(var, "ca_cert_id") == 0) {
3909 os_free(cred->ca_cert_id);
3910 cred->ca_cert_id = val;
3911 return 0;
3912 }
3913
3914 if (os_strcmp(var, "cert_id") == 0) {
3915 os_free(cred->cert_id);
3916 cred->cert_id = val;
3917 return 0;
3918 }
3919
3920 if (os_strcmp(var, "key_id") == 0) {
3921 os_free(cred->key_id);
3922 cred->key_id = val;
3923 return 0;
3924 }
3925
Dmitry Shmidt04949592012-07-19 12:16:46 -07003926 if (os_strcmp(var, "imsi") == 0) {
3927 os_free(cred->imsi);
3928 cred->imsi = val;
3929 return 0;
3930 }
3931
3932 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003933 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003934 cred->milenage = val;
3935 return 0;
3936 }
3937
Dmitry Shmidt051af732013-10-22 13:52:46 -07003938 if (os_strcmp(var, "domain_suffix_match") == 0) {
3939 os_free(cred->domain_suffix_match);
3940 cred->domain_suffix_match = val;
3941 return 0;
3942 }
3943
Dmitry Shmidt04949592012-07-19 12:16:46 -07003944 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003945 char **new_domain;
3946 new_domain = os_realloc_array(cred->domain,
3947 cred->num_domain + 1,
3948 sizeof(char *));
3949 if (new_domain == NULL) {
3950 os_free(val);
3951 return -1;
3952 }
3953 new_domain[cred->num_domain++] = val;
3954 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955 return 0;
3956 }
3957
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003958 if (os_strcmp(var, "phase1") == 0) {
3959 os_free(cred->phase1);
3960 cred->phase1 = val;
3961 return 0;
3962 }
3963
3964 if (os_strcmp(var, "phase2") == 0) {
3965 os_free(cred->phase2);
3966 cred->phase2 = val;
3967 return 0;
3968 }
3969
3970 if (os_strcmp(var, "roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003971 if (len < 3 || len > sizeof(cred->home_ois[0])) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003972 wpa_printf(MSG_ERROR, "Line %d: invalid "
3973 "roaming_consortium length %d (3..15 "
3974 "expected)", line, (int) len);
3975 os_free(val);
3976 return -1;
3977 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003978 wpa_printf(MSG_WARNING,
3979 "Line %d: option roaming_consortium is deprecated and will be removed in the future",
3980 line);
3981 os_memcpy(cred->home_ois[0], val, len);
3982 cred->home_ois_len[0] = len;
3983 cred->num_home_ois = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003984 os_free(val);
3985 return 0;
3986 }
3987
Dmitry Shmidt051af732013-10-22 13:52:46 -07003988 if (os_strcmp(var, "required_roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003989 if (len < 3 || len > sizeof(cred->required_home_ois[0])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003990 wpa_printf(MSG_ERROR, "Line %d: invalid "
3991 "required_roaming_consortium length %d "
3992 "(3..15 expected)", line, (int) len);
3993 os_free(val);
3994 return -1;
3995 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003996 wpa_printf(MSG_WARNING,
3997 "Line %d: option required_roaming_consortium is deprecated and will be removed in the future",
3998 line);
3999 os_memcpy(cred->required_home_ois[0], val, len);
4000 cred->required_home_ois_len[0] = len;
4001 cred->num_required_home_ois = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004002 os_free(val);
4003 return 0;
4004 }
4005
Sunil Ravi77d572f2023-01-17 23:58:31 +00004006 if (os_strcmp(var, "home_ois") == 0) {
4007 res = wpa_config_set_cred_ois(cred->home_ois,
4008 cred->home_ois_len,
4009 &cred->num_home_ois,
4010 val);
4011 if (res < 0)
4012 wpa_printf(MSG_ERROR, "Line %d: invalid home_ois",
4013 line);
4014 os_free(val);
4015 return res;
4016 }
4017
4018 if (os_strcmp(var, "required_home_ois") == 0) {
4019 res = wpa_config_set_cred_ois(cred->required_home_ois,
4020 cred->required_home_ois_len,
4021 &cred->num_required_home_ois,
4022 val);
4023 if (res < 0)
4024 wpa_printf(MSG_ERROR,
4025 "Line %d: invalid required_home_ois", line);
4026 os_free(val);
4027 return res;
4028 }
4029
Roshan Pius3a1667e2018-07-03 15:17:14 -07004030 if (os_strcmp(var, "roaming_consortiums") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00004031 res = wpa_config_set_cred_ois(cred->roaming_consortiums,
4032 cred->roaming_consortiums_len,
4033 &cred->num_roaming_consortiums,
4034 val);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004035 if (res < 0)
4036 wpa_printf(MSG_ERROR,
4037 "Line %d: invalid roaming_consortiums",
4038 line);
4039 os_free(val);
4040 return res;
4041 }
4042
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004043 if (os_strcmp(var, "excluded_ssid") == 0) {
4044 struct excluded_ssid *e;
4045
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004046 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004047 wpa_printf(MSG_ERROR, "Line %d: invalid "
4048 "excluded_ssid length %d", line, (int) len);
4049 os_free(val);
4050 return -1;
4051 }
4052
4053 e = os_realloc_array(cred->excluded_ssid,
4054 cred->num_excluded_ssid + 1,
4055 sizeof(struct excluded_ssid));
4056 if (e == NULL) {
4057 os_free(val);
4058 return -1;
4059 }
4060 cred->excluded_ssid = e;
4061
4062 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
4063 os_memcpy(e->ssid, val, len);
4064 e->ssid_len = len;
4065
4066 os_free(val);
4067
4068 return 0;
4069 }
4070
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004071 if (os_strcmp(var, "roaming_partner") == 0) {
4072 struct roaming_partner *p;
4073 char *pos;
4074
4075 p = os_realloc_array(cred->roaming_partner,
4076 cred->num_roaming_partner + 1,
4077 sizeof(struct roaming_partner));
4078 if (p == NULL) {
4079 os_free(val);
4080 return -1;
4081 }
4082 cred->roaming_partner = p;
4083
4084 p = &cred->roaming_partner[cred->num_roaming_partner];
4085
4086 pos = os_strchr(val, ',');
4087 if (pos == NULL) {
4088 os_free(val);
4089 return -1;
4090 }
4091 *pos++ = '\0';
4092 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
4093 os_free(val);
4094 return -1;
4095 }
4096 os_memcpy(p->fqdn, val, pos - val);
4097
4098 p->exact_match = atoi(pos);
4099
4100 pos = os_strchr(pos, ',');
4101 if (pos == NULL) {
4102 os_free(val);
4103 return -1;
4104 }
4105 *pos++ = '\0';
4106
4107 p->priority = atoi(pos);
4108
4109 pos = os_strchr(pos, ',');
4110 if (pos == NULL) {
4111 os_free(val);
4112 return -1;
4113 }
4114 *pos++ = '\0';
4115
4116 if (os_strlen(pos) >= sizeof(p->country)) {
4117 os_free(val);
4118 return -1;
4119 }
4120 os_memcpy(p->country, pos, os_strlen(pos) + 1);
4121
4122 cred->num_roaming_partner++;
4123 os_free(val);
4124
4125 return 0;
4126 }
4127
4128 if (os_strcmp(var, "provisioning_sp") == 0) {
4129 os_free(cred->provisioning_sp);
4130 cred->provisioning_sp = val;
4131 return 0;
4132 }
4133
Sunil8cd6f4d2022-06-28 18:40:46 +00004134 if (os_strcmp(var, "imsi_privacy_cert") == 0) {
4135 os_free(cred->imsi_privacy_cert);
4136 cred->imsi_privacy_cert = val;
4137 return 0;
4138 }
4139
4140 if (os_strcmp(var, "imsi_privacy_attr") == 0) {
4141 os_free(cred->imsi_privacy_attr);
4142 cred->imsi_privacy_attr = val;
Sunil Ravia04bd252022-05-02 22:54:18 -07004143 return 0;
4144 }
4145
Steven Liu9138d432022-11-23 22:29:05 +00004146 if (os_strcmp(var, "strict_conservative_peer_mode") == 0) {
4147 cred->strict_conservative_peer_mode = atoi(val);
4148 return 0;
4149 }
4150
Dmitry Shmidt04949592012-07-19 12:16:46 -07004151 if (line) {
4152 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
4153 line, var);
4154 }
4155
4156 os_free(val);
4157
4158 return -1;
4159}
4160
4161
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004162static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004163{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004164 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004165 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004166 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004168 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004169 if (buf == NULL)
4170 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 res = os_snprintf(buf, bufsize, "%d", val);
4172 if (os_snprintf_error(bufsize, res)) {
4173 os_free(buf);
4174 buf = NULL;
4175 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004176 return buf;
4177}
4178
4179
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004180static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004181{
4182 if (str == NULL)
4183 return NULL;
4184 return os_strdup(str);
4185}
4186
4187
4188char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
4189{
4190 if (os_strcmp(var, "temporary") == 0)
4191 return alloc_int_str(cred->temporary);
4192
4193 if (os_strcmp(var, "priority") == 0)
4194 return alloc_int_str(cred->priority);
4195
4196 if (os_strcmp(var, "sp_priority") == 0)
4197 return alloc_int_str(cred->sp_priority);
4198
4199 if (os_strcmp(var, "pcsc") == 0)
4200 return alloc_int_str(cred->pcsc);
4201
4202 if (os_strcmp(var, "eap") == 0) {
4203 if (!cred->eap_method)
4204 return NULL;
4205 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4206 cred->eap_method[0].method));
4207 }
4208
4209 if (os_strcmp(var, "update_identifier") == 0)
4210 return alloc_int_str(cred->update_identifier);
4211
4212 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4213 return alloc_int_str(cred->min_dl_bandwidth_home);
4214
4215 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4216 return alloc_int_str(cred->min_ul_bandwidth_home);
4217
4218 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4219 return alloc_int_str(cred->min_dl_bandwidth_roaming);
4220
4221 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4222 return alloc_int_str(cred->min_ul_bandwidth_roaming);
4223
4224 if (os_strcmp(var, "max_bss_load") == 0)
4225 return alloc_int_str(cred->max_bss_load);
4226
4227 if (os_strcmp(var, "req_conn_capab") == 0) {
4228 unsigned int i;
4229 char *buf, *end, *pos;
4230 int ret;
4231
4232 if (!cred->num_req_conn_capab)
4233 return NULL;
4234
4235 buf = os_malloc(4000);
4236 if (buf == NULL)
4237 return NULL;
4238 pos = buf;
4239 end = pos + 4000;
4240 for (i = 0; i < cred->num_req_conn_capab; i++) {
4241 int *ports;
4242
4243 ret = os_snprintf(pos, end - pos, "%s%u",
4244 i > 0 ? "\n" : "",
4245 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004246 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004247 return buf;
4248 pos += ret;
4249
4250 ports = cred->req_conn_capab_port[i];
4251 if (ports) {
4252 int j;
4253 for (j = 0; ports[j] != -1; j++) {
4254 ret = os_snprintf(pos, end - pos,
4255 "%s%d",
4256 j > 0 ? "," : ":",
4257 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004258 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004259 return buf;
4260 pos += ret;
4261 }
4262 }
4263 }
4264
4265 return buf;
4266 }
4267
4268 if (os_strcmp(var, "ocsp") == 0)
4269 return alloc_int_str(cred->ocsp);
4270
4271 if (os_strcmp(var, "realm") == 0)
4272 return alloc_strdup(cred->realm);
4273
4274 if (os_strcmp(var, "username") == 0)
4275 return alloc_strdup(cred->username);
4276
4277 if (os_strcmp(var, "password") == 0) {
4278 if (!cred->password)
4279 return NULL;
4280 return alloc_strdup("*");
4281 }
4282
4283 if (os_strcmp(var, "ca_cert") == 0)
4284 return alloc_strdup(cred->ca_cert);
4285
4286 if (os_strcmp(var, "client_cert") == 0)
4287 return alloc_strdup(cred->client_cert);
4288
4289 if (os_strcmp(var, "private_key") == 0)
4290 return alloc_strdup(cred->private_key);
4291
4292 if (os_strcmp(var, "private_key_passwd") == 0) {
4293 if (!cred->private_key_passwd)
4294 return NULL;
4295 return alloc_strdup("*");
4296 }
4297
4298 if (os_strcmp(var, "imsi") == 0)
4299 return alloc_strdup(cred->imsi);
4300
Sunil8cd6f4d2022-06-28 18:40:46 +00004301 if (os_strcmp(var, "imsi_privacy_cert") == 0)
4302 return alloc_strdup(cred->imsi_privacy_cert);
4303
4304 if (os_strcmp(var, "imsi_privacy_attr") == 0)
4305 return alloc_strdup(cred->imsi_privacy_attr);
Sunil Ravia04bd252022-05-02 22:54:18 -07004306
Steven Liu9138d432022-11-23 22:29:05 +00004307 if (os_strcmp(var, "strict_conservative_peer_mode") == 0)
4308 return alloc_int_str(cred->strict_conservative_peer_mode);
4309
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004310 if (os_strcmp(var, "milenage") == 0) {
4311 if (!(cred->milenage))
4312 return NULL;
4313 return alloc_strdup("*");
4314 }
4315
4316 if (os_strcmp(var, "domain_suffix_match") == 0)
4317 return alloc_strdup(cred->domain_suffix_match);
4318
4319 if (os_strcmp(var, "domain") == 0) {
4320 unsigned int i;
4321 char *buf, *end, *pos;
4322 int ret;
4323
4324 if (!cred->num_domain)
4325 return NULL;
4326
4327 buf = os_malloc(4000);
4328 if (buf == NULL)
4329 return NULL;
4330 pos = buf;
4331 end = pos + 4000;
4332
4333 for (i = 0; i < cred->num_domain; i++) {
4334 ret = os_snprintf(pos, end - pos, "%s%s",
4335 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004337 return buf;
4338 pos += ret;
4339 }
4340
4341 return buf;
4342 }
4343
4344 if (os_strcmp(var, "phase1") == 0)
4345 return alloc_strdup(cred->phase1);
4346
4347 if (os_strcmp(var, "phase2") == 0)
4348 return alloc_strdup(cred->phase2);
4349
4350 if (os_strcmp(var, "roaming_consortium") == 0) {
4351 size_t buflen;
4352 char *buf;
4353
Sunil Ravi77d572f2023-01-17 23:58:31 +00004354 if (!cred->num_home_ois || !cred->home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004355 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004356 buflen = cred->home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004357 buf = os_malloc(buflen);
4358 if (buf == NULL)
4359 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004360 wpa_snprintf_hex(buf, buflen, cred->home_ois[0],
4361 cred->home_ois_len[0]);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004362 return buf;
4363 }
4364
4365 if (os_strcmp(var, "required_roaming_consortium") == 0) {
4366 size_t buflen;
4367 char *buf;
4368
Sunil Ravi77d572f2023-01-17 23:58:31 +00004369 if (!cred->num_required_home_ois ||
4370 !cred->required_home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004371 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004372 buflen = cred->required_home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004373 buf = os_malloc(buflen);
4374 if (buf == NULL)
4375 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004376 wpa_snprintf_hex(buf, buflen, cred->required_home_ois[0],
4377 cred->required_home_ois_len[0]);
4378 return buf;
4379 }
4380
4381 if (os_strcmp(var, "home_ois") == 0) {
4382 size_t buflen;
4383 char *buf, *pos;
4384 size_t i;
4385
4386 if (!cred->num_home_ois)
4387 return NULL;
4388 buflen = cred->num_home_ois * MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4389 buf = os_malloc(buflen);
4390 if (!buf)
4391 return NULL;
4392 pos = buf;
4393 for (i = 0; i < cred->num_home_ois; i++) {
4394 if (i > 0)
4395 *pos++ = ',';
4396 pos += wpa_snprintf_hex(
4397 pos, buf + buflen - pos,
4398 cred->home_ois[i],
4399 cred->home_ois_len[i]);
4400 }
4401 *pos = '\0';
4402 return buf;
4403 }
4404
4405 if (os_strcmp(var, "required_home_ois") == 0) {
4406 size_t buflen;
4407 char *buf, *pos;
4408 size_t i;
4409
4410 if (!cred->num_required_home_ois)
4411 return NULL;
4412 buflen = cred->num_required_home_ois *
4413 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4414 buf = os_malloc(buflen);
4415 if (!buf)
4416 return NULL;
4417 pos = buf;
4418 for (i = 0; i < cred->num_required_home_ois; i++) {
4419 if (i > 0)
4420 *pos++ = ',';
4421 pos += wpa_snprintf_hex(
4422 pos, buf + buflen - pos,
4423 cred->required_home_ois[i],
4424 cred->required_home_ois_len[i]);
4425 }
4426 *pos = '\0';
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004427 return buf;
4428 }
4429
Roshan Pius3a1667e2018-07-03 15:17:14 -07004430 if (os_strcmp(var, "roaming_consortiums") == 0) {
4431 size_t buflen;
4432 char *buf, *pos;
4433 size_t i;
4434
4435 if (!cred->num_roaming_consortiums)
4436 return NULL;
4437 buflen = cred->num_roaming_consortiums *
4438 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4439 buf = os_malloc(buflen);
4440 if (!buf)
4441 return NULL;
4442 pos = buf;
4443 for (i = 0; i < cred->num_roaming_consortiums; i++) {
4444 if (i > 0)
4445 *pos++ = ',';
4446 pos += wpa_snprintf_hex(
4447 pos, buf + buflen - pos,
4448 cred->roaming_consortiums[i],
4449 cred->roaming_consortiums_len[i]);
4450 }
4451 *pos = '\0';
4452 return buf;
4453 }
4454
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004455 if (os_strcmp(var, "excluded_ssid") == 0) {
4456 unsigned int i;
4457 char *buf, *end, *pos;
4458
4459 if (!cred->num_excluded_ssid)
4460 return NULL;
4461
4462 buf = os_malloc(4000);
4463 if (buf == NULL)
4464 return NULL;
4465 pos = buf;
4466 end = pos + 4000;
4467
4468 for (i = 0; i < cred->num_excluded_ssid; i++) {
4469 struct excluded_ssid *e;
4470 int ret;
4471
4472 e = &cred->excluded_ssid[i];
4473 ret = os_snprintf(pos, end - pos, "%s%s",
4474 i > 0 ? "\n" : "",
4475 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004476 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004477 return buf;
4478 pos += ret;
4479 }
4480
4481 return buf;
4482 }
4483
4484 if (os_strcmp(var, "roaming_partner") == 0) {
4485 unsigned int i;
4486 char *buf, *end, *pos;
4487
4488 if (!cred->num_roaming_partner)
4489 return NULL;
4490
4491 buf = os_malloc(4000);
4492 if (buf == NULL)
4493 return NULL;
4494 pos = buf;
4495 end = pos + 4000;
4496
4497 for (i = 0; i < cred->num_roaming_partner; i++) {
4498 struct roaming_partner *p;
4499 int ret;
4500
4501 p = &cred->roaming_partner[i];
4502 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4503 i > 0 ? "\n" : "",
4504 p->fqdn, p->exact_match, p->priority,
4505 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004506 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004507 return buf;
4508 pos += ret;
4509 }
4510
4511 return buf;
4512 }
4513
4514 if (os_strcmp(var, "provisioning_sp") == 0)
4515 return alloc_strdup(cred->provisioning_sp);
4516
4517 return NULL;
4518}
4519
4520
Dmitry Shmidt04949592012-07-19 12:16:46 -07004521struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4522{
4523 struct wpa_cred *cred;
4524
4525 cred = config->cred;
4526 while (cred) {
4527 if (id == cred->id)
4528 break;
4529 cred = cred->next;
4530 }
4531
4532 return cred;
4533}
4534
4535
4536struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4537{
4538 int id;
4539 struct wpa_cred *cred, *last = NULL;
4540
4541 id = -1;
4542 cred = config->cred;
4543 while (cred) {
4544 if (cred->id > id)
4545 id = cred->id;
4546 last = cred;
4547 cred = cred->next;
4548 }
4549 id++;
4550
4551 cred = os_zalloc(sizeof(*cred));
4552 if (cred == NULL)
4553 return NULL;
4554 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004555 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004556 if (last)
4557 last->next = cred;
4558 else
4559 config->cred = cred;
4560
4561 return cred;
4562}
4563
4564
4565int wpa_config_remove_cred(struct wpa_config *config, int id)
4566{
4567 struct wpa_cred *cred, *prev = NULL;
4568
4569 cred = config->cred;
4570 while (cred) {
4571 if (id == cred->id)
4572 break;
4573 prev = cred;
4574 cred = cred->next;
4575 }
4576
4577 if (cred == NULL)
4578 return -1;
4579
4580 if (prev)
4581 prev->next = cred->next;
4582 else
4583 config->cred = cred->next;
4584
4585 wpa_config_free_cred(cred);
4586 return 0;
4587}
4588
4589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590#ifndef CONFIG_NO_CONFIG_BLOBS
4591/**
4592 * wpa_config_get_blob - Get a named configuration blob
4593 * @config: Configuration data from wpa_config_read()
4594 * @name: Name of the blob
4595 * Returns: Pointer to blob data or %NULL if not found
4596 */
4597const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4598 const char *name)
4599{
4600 struct wpa_config_blob *blob = config->blobs;
4601
4602 while (blob) {
4603 if (os_strcmp(blob->name, name) == 0)
4604 return blob;
4605 blob = blob->next;
4606 }
4607 return NULL;
4608}
4609
4610
4611/**
4612 * wpa_config_set_blob - Set or add a named configuration blob
4613 * @config: Configuration data from wpa_config_read()
4614 * @blob: New value for the blob
4615 *
4616 * Adds a new configuration blob or replaces the current value of an existing
4617 * blob.
4618 */
4619void wpa_config_set_blob(struct wpa_config *config,
4620 struct wpa_config_blob *blob)
4621{
4622 wpa_config_remove_blob(config, blob->name);
4623 blob->next = config->blobs;
4624 config->blobs = blob;
4625}
4626
4627
4628/**
4629 * wpa_config_free_blob - Free blob data
4630 * @blob: Pointer to blob to be freed
4631 */
4632void wpa_config_free_blob(struct wpa_config_blob *blob)
4633{
4634 if (blob) {
4635 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004636 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 os_free(blob);
4638 }
4639}
4640
4641
4642/**
4643 * wpa_config_remove_blob - Remove a named configuration blob
4644 * @config: Configuration data from wpa_config_read()
4645 * @name: Name of the blob to remove
4646 * Returns: 0 if blob was removed or -1 if blob was not found
4647 */
4648int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4649{
4650 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4651
4652 while (pos) {
4653 if (os_strcmp(pos->name, name) == 0) {
4654 if (prev)
4655 prev->next = pos->next;
4656 else
4657 config->blobs = pos->next;
4658 wpa_config_free_blob(pos);
4659 return 0;
4660 }
4661 prev = pos;
4662 pos = pos->next;
4663 }
4664
4665 return -1;
4666}
4667#endif /* CONFIG_NO_CONFIG_BLOBS */
4668
4669
4670/**
4671 * wpa_config_alloc_empty - Allocate an empty configuration
4672 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4673 * socket
4674 * @driver_param: Driver parameters
4675 * Returns: Pointer to allocated configuration data or %NULL on failure
4676 */
4677struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4678 const char *driver_param)
4679{
Hai Shalom899fcc72020-10-19 14:38:18 -07004680#define ecw2cw(ecw) ((1 << (ecw)) - 1)
4681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004683 const int aCWmin = 4, aCWmax = 10;
4684 const struct hostapd_wmm_ac_params ac_bk =
4685 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4686 const struct hostapd_wmm_ac_params ac_be =
4687 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4688 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004689 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004690 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004691 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4692 const struct hostapd_tx_queue_params txq_bk =
4693 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4694 const struct hostapd_tx_queue_params txq_be =
4695 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4696 const struct hostapd_tx_queue_params txq_vi =
4697 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4698 const struct hostapd_tx_queue_params txq_vo =
4699 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4700 (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4701
4702#undef ecw2cw
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703
4704 config = os_zalloc(sizeof(*config));
4705 if (config == NULL)
4706 return NULL;
4707 config->eapol_version = DEFAULT_EAPOL_VERSION;
4708 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004709 config->user_mpm = DEFAULT_USER_MPM;
4710 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004711 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004712 config->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004713 config->dot11RSNASAERetransPeriod =
4714 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 config->fast_reauth = DEFAULT_FAST_REAUTH;
4716 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4717 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004718 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004719 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004720 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004721 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4723 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4724 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4725 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004726 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004727 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004728 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Hai Shalom60840252021-02-19 19:02:11 -08004729 config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004730 config->wmm_ac_params[0] = ac_be;
4731 config->wmm_ac_params[1] = ac_bk;
4732 config->wmm_ac_params[2] = ac_vi;
4733 config->wmm_ac_params[3] = ac_vo;
Hai Shalom899fcc72020-10-19 14:38:18 -07004734 config->tx_queue[0] = txq_vo;
4735 config->tx_queue[1] = txq_vi;
4736 config->tx_queue[2] = txq_be;
4737 config->tx_queue[3] = txq_bk;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004738 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004739 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004740 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004741 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004742 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Hai Shalomfdcde762020-04-02 11:19:20 -07004743 config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004745#ifdef CONFIG_MBO
4746 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004747 config->disassoc_imminent_rssi_threshold =
4748 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4749 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004750#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07004751 config->btm_offload = DEFAULT_BTM_OFFLOAD;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 if (ctrl_interface)
4754 config->ctrl_interface = os_strdup(ctrl_interface);
4755 if (driver_param)
4756 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004757 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004759#ifdef CONFIG_TESTING_OPTIONS
4760 config->mld_connect_band_pref = DEFAULT_MLD_CONNECT_BAND_PREF;
4761#endif /* CONFIG_TESTING_OPTIONS */
4762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 return config;
4764}
4765
4766
4767#ifndef CONFIG_NO_STDOUT_DEBUG
4768/**
4769 * wpa_config_debug_dump_networks - Debug dump of configured networks
4770 * @config: Configuration data from wpa_config_read()
4771 */
4772void wpa_config_debug_dump_networks(struct wpa_config *config)
4773{
Hai Shalomfdcde762020-04-02 11:19:20 -07004774 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 struct wpa_ssid *ssid;
4776
4777 for (prio = 0; prio < config->num_prio; prio++) {
4778 ssid = config->pssid[prio];
4779 wpa_printf(MSG_DEBUG, "Priority group %d",
4780 ssid->priority);
4781 while (ssid) {
4782 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4783 ssid->id,
4784 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4785 ssid = ssid->pnext;
4786 }
4787 }
4788}
4789#endif /* CONFIG_NO_STDOUT_DEBUG */
4790
4791
Hai Shalom899fcc72020-10-19 14:38:18 -07004792/**
4793 * Structure for global configuration parsing. This data is used to implement a
4794 * generic parser for the global interface configuration. The table of variables
4795 * is defined below in this file (global_fields[]).
4796 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797struct global_parse_data {
Hai Shalom899fcc72020-10-19 14:38:18 -07004798 /* Configuration variable name */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 char *name;
Hai Shalom899fcc72020-10-19 14:38:18 -07004800
4801 /* Parser function for this variable. The parser functions return 0 or 1
4802 * to indicate success. Value 0 indicates that the parameter value may
4803 * have changed while value 1 means that the value did not change.
4804 * Error cases (failure to parse the string) are indicated by returning
4805 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806 int (*parser)(const struct global_parse_data *data,
4807 struct wpa_config *config, int line, const char *value);
Hai Shalom899fcc72020-10-19 14:38:18 -07004808
4809 /* Getter function to print the variable in text format to buf. */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004810 int (*get)(const char *name, struct wpa_config *config, long offset,
4811 char *buf, size_t buflen, int pretty_print);
Hai Shalom899fcc72020-10-19 14:38:18 -07004812
4813 /* Variable specific parameters for the parser. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 void *param1, *param2, *param3;
Hai Shalom899fcc72020-10-19 14:38:18 -07004815
4816 /* Indicates which configuration variable has changed. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004817 unsigned int changed_flag;
4818};
4819
4820
Sunil Ravi99c035e2024-07-12 01:42:03 +00004821static int
4822wpa_global_config_parse_int_impl(const struct global_parse_data *data,
4823 struct wpa_config *config, int line,
4824 const char *pos, bool check_range)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004826 int val, *dst;
4827 char *end;
Hai Shalom899fcc72020-10-19 14:38:18 -07004828 bool same;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004831 val = strtol(pos, &end, 0);
4832 if (*end) {
4833 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4834 line, pos);
4835 return -1;
4836 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00004837
4838 if (check_range && val < (long) data->param2) {
4839 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4840 "min_value=%ld)", line, data->name, val,
4841 (long) data->param2);
4842 return -1;
4843 }
4844
4845 if (check_range && val > (long) data->param3) {
4846 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4847 "max_value=%ld)", line, data->name, val,
4848 (long) data->param3);
4849 return -1;
4850 }
4851
Hai Shalom899fcc72020-10-19 14:38:18 -07004852 same = *dst == val;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004853 *dst = val;
4854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004855 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4856
Hai Shalom899fcc72020-10-19 14:38:18 -07004857 return same;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858}
4859
4860
Sunil Ravi99c035e2024-07-12 01:42:03 +00004861static int wpa_global_config_parse_int(const struct global_parse_data *data,
4862 struct wpa_config *config, int line,
4863 const char *pos)
4864{
4865 return wpa_global_config_parse_int_impl(data, config, line, pos, false);
4866}
4867
4868
4869static int
4870wpa_global_config_parse_int_range(const struct global_parse_data *data,
4871 struct wpa_config *config, int line,
4872 const char *pos)
4873{
4874 return wpa_global_config_parse_int_impl(data, config, line, pos, true);
4875}
4876
4877
Sunil Ravi876a49b2025-02-03 19:18:32 +00004878static int wpa_global_config_parse_bool(const struct global_parse_data *data,
4879 struct wpa_config *config, int line,
4880 const char *pos)
4881{
4882 int val;
4883 bool *dst;
4884 char *end;
4885 bool same;
4886
4887 dst = (bool *) (((u8 *) config) + (long) data->param1);
4888 val = strtol(pos, &end, 0);
4889 if (*end) {
4890 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4891 line, pos);
4892 return -1;
4893 }
4894
4895 if (val < 0) {
4896 wpa_printf(MSG_ERROR,
4897 "Line %d: too small %s (value=%d min_value=0)",
4898 line, data->name, val);
4899 return -1;
4900 }
4901
4902 if (val > 1) {
4903 wpa_printf(MSG_ERROR,
4904 "Line %d: too large %s (value=%d max_value=1)",
4905 line, data->name, val);
4906 return -1;
4907 }
4908
4909 same = *dst == val;
4910 *dst = val;
4911
4912 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4913
4914 return same;
4915}
4916
4917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918static int wpa_global_config_parse_str(const struct global_parse_data *data,
4919 struct wpa_config *config, int line,
4920 const char *pos)
4921{
Hai Shalom899fcc72020-10-19 14:38:18 -07004922 size_t len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 char **dst, *tmp;
4924
4925 len = os_strlen(pos);
4926 if (data->param2 && len < (size_t) data->param2) {
4927 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4928 "min_len=%ld)", line, data->name,
4929 (unsigned long) len, (long) data->param2);
4930 return -1;
4931 }
4932
4933 if (data->param3 && len > (size_t) data->param3) {
4934 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4935 "max_len=%ld)", line, data->name,
4936 (unsigned long) len, (long) data->param3);
4937 return -1;
4938 }
4939
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004940 if (has_newline(pos)) {
4941 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4942 line, data->name);
4943 return -1;
4944 }
4945
Hai Shalom899fcc72020-10-19 14:38:18 -07004946 dst = (char **) (((u8 *) config) + (long) data->param1);
4947 if (*dst)
4948 prev_len = os_strlen(*dst);
4949 else
4950 prev_len = 0;
4951
4952 /* No change to the previously configured value */
Hai Shalom60840252021-02-19 19:02:11 -08004953 if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
Hai Shalom899fcc72020-10-19 14:38:18 -07004954 return 1;
4955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956 tmp = os_strdup(pos);
4957 if (tmp == NULL)
4958 return -1;
4959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 os_free(*dst);
4961 *dst = tmp;
4962 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4963
4964 return 0;
4965}
4966
4967
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004968static int wpa_config_process_bgscan(const struct global_parse_data *data,
4969 struct wpa_config *config, int line,
4970 const char *pos)
4971{
4972 size_t len;
4973 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004974 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004975
4976 tmp = wpa_config_parse_string(pos, &len);
4977 if (tmp == NULL) {
4978 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4979 line, data->name);
4980 return -1;
4981 }
4982
Dmitry Shmidt97672262014-02-03 13:02:54 -08004983 res = wpa_global_config_parse_str(data, config, line, tmp);
4984 os_free(tmp);
4985 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004986}
4987
4988
Dmitry Shmidt04949592012-07-19 12:16:46 -07004989static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4990 struct wpa_config *config, int line,
4991 const char *pos)
4992{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004993 struct wpabuf **dst, *tmp;
4994
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004995 tmp = wpabuf_parse_bin(pos);
4996 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004997 return -1;
4998
Dmitry Shmidt04949592012-07-19 12:16:46 -07004999 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07005000 if (wpabuf_cmp(*dst, tmp) == 0) {
5001 wpabuf_free(tmp);
5002 return 1;
5003 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005004 wpabuf_free(*dst);
5005 *dst = tmp;
5006 wpa_printf(MSG_DEBUG, "%s", data->name);
5007
5008 return 0;
5009}
5010
5011
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005012static int wpa_config_process_freq_list(const struct global_parse_data *data,
5013 struct wpa_config *config, int line,
5014 const char *value)
5015{
5016 int *freqs;
5017
5018 freqs = wpa_config_parse_int_array(value);
5019 if (freqs == NULL)
5020 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07005021 if (freqs[0] == 0) {
5022 os_free(freqs);
5023 freqs = NULL;
5024 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005025 os_free(config->freq_list);
5026 config->freq_list = freqs;
5027 return 0;
5028}
5029
5030
Hai Shalom60840252021-02-19 19:02:11 -08005031static int
5032wpa_config_process_initial_freq_list(const struct global_parse_data *data,
5033 struct wpa_config *config, int line,
5034 const char *value)
5035{
5036 int *freqs;
5037
5038 freqs = wpa_config_parse_int_array(value);
5039 if (!freqs)
5040 return -1;
5041 if (freqs[0] == 0) {
5042 os_free(freqs);
5043 freqs = NULL;
5044 }
5045 os_free(config->initial_freq_list);
5046 config->initial_freq_list = freqs;
5047 return 0;
5048}
5049
5050
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005051#ifdef CONFIG_P2P
5052static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
5053 struct wpa_config *config, int line,
5054 const char *pos)
5055{
5056 u32 *dst;
5057 struct hostapd_ip_addr addr;
5058
5059 if (hostapd_parse_ip_addr(pos, &addr) < 0)
5060 return -1;
5061 if (addr.af != AF_INET)
5062 return -1;
5063
5064 dst = (u32 *) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07005065 if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
5066 return 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005067 os_memcpy(dst, &addr.u.v4.s_addr, 4);
5068 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
5069 WPA_GET_BE32((u8 *) dst));
5070
5071 return 0;
5072}
5073#endif /* CONFIG_P2P */
5074
5075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005076static int wpa_config_process_country(const struct global_parse_data *data,
5077 struct wpa_config *config, int line,
5078 const char *pos)
5079{
5080 if (!pos[0] || !pos[1]) {
5081 wpa_printf(MSG_DEBUG, "Invalid country set");
5082 return -1;
5083 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005084 if (pos[0] == config->country[0] && pos[1] == config->country[1])
5085 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 config->country[0] = pos[0];
5087 config->country[1] = pos[1];
5088 wpa_printf(MSG_DEBUG, "country='%c%c'",
5089 config->country[0], config->country[1]);
5090 return 0;
5091}
5092
5093
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005094#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095static int wpa_config_process_load_dynamic_eap(
5096 const struct global_parse_data *data, struct wpa_config *config,
5097 int line, const char *so)
5098{
5099 int ret;
5100 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
5101 ret = eap_peer_method_load(so);
5102 if (ret == -2) {
5103 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
5104 "reloading.");
5105 } else if (ret) {
5106 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
5107 "method '%s'.", line, so);
5108 return -1;
5109 }
5110
5111 return 0;
5112}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005113#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114
5115
5116#ifdef CONFIG_WPS
5117
5118static int wpa_config_process_uuid(const struct global_parse_data *data,
5119 struct wpa_config *config, int line,
5120 const char *pos)
5121{
5122 char buf[40];
5123 if (uuid_str2bin(pos, config->uuid)) {
5124 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
5125 return -1;
5126 }
5127 uuid_bin2str(config->uuid, buf, sizeof(buf));
5128 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
5129 return 0;
5130}
5131
5132
5133static int wpa_config_process_device_type(
5134 const struct global_parse_data *data,
5135 struct wpa_config *config, int line, const char *pos)
5136{
5137 return wps_dev_type_str2bin(pos, config->device_type);
5138}
5139
5140
5141static int wpa_config_process_os_version(const struct global_parse_data *data,
5142 struct wpa_config *config, int line,
5143 const char *pos)
5144{
5145 if (hexstr2bin(pos, config->os_version, 4)) {
5146 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
5147 return -1;
5148 }
5149 wpa_printf(MSG_DEBUG, "os_version=%08x",
5150 WPA_GET_BE32(config->os_version));
5151 return 0;
5152}
5153
Dmitry Shmidt04949592012-07-19 12:16:46 -07005154
5155static int wpa_config_process_wps_vendor_ext_m1(
5156 const struct global_parse_data *data,
5157 struct wpa_config *config, int line, const char *pos)
5158{
5159 struct wpabuf *tmp;
5160 int len = os_strlen(pos) / 2;
5161 u8 *p;
5162
5163 if (!len) {
5164 wpa_printf(MSG_ERROR, "Line %d: "
5165 "invalid wps_vendor_ext_m1", line);
5166 return -1;
5167 }
5168
5169 tmp = wpabuf_alloc(len);
5170 if (tmp) {
5171 p = wpabuf_put(tmp, len);
5172
5173 if (hexstr2bin(pos, p, len)) {
5174 wpa_printf(MSG_ERROR, "Line %d: "
5175 "invalid wps_vendor_ext_m1", line);
5176 wpabuf_free(tmp);
5177 return -1;
5178 }
5179
5180 wpabuf_free(config->wps_vendor_ext_m1);
5181 config->wps_vendor_ext_m1 = tmp;
5182 } else {
5183 wpa_printf(MSG_ERROR, "Can not allocate "
5184 "memory for wps_vendor_ext_m1");
5185 return -1;
5186 }
5187
5188 return 0;
5189}
5190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191#endif /* CONFIG_WPS */
5192
5193#ifdef CONFIG_P2P
5194static int wpa_config_process_sec_device_type(
5195 const struct global_parse_data *data,
5196 struct wpa_config *config, int line, const char *pos)
5197{
5198 int idx;
5199
5200 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
5201 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
5202 "items", line);
5203 return -1;
5204 }
5205
5206 idx = config->num_sec_device_types;
5207
5208 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
5209 return -1;
5210
5211 config->num_sec_device_types++;
5212 return 0;
5213}
Dmitry Shmidt04949592012-07-19 12:16:46 -07005214
5215
5216static int wpa_config_process_p2p_pref_chan(
5217 const struct global_parse_data *data,
5218 struct wpa_config *config, int line, const char *pos)
5219{
5220 struct p2p_channel *pref = NULL, *n;
Hai Shalomfdcde762020-04-02 11:19:20 -07005221 size_t num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005222 const char *pos2;
5223 u8 op_class, chan;
5224
5225 /* format: class:chan,class:chan,... */
5226
5227 while (*pos) {
5228 op_class = atoi(pos);
5229 pos2 = os_strchr(pos, ':');
5230 if (pos2 == NULL)
5231 goto fail;
5232 pos2++;
5233 chan = atoi(pos2);
5234
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005235 n = os_realloc_array(pref, num + 1,
5236 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005237 if (n == NULL)
5238 goto fail;
5239 pref = n;
5240 pref[num].op_class = op_class;
5241 pref[num].chan = chan;
5242 num++;
5243
5244 pos = os_strchr(pos2, ',');
5245 if (pos == NULL)
5246 break;
5247 pos++;
5248 }
5249
5250 os_free(config->p2p_pref_chan);
5251 config->p2p_pref_chan = pref;
5252 config->num_p2p_pref_chan = num;
5253 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
5254 (u8 *) config->p2p_pref_chan,
5255 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
5256
5257 return 0;
5258
5259fail:
5260 os_free(pref);
5261 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
5262 return -1;
5263}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005264
5265
5266static int wpa_config_process_p2p_no_go_freq(
5267 const struct global_parse_data *data,
5268 struct wpa_config *config, int line, const char *pos)
5269{
5270 int ret;
5271
5272 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
5273 if (ret < 0) {
5274 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
5275 return -1;
5276 }
5277
5278 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
5279 config->p2p_no_go_freq.num);
5280
5281 return 0;
5282}
5283
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005284static int wpa_config_process_p2p_device_persistent_mac_addr(
5285 const struct global_parse_data *data,
5286 struct wpa_config *config, int line, const char *pos)
5287{
5288 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
5289 wpa_printf(MSG_ERROR,
5290 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
5291 line, pos);
5292 return -1;
5293 }
5294
5295 return 0;
5296}
5297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298#endif /* CONFIG_P2P */
5299
5300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005301static int wpa_config_process_hessid(
5302 const struct global_parse_data *data,
5303 struct wpa_config *config, int line, const char *pos)
5304{
5305 if (hwaddr_aton2(pos, config->hessid) < 0) {
5306 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
5307 line, pos);
5308 return -1;
5309 }
5310
5311 return 0;
5312}
5313
5314
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005315static int wpa_config_process_sae_groups(
5316 const struct global_parse_data *data,
5317 struct wpa_config *config, int line, const char *pos)
5318{
5319 int *groups = wpa_config_parse_int_array(pos);
5320 if (groups == NULL) {
5321 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5322 line, pos);
5323 return -1;
5324 }
5325
5326 os_free(config->sae_groups);
5327 config->sae_groups = groups;
5328
5329 return 0;
5330}
5331
5332
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005333static int wpa_config_process_ap_vendor_elements(
5334 const struct global_parse_data *data,
5335 struct wpa_config *config, int line, const char *pos)
5336{
5337 struct wpabuf *tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005338
Hai Shaloma20dcd72022-02-04 13:43:00 -08005339 if (!*pos) {
5340 wpabuf_free(config->ap_vendor_elements);
5341 config->ap_vendor_elements = NULL;
5342 return 0;
5343 }
5344
5345 tmp = wpabuf_parse_bin(pos);
5346 if (!tmp) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005347 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5348 line);
5349 return -1;
5350 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005351 wpabuf_free(config->ap_vendor_elements);
5352 config->ap_vendor_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005353
Hai Shaloma20dcd72022-02-04 13:43:00 -08005354 return 0;
5355}
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005356
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005357
Hai Shaloma20dcd72022-02-04 13:43:00 -08005358static int wpa_config_process_ap_assocresp_elements(
5359 const struct global_parse_data *data,
5360 struct wpa_config *config, int line, const char *pos)
5361{
5362 struct wpabuf *tmp;
5363
5364 if (!*pos) {
5365 wpabuf_free(config->ap_assocresp_elements);
5366 config->ap_assocresp_elements = NULL;
5367 return 0;
5368 }
5369
5370 tmp = wpabuf_parse_bin(pos);
5371 if (!tmp) {
5372 wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5373 line);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005374 return -1;
5375 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005376 wpabuf_free(config->ap_assocresp_elements);
5377 config->ap_assocresp_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005378
5379 return 0;
5380}
5381
5382
Dmitry Shmidt56052862013-10-04 10:23:25 -07005383#ifdef CONFIG_CTRL_IFACE
5384static int wpa_config_process_no_ctrl_interface(
5385 const struct global_parse_data *data,
5386 struct wpa_config *config, int line, const char *pos)
5387{
5388 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5389 os_free(config->ctrl_interface);
5390 config->ctrl_interface = NULL;
5391 return 0;
5392}
5393#endif /* CONFIG_CTRL_IFACE */
5394
5395
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005396static int wpa_config_get_int(const char *name, struct wpa_config *config,
5397 long offset, char *buf, size_t buflen,
5398 int pretty_print)
5399{
5400 int *val = (int *) (((u8 *) config) + (long) offset);
5401
5402 if (pretty_print)
5403 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5404 return os_snprintf(buf, buflen, "%d", *val);
5405}
5406
5407
Sunil Ravi876a49b2025-02-03 19:18:32 +00005408static int wpa_config_get_bool(const char *name, struct wpa_config *config,
5409 long offset, char *buf, size_t buflen,
5410 int pretty_print)
5411{
5412 bool *val = (bool*) (((u8 *) config) + (long) offset);
5413
5414 if (pretty_print)
5415 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5416 return os_snprintf(buf, buflen, "%d", *val);
5417}
5418
5419
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005420static int wpa_config_get_str(const char *name, struct wpa_config *config,
5421 long offset, char *buf, size_t buflen,
5422 int pretty_print)
5423{
5424 char **val = (char **) (((u8 *) config) + (long) offset);
5425 int res;
5426
5427 if (pretty_print)
5428 res = os_snprintf(buf, buflen, "%s=%s\n", name,
5429 *val ? *val : "null");
5430 else if (!*val)
5431 return -1;
5432 else
5433 res = os_snprintf(buf, buflen, "%s", *val);
5434 if (os_snprintf_error(buflen, res))
5435 res = -1;
5436
5437 return res;
5438}
5439
5440
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005441#ifdef CONFIG_P2P
5442static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5443 long offset, char *buf, size_t buflen,
5444 int pretty_print)
5445{
5446 void *val = ((u8 *) config) + (long) offset;
5447 int res;
5448 char addr[INET_ADDRSTRLEN];
5449
5450 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5451 return -1;
5452
5453 if (pretty_print)
5454 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5455 else
5456 res = os_snprintf(buf, buflen, "%s", addr);
5457
5458 if (os_snprintf_error(buflen, res))
5459 res = -1;
5460
5461 return res;
5462}
5463#endif /* CONFIG_P2P */
5464
5465
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005466#ifdef CONFIG_TESTING_OPTIONS
5467static int wpa_config_process_mld_connect_bssid_pref(
5468 const struct global_parse_data *data,
5469 struct wpa_config *config, int line, const char *pos)
5470{
5471 if (hwaddr_aton2(pos, config->mld_connect_bssid_pref) < 0) {
5472 wpa_printf(MSG_ERROR,
5473 "Line %d: Invalid mld_connect_bssid_pref '%s'",
5474 line, pos);
5475 return -1;
5476 }
5477
5478 return 0;
5479}
5480#endif /* CONFIG_TESTING_OPTIONS */
5481
5482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005483#ifdef OFFSET
5484#undef OFFSET
5485#endif /* OFFSET */
5486/* OFFSET: Get offset of a variable within the wpa_config structure */
5487#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5488
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005489#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5490#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5491#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492#define INT(f) _INT(f), NULL, NULL
Sunil Ravi99c035e2024-07-12 01:42:03 +00005493#define INT_RANGE(f, min, max) #f, wpa_global_config_parse_int_range, \
5494 wpa_config_get_int, OFFSET(f), (void *) min, (void *) max
Sunil Ravi876a49b2025-02-03 19:18:32 +00005495#define BOOL(f) #f, wpa_global_config_parse_bool, wpa_config_get_bool, \
5496 OFFSET(f), NULL, NULL
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005497#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498#define STR(f) _STR(f), NULL, NULL
5499#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005500#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005501#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
5502 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503
5504static const struct global_parse_data global_fields[] = {
5505#ifdef CONFIG_CTRL_IFACE
5506 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005507 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508 { STR(ctrl_interface_group), 0 } /* deprecated */,
5509#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005510#ifdef CONFIG_MACSEC
5511 { INT_RANGE(eapol_version, 1, 3), 0 },
5512#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005514#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515 { INT(ap_scan), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005516 { FUNC(bgscan), CFG_CHANGED_BGSCAN },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005517#ifdef CONFIG_MESH
5518 { INT(user_mpm), 0 },
5519 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005520 { INT(mesh_max_inactivity), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005521 { INT_RANGE(mesh_fwding, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005522 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005523#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005524 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525 { INT(fast_reauth), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005526#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527 { STR(opensc_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005528#endif /* CONFIG_OPENSC_ENGINE_PATH */
5529#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005530 { STR(pkcs11_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005531#endif /* CONFIG_PKCS11_ENGINE_PATH */
5532#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533 { STR(pkcs11_module_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005534#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005535 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005536 { STR(pcsc_reader), 0 },
5537 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07005538 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 { STR(driver_param), 0 },
5540 { INT(dot11RSNAConfigPMKLifetime), 0 },
5541 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5542 { INT(dot11RSNAConfigSATimeout), 0 },
5543#ifndef CONFIG_NO_CONFIG_WRITE
5544 { INT(update_config), 0 },
5545#endif /* CONFIG_NO_CONFIG_WRITE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005546#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 { FUNC_NO_VAR(load_dynamic_eap), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005548#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549#ifdef CONFIG_WPS
5550 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005551 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005552 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5553 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005554 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5555 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5556 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5557 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5558 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5559 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
5560 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5561 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07005562 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005563 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564#endif /* CONFIG_WPS */
5565#ifdef CONFIG_P2P
5566 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005567 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5568 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005569 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5570 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
5572 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5573 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
5574 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5575 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005576 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005577 { INT_RANGE(p2p_passphrase_len, 8, 63),
5578 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005579 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005580 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5581 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005582 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005583 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005584 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08005585 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005586 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005587 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005588 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005589 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005590 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005591 { IPV4(ip_addr_go), 0 },
5592 { IPV4(ip_addr_mask), 0 },
5593 { IPV4(ip_addr_start), 0 },
5594 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005595 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005596 { INT(p2p_device_random_mac_addr), 0 },
5597 { FUNC(p2p_device_persistent_mac_addr), 0 },
5598 { INT(p2p_interface_random_mac_addr), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005599 { INT(p2p_6ghz_disable), 0 },
Shuibing Dai64a8a892023-03-08 10:26:22 -08005600 { INT(p2p_dfs_chan_enable), 0 },
Sunil Ravi876a49b2025-02-03 19:18:32 +00005601 { BOOL(p2p_pairing_setup), 0 },
5602 { BOOL(p2p_pairing_cache), 0 },
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005603 { INT(p2p_bootstrap_methods), 0 },
5604 { INT(p2p_pasn_type), 0 },
5605 { INT(p2p_comeback_after), 0 },
Sunil Ravi876a49b2025-02-03 19:18:32 +00005606 { BOOL(p2p_twt_power_mgmt), 0 },
5607 { BOOL(p2p_chan_switch_req_enable), 0 },
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005608 { INT(p2p_reg_info), 0 },
Sunil Ravic0f5d412024-09-11 22:12:49 +00005609 { INT(dik_cipher), 0},
5610 { BIN(dik), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611#endif /* CONFIG_P2P */
5612 { FUNC(country), CFG_CHANGED_COUNTRY },
5613 { INT(bss_max_count), 0 },
5614 { INT(bss_expiration_age), 0 },
5615 { INT(bss_expiration_scan_count), 0 },
5616 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005617 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07005619 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005620 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621#ifdef CONFIG_HS20
5622 { INT_RANGE(hs20, 0, 1), 0 },
5623#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 { INT_RANGE(interworking, 0, 1), 0 },
5625 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005626 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005627 { INT_RANGE(go_interworking, 0, 1), 0 },
5628 { INT_RANGE(go_access_network_type, 0, 15), 0 },
5629 { INT_RANGE(go_internet, 0, 1), 0 },
5630 { INT_RANGE(go_venue_group, 0, 255), 0 },
5631 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
5633 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005634 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5635 CFG_CHANGED_NFC_PASSWORD_TOKEN },
5636 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5637 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5638 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005639 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5640 { INT(p2p_go_max_inactivity), 0 },
5641 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005642 { INT(okc), 0 },
5643 { INT(pmf), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005644 { INT_RANGE(sae_check_mfp, 0, 1), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005645 { FUNC(sae_groups), 0 },
Hai Shalomfdcde762020-04-02 11:19:20 -07005646 { INT_RANGE(sae_pwe, 0, 3), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005647 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005648 { INT(dtim_period), 0 },
5649 { INT(beacon_int), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005650 { FUNC(ap_assocresp_elements), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005651 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005652 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005653 { FUNC(freq_list), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005654 { FUNC(initial_freq_list), 0},
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005655 { INT(scan_cur_freq), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005656 { INT(scan_res_valid_for_connect), 0},
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005657 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005658 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005659 { INT(tdls_external_control), 0},
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005660 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005661 { INT(p2p_search_delay), 0},
Sunil Ravi77d572f2023-01-17 23:58:31 +00005662 { INT_RANGE(mac_addr, 0, 2), 0 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005663 { INT(rand_addr_lifetime), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005664 { INT_RANGE(preassoc_mac_addr, 0, 2), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005665 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005666 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005667 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005668 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005669#ifdef CONFIG_FST
5670 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5671 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5672 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5673#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005674 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005675 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005676 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005677#ifdef CONFIG_MBO
5678 { STR(non_pref_chan), 0 },
5679 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5680 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005681 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5682 { INT_RANGE(oce, 0, 3), 0 },
5683#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07005684 { INT_RANGE(btm_offload, 0, 1), CFG_CHANGED_DISABLE_BTM_NOTIFY },
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005685 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005686 { INT_RANGE(ftm_responder, 0, 1), 0 },
5687 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Sunil Ravi876a49b2025-02-03 19:18:32 +00005688 { BOOL(twt_requester), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005689 { INT(gas_rand_addr_lifetime), 0 },
5690 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005691#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005692 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005693 { STR(dpp_name), 0 },
5694 { STR(dpp_mud_url), 0 },
Sunil Ravi89eba102022-09-13 21:04:37 -07005695 { STR(dpp_extra_conf_req_name), 0 },
5696 { STR(dpp_extra_conf_req_value), 0 },
5697 { INT_RANGE(dpp_connector_privacy_default, 0, 1), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005698#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005699 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005700 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005701#ifdef CONFIG_WNM
5702 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
Hai Shalomfdcde762020-04-02 11:19:20 -07005703 { INT_RANGE(extended_key_id, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005704#endif /* CONFIG_WNM */
Hai Shalom60840252021-02-19 19:02:11 -08005705 { INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
Sunil Ravi7f769292024-07-23 22:21:32 +00005706 { INT_RANGE(rsn_overriding, 0, 2), 0},
Hai Shalom60840252021-02-19 19:02:11 -08005707#ifdef CONFIG_PASN
5708#ifdef CONFIG_TESTING_OPTIONS
5709 { INT_RANGE(force_kdk_derivation, 0, 1), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005710 { INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005711#endif /* CONFIG_TESTING_OPTIONS */
5712#endif /* CONFIG_PASN */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005713#ifdef CONFIG_TESTING_OPTIONS
5714 { INT_RANGE(mld_force_single_link, 0, 1), 0 },
5715 { INT_RANGE(mld_connect_band_pref, 0, MLD_CONNECT_BAND_PREF_MAX), 0 },
5716 { FUNC(mld_connect_bssid_pref), 0 },
5717#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravi876a49b2025-02-03 19:18:32 +00005718 { BOOL(ft_prepend_pmkid), CFG_CHANGED_FT_PREPEND_PMKID },
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005719 { INT_RANGE(wfa_gen_capa, 0, 2), 0},
5720 { BIN(wfa_gen_capa_supp), 0 },
5721 { BIN(wfa_gen_capa_cert), 0 },
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005722 /* NOTE: When adding new parameters here, add_interface() in
5723 * wpa_supplicant/dbus_new_introspect.c may need to be modified to
5724 * increase the size of the iface->xml buffer. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725};
5726
5727#undef FUNC
Sunil Ravi876a49b2025-02-03 19:18:32 +00005728#undef FUNC_NO_VAR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005729#undef _INT
5730#undef INT
5731#undef INT_RANGE
Sunil Ravi876a49b2025-02-03 19:18:32 +00005732#undef BOOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733#undef _STR
5734#undef STR
5735#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005736#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005737#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005738#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739
5740
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005741int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5742{
5743 int result = 0;
5744 size_t i;
5745
5746 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5747 const struct global_parse_data *field = &global_fields[i];
5748 int tmp;
5749
5750 if (!field->get)
5751 continue;
5752
5753 tmp = field->get(field->name, config, (long) field->param1,
5754 buf, buflen, 1);
5755 if (tmp < 0)
5756 return -1;
5757 buf += tmp;
5758 buflen -= tmp;
5759 result += tmp;
5760 }
5761 return result;
5762}
5763
5764
5765int wpa_config_get_value(const char *name, struct wpa_config *config,
5766 char *buf, size_t buflen)
5767{
5768 size_t i;
5769
5770 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5771 const struct global_parse_data *field = &global_fields[i];
5772
5773 if (os_strcmp(name, field->name) != 0)
5774 continue;
5775 if (!field->get)
5776 break;
5777 return field->get(name, config, (long) field->param1,
5778 buf, buflen, 0);
5779 }
5780
5781 return -1;
5782}
5783
5784
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005785int wpa_config_get_num_global_field_names(void)
5786{
5787 return NUM_GLOBAL_FIELDS;
5788}
5789
5790
5791const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5792{
5793 if (i >= NUM_GLOBAL_FIELDS)
5794 return NULL;
5795
5796 if (no_var)
5797 *no_var = !global_fields[i].param1;
5798 return global_fields[i].name;
5799}
5800
5801
Hai Shalom899fcc72020-10-19 14:38:18 -07005802/**
5803 * wpa_config_process_global - Set a variable in global configuration
5804 * @config: Pointer to global configuration data
5805 * @pos: Name and value in the format "{name}={value}"
5806 * @line: Line number in configuration file or 0 if not used
5807 * Returns: 0 on success with a possible change in value, 1 on success with no
5808 * change to previously configured value, or -1 on failure
5809 *
5810 * This function can be used to set global configuration variables based on
5811 * both the configuration file and management interface input. The value
5812 * parameter must be in the same format as the text-based configuration file is
5813 * using. For example, strings are using double quotation marks.
5814 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5816{
5817 size_t i;
5818 int ret = 0;
5819
5820 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5821 const struct global_parse_data *field = &global_fields[i];
5822 size_t flen = os_strlen(field->name);
5823 if (os_strncmp(pos, field->name, flen) != 0 ||
5824 pos[flen] != '=')
5825 continue;
5826
Hai Shalom899fcc72020-10-19 14:38:18 -07005827 ret = field->parser(field, config, line, pos + flen + 1);
5828 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 wpa_printf(MSG_ERROR, "Line %d: failed to "
5830 "parse '%s'.", line, pos);
5831 ret = -1;
5832 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005833 if (ret == 1)
5834 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005835 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5836 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005837 config->changed_parameters |= field->changed_flag;
5838 break;
5839 }
5840 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005841#ifdef CONFIG_AP
Hai Shalom899fcc72020-10-19 14:38:18 -07005842 if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5843 char *tmp = os_strchr(pos, '=');
5844
5845 if (!tmp) {
5846 if (line < 0)
5847 wpa_printf(MSG_ERROR,
5848 "Line %d: invalid line %s",
5849 line, pos);
5850 return -1;
5851 }
5852 *tmp++ = '\0';
5853 if (hostapd_config_tx_queue(config->tx_queue, pos,
5854 tmp)) {
5855 wpa_printf(MSG_ERROR,
5856 "Line %d: invalid TX queue item",
5857 line);
5858 return -1;
5859 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005860 return ret;
Hai Shalom899fcc72020-10-19 14:38:18 -07005861 }
5862
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005863 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5864 char *tmp = os_strchr(pos, '=');
5865 if (tmp == NULL) {
5866 if (line < 0)
5867 return -1;
5868 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5869 "'%s'", line, pos);
5870 return -1;
5871 }
5872 *tmp++ = '\0';
5873 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5874 tmp)) {
5875 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5876 "AC item", line);
5877 return -1;
5878 }
Hai Shalomc3565922019-10-28 11:58:20 -07005879 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005880 }
5881#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882 if (line < 0)
5883 return -1;
5884 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5885 line, pos);
5886 ret = -1;
5887 }
5888
5889 return ret;
5890}
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005891
5892
5893int wpa_config_set_identity(struct wpa_dev_ik *identity, const char *var,
5894 const char *value, int line)
5895{
5896 char *val;
5897 size_t len;
5898 int ret = 0;
5899
5900 if (os_strcmp(var, "dik_cipher") == 0) {
5901 identity->dik_cipher = atoi(value);
5902 return 0;
5903 }
5904
5905 val = wpa_config_parse_string(value, &len);
5906 if (!val) {
5907 wpa_printf(MSG_ERROR,
5908 "Line %d: invalid field '%s' string value '%s'.",
5909 line, var, value);
5910 return -1;
5911 }
5912
5913 if (os_strcmp(var, "dik") == 0) {
5914 wpabuf_free(identity->dik);
5915 identity->dik = wpabuf_alloc_copy(val, len);
5916 if (!identity->dik)
5917 ret = -1;
5918 } else if (os_strcmp(var, "pmk") == 0) {
5919 wpabuf_free(identity->pmk);
5920 identity->pmk = wpabuf_alloc_copy(val, len);
5921 if (!identity->pmk)
5922 ret = -1;
5923 } else if (os_strcmp(var, "pmkid") == 0) {
5924 if (len == PMKID_LEN) {
5925 wpabuf_free(identity->pmkid);
5926 identity->pmkid = wpabuf_alloc_copy(val, len);
5927 if (!identity->pmkid)
5928 ret = -1;
5929 } else {
5930 wpa_printf(MSG_ERROR,
5931 "Line %d: invalid field '%s' string value '%s'.",
5932 line, var, value);
5933 ret = -1;
5934 }
5935 } else if (line) {
5936 wpa_printf(MSG_ERROR, "Line %d: unknown identity field '%s'.",
5937 line, var);
5938 ret = -1;
5939 }
5940
5941 os_free(val);
5942 return ret;
5943}
5944
5945
5946void wpa_config_free_identity(struct wpa_dev_ik *identity)
5947{
5948 wpabuf_clear_free(identity->dik);
5949 wpabuf_clear_free(identity->pmk);
5950 wpabuf_free(identity->pmkid);
5951 os_free(identity);
5952}
5953
5954
5955/**
5956 * wpa_config_add_identity - Add a new device identity with empty configuration
5957 * @config: Configuration data from wpa_config_read()
5958 * Returns: The new device identity or %NULL if operation failed
5959 */
5960struct wpa_dev_ik * wpa_config_add_identity(struct wpa_config *config)
5961{
5962 int id;
5963 struct wpa_dev_ik *identity, *last = NULL;
5964
Sunil Ravi876a49b2025-02-03 19:18:32 +00005965 id = 0;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005966 identity = config->identity;
5967 while (identity) {
5968 if (identity->id > id)
5969 id = identity->id;
5970 last = identity;
5971 identity = identity->next;
5972 }
5973 id++;
5974
5975 identity = os_zalloc(sizeof(*identity));
5976 if (!identity)
5977 return NULL;
5978 identity->id = id;
5979 if (last)
5980 last->next = identity;
5981 else
5982 config->identity = identity;
5983
5984 return identity;
5985}
5986
5987
5988/**
5989 * wpa_config_remove_identity - Remove a configured identity based on id
5990 * @config: Configuration data from wpa_config_read()
5991 * @id: Unique network id to search for
5992 * Returns: 0 on success, or -1 if the network was not found
5993 */
5994int wpa_config_remove_identity(struct wpa_config *config, int id)
5995{
5996 struct wpa_dev_ik *identity, *prev = NULL;
5997
5998 identity = config->identity;
5999 while (identity) {
6000 if (id == identity->id)
6001 break;
6002 prev = identity;
6003 identity = identity->next;
6004 }
6005
6006 if (!identity)
6007 return -1;
6008
6009 if (prev)
6010 prev->next = identity->next;
6011 else
6012 config->identity = identity->next;
6013
6014 wpa_config_free_identity(identity);
6015 return 0;
6016}