blob: c949babac42f5fe1e304e9a86e248b2e2e78603f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Hai Shalomc3565922019-10-28 11:58:20 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/uuid.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt29333592017-01-09 12:27:11 -080014#include "common/ieee802_1x_defs.h"
Hai Shalomc3565922019-10-28 11:58:20 -070015#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "crypto/sha1.h"
17#include "rsn_supp/wpa.h"
18#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080020#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "config.h"
22
23
24#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25#define NO_CONFIG_WRITE
26#endif
27
28/*
29 * Structure for network configuration parsing. This data is used to implement
30 * a generic parser for each network block variable. The table of configuration
31 * variables is defined below in this file (ssid_fields[]).
32 */
33struct parse_data {
34 /* Configuration variable name */
35 char *name;
36
Dmitry Shmidte4663042016-04-04 10:07:49 -070037 /* Parser function for this variable. The parser functions return 0 or 1
38 * to indicate success. Value 0 indicates that the parameter value may
39 * have changed while value 1 means that the value did not change.
40 * Error cases (failure to parse the string) are indicated by returning
41 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
43 int line, const char *value);
44
45#ifndef NO_CONFIG_WRITE
46 /* Writer function (i.e., to get the variable in text format from
47 * internal presentation). */
48 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
49#endif /* NO_CONFIG_WRITE */
50
51 /* Variable specific parameters for the parser. */
52 void *param1, *param2, *param3, *param4;
53
54 /* 0 = this variable can be included in debug output and ctrl_iface
55 * 1 = this variable contains key/private data and it must not be
56 * included in debug output unless explicitly requested. In
57 * addition, this variable will not be readable through the
58 * ctrl_iface.
59 */
60 int key_data;
61};
62
63
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static int wpa_config_parse_str(const struct parse_data *data,
65 struct wpa_ssid *ssid,
66 int line, const char *value)
67{
Dmitry Shmidte4663042016-04-04 10:07:49 -070068 size_t res_len, *dst_len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 char **dst, *tmp;
70
71 if (os_strcmp(value, "NULL") == 0) {
72 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
73 data->name);
74 tmp = NULL;
75 res_len = 0;
76 goto set;
77 }
78
79 tmp = wpa_config_parse_string(value, &res_len);
80 if (tmp == NULL) {
81 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
82 line, data->name,
83 data->key_data ? "[KEY DATA REMOVED]" : value);
84 return -1;
85 }
86
87 if (data->key_data) {
88 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
89 (u8 *) tmp, res_len);
90 } else {
91 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
92 (u8 *) tmp, res_len);
93 }
94
95 if (data->param3 && res_len < (size_t) data->param3) {
96 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
97 "min_len=%ld)", line, data->name,
98 (unsigned long) res_len, (long) data->param3);
99 os_free(tmp);
100 return -1;
101 }
102
103 if (data->param4 && res_len > (size_t) data->param4) {
104 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
105 "max_len=%ld)", line, data->name,
106 (unsigned long) res_len, (long) data->param4);
107 os_free(tmp);
108 return -1;
109 }
110
111set:
112 dst = (char **) (((u8 *) ssid) + (long) data->param1);
113 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700114
115 if (data->param2)
116 prev_len = *dst_len;
117 else if (*dst)
118 prev_len = os_strlen(*dst);
119 else
120 prev_len = 0;
121 if ((*dst == NULL && tmp == NULL) ||
122 (*dst && tmp && prev_len == res_len &&
123 os_memcmp(*dst, tmp, res_len) == 0)) {
124 /* No change to the previously configured value */
125 os_free(tmp);
126 return 1;
127 }
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 os_free(*dst);
130 *dst = tmp;
131 if (data->param2)
132 *dst_len = res_len;
133
134 return 0;
135}
136
137
138#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
140{
141 char *buf;
142
143 buf = os_malloc(len + 3);
144 if (buf == NULL)
145 return NULL;
146 buf[0] = '"';
147 os_memcpy(buf + 1, value, len);
148 buf[len + 1] = '"';
149 buf[len + 2] = '\0';
150
151 return buf;
152}
153
154
155static char * wpa_config_write_string_hex(const u8 *value, size_t len)
156{
157 char *buf;
158
159 buf = os_zalloc(2 * len + 1);
160 if (buf == NULL)
161 return NULL;
162 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
163
164 return buf;
165}
166
167
168static char * wpa_config_write_string(const u8 *value, size_t len)
169{
170 if (value == NULL)
171 return NULL;
172
173 if (is_hex(value, len))
174 return wpa_config_write_string_hex(value, len);
175 else
176 return wpa_config_write_string_ascii(value, len);
177}
178
179
180static char * wpa_config_write_str(const struct parse_data *data,
181 struct wpa_ssid *ssid)
182{
183 size_t len;
184 char **src;
185
186 src = (char **) (((u8 *) ssid) + (long) data->param1);
187 if (*src == NULL)
188 return NULL;
189
190 if (data->param2)
191 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
192 else
193 len = os_strlen(*src);
194
195 return wpa_config_write_string((const u8 *) *src, len);
196}
197#endif /* NO_CONFIG_WRITE */
198
199
Sunil Ravi99c035e2024-07-12 01:42:03 +0000200static int wpa_config_parse_int_impl(const struct parse_data *data,
201 struct wpa_ssid *ssid,
202 int line, const char *value,
203 bool check_range)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700205 int val, *dst;
206 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207
208 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700209 val = strtol(value, &end, 0);
210 if (*end) {
211 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
212 line, value);
213 return -1;
214 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700215
Sunil Ravi99c035e2024-07-12 01:42:03 +0000216 if (check_range && val < (long) data->param3) {
217 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
218 "min_value=%ld)", line, data->name, val,
219 (long) data->param3);
220 return -1;
221 }
222
223 if (check_range && val > (long) data->param4) {
224 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
225 "max_value=%ld)", line, data->name, val,
226 (long) data->param4);
227 return -1;
228 }
229
Dmitry Shmidte4663042016-04-04 10:07:49 -0700230 if (*dst == val)
231 return 1;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000232
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700233 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 return 0;
237}
238
239
Sunil Ravi99c035e2024-07-12 01:42:03 +0000240static int wpa_config_parse_int(const struct parse_data *data,
241 struct wpa_ssid *ssid,
242 int line, const char *value)
243{
244 return wpa_config_parse_int_impl(data, ssid, line, value, false);
245}
246
247
248static int wpa_config_parse_int_range(const struct parse_data *data,
249 struct wpa_ssid *ssid,
250 int line, const char *value)
251{
252 return wpa_config_parse_int_impl(data, ssid, line, value, true);
253}
254
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256#ifndef NO_CONFIG_WRITE
257static char * wpa_config_write_int(const struct parse_data *data,
258 struct wpa_ssid *ssid)
259{
260 int *src, res;
261 char *value;
262
263 src = (int *) (((u8 *) ssid) + (long) data->param1);
264
265 value = os_malloc(20);
266 if (value == NULL)
267 return NULL;
268 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800269 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 os_free(value);
271 return NULL;
272 }
273 value[20 - 1] = '\0';
274 return value;
275}
276#endif /* NO_CONFIG_WRITE */
277
278
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800279static int wpa_config_parse_addr_list(const struct parse_data *data,
280 int line, const char *value,
281 u8 **list, size_t *num, char *name,
282 u8 abort_on_error, u8 masked)
283{
284 const char *pos;
285 u8 *buf, *n, addr[2 * ETH_ALEN];
286 size_t count;
287
288 buf = NULL;
289 count = 0;
290
291 pos = value;
292 while (pos && *pos) {
293 while (*pos == ' ')
294 pos++;
295
296 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
297 if (abort_on_error || count == 0) {
298 wpa_printf(MSG_ERROR,
299 "Line %d: Invalid %s address '%s'",
300 line, name, value);
301 os_free(buf);
302 return -1;
303 }
304 /* continue anyway since this could have been from a
305 * truncated configuration file line */
306 wpa_printf(MSG_INFO,
307 "Line %d: Ignore likely truncated %s address '%s'",
308 line, name, pos);
309 } else {
310 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
311 if (n == NULL) {
312 os_free(buf);
313 return -1;
314 }
315 buf = n;
316 os_memmove(buf + 2 * ETH_ALEN, buf,
317 count * 2 * ETH_ALEN);
318 os_memcpy(buf, addr, 2 * ETH_ALEN);
319 count++;
320 wpa_printf(MSG_MSGDUMP,
321 "%s: addr=" MACSTR " mask=" MACSTR,
322 name, MAC2STR(addr),
323 MAC2STR(&addr[ETH_ALEN]));
324 }
325
326 pos = os_strchr(pos, ' ');
327 }
328
329 os_free(*list);
330 *list = buf;
331 *num = count;
332
333 return 0;
334}
335
336
337#ifndef NO_CONFIG_WRITE
338static char * wpa_config_write_addr_list(const struct parse_data *data,
339 const u8 *list, size_t num, char *name)
340{
341 char *value, *end, *pos;
342 int res;
343 size_t i;
344
345 if (list == NULL || num == 0)
346 return NULL;
347
348 value = os_malloc(2 * 20 * num);
349 if (value == NULL)
350 return NULL;
351 pos = value;
352 end = value + 2 * 20 * num;
353
354 for (i = num; i > 0; i--) {
355 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
356 const u8 *m = a + ETH_ALEN;
357
358 if (i < num)
359 *pos++ = ' ';
360 res = hwaddr_mask_txt(pos, end - pos, a, m);
361 if (res < 0) {
362 os_free(value);
363 return NULL;
364 }
365 pos += res;
366 }
367
368 return value;
369}
370#endif /* NO_CONFIG_WRITE */
371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372static int wpa_config_parse_bssid(const struct parse_data *data,
373 struct wpa_ssid *ssid, int line,
374 const char *value)
375{
376 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
377 os_strcmp(value, "any") == 0) {
378 ssid->bssid_set = 0;
379 wpa_printf(MSG_MSGDUMP, "BSSID any");
380 return 0;
381 }
382 if (hwaddr_aton(value, ssid->bssid)) {
383 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
384 line, value);
385 return -1;
386 }
387 ssid->bssid_set = 1;
388 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
389 return 0;
390}
391
392
393#ifndef NO_CONFIG_WRITE
394static char * wpa_config_write_bssid(const struct parse_data *data,
395 struct wpa_ssid *ssid)
396{
397 char *value;
398 int res;
399
400 if (!ssid->bssid_set)
401 return NULL;
402
403 value = os_malloc(20);
404 if (value == NULL)
405 return NULL;
406 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800407 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 os_free(value);
409 return NULL;
410 }
411 value[20 - 1] = '\0';
412 return value;
413}
414#endif /* NO_CONFIG_WRITE */
415
416
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700417static int wpa_config_parse_bssid_hint(const struct parse_data *data,
418 struct wpa_ssid *ssid, int line,
419 const char *value)
420{
421 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
422 os_strcmp(value, "any") == 0) {
423 ssid->bssid_hint_set = 0;
424 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
425 return 0;
426 }
427 if (hwaddr_aton(value, ssid->bssid_hint)) {
428 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
429 line, value);
430 return -1;
431 }
432 ssid->bssid_hint_set = 1;
433 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
434 return 0;
435}
436
437
438#ifndef NO_CONFIG_WRITE
439static char * wpa_config_write_bssid_hint(const struct parse_data *data,
440 struct wpa_ssid *ssid)
441{
442 char *value;
443 int res;
444
445 if (!ssid->bssid_hint_set)
446 return NULL;
447
448 value = os_malloc(20);
449 if (!value)
450 return NULL;
451 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
452 if (os_snprintf_error(20, res)) {
453 os_free(value);
454 return NULL;
455 }
456 return value;
457}
458#endif /* NO_CONFIG_WRITE */
459
460
Hai Shalom60840252021-02-19 19:02:11 -0800461static int wpa_config_parse_bssid_ignore(const struct parse_data *data,
462 struct wpa_ssid *ssid, int line,
463 const char *value)
464{
465 return wpa_config_parse_addr_list(data, line, value,
466 &ssid->bssid_ignore,
467 &ssid->num_bssid_ignore,
468 "bssid_ignore", 1, 1);
469}
470
471
472/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800473static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
474 struct wpa_ssid *ssid, int line,
475 const char *value)
476{
477 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800478 &ssid->bssid_ignore,
479 &ssid->num_bssid_ignore,
480 "bssid_ignore", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800481}
482
483
484#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800485
486static char * wpa_config_write_bssid_ignore(const struct parse_data *data,
487 struct wpa_ssid *ssid)
488{
489 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
490 ssid->num_bssid_ignore,
491 "bssid_ignore");
492}
493
494
495/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800496static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
497 struct wpa_ssid *ssid)
498{
Hai Shalom60840252021-02-19 19:02:11 -0800499 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
500 ssid->num_bssid_ignore,
501 "bssid_ignore");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800502}
Hai Shalom60840252021-02-19 19:02:11 -0800503
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800504#endif /* NO_CONFIG_WRITE */
505
506
Hai Shalom60840252021-02-19 19:02:11 -0800507static int wpa_config_parse_bssid_accept(const struct parse_data *data,
508 struct wpa_ssid *ssid, int line,
509 const char *value)
510{
511 return wpa_config_parse_addr_list(data, line, value,
512 &ssid->bssid_accept,
513 &ssid->num_bssid_accept,
514 "bssid_accept", 1, 1);
515}
516
517
518/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800519static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
520 struct wpa_ssid *ssid, int line,
521 const char *value)
522{
523 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800524 &ssid->bssid_accept,
525 &ssid->num_bssid_accept,
526 "bssid_accept", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800527}
528
529
530#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800531
532static char * wpa_config_write_bssid_accept(const struct parse_data *data,
533 struct wpa_ssid *ssid)
534{
535 return wpa_config_write_addr_list(data, ssid->bssid_accept,
536 ssid->num_bssid_accept,
537 "bssid_accept");
538}
539
540
541/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800542static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
543 struct wpa_ssid *ssid)
544{
Hai Shalom60840252021-02-19 19:02:11 -0800545 return wpa_config_write_addr_list(data, ssid->bssid_accept,
546 ssid->num_bssid_accept,
547 "bssid_accept");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800548}
Hai Shalom60840252021-02-19 19:02:11 -0800549
550#endif /* NO_CONFIG_WRITE */
551
552
553#ifndef NO_CONFIG_WRITE
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800554#endif /* NO_CONFIG_WRITE */
555
556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557static int wpa_config_parse_psk(const struct parse_data *data,
558 struct wpa_ssid *ssid, int line,
559 const char *value)
560{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700561#ifdef CONFIG_EXT_PASSWORD
562 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700563 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700564 ssid->passphrase = NULL;
565 ssid->psk_set = 0;
566 os_free(ssid->ext_psk);
567 ssid->ext_psk = os_strdup(value + 4);
568 if (ssid->ext_psk == NULL)
569 return -1;
570 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
571 ssid->ext_psk);
572 return 0;
573 }
574#endif /* CONFIG_EXT_PASSWORD */
575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 if (*value == '"') {
577#ifndef CONFIG_NO_PBKDF2
578 const char *pos;
579 size_t len;
580
581 value++;
582 pos = os_strrchr(value, '"');
583 if (pos)
584 len = pos - value;
585 else
586 len = os_strlen(value);
587 if (len < 8 || len > 63) {
588 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
589 "length %lu (expected: 8..63) '%s'.",
590 line, (unsigned long) len, value);
591 return -1;
592 }
593 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
594 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700595 if (has_ctrl_char((u8 *) value, len)) {
596 wpa_printf(MSG_ERROR,
597 "Line %d: Invalid passphrase character",
598 line);
599 return -1;
600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700602 os_memcmp(ssid->passphrase, value, len) == 0) {
603 /* No change to the previously configured value */
604 return 1;
605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700607 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700608 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 if (ssid->passphrase == NULL)
610 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 return 0;
612#else /* CONFIG_NO_PBKDF2 */
613 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
614 "supported.", line);
615 return -1;
616#endif /* CONFIG_NO_PBKDF2 */
617 }
618
619 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
620 value[PMK_LEN * 2] != '\0') {
621 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
622 line, value);
623 return -1;
624 }
625
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700626 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 ssid->passphrase = NULL;
628
629 ssid->psk_set = 1;
630 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
631 return 0;
632}
633
634
635#ifndef NO_CONFIG_WRITE
636static char * wpa_config_write_psk(const struct parse_data *data,
637 struct wpa_ssid *ssid)
638{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639#ifdef CONFIG_EXT_PASSWORD
640 if (ssid->ext_psk) {
641 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
642 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800643 int res;
644
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700645 if (buf == NULL)
646 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800647 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
648 if (os_snprintf_error(len, res)) {
649 os_free(buf);
650 buf = NULL;
651 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652 return buf;
653 }
654#endif /* CONFIG_EXT_PASSWORD */
655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 if (ssid->passphrase)
657 return wpa_config_write_string_ascii(
658 (const u8 *) ssid->passphrase,
659 os_strlen(ssid->passphrase));
660
661 if (ssid->psk_set)
662 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
663
664 return NULL;
665}
666#endif /* NO_CONFIG_WRITE */
667
668
669static int wpa_config_parse_proto(const struct parse_data *data,
670 struct wpa_ssid *ssid, int line,
671 const char *value)
672{
673 int val = 0, last, errors = 0;
674 char *start, *end, *buf;
675
676 buf = os_strdup(value);
677 if (buf == NULL)
678 return -1;
679 start = buf;
680
681 while (*start != '\0') {
682 while (*start == ' ' || *start == '\t')
683 start++;
684 if (*start == '\0')
685 break;
686 end = start;
687 while (*end != ' ' && *end != '\t' && *end != '\0')
688 end++;
689 last = *end == '\0';
690 *end = '\0';
691 if (os_strcmp(start, "WPA") == 0)
692 val |= WPA_PROTO_WPA;
693 else if (os_strcmp(start, "RSN") == 0 ||
694 os_strcmp(start, "WPA2") == 0)
695 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800696 else if (os_strcmp(start, "OSEN") == 0)
697 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 else {
699 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
700 line, start);
701 errors++;
702 }
703
704 if (last)
705 break;
706 start = end + 1;
707 }
708 os_free(buf);
709
710 if (val == 0) {
711 wpa_printf(MSG_ERROR,
712 "Line %d: no proto values configured.", line);
713 errors++;
714 }
715
Dmitry Shmidte4663042016-04-04 10:07:49 -0700716 if (!errors && ssid->proto == val)
717 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
719 ssid->proto = val;
720 return errors ? -1 : 0;
721}
722
723
724#ifndef NO_CONFIG_WRITE
725static char * wpa_config_write_proto(const struct parse_data *data,
726 struct wpa_ssid *ssid)
727{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700728 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 char *buf, *pos, *end;
730
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800731 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 if (buf == NULL)
733 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800734 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735
736 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700737 ret = os_snprintf(pos, end - pos, "%sWPA",
738 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800739 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 return buf;
741 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 }
743
744 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700745 ret = os_snprintf(pos, end - pos, "%sRSN",
746 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800747 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 return buf;
749 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 }
751
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800752 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700753 ret = os_snprintf(pos, end - pos, "%sOSEN",
754 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800755 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800756 return buf;
757 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700758 }
759
760 if (pos == buf) {
761 os_free(buf);
762 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800763 }
764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 return buf;
766}
767#endif /* NO_CONFIG_WRITE */
768
769
770static int wpa_config_parse_key_mgmt(const struct parse_data *data,
771 struct wpa_ssid *ssid, int line,
772 const char *value)
773{
774 int val = 0, last, errors = 0;
775 char *start, *end, *buf;
776
777 buf = os_strdup(value);
778 if (buf == NULL)
779 return -1;
780 start = buf;
781
782 while (*start != '\0') {
783 while (*start == ' ' || *start == '\t')
784 start++;
785 if (*start == '\0')
786 break;
787 end = start;
788 while (*end != ' ' && *end != '\t' && *end != '\0')
789 end++;
790 last = *end == '\0';
791 *end = '\0';
792 if (os_strcmp(start, "WPA-PSK") == 0)
793 val |= WPA_KEY_MGMT_PSK;
794 else if (os_strcmp(start, "WPA-EAP") == 0)
795 val |= WPA_KEY_MGMT_IEEE8021X;
796 else if (os_strcmp(start, "IEEE8021X") == 0)
797 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
798 else if (os_strcmp(start, "NONE") == 0)
799 val |= WPA_KEY_MGMT_NONE;
800 else if (os_strcmp(start, "WPA-NONE") == 0)
801 val |= WPA_KEY_MGMT_WPA_NONE;
802#ifdef CONFIG_IEEE80211R
803 else if (os_strcmp(start, "FT-PSK") == 0)
804 val |= WPA_KEY_MGMT_FT_PSK;
805 else if (os_strcmp(start, "FT-EAP") == 0)
806 val |= WPA_KEY_MGMT_FT_IEEE8021X;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700807#ifdef CONFIG_SHA384
808 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
809 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
810#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811#endif /* CONFIG_IEEE80211R */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000812#ifdef CONFIG_SHA384
813 else if (os_strcmp(start, "WPA-EAP-SHA384") == 0)
814 val |= WPA_KEY_MGMT_IEEE8021X_SHA384;
815#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
817 val |= WPA_KEY_MGMT_PSK_SHA256;
818 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
819 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820#ifdef CONFIG_WPS
821 else if (os_strcmp(start, "WPS") == 0)
822 val |= WPA_KEY_MGMT_WPS;
823#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800824#ifdef CONFIG_SAE
825 else if (os_strcmp(start, "SAE") == 0)
826 val |= WPA_KEY_MGMT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700827 else if (os_strcmp(start, "SAE-EXT-KEY") == 0)
828 val |= WPA_KEY_MGMT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800829 else if (os_strcmp(start, "FT-SAE") == 0)
830 val |= WPA_KEY_MGMT_FT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700831 else if (os_strcmp(start, "FT-SAE-EXT-KEY") == 0)
832 val |= WPA_KEY_MGMT_FT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800833#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800834#ifdef CONFIG_HS20
835 else if (os_strcmp(start, "OSEN") == 0)
836 val |= WPA_KEY_MGMT_OSEN;
837#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800838#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800839 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
840 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800841#endif /* CONFIG_SUITEB */
842#ifdef CONFIG_SUITEB192
843 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
844 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
845#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800846#ifdef CONFIG_FILS
847 else if (os_strcmp(start, "FILS-SHA256") == 0)
848 val |= WPA_KEY_MGMT_FILS_SHA256;
849 else if (os_strcmp(start, "FILS-SHA384") == 0)
850 val |= WPA_KEY_MGMT_FILS_SHA384;
851#ifdef CONFIG_IEEE80211R
852 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
853 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
854 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
855 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
856#endif /* CONFIG_IEEE80211R */
857#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700858#ifdef CONFIG_OWE
859 else if (os_strcmp(start, "OWE") == 0)
860 val |= WPA_KEY_MGMT_OWE;
861#endif /* CONFIG_OWE */
862#ifdef CONFIG_DPP
863 else if (os_strcmp(start, "DPP") == 0)
864 val |= WPA_KEY_MGMT_DPP;
865#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 else {
867 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
868 line, start);
869 errors++;
870 }
871
872 if (last)
873 break;
874 start = end + 1;
875 }
876 os_free(buf);
877
878 if (val == 0) {
879 wpa_printf(MSG_ERROR,
880 "Line %d: no key_mgmt values configured.", line);
881 errors++;
882 }
883
Dmitry Shmidte4663042016-04-04 10:07:49 -0700884 if (!errors && ssid->key_mgmt == val)
885 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
887 ssid->key_mgmt = val;
888 return errors ? -1 : 0;
889}
890
891
892#ifndef NO_CONFIG_WRITE
893static char * wpa_config_write_key_mgmt(const struct parse_data *data,
894 struct wpa_ssid *ssid)
895{
896 char *buf, *pos, *end;
897 int ret;
898
Dmitry Shmidt96571392013-10-14 12:54:46 -0700899 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 if (buf == NULL)
901 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700902 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
904 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
905 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
906 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800907 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 end[-1] = '\0';
909 return buf;
910 }
911 pos += ret;
912 }
913
914 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
915 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
916 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800917 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 end[-1] = '\0';
919 return buf;
920 }
921 pos += ret;
922 }
923
924 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
925 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
926 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800927 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 end[-1] = '\0';
929 return buf;
930 }
931 pos += ret;
932 }
933
934 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
935 ret = os_snprintf(pos, end - pos, "%sNONE",
936 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800937 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 end[-1] = '\0';
939 return buf;
940 }
941 pos += ret;
942 }
943
944 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
945 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
946 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800947 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 end[-1] = '\0';
949 return buf;
950 }
951 pos += ret;
952 }
953
954#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700955 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
956 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
957 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700959 end[-1] = '\0';
960 return buf;
961 }
962 pos += ret;
963 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
Dmitry Shmidt96571392013-10-14 12:54:46 -0700965 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
966 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
967 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800968 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700969 end[-1] = '\0';
970 return buf;
971 }
972 pos += ret;
973 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700974
975#ifdef CONFIG_SHA384
976 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
977 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
978 pos == buf ? "" : " ");
979 if (os_snprintf_error(end - pos, ret)) {
980 end[-1] = '\0';
981 return buf;
982 }
983 pos += ret;
984 }
985#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986#endif /* CONFIG_IEEE80211R */
987
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000988#ifdef CONFIG_SHA384
989 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
990 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA384",
991 pos == buf ? "" : " ");
992 if (os_snprintf_error(end - pos, ret)) {
993 end[-1] = '\0';
994 return buf;
995 }
996 pos += ret;
997 }
998#endif /* CONFIG_SHA384 */
999
Dmitry Shmidt96571392013-10-14 12:54:46 -07001000 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1001 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
1002 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001004 end[-1] = '\0';
1005 return buf;
1006 }
1007 pos += ret;
1008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009
Dmitry Shmidt96571392013-10-14 12:54:46 -07001010 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1011 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
1012 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001013 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001014 end[-1] = '\0';
1015 return buf;
1016 }
1017 pos += ret;
1018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019
1020#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -07001021 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1022 ret = os_snprintf(pos, end - pos, "%sWPS",
1023 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001024 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 end[-1] = '\0';
1026 return buf;
1027 }
1028 pos += ret;
1029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030#endif /* CONFIG_WPS */
1031
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001032#ifdef CONFIG_SAE
1033 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1034 ret = os_snprintf(pos, end - pos, "%sSAE",
1035 pos == buf ? "" : " ");
1036 if (os_snprintf_error(end - pos, ret)) {
1037 end[-1] = '\0';
1038 return buf;
1039 }
1040 pos += ret;
1041 }
1042
Sunil Ravi89eba102022-09-13 21:04:37 -07001043 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1044 ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
1045 pos == buf ? "" : " ");
1046 if (os_snprintf_error(end - pos, ret)) {
1047 end[-1] = '\0';
1048 return buf;
1049 }
1050 pos += ret;
1051 }
1052
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001053 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1054 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1055 pos == buf ? "" : " ");
1056 if (os_snprintf_error(end - pos, ret)) {
1057 end[-1] = '\0';
1058 return buf;
1059 }
1060 pos += ret;
1061 }
Sunil Ravi89eba102022-09-13 21:04:37 -07001062
1063 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1064 ret = os_snprintf(pos, end - pos, "%sFT-SAE-EXT-KEY",
1065 pos == buf ? "" : " ");
1066 if (os_snprintf_error(end - pos, ret)) {
1067 end[-1] = '\0';
1068 return buf;
1069 }
1070 pos += ret;
1071 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001072#endif /* CONFIG_SAE */
1073
1074#ifdef CONFIG_HS20
1075 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
1076 ret = os_snprintf(pos, end - pos, "%sOSEN",
1077 pos == buf ? "" : " ");
1078 if (os_snprintf_error(end - pos, ret)) {
1079 end[-1] = '\0';
1080 return buf;
1081 }
1082 pos += ret;
1083 }
1084#endif /* CONFIG_HS20 */
1085
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001086#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001087 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1088 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1089 pos == buf ? "" : " ");
1090 if (os_snprintf_error(end - pos, ret)) {
1091 end[-1] = '\0';
1092 return buf;
1093 }
1094 pos += ret;
1095 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001096#endif /* CONFIG_SUITEB */
1097
1098#ifdef CONFIG_SUITEB192
1099 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1100 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1101 pos == buf ? "" : " ");
1102 if (os_snprintf_error(end - pos, ret)) {
1103 end[-1] = '\0';
1104 return buf;
1105 }
1106 pos += ret;
1107 }
1108#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001109
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001110#ifdef CONFIG_FILS
1111 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1112 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1113 pos == buf ? "" : " ");
1114 if (os_snprintf_error(end - pos, ret)) {
1115 end[-1] = '\0';
1116 return buf;
1117 }
1118 pos += ret;
1119 }
1120 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1121 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1122 pos == buf ? "" : " ");
1123 if (os_snprintf_error(end - pos, ret)) {
1124 end[-1] = '\0';
1125 return buf;
1126 }
1127 pos += ret;
1128 }
1129#ifdef CONFIG_IEEE80211R
1130 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1131 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1132 pos == buf ? "" : " ");
1133 if (os_snprintf_error(end - pos, ret)) {
1134 end[-1] = '\0';
1135 return buf;
1136 }
1137 pos += ret;
1138 }
1139 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1140 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1141 pos == buf ? "" : " ");
1142 if (os_snprintf_error(end - pos, ret)) {
1143 end[-1] = '\0';
1144 return buf;
1145 }
1146 pos += ret;
1147 }
1148#endif /* CONFIG_IEEE80211R */
1149#endif /* CONFIG_FILS */
1150
Hai Shalom74f70d42019-02-11 14:42:39 -08001151#ifdef CONFIG_DPP
1152 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1153 ret = os_snprintf(pos, end - pos, "%sDPP",
1154 pos == buf ? "" : " ");
1155 if (os_snprintf_error(end - pos, ret)) {
1156 end[-1] = '\0';
1157 return buf;
1158 }
1159 pos += ret;
1160 }
1161#endif /* CONFIG_DPP */
1162
1163#ifdef CONFIG_OWE
1164 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1165 ret = os_snprintf(pos, end - pos, "%sOWE",
1166 pos == buf ? "" : " ");
1167 if (os_snprintf_error(end - pos, ret)) {
1168 end[-1] = '\0';
1169 return buf;
1170 }
1171 pos += ret;
1172 }
1173#endif /* CONFIG_OWE */
1174
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001175 if (pos == buf) {
1176 os_free(buf);
1177 buf = NULL;
1178 }
1179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 return buf;
1181}
1182#endif /* NO_CONFIG_WRITE */
1183
1184
1185static int wpa_config_parse_cipher(int line, const char *value)
1186{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001187#ifdef CONFIG_NO_WPA
1188 return -1;
1189#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001190 int val = wpa_parse_cipher(value);
1191 if (val < 0) {
1192 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1193 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 if (val == 0) {
1197 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1198 line);
1199 return -1;
1200 }
1201 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001202#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203}
1204
1205
1206#ifndef NO_CONFIG_WRITE
1207static char * wpa_config_write_cipher(int cipher)
1208{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001209#ifdef CONFIG_NO_WPA
1210 return NULL;
1211#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001212 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 if (buf == NULL)
1214 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001216 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1217 os_free(buf);
1218 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 }
1220
1221 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001222#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223}
1224#endif /* NO_CONFIG_WRITE */
1225
1226
1227static int wpa_config_parse_pairwise(const struct parse_data *data,
1228 struct wpa_ssid *ssid, int line,
1229 const char *value)
1230{
1231 int val;
1232 val = wpa_config_parse_cipher(line, value);
1233 if (val == -1)
1234 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001235 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1237 "(0x%x).", line, val);
1238 return -1;
1239 }
1240
Dmitry Shmidte4663042016-04-04 10:07:49 -07001241 if (ssid->pairwise_cipher == val)
1242 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1244 ssid->pairwise_cipher = val;
1245 return 0;
1246}
1247
1248
1249#ifndef NO_CONFIG_WRITE
1250static char * wpa_config_write_pairwise(const struct parse_data *data,
1251 struct wpa_ssid *ssid)
1252{
1253 return wpa_config_write_cipher(ssid->pairwise_cipher);
1254}
1255#endif /* NO_CONFIG_WRITE */
1256
1257
1258static int wpa_config_parse_group(const struct parse_data *data,
1259 struct wpa_ssid *ssid, int line,
1260 const char *value)
1261{
1262 int val;
1263 val = wpa_config_parse_cipher(line, value);
1264 if (val == -1)
1265 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001266
1267 /*
1268 * Backwards compatibility - filter out WEP ciphers that were previously
1269 * allowed.
1270 */
1271 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1272
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001273 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1275 "(0x%x).", line, val);
1276 return -1;
1277 }
1278
Dmitry Shmidte4663042016-04-04 10:07:49 -07001279 if (ssid->group_cipher == val)
1280 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1282 ssid->group_cipher = val;
1283 return 0;
1284}
1285
1286
1287#ifndef NO_CONFIG_WRITE
1288static char * wpa_config_write_group(const struct parse_data *data,
1289 struct wpa_ssid *ssid)
1290{
1291 return wpa_config_write_cipher(ssid->group_cipher);
1292}
1293#endif /* NO_CONFIG_WRITE */
1294
1295
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001296static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1297 struct wpa_ssid *ssid, int line,
1298 const char *value)
1299{
1300 int val;
1301
1302 val = wpa_config_parse_cipher(line, value);
1303 if (val == -1)
1304 return -1;
1305
1306 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1307 wpa_printf(MSG_ERROR,
1308 "Line %d: not allowed group management cipher (0x%x).",
1309 line, val);
1310 return -1;
1311 }
1312
1313 if (ssid->group_mgmt_cipher == val)
1314 return 1;
1315 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1316 ssid->group_mgmt_cipher = val;
1317 return 0;
1318}
1319
1320
1321#ifndef NO_CONFIG_WRITE
1322static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1323 struct wpa_ssid *ssid)
1324{
1325 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1326}
1327#endif /* NO_CONFIG_WRITE */
1328
1329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330static int wpa_config_parse_auth_alg(const struct parse_data *data,
1331 struct wpa_ssid *ssid, int line,
1332 const char *value)
1333{
1334 int val = 0, last, errors = 0;
1335 char *start, *end, *buf;
1336
1337 buf = os_strdup(value);
1338 if (buf == NULL)
1339 return -1;
1340 start = buf;
1341
1342 while (*start != '\0') {
1343 while (*start == ' ' || *start == '\t')
1344 start++;
1345 if (*start == '\0')
1346 break;
1347 end = start;
1348 while (*end != ' ' && *end != '\t' && *end != '\0')
1349 end++;
1350 last = *end == '\0';
1351 *end = '\0';
1352 if (os_strcmp(start, "OPEN") == 0)
1353 val |= WPA_AUTH_ALG_OPEN;
1354 else if (os_strcmp(start, "SHARED") == 0)
1355 val |= WPA_AUTH_ALG_SHARED;
1356 else if (os_strcmp(start, "LEAP") == 0)
1357 val |= WPA_AUTH_ALG_LEAP;
1358 else {
1359 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1360 line, start);
1361 errors++;
1362 }
1363
1364 if (last)
1365 break;
1366 start = end + 1;
1367 }
1368 os_free(buf);
1369
1370 if (val == 0) {
1371 wpa_printf(MSG_ERROR,
1372 "Line %d: no auth_alg values configured.", line);
1373 errors++;
1374 }
1375
Dmitry Shmidte4663042016-04-04 10:07:49 -07001376 if (!errors && ssid->auth_alg == val)
1377 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1379 ssid->auth_alg = val;
1380 return errors ? -1 : 0;
1381}
1382
1383
1384#ifndef NO_CONFIG_WRITE
1385static char * wpa_config_write_auth_alg(const struct parse_data *data,
1386 struct wpa_ssid *ssid)
1387{
1388 char *buf, *pos, *end;
1389 int ret;
1390
1391 pos = buf = os_zalloc(30);
1392 if (buf == NULL)
1393 return NULL;
1394 end = buf + 30;
1395
1396 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1397 ret = os_snprintf(pos, end - pos, "%sOPEN",
1398 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001399 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 end[-1] = '\0';
1401 return buf;
1402 }
1403 pos += ret;
1404 }
1405
1406 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1407 ret = os_snprintf(pos, end - pos, "%sSHARED",
1408 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001409 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 end[-1] = '\0';
1411 return buf;
1412 }
1413 pos += ret;
1414 }
1415
1416 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1417 ret = os_snprintf(pos, end - pos, "%sLEAP",
1418 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001419 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 end[-1] = '\0';
1421 return buf;
1422 }
1423 pos += ret;
1424 }
1425
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001426 if (pos == buf) {
1427 os_free(buf);
1428 buf = NULL;
1429 }
1430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 return buf;
1432}
1433#endif /* NO_CONFIG_WRITE */
1434
1435
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001436static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437{
1438 int *freqs;
1439 size_t used, len;
1440 const char *pos;
1441
1442 used = 0;
1443 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001444 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 if (freqs == NULL)
1446 return NULL;
1447
1448 pos = value;
1449 while (pos) {
1450 while (*pos == ' ')
1451 pos++;
1452 if (used == len) {
1453 int *n;
1454 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001455 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 if (n == NULL) {
1457 os_free(freqs);
1458 return NULL;
1459 }
1460 for (i = len; i <= len * 2; i++)
1461 n[i] = 0;
1462 freqs = n;
1463 len *= 2;
1464 }
1465
1466 freqs[used] = atoi(pos);
1467 if (freqs[used] == 0)
1468 break;
1469 used++;
1470 pos = os_strchr(pos + 1, ' ');
1471 }
1472
1473 return freqs;
1474}
1475
1476
1477static int wpa_config_parse_scan_freq(const struct parse_data *data,
1478 struct wpa_ssid *ssid, int line,
1479 const char *value)
1480{
1481 int *freqs;
1482
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 if (freqs == NULL)
1485 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001486 if (freqs[0] == 0) {
1487 os_free(freqs);
1488 freqs = NULL;
1489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 os_free(ssid->scan_freq);
1491 ssid->scan_freq = freqs;
1492
1493 return 0;
1494}
1495
1496
1497static int wpa_config_parse_freq_list(const struct parse_data *data,
1498 struct wpa_ssid *ssid, int line,
1499 const char *value)
1500{
1501 int *freqs;
1502
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001503 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 if (freqs == NULL)
1505 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001506 if (freqs[0] == 0) {
1507 os_free(freqs);
1508 freqs = NULL;
1509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 os_free(ssid->freq_list);
1511 ssid->freq_list = freqs;
1512
1513 return 0;
1514}
1515
1516
1517#ifndef NO_CONFIG_WRITE
1518static char * wpa_config_write_freqs(const struct parse_data *data,
1519 const int *freqs)
1520{
1521 char *buf, *pos, *end;
1522 int i, ret;
1523 size_t count;
1524
1525 if (freqs == NULL)
1526 return NULL;
1527
1528 count = 0;
1529 for (i = 0; freqs[i]; i++)
1530 count++;
1531
1532 pos = buf = os_zalloc(10 * count + 1);
1533 if (buf == NULL)
1534 return NULL;
1535 end = buf + 10 * count + 1;
1536
1537 for (i = 0; freqs[i]; i++) {
1538 ret = os_snprintf(pos, end - pos, "%s%u",
1539 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001540 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 end[-1] = '\0';
1542 return buf;
1543 }
1544 pos += ret;
1545 }
1546
1547 return buf;
1548}
1549
1550
1551static char * wpa_config_write_scan_freq(const struct parse_data *data,
1552 struct wpa_ssid *ssid)
1553{
1554 return wpa_config_write_freqs(data, ssid->scan_freq);
1555}
1556
1557
1558static char * wpa_config_write_freq_list(const struct parse_data *data,
1559 struct wpa_ssid *ssid)
1560{
1561 return wpa_config_write_freqs(data, ssid->freq_list);
1562}
1563#endif /* NO_CONFIG_WRITE */
1564
1565
1566#ifdef IEEE8021X_EAPOL
1567static int wpa_config_parse_eap(const struct parse_data *data,
1568 struct wpa_ssid *ssid, int line,
1569 const char *value)
1570{
1571 int last, errors = 0;
1572 char *start, *end, *buf;
1573 struct eap_method_type *methods = NULL, *tmp;
1574 size_t num_methods = 0;
1575
1576 buf = os_strdup(value);
1577 if (buf == NULL)
1578 return -1;
1579 start = buf;
1580
1581 while (*start != '\0') {
1582 while (*start == ' ' || *start == '\t')
1583 start++;
1584 if (*start == '\0')
1585 break;
1586 end = start;
1587 while (*end != ' ' && *end != '\t' && *end != '\0')
1588 end++;
1589 last = *end == '\0';
1590 *end = '\0';
1591 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001592 methods = os_realloc_array(methods, num_methods + 1,
1593 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 if (methods == NULL) {
1595 os_free(tmp);
1596 os_free(buf);
1597 return -1;
1598 }
1599 methods[num_methods].method = eap_peer_get_type(
1600 start, &methods[num_methods].vendor);
1601 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1602 methods[num_methods].method == EAP_TYPE_NONE) {
1603 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1604 "'%s'", line, start);
1605 wpa_printf(MSG_ERROR, "You may need to add support for"
1606 " this EAP method during wpa_supplicant\n"
1607 "build time configuration.\n"
1608 "See README for more information.");
1609 errors++;
1610 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1611 methods[num_methods].method == EAP_TYPE_LEAP)
1612 ssid->leap++;
1613 else
1614 ssid->non_leap++;
1615 num_methods++;
1616 if (last)
1617 break;
1618 start = end + 1;
1619 }
1620 os_free(buf);
1621
1622 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (methods == NULL) {
1625 os_free(tmp);
1626 return -1;
1627 }
1628 methods[num_methods].vendor = EAP_VENDOR_IETF;
1629 methods[num_methods].method = EAP_TYPE_NONE;
1630 num_methods++;
1631
Dmitry Shmidte4663042016-04-04 10:07:49 -07001632 if (!errors && ssid->eap.eap_methods) {
1633 struct eap_method_type *prev_m;
1634 size_t i, j, prev_methods, match = 0;
1635
1636 prev_m = ssid->eap.eap_methods;
1637 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1638 prev_m[i].method != EAP_TYPE_NONE; i++) {
1639 /* Count the methods */
1640 }
1641 prev_methods = i + 1;
1642
1643 for (i = 0; prev_methods == num_methods && i < prev_methods;
1644 i++) {
1645 for (j = 0; j < num_methods; j++) {
1646 if (prev_m[i].vendor == methods[j].vendor &&
1647 prev_m[i].method == methods[j].method) {
1648 match++;
1649 break;
1650 }
1651 }
1652 }
1653 if (match == num_methods) {
1654 os_free(methods);
1655 return 1;
1656 }
1657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1659 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001660 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 ssid->eap.eap_methods = methods;
1662 return errors ? -1 : 0;
1663}
1664
1665
Dmitry Shmidt83474442015-04-15 13:47:09 -07001666#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667static char * wpa_config_write_eap(const struct parse_data *data,
1668 struct wpa_ssid *ssid)
1669{
1670 int i, ret;
1671 char *buf, *pos, *end;
1672 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1673 const char *name;
1674
1675 if (eap_methods == NULL)
1676 return NULL;
1677
1678 pos = buf = os_zalloc(100);
1679 if (buf == NULL)
1680 return NULL;
1681 end = buf + 100;
1682
1683 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1684 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1685 name = eap_get_name(eap_methods[i].vendor,
1686 eap_methods[i].method);
1687 if (name) {
1688 ret = os_snprintf(pos, end - pos, "%s%s",
1689 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001690 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 break;
1692 pos += ret;
1693 }
1694 }
1695
1696 end[-1] = '\0';
1697
1698 return buf;
1699}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001700#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701
1702
1703static int wpa_config_parse_password(const struct parse_data *data,
1704 struct wpa_ssid *ssid, int line,
1705 const char *value)
1706{
1707 u8 *hash;
1708
1709 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001710 if (!ssid->eap.password)
1711 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001713 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 ssid->eap.password = NULL;
1715 ssid->eap.password_len = 0;
1716 return 0;
1717 }
1718
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001719#ifdef CONFIG_EXT_PASSWORD
1720 if (os_strncmp(value, "ext:", 4) == 0) {
1721 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001722 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001723 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001724 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001725 ssid->eap.password = (u8 *) name;
1726 ssid->eap.password_len = os_strlen(name);
1727 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1728 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1729 return 0;
1730 }
1731#endif /* CONFIG_EXT_PASSWORD */
1732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 if (os_strncmp(value, "hash:", 5) != 0) {
1734 char *tmp;
1735 size_t res_len;
1736
1737 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001738 if (!tmp) {
1739 wpa_printf(MSG_ERROR,
1740 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 return -1;
1742 }
1743 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1744 (u8 *) tmp, res_len);
1745
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001746 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 ssid->eap.password = (u8 *) tmp;
1748 ssid->eap.password_len = res_len;
1749 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001750 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751
1752 return 0;
1753 }
1754
1755
1756 /* NtPasswordHash: hash:<32 hex digits> */
1757 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001758 wpa_printf(MSG_ERROR,
1759 "Line %d: Invalid password hash length (expected 32 hex digits)",
1760 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 return -1;
1762 }
1763
1764 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001765 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 return -1;
1767
1768 if (hexstr2bin(value + 5, hash, 16)) {
1769 os_free(hash);
1770 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1771 return -1;
1772 }
1773
1774 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1775
Dmitry Shmidte4663042016-04-04 10:07:49 -07001776 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1777 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1778 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1779 bin_clear_free(hash, 16);
1780 return 1;
1781 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001782 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 ssid->eap.password = hash;
1784 ssid->eap.password_len = 16;
1785 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001786 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787
1788 return 0;
1789}
1790
1791
Hai Shalomc3565922019-10-28 11:58:20 -07001792static int wpa_config_parse_machine_password(const struct parse_data *data,
1793 struct wpa_ssid *ssid, int line,
1794 const char *value)
1795{
1796 u8 *hash;
1797
1798 if (os_strcmp(value, "NULL") == 0) {
1799 if (!ssid->eap.machine_password)
1800 return 1; /* Already unset */
1801 wpa_printf(MSG_DEBUG,
1802 "Unset configuration string 'machine_password'");
1803 bin_clear_free(ssid->eap.machine_password,
1804 ssid->eap.machine_password_len);
1805 ssid->eap.machine_password = NULL;
1806 ssid->eap.machine_password_len = 0;
1807 return 0;
1808 }
1809
1810#ifdef CONFIG_EXT_PASSWORD
1811 if (os_strncmp(value, "ext:", 4) == 0) {
1812 char *name = os_strdup(value + 4);
1813
1814 if (!name)
1815 return -1;
1816 bin_clear_free(ssid->eap.machine_password,
1817 ssid->eap.machine_password_len);
1818 ssid->eap.machine_password = (u8 *) name;
1819 ssid->eap.machine_password_len = os_strlen(name);
1820 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1821 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1822 return 0;
1823 }
1824#endif /* CONFIG_EXT_PASSWORD */
1825
1826 if (os_strncmp(value, "hash:", 5) != 0) {
1827 char *tmp;
1828 size_t res_len;
1829
1830 tmp = wpa_config_parse_string(value, &res_len);
1831 if (!tmp) {
1832 wpa_printf(MSG_ERROR,
1833 "Line %d: failed to parse machine_password.",
1834 line);
1835 return -1;
1836 }
1837 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1838 (u8 *) tmp, res_len);
1839
1840 bin_clear_free(ssid->eap.machine_password,
1841 ssid->eap.machine_password_len);
1842 ssid->eap.machine_password = (u8 *) tmp;
1843 ssid->eap.machine_password_len = res_len;
1844 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1845 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1846
1847 return 0;
1848 }
1849
1850
1851 /* NtPasswordHash: hash:<32 hex digits> */
1852 if (os_strlen(value + 5) != 2 * 16) {
1853 wpa_printf(MSG_ERROR,
1854 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1855 line);
1856 return -1;
1857 }
1858
1859 hash = os_malloc(16);
1860 if (!hash)
1861 return -1;
1862
1863 if (hexstr2bin(value + 5, hash, 16)) {
1864 os_free(hash);
1865 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1866 line);
1867 return -1;
1868 }
1869
1870 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1871
1872 if (ssid->eap.machine_password &&
1873 ssid->eap.machine_password_len == 16 &&
1874 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1875 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1876 bin_clear_free(hash, 16);
1877 return 1;
1878 }
1879 bin_clear_free(ssid->eap.machine_password,
1880 ssid->eap.machine_password_len);
1881 ssid->eap.machine_password = hash;
1882 ssid->eap.machine_password_len = 16;
1883 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1884 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1885
1886 return 0;
1887}
1888
1889
Dmitry Shmidt83474442015-04-15 13:47:09 -07001890#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892static char * wpa_config_write_password(const struct parse_data *data,
1893 struct wpa_ssid *ssid)
1894{
1895 char *buf;
1896
Hai Shalomc3565922019-10-28 11:58:20 -07001897 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 return NULL;
1899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900#ifdef CONFIG_EXT_PASSWORD
1901 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1902 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001903 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904 return NULL;
1905 os_memcpy(buf, "ext:", 4);
1906 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1907 return buf;
1908 }
1909#endif /* CONFIG_EXT_PASSWORD */
1910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1912 return wpa_config_write_string(
1913 ssid->eap.password, ssid->eap.password_len);
1914 }
1915
1916 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001917 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 return NULL;
1919
1920 os_memcpy(buf, "hash:", 5);
1921 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1922
1923 return buf;
1924}
Hai Shalomc3565922019-10-28 11:58:20 -07001925
1926
1927static char * wpa_config_write_machine_password(const struct parse_data *data,
1928 struct wpa_ssid *ssid)
1929{
1930 char *buf;
1931
1932 if (!ssid->eap.machine_password)
1933 return NULL;
1934
1935#ifdef CONFIG_EXT_PASSWORD
1936 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1937 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1938 if (!buf)
1939 return NULL;
1940 os_memcpy(buf, "ext:", 4);
1941 os_memcpy(buf + 4, ssid->eap.machine_password,
1942 ssid->eap.machine_password_len);
1943 return buf;
1944 }
1945#endif /* CONFIG_EXT_PASSWORD */
1946
1947 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1948 return wpa_config_write_string(
1949 ssid->eap.machine_password,
1950 ssid->eap.machine_password_len);
1951 }
1952
1953 buf = os_malloc(5 + 32 + 1);
1954 if (!buf)
1955 return NULL;
1956
1957 os_memcpy(buf, "hash:", 5);
1958 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1959
1960 return buf;
1961}
1962
Dmitry Shmidt83474442015-04-15 13:47:09 -07001963#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964#endif /* IEEE8021X_EAPOL */
1965
1966
Hai Shalomfdcde762020-04-02 11:19:20 -07001967#ifdef CONFIG_WEP
1968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1970 const char *value, int idx)
1971{
1972 char *buf, title[20];
1973 int res;
1974
1975 buf = wpa_config_parse_string(value, len);
1976 if (buf == NULL) {
1977 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1978 line, idx, value);
1979 return -1;
1980 }
1981 if (*len > MAX_WEP_KEY_LEN) {
1982 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1983 line, idx, value);
1984 os_free(buf);
1985 return -1;
1986 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001987 if (*len && *len != 5 && *len != 13 && *len != 16) {
1988 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1989 "this network block will be ignored",
1990 line, (unsigned int) *len);
1991 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001993 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001995 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1997 return 0;
1998}
1999
2000
2001static int wpa_config_parse_wep_key0(const struct parse_data *data,
2002 struct wpa_ssid *ssid, int line,
2003 const char *value)
2004{
2005 return wpa_config_parse_wep_key(ssid->wep_key[0],
2006 &ssid->wep_key_len[0], line,
2007 value, 0);
2008}
2009
2010
2011static int wpa_config_parse_wep_key1(const struct parse_data *data,
2012 struct wpa_ssid *ssid, int line,
2013 const char *value)
2014{
2015 return wpa_config_parse_wep_key(ssid->wep_key[1],
2016 &ssid->wep_key_len[1], line,
2017 value, 1);
2018}
2019
2020
2021static int wpa_config_parse_wep_key2(const struct parse_data *data,
2022 struct wpa_ssid *ssid, int line,
2023 const char *value)
2024{
2025 return wpa_config_parse_wep_key(ssid->wep_key[2],
2026 &ssid->wep_key_len[2], line,
2027 value, 2);
2028}
2029
2030
2031static int wpa_config_parse_wep_key3(const struct parse_data *data,
2032 struct wpa_ssid *ssid, int line,
2033 const char *value)
2034{
2035 return wpa_config_parse_wep_key(ssid->wep_key[3],
2036 &ssid->wep_key_len[3], line,
2037 value, 3);
2038}
2039
2040
2041#ifndef NO_CONFIG_WRITE
2042static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2043{
2044 if (ssid->wep_key_len[idx] == 0)
2045 return NULL;
2046 return wpa_config_write_string(ssid->wep_key[idx],
2047 ssid->wep_key_len[idx]);
2048}
2049
2050
2051static char * wpa_config_write_wep_key0(const struct parse_data *data,
2052 struct wpa_ssid *ssid)
2053{
2054 return wpa_config_write_wep_key(ssid, 0);
2055}
2056
2057
2058static char * wpa_config_write_wep_key1(const struct parse_data *data,
2059 struct wpa_ssid *ssid)
2060{
2061 return wpa_config_write_wep_key(ssid, 1);
2062}
2063
2064
2065static char * wpa_config_write_wep_key2(const struct parse_data *data,
2066 struct wpa_ssid *ssid)
2067{
2068 return wpa_config_write_wep_key(ssid, 2);
2069}
2070
2071
2072static char * wpa_config_write_wep_key3(const struct parse_data *data,
2073 struct wpa_ssid *ssid)
2074{
2075 return wpa_config_write_wep_key(ssid, 3);
2076}
2077#endif /* NO_CONFIG_WRITE */
2078
Hai Shalomfdcde762020-04-02 11:19:20 -07002079#endif /* CONFIG_WEP */
2080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082#ifdef CONFIG_P2P
2083
Dmitry Shmidt54605472013-11-08 11:10:19 -08002084static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2085 struct wpa_ssid *ssid, int line,
2086 const char *value)
2087{
2088 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2089 os_strcmp(value, "any") == 0) {
2090 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2091 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2092 return 0;
2093 }
2094 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2095 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2096 line, value);
2097 return -1;
2098 }
2099 ssid->bssid_set = 1;
2100 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2101 MAC2STR(ssid->go_p2p_dev_addr));
2102 return 0;
2103}
2104
2105
2106#ifndef NO_CONFIG_WRITE
2107static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2108 struct wpa_ssid *ssid)
2109{
2110 char *value;
2111 int res;
2112
2113 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2114 return NULL;
2115
2116 value = os_malloc(20);
2117 if (value == NULL)
2118 return NULL;
2119 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002120 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002121 os_free(value);
2122 return NULL;
2123 }
2124 value[20 - 1] = '\0';
2125 return value;
2126}
2127#endif /* NO_CONFIG_WRITE */
2128
2129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2131 struct wpa_ssid *ssid, int line,
2132 const char *value)
2133{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002134 return wpa_config_parse_addr_list(data, line, value,
2135 &ssid->p2p_client_list,
2136 &ssid->num_p2p_clients,
2137 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002138}
2139
2140
2141#ifndef NO_CONFIG_WRITE
2142static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2143 struct wpa_ssid *ssid)
2144{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002145 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2146 ssid->num_p2p_clients,
2147 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002148}
2149#endif /* NO_CONFIG_WRITE */
2150
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002151
2152static int wpa_config_parse_psk_list(const struct parse_data *data,
2153 struct wpa_ssid *ssid, int line,
2154 const char *value)
2155{
2156 struct psk_list_entry *p;
2157 const char *pos;
2158
2159 p = os_zalloc(sizeof(*p));
2160 if (p == NULL)
2161 return -1;
2162
2163 pos = value;
2164 if (os_strncmp(pos, "P2P-", 4) == 0) {
2165 p->p2p = 1;
2166 pos += 4;
2167 }
2168
2169 if (hwaddr_aton(pos, p->addr)) {
2170 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2171 line, pos);
2172 os_free(p);
2173 return -1;
2174 }
2175 pos += 17;
2176 if (*pos != '-') {
2177 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2178 line, pos);
2179 os_free(p);
2180 return -1;
2181 }
2182 pos++;
2183
2184 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2185 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2186 line, pos);
2187 os_free(p);
2188 return -1;
2189 }
2190
2191 dl_list_add(&ssid->psk_list, &p->list);
2192
2193 return 0;
2194}
2195
2196
2197#ifndef NO_CONFIG_WRITE
2198static char * wpa_config_write_psk_list(const struct parse_data *data,
2199 struct wpa_ssid *ssid)
2200{
2201 return NULL;
2202}
2203#endif /* NO_CONFIG_WRITE */
2204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002205#endif /* CONFIG_P2P */
2206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002207
2208#ifdef CONFIG_MESH
2209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2211 struct wpa_ssid *ssid, int line,
2212 const char *value)
2213{
2214 int *rates = wpa_config_parse_int_array(value);
2215
2216 if (rates == NULL) {
2217 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2218 line, value);
2219 return -1;
2220 }
2221 if (rates[0] == 0) {
2222 os_free(rates);
2223 rates = NULL;
2224 }
2225
2226 os_free(ssid->mesh_basic_rates);
2227 ssid->mesh_basic_rates = rates;
2228
2229 return 0;
2230}
2231
2232
2233#ifndef NO_CONFIG_WRITE
2234
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002235static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2236 struct wpa_ssid *ssid)
2237{
2238 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2239}
2240
2241#endif /* NO_CONFIG_WRITE */
2242
2243#endif /* CONFIG_MESH */
2244
2245
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002246#ifdef CONFIG_MACSEC
2247
2248static int wpa_config_parse_mka_cak(const struct parse_data *data,
2249 struct wpa_ssid *ssid, int line,
2250 const char *value)
2251{
Hai Shalom74f70d42019-02-11 14:42:39 -08002252 size_t len;
2253
2254 len = os_strlen(value);
2255 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2256 (len != 2 * 16 && len != 2 * 32) ||
2257 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002258 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2259 line, value);
2260 return -1;
2261 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002262 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002263 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2264
Hai Shalom74f70d42019-02-11 14:42:39 -08002265 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2266 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002267 return 0;
2268}
2269
2270
2271static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2272 struct wpa_ssid *ssid, int line,
2273 const char *value)
2274{
Hai Shalom74f70d42019-02-11 14:42:39 -08002275 size_t len;
2276
2277 len = os_strlen(value);
2278 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2279 len < 2 || /* too short */
2280 len % 2 != 0 /* not an integral number of bytes */) {
2281 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2282 line, value);
2283 return -1;
2284 }
2285 ssid->mka_ckn_len = len / 2;
2286 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002287 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2288 line, value);
2289 return -1;
2290 }
2291
2292 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2293
Hai Shalom74f70d42019-02-11 14:42:39 -08002294 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2295 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002296 return 0;
2297}
2298
2299
2300#ifndef NO_CONFIG_WRITE
2301
2302static char * wpa_config_write_mka_cak(const struct parse_data *data,
2303 struct wpa_ssid *ssid)
2304{
2305 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2306 return NULL;
2307
Hai Shalom74f70d42019-02-11 14:42:39 -08002308 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002309}
2310
2311
2312static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2313 struct wpa_ssid *ssid)
2314{
2315 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2316 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002317 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002318}
2319
2320#endif /* NO_CONFIG_WRITE */
2321
2322#endif /* CONFIG_MACSEC */
2323
2324
Hai Shalom74f70d42019-02-11 14:42:39 -08002325#ifdef CONFIG_OCV
2326
2327static int wpa_config_parse_ocv(const struct parse_data *data,
2328 struct wpa_ssid *ssid, int line,
2329 const char *value)
2330{
2331 char *end;
2332
2333 ssid->ocv = strtol(value, &end, 0);
2334 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2335 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2336 line, value);
2337 return -1;
2338 }
2339 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2340 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2341 return 0;
2342}
2343
2344
2345#ifndef NO_CONFIG_WRITE
2346static char * wpa_config_write_ocv(const struct parse_data *data,
2347 struct wpa_ssid *ssid)
2348{
2349 char *value = os_malloc(20);
2350
2351 if (!value)
2352 return NULL;
2353 os_snprintf(value, 20, "%d", ssid->ocv);
2354 value[20 - 1] = '\0';
2355 return value;
2356}
2357#endif /* NO_CONFIG_WRITE */
2358
2359#endif /* CONFIG_OCV */
2360
2361
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002362static int wpa_config_parse_peerkey(const struct parse_data *data,
2363 struct wpa_ssid *ssid, int line,
2364 const char *value)
2365{
2366 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2367 return 0;
2368}
2369
2370
2371#ifndef NO_CONFIG_WRITE
2372static char * wpa_config_write_peerkey(const struct parse_data *data,
2373 struct wpa_ssid *ssid)
2374{
2375 return NULL;
2376}
2377#endif /* NO_CONFIG_WRITE */
2378
2379
Sunil Ravi77d572f2023-01-17 23:58:31 +00002380static int wpa_config_parse_mac_value(const struct parse_data *data,
2381 struct wpa_ssid *ssid, int line,
2382 const char *value)
2383{
2384 u8 mac_value[ETH_ALEN];
2385
2386 if (hwaddr_aton(value, mac_value) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002387 if (ether_addr_equal(mac_value, ssid->mac_value))
Sunil Ravi77d572f2023-01-17 23:58:31 +00002388 return 1;
2389 os_memcpy(ssid->mac_value, mac_value, ETH_ALEN);
2390 return 0;
2391 }
2392
2393 wpa_printf(MSG_ERROR, "Line %d: Invalid MAC address '%s'",
2394 line, value);
2395 return -1;
2396}
2397
2398
2399#ifndef NO_CONFIG_WRITE
2400static char * wpa_config_write_mac_value(const struct parse_data *data,
2401 struct wpa_ssid *ssid)
2402{
2403 const size_t size = 3 * ETH_ALEN;
2404 char *value;
2405 int res;
2406
2407 if (ssid->mac_addr != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS)
2408 return NULL;
2409
2410 value = os_malloc(size);
2411 if (!value)
2412 return NULL;
2413 res = os_snprintf(value, size, MACSTR, MAC2STR(ssid->mac_value));
2414 if (os_snprintf_error(size, res)) {
2415 os_free(value);
2416 return NULL;
2417 }
2418 value[size - 1] = '\0';
2419 return value;
2420}
2421#endif /* NO_CONFIG_WRITE */
2422
2423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424/* Helper macros for network block parser */
2425
2426#ifdef OFFSET
2427#undef OFFSET
2428#endif /* OFFSET */
2429/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2430#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2431
2432/* STR: Define a string variable for an ASCII string; f = field name */
2433#ifdef NO_CONFIG_WRITE
2434#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002435#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436#else /* NO_CONFIG_WRITE */
2437#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002438#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2439 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440#endif /* NO_CONFIG_WRITE */
2441#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002442#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002444#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445
2446/* STR_LEN: Define a string variable with a separate variable for storing the
2447 * data length. Unlike STR(), this can be used to store arbitrary binary data
2448 * (i.e., even nul termination character). */
2449#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002450#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002452#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2454
2455/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2456 * explicitly specified. */
2457#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2458#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2459#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2460
2461#ifdef NO_CONFIG_WRITE
2462#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002463#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464#else /* NO_CONFIG_WRITE */
2465#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2466 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002467#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2468 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469#endif /* NO_CONFIG_WRITE */
2470
2471/* INT: Define an integer variable */
2472#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002473#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474
2475/* INT_RANGE: Define an integer variable with allowed value range */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002476#ifdef NO_CONFIG_WRITE
2477#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, OFFSET(f), \
2478 (void *) 0, (void *) (min), (void *) (max), 0
2479#else /* NO_CONFIG_WRITE */
2480#define INT_RANGE(f, min, max) #f, wpa_config_parse_int_range, \
2481 wpa_config_write_int, OFFSET(f), \
2482 (void *) 0, (void *) (min), (void *) (max), 0
2483#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
2485/* FUNC: Define a configuration variable that uses a custom function for
2486 * parsing and writing the value. */
2487#ifdef NO_CONFIG_WRITE
2488#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2489#else /* NO_CONFIG_WRITE */
2490#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2491 NULL, NULL, NULL, NULL
2492#endif /* NO_CONFIG_WRITE */
2493#define FUNC(f) _FUNC(f), 0
2494#define FUNC_KEY(f) _FUNC(f), 1
2495
2496/*
2497 * Table of network configuration variables. This table is used to parse each
2498 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2499 * that is inside a network block.
2500 *
2501 * This table is generated using the helper macros defined above and with
2502 * generous help from the C pre-processor. The field name is stored as a string
2503 * into .name and for STR and INT types, the offset of the target buffer within
2504 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2505 * offset to the field containing the length of the configuration variable.
2506 * .param3 and .param4 can be used to mark the allowed range (length for STR
2507 * and value for INT).
2508 *
2509 * For each configuration line in wpa_supplicant.conf, the parser goes through
2510 * this table and select the entry that matches with the field name. The parser
2511 * function (.parser) is then called to parse the actual value of the field.
2512 *
2513 * This kind of mechanism makes it easy to add new configuration parameters,
2514 * since only one line needs to be added into this table and into the
2515 * struct wpa_ssid definition if the new variable is either a string or
2516 * integer. More complex types will need to use their own parser and writer
2517 * functions.
2518 */
2519static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002520 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 { INT_RANGE(scan_ssid, 0, 1) },
2522 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002523 { FUNC(bssid_hint) },
Hai Shalom60840252021-02-19 19:02:11 -08002524 { FUNC(bssid_ignore) },
2525 { FUNC(bssid_accept) },
2526 { FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2527 { FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002529 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002530 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002531 { STR(sae_password_id) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 { FUNC(proto) },
2533 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002534 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 { FUNC(pairwise) },
2536 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002537 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 { FUNC(auth_alg) },
2539 { FUNC(scan_freq) },
2540 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002541 { INT_RANGE(ht, 0, 1) },
2542 { INT_RANGE(vht, 0, 1) },
Sunil Ravi640215c2023-06-28 23:08:09 +00002543 { INT_RANGE(he, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002544 { INT_RANGE(ht40, -1, 1) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002545 { INT_RANGE(max_oper_chwidth, CONF_OPER_CHWIDTH_USE_HT,
2546 CONF_OPER_CHWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002547 { INT(vht_center_freq1) },
2548 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549#ifdef IEEE8021X_EAPOL
2550 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002551 { STR_LENe(identity, identity) },
2552 { STR_LENe(anonymous_identity, anonymous_identity) },
2553 { STR_LENe(imsi_identity, imsi_identity) },
2554 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002556 { FUNC_KEY(machine_password) },
2557 { STRe(ca_cert, cert.ca_cert) },
2558 { STRe(ca_path, cert.ca_path) },
2559 { STRe(client_cert, cert.client_cert) },
2560 { STRe(private_key, cert.private_key) },
2561 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002562 { STRe(subject_match, cert.subject_match) },
2563 { STRe(check_cert_subject, cert.check_cert_subject) },
2564 { STRe(altsubject_match, cert.altsubject_match) },
2565 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2566 { STRe(domain_match, cert.domain_match) },
2567 { STRe(ca_cert2, phase2_cert.ca_cert) },
2568 { STRe(ca_path2, phase2_cert.ca_path) },
2569 { STRe(client_cert2, phase2_cert.client_cert) },
2570 { STRe(private_key2, phase2_cert.private_key) },
2571 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002572 { STRe(subject_match2, phase2_cert.subject_match) },
2573 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2574 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2575 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2576 { STRe(domain_match2, phase2_cert.domain_match) },
2577 { STRe(phase1, phase1) },
2578 { STRe(phase2, phase2) },
2579 { STRe(machine_phase2, machine_phase2) },
2580 { STRe(pcsc, pcsc) },
2581 { STR_KEYe(pin, cert.pin) },
2582 { STRe(engine_id, cert.engine_id) },
2583 { STRe(key_id, cert.key_id) },
2584 { STRe(cert_id, cert.cert_id) },
2585 { STRe(ca_cert_id, cert.ca_cert_id) },
2586 { STR_KEYe(pin2, phase2_cert.pin) },
2587 { STRe(engine_id2, phase2_cert.engine_id) },
2588 { STRe(key_id2, phase2_cert.key_id) },
2589 { STRe(cert_id2, phase2_cert.cert_id) },
2590 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2591 { INTe(engine, cert.engine) },
2592 { INTe(engine2, phase2_cert.engine) },
2593 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2594 { STRe(machine_ca_path, machine_cert.ca_path) },
2595 { STRe(machine_client_cert, machine_cert.client_cert) },
2596 { STRe(machine_private_key, machine_cert.private_key) },
2597 { STR_KEYe(machine_private_key_passwd,
2598 machine_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002599 { STRe(machine_subject_match, machine_cert.subject_match) },
2600 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2601 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2602 { STRe(machine_domain_suffix_match,
2603 machine_cert.domain_suffix_match) },
2604 { STRe(machine_domain_match, machine_cert.domain_match) },
2605 { STR_KEYe(machine_pin, machine_cert.pin) },
2606 { STRe(machine_engine_id, machine_cert.engine_id) },
2607 { STRe(machine_key_id, machine_cert.key_id) },
2608 { STRe(machine_cert_id, machine_cert.cert_id) },
2609 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2610 { INTe(machine_engine, machine_cert.engine) },
2611 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002613 { INTe(sim_num, sim_num) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002614 { STRe(imsi_privacy_cert, imsi_privacy_cert) },
2615 { STRe(imsi_privacy_attr, imsi_privacy_attr) },
Steven Liu9138d432022-11-23 22:29:05 +00002616 { INTe(strict_conservative_peer_mode, strict_conservative_peer_mode) },
Hai Shalomc3565922019-10-28 11:58:20 -07002617 { STRe(openssl_ciphers, openssl_ciphers) },
2618 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -07002620#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 { FUNC_KEY(wep_key0) },
2622 { FUNC_KEY(wep_key1) },
2623 { FUNC_KEY(wep_key2) },
2624 { FUNC_KEY(wep_key3) },
2625 { INT(wep_tx_keyidx) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002626#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 { INT(priority) },
2628#ifdef IEEE8021X_EAPOL
2629 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002630 { STRe(pac_file, pac_file) },
2631 { INTe(fragment_size, fragment_size) },
2632 { INTe(ocsp, cert.ocsp) },
2633 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002635#ifdef CONFIG_MESH
2636 { INT_RANGE(mode, 0, 5) },
2637 { INT_RANGE(no_auto_peer, 0, 1) },
Hai Shaloma20dcd72022-02-04 13:43:00 -08002638 { INT_RANGE(mesh_fwding, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002639 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002640#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002642#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 { INT_RANGE(proactive_key_caching, 0, 1) },
2644 { INT_RANGE(disabled, 0, 2) },
2645 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002647#ifdef CONFIG_OCV
2648 { FUNC(ocv) },
2649#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002650 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002652 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002653 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002654 { INT_RANGE(enable_edmg, 0, 1) },
2655 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002656#ifdef CONFIG_ACS
2657 { INT_RANGE(acs, 0, 1) },
2658#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002659#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 { FUNC(mesh_basic_rates) },
2661 { INT(dot11MeshMaxRetries) },
2662 { INT(dot11MeshRetryTimeout) },
2663 { INT(dot11MeshConfirmTimeout) },
2664 { INT(dot11MeshHoldingTimeout) },
2665#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 { INT(wpa_ptk_rekey) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002667 { INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002668 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002670 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002672 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002673 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002674 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002675#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002676#ifdef CONFIG_HT_OVERRIDES
2677 { INT_RANGE(disable_ht, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002678 { INT_RANGE(disable_ht40, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002679 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002680 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002681 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002682 { INT_RANGE(tx_stbc, -1, 1) },
2683 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002684 { INT_RANGE(disable_max_amsdu, -1, 1) },
2685 { INT_RANGE(ampdu_factor, -1, 3) },
2686 { INT_RANGE(ampdu_density, -1, 7) },
2687 { STR(ht_mcs) },
2688#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002689#ifdef CONFIG_VHT_OVERRIDES
2690 { INT_RANGE(disable_vht, 0, 1) },
2691 { INT(vht_capa) },
2692 { INT(vht_capa_mask) },
2693 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2694 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2695 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2696 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2697 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2698 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2699 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2700 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2701 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2702 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2703 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2704 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2705 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2706 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2707 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2708 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2709#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07002710#ifdef CONFIG_HE_OVERRIDES
2711 { INT_RANGE(disable_he, 0, 1)},
2712#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002713 { INT(ap_max_inactivity) },
2714 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002715 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002716#ifdef CONFIG_MACSEC
2717 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002718 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002719 { INT_RANGE(macsec_replay_protect, 0, 1) },
2720 { INT(macsec_replay_window) },
Sunil Ravi036cec52023-03-29 11:35:17 -07002721 { INT_RANGE(macsec_offload, 0, 2) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002722 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002723 { INT_RANGE(mka_priority, 0, 255) },
Sunil Ravia04bd252022-05-02 22:54:18 -07002724 { INT_RANGE(macsec_csindex, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002725 { FUNC_KEY(mka_cak) },
2726 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002727#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002728#ifdef CONFIG_HS20
2729 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002730 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002731#endif /* CONFIG_HS20 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002732 { INT_RANGE(mac_addr, 0, 3) },
2733 { FUNC_KEY(mac_value) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002734 { INT_RANGE(pbss, 0, 2) },
2735 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002736 { INT_RANGE(fils_dh_group, 0, 65535) },
2737#ifdef CONFIG_DPP
2738 { STR(dpp_connector) },
2739 { STR_LEN(dpp_netaccesskey) },
2740 { INT(dpp_netaccesskey_expiry) },
2741 { STR_LEN(dpp_csign) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002742 { STR_LEN(dpp_pp_key) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002743 { INT_RANGE(dpp_pfs, 0, 2) },
Sunil Ravi89eba102022-09-13 21:04:37 -07002744 { INT_RANGE(dpp_connector_privacy, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002745#endif /* CONFIG_DPP */
2746 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002747 { INT_RANGE(owe_only, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002748 { INT_RANGE(owe_ptk_workaround, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002749 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002750 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Sunil Ravi99c035e2024-07-12 01:42:03 +00002751 { INT_RANGE(multi_ap_profile, MULTI_AP_PROFILE_1,
2752 MULTI_AP_PROFILE_MAX) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002753 { INT_RANGE(beacon_prot, 0, 1) },
2754 { INT_RANGE(transition_disable, 0, 255) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002755 { INT_RANGE(sae_pk, 0, 2) },
Sunil Ravi77d572f2023-01-17 23:58:31 +00002756 { INT_RANGE(disable_eht, 0, 1)},
Sunil Ravi036cec52023-03-29 11:35:17 -07002757 { INT_RANGE(enable_4addr_mode, 0, 1)},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758};
2759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760#undef OFFSET
2761#undef _STR
2762#undef STR
2763#undef STR_KEY
2764#undef _STR_LEN
2765#undef STR_LEN
2766#undef STR_LEN_KEY
2767#undef _STR_RANGE
2768#undef STR_RANGE
2769#undef STR_RANGE_KEY
2770#undef _INT
2771#undef INT
2772#undef INT_RANGE
2773#undef _FUNC
2774#undef FUNC
2775#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002776#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
2778
2779/**
2780 * wpa_config_add_prio_network - Add a network to priority lists
2781 * @config: Configuration data from wpa_config_read()
2782 * @ssid: Pointer to the network configuration to be added to the list
2783 * Returns: 0 on success, -1 on failure
2784 *
2785 * This function is used to add a network block to the priority list of
2786 * networks. This must be called for each network when reading in the full
2787 * configuration. In addition, this can be used indirectly when updating
2788 * priorities by calling wpa_config_update_prio_list().
2789 */
2790int wpa_config_add_prio_network(struct wpa_config *config,
2791 struct wpa_ssid *ssid)
2792{
Hai Shalomfdcde762020-04-02 11:19:20 -07002793 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 struct wpa_ssid *prev, **nlist;
2795
2796 /*
2797 * Add to an existing priority list if one is available for the
2798 * configured priority level for this network.
2799 */
2800 for (prio = 0; prio < config->num_prio; prio++) {
2801 prev = config->pssid[prio];
2802 if (prev->priority == ssid->priority) {
2803 while (prev->pnext)
2804 prev = prev->pnext;
2805 prev->pnext = ssid;
2806 return 0;
2807 }
2808 }
2809
2810 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002811 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2812 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 if (nlist == NULL)
2814 return -1;
2815
2816 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002817 if (nlist[prio]->priority < ssid->priority) {
2818 os_memmove(&nlist[prio + 1], &nlist[prio],
2819 (config->num_prio - prio) *
2820 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 }
2824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 nlist[prio] = ssid;
2826 config->num_prio++;
2827 config->pssid = nlist;
2828
2829 return 0;
2830}
2831
2832
2833/**
2834 * wpa_config_update_prio_list - Update network priority list
2835 * @config: Configuration data from wpa_config_read()
2836 * Returns: 0 on success, -1 on failure
2837 *
2838 * This function is called to update the priority list of networks in the
2839 * configuration when a network is being added or removed. This is also called
2840 * if a priority for a network is changed.
2841 */
2842int wpa_config_update_prio_list(struct wpa_config *config)
2843{
2844 struct wpa_ssid *ssid;
2845 int ret = 0;
2846
2847 os_free(config->pssid);
2848 config->pssid = NULL;
2849 config->num_prio = 0;
2850
2851 ssid = config->ssid;
2852 while (ssid) {
2853 ssid->pnext = NULL;
2854 if (wpa_config_add_prio_network(config, ssid) < 0)
2855 ret = -1;
2856 ssid = ssid->next;
2857 }
2858
2859 return ret;
2860}
2861
2862
2863#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002864
2865static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2866{
2867 os_free(cert->ca_cert);
2868 os_free(cert->ca_path);
2869 os_free(cert->client_cert);
2870 os_free(cert->private_key);
2871 str_clear_free(cert->private_key_passwd);
Hai Shalomc3565922019-10-28 11:58:20 -07002872 os_free(cert->subject_match);
2873 os_free(cert->check_cert_subject);
2874 os_free(cert->altsubject_match);
2875 os_free(cert->domain_suffix_match);
2876 os_free(cert->domain_match);
2877 str_clear_free(cert->pin);
2878 os_free(cert->engine_id);
2879 os_free(cert->key_id);
2880 os_free(cert->cert_id);
2881 os_free(cert->ca_cert_id);
2882}
2883
2884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885static void eap_peer_config_free(struct eap_peer_config *eap)
2886{
2887 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002888 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002890 os_free(eap->imsi_identity);
Sunil8cd6f4d2022-06-28 18:40:46 +00002891 os_free(eap->imsi_privacy_cert);
2892 os_free(eap->imsi_privacy_attr);
Hai Shalomc3565922019-10-28 11:58:20 -07002893 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002894 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002895 bin_clear_free(eap->machine_password, eap->machine_password_len);
2896 eap_peer_config_free_cert(&eap->cert);
2897 eap_peer_config_free_cert(&eap->phase2_cert);
2898 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 os_free(eap->phase1);
2900 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002901 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 os_free(eap->otp);
2904 os_free(eap->pending_req_otp);
2905 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002906 bin_clear_free(eap->new_password, eap->new_password_len);
2907 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002908 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909}
Hai Shalomc3565922019-10-28 11:58:20 -07002910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911#endif /* IEEE8021X_EAPOL */
2912
2913
2914/**
2915 * wpa_config_free_ssid - Free network/ssid configuration data
2916 * @ssid: Configuration data for the network
2917 *
2918 * This function frees all resources allocated for the network configuration
2919 * data.
2920 */
2921void wpa_config_free_ssid(struct wpa_ssid *ssid)
2922{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002923 struct psk_list_entry *psk;
2924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002926 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002927 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002928 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002929 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930#ifdef IEEE8021X_EAPOL
2931 eap_peer_config_free(&ssid->eap);
2932#endif /* IEEE8021X_EAPOL */
2933 os_free(ssid->id_str);
2934 os_free(ssid->scan_freq);
2935 os_free(ssid->freq_list);
2936 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002937 os_free(ssid->p2p_client_list);
Hai Shalom60840252021-02-19 19:02:11 -08002938 os_free(ssid->bssid_ignore);
2939 os_free(ssid->bssid_accept);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002940#ifdef CONFIG_HT_OVERRIDES
2941 os_free(ssid->ht_mcs);
2942#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002943#ifdef CONFIG_MESH
2944 os_free(ssid->mesh_basic_rates);
2945#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002946#ifdef CONFIG_HS20
2947 os_free(ssid->roaming_consortium_selection);
2948#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002949 os_free(ssid->dpp_connector);
2950 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2951 os_free(ssid->dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -07002952 os_free(ssid->dpp_pp_key);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002953 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2954 list))) {
2955 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002956 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002957 }
Hai Shalomc3565922019-10-28 11:58:20 -07002958#ifdef CONFIG_SAE
2959 sae_deinit_pt(ssid->pt);
2960#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002961 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962}
2963
2964
Dmitry Shmidt04949592012-07-19 12:16:46 -07002965void wpa_config_free_cred(struct wpa_cred *cred)
2966{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002967 size_t i;
2968
Dmitry Shmidt04949592012-07-19 12:16:46 -07002969 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002970 str_clear_free(cred->username);
2971 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972 os_free(cred->ca_cert);
2973 os_free(cred->client_cert);
2974 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002975 str_clear_free(cred->private_key_passwd);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002976 os_free(cred->engine_id);
2977 os_free(cred->ca_cert_id);
2978 os_free(cred->cert_id);
2979 os_free(cred->key_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002980 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002981 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002982 for (i = 0; i < cred->num_domain; i++)
2983 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002984 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002985 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002986 os_free(cred->eap_method);
2987 os_free(cred->phase1);
2988 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002989 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002990 os_free(cred->roaming_partner);
2991 os_free(cred->provisioning_sp);
2992 for (i = 0; i < cred->num_req_conn_capab; i++)
2993 os_free(cred->req_conn_capab_port[i]);
2994 os_free(cred->req_conn_capab_port);
2995 os_free(cred->req_conn_capab_proto);
Sunil8cd6f4d2022-06-28 18:40:46 +00002996 os_free(cred->imsi_privacy_cert);
2997 os_free(cred->imsi_privacy_attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002998 os_free(cred);
2999}
3000
3001
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003002void wpa_config_flush_blobs(struct wpa_config *config)
3003{
3004#ifndef CONFIG_NO_CONFIG_BLOBS
3005 struct wpa_config_blob *blob, *prev;
3006
3007 blob = config->blobs;
3008 config->blobs = NULL;
3009 while (blob) {
3010 prev = blob;
3011 blob = blob->next;
3012 wpa_config_free_blob(prev);
3013 }
3014#endif /* CONFIG_NO_CONFIG_BLOBS */
3015}
3016
3017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018/**
3019 * wpa_config_free - Free configuration data
3020 * @config: Configuration data from wpa_config_read()
3021 *
3022 * This function frees all resources allocated for the configuration data by
3023 * wpa_config_read().
3024 */
3025void wpa_config_free(struct wpa_config *config)
3026{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003028 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003029 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030
3031 ssid = config->ssid;
3032 while (ssid) {
3033 prev = ssid;
3034 ssid = ssid->next;
3035 wpa_config_free_ssid(prev);
3036 }
3037
Dmitry Shmidt04949592012-07-19 12:16:46 -07003038 cred = config->cred;
3039 while (cred) {
3040 cprev = cred;
3041 cred = cred->next;
3042 wpa_config_free_cred(cprev);
3043 }
3044
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003045 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
Dmitry Shmidt04949592012-07-19 12:16:46 -07003047 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003048 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
3049 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 os_free(config->ctrl_interface);
3051 os_free(config->ctrl_interface_group);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003052#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 os_free(config->opensc_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003054#endif /* CONFIG_OPENSC_ENGINE_PATH */
3055#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 os_free(config->pkcs11_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003057#endif /* CONFIG_PKCS11_ENGINE_PATH */
3058#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 os_free(config->pkcs11_module_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003060#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003061 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003062 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003063 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 os_free(config->driver_param);
3065 os_free(config->device_name);
3066 os_free(config->manufacturer);
3067 os_free(config->model_name);
3068 os_free(config->model_number);
3069 os_free(config->serial_number);
3070 os_free(config->config_methods);
3071 os_free(config->p2p_ssid_postfix);
3072 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003073 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003074 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003075 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003076 os_free(config->freq_list);
Hai Shalom60840252021-02-19 19:02:11 -08003077 os_free(config->initial_freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003078 wpabuf_free(config->wps_nfc_dh_pubkey);
3079 wpabuf_free(config->wps_nfc_dh_privkey);
3080 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003081 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003082 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003083 wpabuf_free(config->ap_vendor_elements);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003084 wpabuf_free(config->ap_assocresp_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003085 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003086 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003087 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003088 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003089 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003090#ifdef CONFIG_MBO
3091 os_free(config->non_pref_chan);
3092#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07003093 os_free(config->dpp_name);
3094 os_free(config->dpp_mud_url);
Sunil Ravi89eba102022-09-13 21:04:37 -07003095 os_free(config->dpp_extra_conf_req_name);
3096 os_free(config->dpp_extra_conf_req_value);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 os_free(config);
3099}
3100
3101
3102/**
3103 * wpa_config_foreach_network - Iterate over each configured network
3104 * @config: Configuration data from wpa_config_read()
3105 * @func: Callback function to process each network
3106 * @arg: Opaque argument to pass to callback function
3107 *
3108 * Iterate over the set of configured networks calling the specified
3109 * function for each item. We guard against callbacks removing the
3110 * supplied network.
3111 */
3112void wpa_config_foreach_network(struct wpa_config *config,
3113 void (*func)(void *, struct wpa_ssid *),
3114 void *arg)
3115{
3116 struct wpa_ssid *ssid, *next;
3117
3118 ssid = config->ssid;
3119 while (ssid) {
3120 next = ssid->next;
3121 func(arg, ssid);
3122 ssid = next;
3123 }
3124}
3125
3126
3127/**
3128 * wpa_config_get_network - Get configured network based on id
3129 * @config: Configuration data from wpa_config_read()
3130 * @id: Unique network id to search for
3131 * Returns: Network configuration or %NULL if not found
3132 */
3133struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3134{
3135 struct wpa_ssid *ssid;
3136
3137 ssid = config->ssid;
3138 while (ssid) {
3139 if (id == ssid->id)
3140 break;
3141 ssid = ssid->next;
3142 }
3143
3144 return ssid;
3145}
3146
3147
3148/**
3149 * wpa_config_add_network - Add a new network with empty configuration
3150 * @config: Configuration data from wpa_config_read()
3151 * Returns: The new network configuration or %NULL if operation failed
3152 */
3153struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3154{
3155 int id;
3156 struct wpa_ssid *ssid, *last = NULL;
3157
3158 id = -1;
3159 ssid = config->ssid;
3160 while (ssid) {
3161 if (ssid->id > id)
3162 id = ssid->id;
3163 last = ssid;
3164 ssid = ssid->next;
3165 }
3166 id++;
3167
3168 ssid = os_zalloc(sizeof(*ssid));
3169 if (ssid == NULL)
3170 return NULL;
3171 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003172 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 if (last)
3174 last->next = ssid;
3175 else
3176 config->ssid = ssid;
3177
3178 wpa_config_update_prio_list(config);
3179
3180 return ssid;
3181}
3182
3183
3184/**
3185 * wpa_config_remove_network - Remove a configured network based on id
3186 * @config: Configuration data from wpa_config_read()
3187 * @id: Unique network id to search for
3188 * Returns: 0 on success, or -1 if the network was not found
3189 */
3190int wpa_config_remove_network(struct wpa_config *config, int id)
3191{
3192 struct wpa_ssid *ssid, *prev = NULL;
3193
3194 ssid = config->ssid;
3195 while (ssid) {
3196 if (id == ssid->id)
3197 break;
3198 prev = ssid;
3199 ssid = ssid->next;
3200 }
3201
3202 if (ssid == NULL)
3203 return -1;
3204
3205 if (prev)
3206 prev->next = ssid->next;
3207 else
3208 config->ssid = ssid->next;
3209
3210 wpa_config_update_prio_list(config);
3211 wpa_config_free_ssid(ssid);
3212 return 0;
3213}
3214
3215
3216/**
3217 * wpa_config_set_network_defaults - Set network default values
3218 * @ssid: Pointer to network configuration data
3219 */
3220void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3221{
3222 ssid->proto = DEFAULT_PROTO;
3223 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3224 ssid->group_cipher = DEFAULT_GROUP;
3225 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Hai Shalomfdcde762020-04-02 11:19:20 -07003226 ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003227 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003228 ssid->ht = 1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003229 ssid->vht = 1;
3230 ssid->he = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231#ifdef IEEE8021X_EAPOL
3232 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3233 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3234 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003235 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003238 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3239 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3240 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3241 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003242 ssid->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003243 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003244#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003245#ifdef CONFIG_HT_OVERRIDES
3246 ssid->disable_ht = DEFAULT_DISABLE_HT;
3247 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003248 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003249 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003250 ssid->tx_stbc = DEFAULT_TX_STBC;
3251 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003252 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3253 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3254 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3255#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003256#ifdef CONFIG_VHT_OVERRIDES
3257 ssid->vht_rx_mcs_nss_1 = -1;
3258 ssid->vht_rx_mcs_nss_2 = -1;
3259 ssid->vht_rx_mcs_nss_3 = -1;
3260 ssid->vht_rx_mcs_nss_4 = -1;
3261 ssid->vht_rx_mcs_nss_5 = -1;
3262 ssid->vht_rx_mcs_nss_6 = -1;
3263 ssid->vht_rx_mcs_nss_7 = -1;
3264 ssid->vht_rx_mcs_nss_8 = -1;
3265 ssid->vht_tx_mcs_nss_1 = -1;
3266 ssid->vht_tx_mcs_nss_2 = -1;
3267 ssid->vht_tx_mcs_nss_3 = -1;
3268 ssid->vht_tx_mcs_nss_4 = -1;
3269 ssid->vht_tx_mcs_nss_5 = -1;
3270 ssid->vht_tx_mcs_nss_6 = -1;
3271 ssid->vht_tx_mcs_nss_7 = -1;
3272 ssid->vht_tx_mcs_nss_8 = -1;
3273#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003274 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003275 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003276 ssid->sae_pwe = DEFAULT_SAE_PWE;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003277#ifdef CONFIG_MACSEC
3278 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3279#endif /* CONFIG_MACSEC */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003280 ssid->mac_addr = WPAS_MAC_ADDR_STYLE_NOT_SET;
Hai Shalom74f70d42019-02-11 14:42:39 -08003281 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282}
3283
3284
Sunil Ravia04bd252022-05-02 22:54:18 -07003285static const char *removed_fields[] = {
3286 "dh_file",
3287 "dh_file2",
3288 "machine_dh_file",
3289 NULL
3290};
3291
3292static bool removed_field(const char *field)
3293{
3294 int i;
3295
3296 for (i = 0; removed_fields[i]; i++) {
3297 if (os_strcmp(field, removed_fields[i]) == 0)
3298 return true;
3299 }
3300
3301 return false;
3302}
3303
3304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305/**
3306 * wpa_config_set - Set a variable in network configuration
3307 * @ssid: Pointer to network configuration data
3308 * @var: Variable name, e.g., "ssid"
3309 * @value: Variable value
3310 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003311 * Returns: 0 on success with possible change in the value, 1 on success with
3312 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 *
3314 * This function can be used to set network configuration variables based on
3315 * both the configuration file and management interface input. The value
3316 * parameter must be in the same format as the text-based configuration file is
3317 * using. For example, strings are using double quotation marks.
3318 */
3319int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3320 int line)
3321{
3322 size_t i;
3323 int ret = 0;
3324
3325 if (ssid == NULL || var == NULL || value == NULL)
3326 return -1;
3327
3328 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3329 const struct parse_data *field = &ssid_fields[i];
3330 if (os_strcmp(var, field->name) != 0)
3331 continue;
3332
Dmitry Shmidte4663042016-04-04 10:07:49 -07003333 ret = field->parser(field, ssid, line, value);
3334 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335 if (line) {
3336 wpa_printf(MSG_ERROR, "Line %d: failed to "
3337 "parse %s '%s'.", line, var, value);
3338 }
3339 ret = -1;
3340 }
Hai Shalomc3565922019-10-28 11:58:20 -07003341#ifdef CONFIG_SAE
3342 if (os_strcmp(var, "ssid") == 0 ||
3343 os_strcmp(var, "psk") == 0 ||
3344 os_strcmp(var, "sae_password") == 0 ||
3345 os_strcmp(var, "sae_password_id") == 0) {
3346 sae_deinit_pt(ssid->pt);
3347 ssid->pt = NULL;
3348 }
3349#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 break;
3351 }
3352 if (i == NUM_SSID_FIELDS) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003353 if (removed_field(var)) {
3354 wpa_printf(MSG_INFO,
3355 "Line %d: Ignore removed configuration field '%s'",
3356 line, var);
3357 return ret;
3358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359 if (line) {
3360 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3361 "'%s'.", line, var);
3362 }
3363 ret = -1;
3364 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003365 ssid->was_recently_reconfigured = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366
3367 return ret;
3368}
3369
3370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003371int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3372 const char *value)
3373{
3374 size_t len;
3375 char *buf;
3376 int ret;
3377
3378 len = os_strlen(value);
3379 buf = os_malloc(len + 3);
3380 if (buf == NULL)
3381 return -1;
3382 buf[0] = '"';
3383 os_memcpy(buf + 1, value, len);
3384 buf[len + 1] = '"';
3385 buf[len + 2] = '\0';
3386 ret = wpa_config_set(ssid, var, buf, 0);
3387 os_free(buf);
3388 return ret;
3389}
3390
3391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392/**
3393 * wpa_config_get_all - Get all options from network configuration
3394 * @ssid: Pointer to network configuration data
3395 * @get_keys: Determines if keys/passwords will be included in returned list
3396 * (if they may be exported)
3397 * Returns: %NULL terminated list of all set keys and their values in the form
3398 * of [key1, val1, key2, val2, ... , NULL]
3399 *
3400 * This function can be used to get list of all configured network properties.
3401 * The caller is responsible for freeing the returned list and all its
3402 * elements.
3403 */
3404char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3405{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003406#ifdef NO_CONFIG_WRITE
3407 return NULL;
3408#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 const struct parse_data *field;
3410 char *key, *value;
3411 size_t i;
3412 char **props;
3413 int fields_num;
3414
3415 get_keys = get_keys && ssid->export_keys;
3416
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003417 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 if (!props)
3419 return NULL;
3420
3421 fields_num = 0;
3422 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3423 field = &ssid_fields[i];
3424 if (field->key_data && !get_keys)
3425 continue;
3426 value = field->writer(field, ssid);
3427 if (value == NULL)
3428 continue;
3429 if (os_strlen(value) == 0) {
3430 os_free(value);
3431 continue;
3432 }
3433
3434 key = os_strdup(field->name);
3435 if (key == NULL) {
3436 os_free(value);
3437 goto err;
3438 }
3439
3440 props[fields_num * 2] = key;
3441 props[fields_num * 2 + 1] = value;
3442
3443 fields_num++;
3444 }
3445
3446 return props;
3447
3448err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003449 for (i = 0; props[i]; i++)
3450 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 os_free(props);
3452 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003453#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454}
3455
3456
3457#ifndef NO_CONFIG_WRITE
3458/**
3459 * wpa_config_get - Get a variable in network configuration
3460 * @ssid: Pointer to network configuration data
3461 * @var: Variable name, e.g., "ssid"
3462 * Returns: Value of the variable or %NULL on failure
3463 *
3464 * This function can be used to get network configuration variables. The
3465 * returned value is a copy of the configuration variable in text format, i.e,.
3466 * the same format that the text-based configuration file and wpa_config_set()
3467 * are using for the value. The caller is responsible for freeing the returned
3468 * value.
3469 */
3470char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3471{
3472 size_t i;
3473
3474 if (ssid == NULL || var == NULL)
3475 return NULL;
3476
3477 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3478 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003479 if (os_strcmp(var, field->name) == 0) {
3480 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003481
3482 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003483 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003484 "Found newline in value for %s; not returning it",
3485 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003486 os_free(ret);
3487 ret = NULL;
3488 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003489
Paul Stewart85c72c62016-03-03 15:33:47 -08003490 return ret;
3491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492 }
3493
3494 return NULL;
3495}
3496
3497
3498/**
3499 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3500 * @ssid: Pointer to network configuration data
3501 * @var: Variable name, e.g., "ssid"
3502 * Returns: Value of the variable or %NULL on failure
3503 *
3504 * This function can be used to get network configuration variable like
3505 * wpa_config_get(). The only difference is that this functions does not expose
3506 * key/password material from the configuration. In case a key/password field
3507 * is requested, the returned value is an empty string or %NULL if the variable
3508 * is not set or "*" if the variable is set (regardless of its value). The
3509 * returned value is a copy of the configuration variable in text format, i.e,.
3510 * the same format that the text-based configuration file and wpa_config_set()
3511 * are using for the value. The caller is responsible for freeing the returned
3512 * value.
3513 */
3514char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3515{
3516 size_t i;
3517
3518 if (ssid == NULL || var == NULL)
3519 return NULL;
3520
3521 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3522 const struct parse_data *field = &ssid_fields[i];
3523 if (os_strcmp(var, field->name) == 0) {
3524 char *res = field->writer(field, ssid);
3525 if (field->key_data) {
3526 if (res && res[0]) {
3527 wpa_printf(MSG_DEBUG, "Do not allow "
3528 "key_data field to be "
3529 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003530 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 return os_strdup("*");
3532 }
3533
3534 os_free(res);
3535 return NULL;
3536 }
3537 return res;
3538 }
3539 }
3540
3541 return NULL;
3542}
3543#endif /* NO_CONFIG_WRITE */
3544
3545
3546/**
3547 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3548 * @ssid: Pointer to network configuration data
3549 *
3550 * This function must be called to update WPA PSK when either SSID or the
3551 * passphrase has changed for the network configuration.
3552 */
3553void wpa_config_update_psk(struct wpa_ssid *ssid)
3554{
3555#ifndef CONFIG_NO_PBKDF2
Sunil Ravia04bd252022-05-02 22:54:18 -07003556 if (pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3557 ssid->psk, PMK_LEN) != 0) {
3558 wpa_printf(MSG_ERROR, "Error in pbkdf2_sha1()");
3559 return;
3560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3562 ssid->psk, PMK_LEN);
3563 ssid->psk_set = 1;
3564#endif /* CONFIG_NO_PBKDF2 */
3565}
3566
3567
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003568static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3569 const char *value)
3570{
3571 u8 *proto;
3572 int **port;
3573 int *ports, *nports;
3574 const char *pos;
3575 unsigned int num_ports;
3576
3577 proto = os_realloc_array(cred->req_conn_capab_proto,
3578 cred->num_req_conn_capab + 1, sizeof(u8));
3579 if (proto == NULL)
3580 return -1;
3581 cred->req_conn_capab_proto = proto;
3582
3583 port = os_realloc_array(cred->req_conn_capab_port,
3584 cred->num_req_conn_capab + 1, sizeof(int *));
3585 if (port == NULL)
3586 return -1;
3587 cred->req_conn_capab_port = port;
3588
3589 proto[cred->num_req_conn_capab] = atoi(value);
3590
3591 pos = os_strchr(value, ':');
3592 if (pos == NULL) {
3593 port[cred->num_req_conn_capab] = NULL;
3594 cred->num_req_conn_capab++;
3595 return 0;
3596 }
3597 pos++;
3598
3599 ports = NULL;
3600 num_ports = 0;
3601
3602 while (*pos) {
3603 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3604 if (nports == NULL) {
3605 os_free(ports);
3606 return -1;
3607 }
3608 ports = nports;
3609 ports[num_ports++] = atoi(pos);
3610
3611 pos = os_strchr(pos, ',');
3612 if (pos == NULL)
3613 break;
3614 pos++;
3615 }
3616
3617 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3618 if (nports == NULL) {
3619 os_free(ports);
3620 return -1;
3621 }
3622 ports = nports;
3623 ports[num_ports] = -1;
3624
3625 port[cred->num_req_conn_capab] = ports;
3626 cred->num_req_conn_capab++;
3627 return 0;
3628}
3629
3630
Sunil Ravi77d572f2023-01-17 23:58:31 +00003631static int
3632wpa_config_set_cred_ois(u8 cred_ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN],
3633 size_t cred_ois_len[MAX_ROAMING_CONS],
3634 unsigned int *cred_num_ois,
3635 const char *value)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003636{
Sunil Ravi77d572f2023-01-17 23:58:31 +00003637 u8 ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3638 size_t ois_len[MAX_ROAMING_CONS];
3639 unsigned int num_ois = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003640 const char *pos, *end;
3641 size_t len;
3642
Sunil Ravi77d572f2023-01-17 23:58:31 +00003643 len = os_strlen(value);
3644 if (len / 2 < 3) {
3645 wpa_printf(MSG_ERROR,
3646 "Invalid organisation identifier (OI) list: %s",
3647 value);
3648 return -1;
3649 }
3650
3651 os_memset(ois, 0, sizeof(ois));
3652 os_memset(ois_len, 0, sizeof(ois_len));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003653
3654 for (pos = value;;) {
3655 end = os_strchr(pos, ',');
3656 len = end ? (size_t) (end - pos) : os_strlen(pos);
3657 if (!end && len == 0)
3658 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003659 if (len / 2 < 3 || (len & 1) != 0 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003660 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3661 hexstr2bin(pos,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003662 ois[num_ois],
Roshan Pius3a1667e2018-07-03 15:17:14 -07003663 len / 2) < 0) {
3664 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003665 "Invalid organisation identifier (OI) entry: %s",
Roshan Pius3a1667e2018-07-03 15:17:14 -07003666 pos);
3667 return -1;
3668 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003669 ois_len[num_ois] = len / 2;
3670 num_ois++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003671
3672 if (!end)
3673 break;
3674
Sunil Ravi77d572f2023-01-17 23:58:31 +00003675 if (num_ois >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003676 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003677 "Too many OIs");
Roshan Pius3a1667e2018-07-03 15:17:14 -07003678 return -1;
3679 }
3680
Roshan Pius3a1667e2018-07-03 15:17:14 -07003681 pos = end + 1;
3682 }
3683
Sunil Ravi77d572f2023-01-17 23:58:31 +00003684 os_memcpy(cred_ois, ois, sizeof(ois));
3685 os_memcpy(cred_ois_len, ois_len, sizeof(ois_len));
3686 *cred_num_ois = num_ois;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003687
3688 return 0;
3689}
3690
3691
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3693 const char *value, int line)
3694{
3695 char *val;
3696 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003697 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003699 if (os_strcmp(var, "temporary") == 0) {
3700 cred->temporary = atoi(value);
3701 return 0;
3702 }
3703
Dmitry Shmidt04949592012-07-19 12:16:46 -07003704 if (os_strcmp(var, "priority") == 0) {
3705 cred->priority = atoi(value);
3706 return 0;
3707 }
3708
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003709 if (os_strcmp(var, "sp_priority") == 0) {
3710 int prio = atoi(value);
3711 if (prio < 0 || prio > 255)
3712 return -1;
3713 cred->sp_priority = prio;
3714 return 0;
3715 }
3716
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 if (os_strcmp(var, "pcsc") == 0) {
3718 cred->pcsc = atoi(value);
3719 return 0;
3720 }
3721
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003722 if (os_strcmp(var, "eap") == 0) {
3723 struct eap_method_type method;
3724 method.method = eap_peer_get_type(value, &method.vendor);
3725 if (method.vendor == EAP_VENDOR_IETF &&
3726 method.method == EAP_TYPE_NONE) {
3727 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3728 "for a credential", line, value);
3729 return -1;
3730 }
3731 os_free(cred->eap_method);
3732 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3733 if (cred->eap_method == NULL)
3734 return -1;
3735 os_memcpy(cred->eap_method, &method, sizeof(method));
3736 return 0;
3737 }
3738
3739 if (os_strcmp(var, "password") == 0 &&
3740 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003741 if (has_newline(value))
3742 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003743 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003744 cred->password = os_strdup(value);
3745 cred->ext_password = 1;
3746 return 0;
3747 }
3748
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003749 if (os_strcmp(var, "update_identifier") == 0) {
3750 cred->update_identifier = atoi(value);
3751 return 0;
3752 }
3753
3754 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3755 cred->min_dl_bandwidth_home = atoi(value);
3756 return 0;
3757 }
3758
3759 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3760 cred->min_ul_bandwidth_home = atoi(value);
3761 return 0;
3762 }
3763
3764 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3765 cred->min_dl_bandwidth_roaming = atoi(value);
3766 return 0;
3767 }
3768
3769 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3770 cred->min_ul_bandwidth_roaming = atoi(value);
3771 return 0;
3772 }
3773
3774 if (os_strcmp(var, "max_bss_load") == 0) {
3775 cred->max_bss_load = atoi(value);
3776 return 0;
3777 }
3778
3779 if (os_strcmp(var, "req_conn_capab") == 0)
3780 return wpa_config_set_cred_req_conn_capab(cred, value);
3781
3782 if (os_strcmp(var, "ocsp") == 0) {
3783 cred->ocsp = atoi(value);
3784 return 0;
3785 }
3786
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003787 if (os_strcmp(var, "sim_num") == 0) {
3788 cred->sim_num = atoi(value);
3789 return 0;
3790 }
3791
Hai Shaloma20dcd72022-02-04 13:43:00 -08003792 if (os_strcmp(var, "engine") == 0) {
3793 cred->engine = atoi(value);
3794 return 0;
3795 }
3796
Dmitry Shmidt04949592012-07-19 12:16:46 -07003797 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003798 if (val == NULL ||
3799 (os_strcmp(var, "excluded_ssid") != 0 &&
3800 os_strcmp(var, "roaming_consortium") != 0 &&
3801 os_strcmp(var, "required_roaming_consortium") != 0 &&
3802 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003803 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3804 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003805 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 return -1;
3807 }
3808
3809 if (os_strcmp(var, "realm") == 0) {
3810 os_free(cred->realm);
3811 cred->realm = val;
3812 return 0;
3813 }
3814
3815 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003816 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003817 cred->username = val;
3818 return 0;
3819 }
3820
3821 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003822 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003823 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003824 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003825 return 0;
3826 }
3827
3828 if (os_strcmp(var, "ca_cert") == 0) {
3829 os_free(cred->ca_cert);
3830 cred->ca_cert = val;
3831 return 0;
3832 }
3833
3834 if (os_strcmp(var, "client_cert") == 0) {
3835 os_free(cred->client_cert);
3836 cred->client_cert = val;
3837 return 0;
3838 }
3839
3840 if (os_strcmp(var, "private_key") == 0) {
3841 os_free(cred->private_key);
3842 cred->private_key = val;
3843 return 0;
3844 }
3845
3846 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003847 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003848 cred->private_key_passwd = val;
3849 return 0;
3850 }
3851
Hai Shaloma20dcd72022-02-04 13:43:00 -08003852 if (os_strcmp(var, "engine_id") == 0) {
3853 os_free(cred->engine_id);
3854 cred->engine_id = val;
3855 return 0;
3856 }
3857
3858 if (os_strcmp(var, "ca_cert_id") == 0) {
3859 os_free(cred->ca_cert_id);
3860 cred->ca_cert_id = val;
3861 return 0;
3862 }
3863
3864 if (os_strcmp(var, "cert_id") == 0) {
3865 os_free(cred->cert_id);
3866 cred->cert_id = val;
3867 return 0;
3868 }
3869
3870 if (os_strcmp(var, "key_id") == 0) {
3871 os_free(cred->key_id);
3872 cred->key_id = val;
3873 return 0;
3874 }
3875
Dmitry Shmidt04949592012-07-19 12:16:46 -07003876 if (os_strcmp(var, "imsi") == 0) {
3877 os_free(cred->imsi);
3878 cred->imsi = val;
3879 return 0;
3880 }
3881
3882 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003883 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003884 cred->milenage = val;
3885 return 0;
3886 }
3887
Dmitry Shmidt051af732013-10-22 13:52:46 -07003888 if (os_strcmp(var, "domain_suffix_match") == 0) {
3889 os_free(cred->domain_suffix_match);
3890 cred->domain_suffix_match = val;
3891 return 0;
3892 }
3893
Dmitry Shmidt04949592012-07-19 12:16:46 -07003894 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003895 char **new_domain;
3896 new_domain = os_realloc_array(cred->domain,
3897 cred->num_domain + 1,
3898 sizeof(char *));
3899 if (new_domain == NULL) {
3900 os_free(val);
3901 return -1;
3902 }
3903 new_domain[cred->num_domain++] = val;
3904 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003905 return 0;
3906 }
3907
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003908 if (os_strcmp(var, "phase1") == 0) {
3909 os_free(cred->phase1);
3910 cred->phase1 = val;
3911 return 0;
3912 }
3913
3914 if (os_strcmp(var, "phase2") == 0) {
3915 os_free(cred->phase2);
3916 cred->phase2 = val;
3917 return 0;
3918 }
3919
3920 if (os_strcmp(var, "roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003921 if (len < 3 || len > sizeof(cred->home_ois[0])) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003922 wpa_printf(MSG_ERROR, "Line %d: invalid "
3923 "roaming_consortium length %d (3..15 "
3924 "expected)", line, (int) len);
3925 os_free(val);
3926 return -1;
3927 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003928 wpa_printf(MSG_WARNING,
3929 "Line %d: option roaming_consortium is deprecated and will be removed in the future",
3930 line);
3931 os_memcpy(cred->home_ois[0], val, len);
3932 cred->home_ois_len[0] = len;
3933 cred->num_home_ois = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003934 os_free(val);
3935 return 0;
3936 }
3937
Dmitry Shmidt051af732013-10-22 13:52:46 -07003938 if (os_strcmp(var, "required_roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003939 if (len < 3 || len > sizeof(cred->required_home_ois[0])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003940 wpa_printf(MSG_ERROR, "Line %d: invalid "
3941 "required_roaming_consortium length %d "
3942 "(3..15 expected)", line, (int) len);
3943 os_free(val);
3944 return -1;
3945 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003946 wpa_printf(MSG_WARNING,
3947 "Line %d: option required_roaming_consortium is deprecated and will be removed in the future",
3948 line);
3949 os_memcpy(cred->required_home_ois[0], val, len);
3950 cred->required_home_ois_len[0] = len;
3951 cred->num_required_home_ois = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003952 os_free(val);
3953 return 0;
3954 }
3955
Sunil Ravi77d572f2023-01-17 23:58:31 +00003956 if (os_strcmp(var, "home_ois") == 0) {
3957 res = wpa_config_set_cred_ois(cred->home_ois,
3958 cred->home_ois_len,
3959 &cred->num_home_ois,
3960 val);
3961 if (res < 0)
3962 wpa_printf(MSG_ERROR, "Line %d: invalid home_ois",
3963 line);
3964 os_free(val);
3965 return res;
3966 }
3967
3968 if (os_strcmp(var, "required_home_ois") == 0) {
3969 res = wpa_config_set_cred_ois(cred->required_home_ois,
3970 cred->required_home_ois_len,
3971 &cred->num_required_home_ois,
3972 val);
3973 if (res < 0)
3974 wpa_printf(MSG_ERROR,
3975 "Line %d: invalid required_home_ois", line);
3976 os_free(val);
3977 return res;
3978 }
3979
Roshan Pius3a1667e2018-07-03 15:17:14 -07003980 if (os_strcmp(var, "roaming_consortiums") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003981 res = wpa_config_set_cred_ois(cred->roaming_consortiums,
3982 cred->roaming_consortiums_len,
3983 &cred->num_roaming_consortiums,
3984 val);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003985 if (res < 0)
3986 wpa_printf(MSG_ERROR,
3987 "Line %d: invalid roaming_consortiums",
3988 line);
3989 os_free(val);
3990 return res;
3991 }
3992
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003993 if (os_strcmp(var, "excluded_ssid") == 0) {
3994 struct excluded_ssid *e;
3995
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003996 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003997 wpa_printf(MSG_ERROR, "Line %d: invalid "
3998 "excluded_ssid length %d", line, (int) len);
3999 os_free(val);
4000 return -1;
4001 }
4002
4003 e = os_realloc_array(cred->excluded_ssid,
4004 cred->num_excluded_ssid + 1,
4005 sizeof(struct excluded_ssid));
4006 if (e == NULL) {
4007 os_free(val);
4008 return -1;
4009 }
4010 cred->excluded_ssid = e;
4011
4012 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
4013 os_memcpy(e->ssid, val, len);
4014 e->ssid_len = len;
4015
4016 os_free(val);
4017
4018 return 0;
4019 }
4020
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004021 if (os_strcmp(var, "roaming_partner") == 0) {
4022 struct roaming_partner *p;
4023 char *pos;
4024
4025 p = os_realloc_array(cred->roaming_partner,
4026 cred->num_roaming_partner + 1,
4027 sizeof(struct roaming_partner));
4028 if (p == NULL) {
4029 os_free(val);
4030 return -1;
4031 }
4032 cred->roaming_partner = p;
4033
4034 p = &cred->roaming_partner[cred->num_roaming_partner];
4035
4036 pos = os_strchr(val, ',');
4037 if (pos == NULL) {
4038 os_free(val);
4039 return -1;
4040 }
4041 *pos++ = '\0';
4042 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
4043 os_free(val);
4044 return -1;
4045 }
4046 os_memcpy(p->fqdn, val, pos - val);
4047
4048 p->exact_match = atoi(pos);
4049
4050 pos = os_strchr(pos, ',');
4051 if (pos == NULL) {
4052 os_free(val);
4053 return -1;
4054 }
4055 *pos++ = '\0';
4056
4057 p->priority = atoi(pos);
4058
4059 pos = os_strchr(pos, ',');
4060 if (pos == NULL) {
4061 os_free(val);
4062 return -1;
4063 }
4064 *pos++ = '\0';
4065
4066 if (os_strlen(pos) >= sizeof(p->country)) {
4067 os_free(val);
4068 return -1;
4069 }
4070 os_memcpy(p->country, pos, os_strlen(pos) + 1);
4071
4072 cred->num_roaming_partner++;
4073 os_free(val);
4074
4075 return 0;
4076 }
4077
4078 if (os_strcmp(var, "provisioning_sp") == 0) {
4079 os_free(cred->provisioning_sp);
4080 cred->provisioning_sp = val;
4081 return 0;
4082 }
4083
Sunil8cd6f4d2022-06-28 18:40:46 +00004084 if (os_strcmp(var, "imsi_privacy_cert") == 0) {
4085 os_free(cred->imsi_privacy_cert);
4086 cred->imsi_privacy_cert = val;
4087 return 0;
4088 }
4089
4090 if (os_strcmp(var, "imsi_privacy_attr") == 0) {
4091 os_free(cred->imsi_privacy_attr);
4092 cred->imsi_privacy_attr = val;
Sunil Ravia04bd252022-05-02 22:54:18 -07004093 return 0;
4094 }
4095
Steven Liu9138d432022-11-23 22:29:05 +00004096 if (os_strcmp(var, "strict_conservative_peer_mode") == 0) {
4097 cred->strict_conservative_peer_mode = atoi(val);
4098 return 0;
4099 }
4100
Dmitry Shmidt04949592012-07-19 12:16:46 -07004101 if (line) {
4102 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
4103 line, var);
4104 }
4105
4106 os_free(val);
4107
4108 return -1;
4109}
4110
4111
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004112static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004113{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004114 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004115 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004117
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004118 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004119 if (buf == NULL)
4120 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004121 res = os_snprintf(buf, bufsize, "%d", val);
4122 if (os_snprintf_error(bufsize, res)) {
4123 os_free(buf);
4124 buf = NULL;
4125 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004126 return buf;
4127}
4128
4129
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004130static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004131{
4132 if (str == NULL)
4133 return NULL;
4134 return os_strdup(str);
4135}
4136
4137
4138char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
4139{
4140 if (os_strcmp(var, "temporary") == 0)
4141 return alloc_int_str(cred->temporary);
4142
4143 if (os_strcmp(var, "priority") == 0)
4144 return alloc_int_str(cred->priority);
4145
4146 if (os_strcmp(var, "sp_priority") == 0)
4147 return alloc_int_str(cred->sp_priority);
4148
4149 if (os_strcmp(var, "pcsc") == 0)
4150 return alloc_int_str(cred->pcsc);
4151
4152 if (os_strcmp(var, "eap") == 0) {
4153 if (!cred->eap_method)
4154 return NULL;
4155 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4156 cred->eap_method[0].method));
4157 }
4158
4159 if (os_strcmp(var, "update_identifier") == 0)
4160 return alloc_int_str(cred->update_identifier);
4161
4162 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4163 return alloc_int_str(cred->min_dl_bandwidth_home);
4164
4165 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4166 return alloc_int_str(cred->min_ul_bandwidth_home);
4167
4168 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4169 return alloc_int_str(cred->min_dl_bandwidth_roaming);
4170
4171 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4172 return alloc_int_str(cred->min_ul_bandwidth_roaming);
4173
4174 if (os_strcmp(var, "max_bss_load") == 0)
4175 return alloc_int_str(cred->max_bss_load);
4176
4177 if (os_strcmp(var, "req_conn_capab") == 0) {
4178 unsigned int i;
4179 char *buf, *end, *pos;
4180 int ret;
4181
4182 if (!cred->num_req_conn_capab)
4183 return NULL;
4184
4185 buf = os_malloc(4000);
4186 if (buf == NULL)
4187 return NULL;
4188 pos = buf;
4189 end = pos + 4000;
4190 for (i = 0; i < cred->num_req_conn_capab; i++) {
4191 int *ports;
4192
4193 ret = os_snprintf(pos, end - pos, "%s%u",
4194 i > 0 ? "\n" : "",
4195 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004196 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004197 return buf;
4198 pos += ret;
4199
4200 ports = cred->req_conn_capab_port[i];
4201 if (ports) {
4202 int j;
4203 for (j = 0; ports[j] != -1; j++) {
4204 ret = os_snprintf(pos, end - pos,
4205 "%s%d",
4206 j > 0 ? "," : ":",
4207 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004208 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004209 return buf;
4210 pos += ret;
4211 }
4212 }
4213 }
4214
4215 return buf;
4216 }
4217
4218 if (os_strcmp(var, "ocsp") == 0)
4219 return alloc_int_str(cred->ocsp);
4220
4221 if (os_strcmp(var, "realm") == 0)
4222 return alloc_strdup(cred->realm);
4223
4224 if (os_strcmp(var, "username") == 0)
4225 return alloc_strdup(cred->username);
4226
4227 if (os_strcmp(var, "password") == 0) {
4228 if (!cred->password)
4229 return NULL;
4230 return alloc_strdup("*");
4231 }
4232
4233 if (os_strcmp(var, "ca_cert") == 0)
4234 return alloc_strdup(cred->ca_cert);
4235
4236 if (os_strcmp(var, "client_cert") == 0)
4237 return alloc_strdup(cred->client_cert);
4238
4239 if (os_strcmp(var, "private_key") == 0)
4240 return alloc_strdup(cred->private_key);
4241
4242 if (os_strcmp(var, "private_key_passwd") == 0) {
4243 if (!cred->private_key_passwd)
4244 return NULL;
4245 return alloc_strdup("*");
4246 }
4247
4248 if (os_strcmp(var, "imsi") == 0)
4249 return alloc_strdup(cred->imsi);
4250
Sunil8cd6f4d2022-06-28 18:40:46 +00004251 if (os_strcmp(var, "imsi_privacy_cert") == 0)
4252 return alloc_strdup(cred->imsi_privacy_cert);
4253
4254 if (os_strcmp(var, "imsi_privacy_attr") == 0)
4255 return alloc_strdup(cred->imsi_privacy_attr);
Sunil Ravia04bd252022-05-02 22:54:18 -07004256
Steven Liu9138d432022-11-23 22:29:05 +00004257 if (os_strcmp(var, "strict_conservative_peer_mode") == 0)
4258 return alloc_int_str(cred->strict_conservative_peer_mode);
4259
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004260 if (os_strcmp(var, "milenage") == 0) {
4261 if (!(cred->milenage))
4262 return NULL;
4263 return alloc_strdup("*");
4264 }
4265
4266 if (os_strcmp(var, "domain_suffix_match") == 0)
4267 return alloc_strdup(cred->domain_suffix_match);
4268
4269 if (os_strcmp(var, "domain") == 0) {
4270 unsigned int i;
4271 char *buf, *end, *pos;
4272 int ret;
4273
4274 if (!cred->num_domain)
4275 return NULL;
4276
4277 buf = os_malloc(4000);
4278 if (buf == NULL)
4279 return NULL;
4280 pos = buf;
4281 end = pos + 4000;
4282
4283 for (i = 0; i < cred->num_domain; i++) {
4284 ret = os_snprintf(pos, end - pos, "%s%s",
4285 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004286 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004287 return buf;
4288 pos += ret;
4289 }
4290
4291 return buf;
4292 }
4293
4294 if (os_strcmp(var, "phase1") == 0)
4295 return alloc_strdup(cred->phase1);
4296
4297 if (os_strcmp(var, "phase2") == 0)
4298 return alloc_strdup(cred->phase2);
4299
4300 if (os_strcmp(var, "roaming_consortium") == 0) {
4301 size_t buflen;
4302 char *buf;
4303
Sunil Ravi77d572f2023-01-17 23:58:31 +00004304 if (!cred->num_home_ois || !cred->home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004305 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004306 buflen = cred->home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004307 buf = os_malloc(buflen);
4308 if (buf == NULL)
4309 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004310 wpa_snprintf_hex(buf, buflen, cred->home_ois[0],
4311 cred->home_ois_len[0]);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004312 return buf;
4313 }
4314
4315 if (os_strcmp(var, "required_roaming_consortium") == 0) {
4316 size_t buflen;
4317 char *buf;
4318
Sunil Ravi77d572f2023-01-17 23:58:31 +00004319 if (!cred->num_required_home_ois ||
4320 !cred->required_home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004321 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004322 buflen = cred->required_home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004323 buf = os_malloc(buflen);
4324 if (buf == NULL)
4325 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004326 wpa_snprintf_hex(buf, buflen, cred->required_home_ois[0],
4327 cred->required_home_ois_len[0]);
4328 return buf;
4329 }
4330
4331 if (os_strcmp(var, "home_ois") == 0) {
4332 size_t buflen;
4333 char *buf, *pos;
4334 size_t i;
4335
4336 if (!cred->num_home_ois)
4337 return NULL;
4338 buflen = cred->num_home_ois * MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4339 buf = os_malloc(buflen);
4340 if (!buf)
4341 return NULL;
4342 pos = buf;
4343 for (i = 0; i < cred->num_home_ois; i++) {
4344 if (i > 0)
4345 *pos++ = ',';
4346 pos += wpa_snprintf_hex(
4347 pos, buf + buflen - pos,
4348 cred->home_ois[i],
4349 cred->home_ois_len[i]);
4350 }
4351 *pos = '\0';
4352 return buf;
4353 }
4354
4355 if (os_strcmp(var, "required_home_ois") == 0) {
4356 size_t buflen;
4357 char *buf, *pos;
4358 size_t i;
4359
4360 if (!cred->num_required_home_ois)
4361 return NULL;
4362 buflen = cred->num_required_home_ois *
4363 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4364 buf = os_malloc(buflen);
4365 if (!buf)
4366 return NULL;
4367 pos = buf;
4368 for (i = 0; i < cred->num_required_home_ois; i++) {
4369 if (i > 0)
4370 *pos++ = ',';
4371 pos += wpa_snprintf_hex(
4372 pos, buf + buflen - pos,
4373 cred->required_home_ois[i],
4374 cred->required_home_ois_len[i]);
4375 }
4376 *pos = '\0';
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004377 return buf;
4378 }
4379
Roshan Pius3a1667e2018-07-03 15:17:14 -07004380 if (os_strcmp(var, "roaming_consortiums") == 0) {
4381 size_t buflen;
4382 char *buf, *pos;
4383 size_t i;
4384
4385 if (!cred->num_roaming_consortiums)
4386 return NULL;
4387 buflen = cred->num_roaming_consortiums *
4388 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_roaming_consortiums; i++) {
4394 if (i > 0)
4395 *pos++ = ',';
4396 pos += wpa_snprintf_hex(
4397 pos, buf + buflen - pos,
4398 cred->roaming_consortiums[i],
4399 cred->roaming_consortiums_len[i]);
4400 }
4401 *pos = '\0';
4402 return buf;
4403 }
4404
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004405 if (os_strcmp(var, "excluded_ssid") == 0) {
4406 unsigned int i;
4407 char *buf, *end, *pos;
4408
4409 if (!cred->num_excluded_ssid)
4410 return NULL;
4411
4412 buf = os_malloc(4000);
4413 if (buf == NULL)
4414 return NULL;
4415 pos = buf;
4416 end = pos + 4000;
4417
4418 for (i = 0; i < cred->num_excluded_ssid; i++) {
4419 struct excluded_ssid *e;
4420 int ret;
4421
4422 e = &cred->excluded_ssid[i];
4423 ret = os_snprintf(pos, end - pos, "%s%s",
4424 i > 0 ? "\n" : "",
4425 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004427 return buf;
4428 pos += ret;
4429 }
4430
4431 return buf;
4432 }
4433
4434 if (os_strcmp(var, "roaming_partner") == 0) {
4435 unsigned int i;
4436 char *buf, *end, *pos;
4437
4438 if (!cred->num_roaming_partner)
4439 return NULL;
4440
4441 buf = os_malloc(4000);
4442 if (buf == NULL)
4443 return NULL;
4444 pos = buf;
4445 end = pos + 4000;
4446
4447 for (i = 0; i < cred->num_roaming_partner; i++) {
4448 struct roaming_partner *p;
4449 int ret;
4450
4451 p = &cred->roaming_partner[i];
4452 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4453 i > 0 ? "\n" : "",
4454 p->fqdn, p->exact_match, p->priority,
4455 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004456 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004457 return buf;
4458 pos += ret;
4459 }
4460
4461 return buf;
4462 }
4463
4464 if (os_strcmp(var, "provisioning_sp") == 0)
4465 return alloc_strdup(cred->provisioning_sp);
4466
4467 return NULL;
4468}
4469
4470
Dmitry Shmidt04949592012-07-19 12:16:46 -07004471struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4472{
4473 struct wpa_cred *cred;
4474
4475 cred = config->cred;
4476 while (cred) {
4477 if (id == cred->id)
4478 break;
4479 cred = cred->next;
4480 }
4481
4482 return cred;
4483}
4484
4485
4486struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4487{
4488 int id;
4489 struct wpa_cred *cred, *last = NULL;
4490
4491 id = -1;
4492 cred = config->cred;
4493 while (cred) {
4494 if (cred->id > id)
4495 id = cred->id;
4496 last = cred;
4497 cred = cred->next;
4498 }
4499 id++;
4500
4501 cred = os_zalloc(sizeof(*cred));
4502 if (cred == NULL)
4503 return NULL;
4504 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004505 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004506 if (last)
4507 last->next = cred;
4508 else
4509 config->cred = cred;
4510
4511 return cred;
4512}
4513
4514
4515int wpa_config_remove_cred(struct wpa_config *config, int id)
4516{
4517 struct wpa_cred *cred, *prev = NULL;
4518
4519 cred = config->cred;
4520 while (cred) {
4521 if (id == cred->id)
4522 break;
4523 prev = cred;
4524 cred = cred->next;
4525 }
4526
4527 if (cred == NULL)
4528 return -1;
4529
4530 if (prev)
4531 prev->next = cred->next;
4532 else
4533 config->cred = cred->next;
4534
4535 wpa_config_free_cred(cred);
4536 return 0;
4537}
4538
4539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540#ifndef CONFIG_NO_CONFIG_BLOBS
4541/**
4542 * wpa_config_get_blob - Get a named configuration blob
4543 * @config: Configuration data from wpa_config_read()
4544 * @name: Name of the blob
4545 * Returns: Pointer to blob data or %NULL if not found
4546 */
4547const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4548 const char *name)
4549{
4550 struct wpa_config_blob *blob = config->blobs;
4551
4552 while (blob) {
4553 if (os_strcmp(blob->name, name) == 0)
4554 return blob;
4555 blob = blob->next;
4556 }
4557 return NULL;
4558}
4559
4560
4561/**
4562 * wpa_config_set_blob - Set or add a named configuration blob
4563 * @config: Configuration data from wpa_config_read()
4564 * @blob: New value for the blob
4565 *
4566 * Adds a new configuration blob or replaces the current value of an existing
4567 * blob.
4568 */
4569void wpa_config_set_blob(struct wpa_config *config,
4570 struct wpa_config_blob *blob)
4571{
4572 wpa_config_remove_blob(config, blob->name);
4573 blob->next = config->blobs;
4574 config->blobs = blob;
4575}
4576
4577
4578/**
4579 * wpa_config_free_blob - Free blob data
4580 * @blob: Pointer to blob to be freed
4581 */
4582void wpa_config_free_blob(struct wpa_config_blob *blob)
4583{
4584 if (blob) {
4585 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004586 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 os_free(blob);
4588 }
4589}
4590
4591
4592/**
4593 * wpa_config_remove_blob - Remove a named configuration blob
4594 * @config: Configuration data from wpa_config_read()
4595 * @name: Name of the blob to remove
4596 * Returns: 0 if blob was removed or -1 if blob was not found
4597 */
4598int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4599{
4600 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4601
4602 while (pos) {
4603 if (os_strcmp(pos->name, name) == 0) {
4604 if (prev)
4605 prev->next = pos->next;
4606 else
4607 config->blobs = pos->next;
4608 wpa_config_free_blob(pos);
4609 return 0;
4610 }
4611 prev = pos;
4612 pos = pos->next;
4613 }
4614
4615 return -1;
4616}
4617#endif /* CONFIG_NO_CONFIG_BLOBS */
4618
4619
4620/**
4621 * wpa_config_alloc_empty - Allocate an empty configuration
4622 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4623 * socket
4624 * @driver_param: Driver parameters
4625 * Returns: Pointer to allocated configuration data or %NULL on failure
4626 */
4627struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4628 const char *driver_param)
4629{
Hai Shalom899fcc72020-10-19 14:38:18 -07004630#define ecw2cw(ecw) ((1 << (ecw)) - 1)
4631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004633 const int aCWmin = 4, aCWmax = 10;
4634 const struct hostapd_wmm_ac_params ac_bk =
4635 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4636 const struct hostapd_wmm_ac_params ac_be =
4637 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4638 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004639 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004640 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004641 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4642 const struct hostapd_tx_queue_params txq_bk =
4643 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4644 const struct hostapd_tx_queue_params txq_be =
4645 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4646 const struct hostapd_tx_queue_params txq_vi =
4647 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4648 const struct hostapd_tx_queue_params txq_vo =
4649 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4650 (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4651
4652#undef ecw2cw
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653
4654 config = os_zalloc(sizeof(*config));
4655 if (config == NULL)
4656 return NULL;
4657 config->eapol_version = DEFAULT_EAPOL_VERSION;
4658 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004659 config->user_mpm = DEFAULT_USER_MPM;
4660 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004661 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004662 config->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004663 config->dot11RSNASAERetransPeriod =
4664 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 config->fast_reauth = DEFAULT_FAST_REAUTH;
4666 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4667 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004668 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004669 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004670 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004671 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4673 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4674 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4675 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004676 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004677 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004678 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Hai Shalom60840252021-02-19 19:02:11 -08004679 config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004680 config->wmm_ac_params[0] = ac_be;
4681 config->wmm_ac_params[1] = ac_bk;
4682 config->wmm_ac_params[2] = ac_vi;
4683 config->wmm_ac_params[3] = ac_vo;
Hai Shalom899fcc72020-10-19 14:38:18 -07004684 config->tx_queue[0] = txq_vo;
4685 config->tx_queue[1] = txq_vi;
4686 config->tx_queue[2] = txq_be;
4687 config->tx_queue[3] = txq_bk;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004688 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004689 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004690 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004691 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004692 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Hai Shalomfdcde762020-04-02 11:19:20 -07004693 config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004695#ifdef CONFIG_MBO
4696 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004697 config->disassoc_imminent_rssi_threshold =
4698 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4699 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004700#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07004701 config->btm_offload = DEFAULT_BTM_OFFLOAD;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 if (ctrl_interface)
4704 config->ctrl_interface = os_strdup(ctrl_interface);
4705 if (driver_param)
4706 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004707 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004709#ifdef CONFIG_TESTING_OPTIONS
4710 config->mld_connect_band_pref = DEFAULT_MLD_CONNECT_BAND_PREF;
4711#endif /* CONFIG_TESTING_OPTIONS */
4712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 return config;
4714}
4715
4716
4717#ifndef CONFIG_NO_STDOUT_DEBUG
4718/**
4719 * wpa_config_debug_dump_networks - Debug dump of configured networks
4720 * @config: Configuration data from wpa_config_read()
4721 */
4722void wpa_config_debug_dump_networks(struct wpa_config *config)
4723{
Hai Shalomfdcde762020-04-02 11:19:20 -07004724 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 struct wpa_ssid *ssid;
4726
4727 for (prio = 0; prio < config->num_prio; prio++) {
4728 ssid = config->pssid[prio];
4729 wpa_printf(MSG_DEBUG, "Priority group %d",
4730 ssid->priority);
4731 while (ssid) {
4732 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4733 ssid->id,
4734 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4735 ssid = ssid->pnext;
4736 }
4737 }
4738}
4739#endif /* CONFIG_NO_STDOUT_DEBUG */
4740
4741
Hai Shalom899fcc72020-10-19 14:38:18 -07004742/**
4743 * Structure for global configuration parsing. This data is used to implement a
4744 * generic parser for the global interface configuration. The table of variables
4745 * is defined below in this file (global_fields[]).
4746 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747struct global_parse_data {
Hai Shalom899fcc72020-10-19 14:38:18 -07004748 /* Configuration variable name */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749 char *name;
Hai Shalom899fcc72020-10-19 14:38:18 -07004750
4751 /* Parser function for this variable. The parser functions return 0 or 1
4752 * to indicate success. Value 0 indicates that the parameter value may
4753 * have changed while value 1 means that the value did not change.
4754 * Error cases (failure to parse the string) are indicated by returning
4755 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 int (*parser)(const struct global_parse_data *data,
4757 struct wpa_config *config, int line, const char *value);
Hai Shalom899fcc72020-10-19 14:38:18 -07004758
4759 /* Getter function to print the variable in text format to buf. */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004760 int (*get)(const char *name, struct wpa_config *config, long offset,
4761 char *buf, size_t buflen, int pretty_print);
Hai Shalom899fcc72020-10-19 14:38:18 -07004762
4763 /* Variable specific parameters for the parser. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764 void *param1, *param2, *param3;
Hai Shalom899fcc72020-10-19 14:38:18 -07004765
4766 /* Indicates which configuration variable has changed. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004767 unsigned int changed_flag;
4768};
4769
4770
Sunil Ravi99c035e2024-07-12 01:42:03 +00004771static int
4772wpa_global_config_parse_int_impl(const struct global_parse_data *data,
4773 struct wpa_config *config, int line,
4774 const char *pos, bool check_range)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004776 int val, *dst;
4777 char *end;
Hai Shalom899fcc72020-10-19 14:38:18 -07004778 bool same;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004781 val = strtol(pos, &end, 0);
4782 if (*end) {
4783 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4784 line, pos);
4785 return -1;
4786 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00004787
4788 if (check_range && val < (long) data->param2) {
4789 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4790 "min_value=%ld)", line, data->name, val,
4791 (long) data->param2);
4792 return -1;
4793 }
4794
4795 if (check_range && val > (long) data->param3) {
4796 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4797 "max_value=%ld)", line, data->name, val,
4798 (long) data->param3);
4799 return -1;
4800 }
4801
Hai Shalom899fcc72020-10-19 14:38:18 -07004802 same = *dst == val;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004803 *dst = val;
4804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4806
Hai Shalom899fcc72020-10-19 14:38:18 -07004807 return same;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808}
4809
4810
Sunil Ravi99c035e2024-07-12 01:42:03 +00004811static int wpa_global_config_parse_int(const struct global_parse_data *data,
4812 struct wpa_config *config, int line,
4813 const char *pos)
4814{
4815 return wpa_global_config_parse_int_impl(data, config, line, pos, false);
4816}
4817
4818
4819static int
4820wpa_global_config_parse_int_range(const struct global_parse_data *data,
4821 struct wpa_config *config, int line,
4822 const char *pos)
4823{
4824 return wpa_global_config_parse_int_impl(data, config, line, pos, true);
4825}
4826
4827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828static int wpa_global_config_parse_str(const struct global_parse_data *data,
4829 struct wpa_config *config, int line,
4830 const char *pos)
4831{
Hai Shalom899fcc72020-10-19 14:38:18 -07004832 size_t len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 char **dst, *tmp;
4834
4835 len = os_strlen(pos);
4836 if (data->param2 && len < (size_t) data->param2) {
4837 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4838 "min_len=%ld)", line, data->name,
4839 (unsigned long) len, (long) data->param2);
4840 return -1;
4841 }
4842
4843 if (data->param3 && len > (size_t) data->param3) {
4844 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4845 "max_len=%ld)", line, data->name,
4846 (unsigned long) len, (long) data->param3);
4847 return -1;
4848 }
4849
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004850 if (has_newline(pos)) {
4851 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4852 line, data->name);
4853 return -1;
4854 }
4855
Hai Shalom899fcc72020-10-19 14:38:18 -07004856 dst = (char **) (((u8 *) config) + (long) data->param1);
4857 if (*dst)
4858 prev_len = os_strlen(*dst);
4859 else
4860 prev_len = 0;
4861
4862 /* No change to the previously configured value */
Hai Shalom60840252021-02-19 19:02:11 -08004863 if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
Hai Shalom899fcc72020-10-19 14:38:18 -07004864 return 1;
4865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 tmp = os_strdup(pos);
4867 if (tmp == NULL)
4868 return -1;
4869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 os_free(*dst);
4871 *dst = tmp;
4872 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4873
4874 return 0;
4875}
4876
4877
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004878static int wpa_config_process_bgscan(const struct global_parse_data *data,
4879 struct wpa_config *config, int line,
4880 const char *pos)
4881{
4882 size_t len;
4883 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004884 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004885
4886 tmp = wpa_config_parse_string(pos, &len);
4887 if (tmp == NULL) {
4888 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4889 line, data->name);
4890 return -1;
4891 }
4892
Dmitry Shmidt97672262014-02-03 13:02:54 -08004893 res = wpa_global_config_parse_str(data, config, line, tmp);
4894 os_free(tmp);
4895 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004896}
4897
4898
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4900 struct wpa_config *config, int line,
4901 const char *pos)
4902{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004903 struct wpabuf **dst, *tmp;
4904
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004905 tmp = wpabuf_parse_bin(pos);
4906 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004907 return -1;
4908
Dmitry Shmidt04949592012-07-19 12:16:46 -07004909 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004910 if (wpabuf_cmp(*dst, tmp) == 0) {
4911 wpabuf_free(tmp);
4912 return 1;
4913 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004914 wpabuf_free(*dst);
4915 *dst = tmp;
4916 wpa_printf(MSG_DEBUG, "%s", data->name);
4917
4918 return 0;
4919}
4920
4921
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004922static int wpa_config_process_freq_list(const struct global_parse_data *data,
4923 struct wpa_config *config, int line,
4924 const char *value)
4925{
4926 int *freqs;
4927
4928 freqs = wpa_config_parse_int_array(value);
4929 if (freqs == NULL)
4930 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004931 if (freqs[0] == 0) {
4932 os_free(freqs);
4933 freqs = NULL;
4934 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004935 os_free(config->freq_list);
4936 config->freq_list = freqs;
4937 return 0;
4938}
4939
4940
Hai Shalom60840252021-02-19 19:02:11 -08004941static int
4942wpa_config_process_initial_freq_list(const struct global_parse_data *data,
4943 struct wpa_config *config, int line,
4944 const char *value)
4945{
4946 int *freqs;
4947
4948 freqs = wpa_config_parse_int_array(value);
4949 if (!freqs)
4950 return -1;
4951 if (freqs[0] == 0) {
4952 os_free(freqs);
4953 freqs = NULL;
4954 }
4955 os_free(config->initial_freq_list);
4956 config->initial_freq_list = freqs;
4957 return 0;
4958}
4959
4960
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004961#ifdef CONFIG_P2P
4962static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4963 struct wpa_config *config, int line,
4964 const char *pos)
4965{
4966 u32 *dst;
4967 struct hostapd_ip_addr addr;
4968
4969 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4970 return -1;
4971 if (addr.af != AF_INET)
4972 return -1;
4973
4974 dst = (u32 *) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004975 if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
4976 return 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004977 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4978 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4979 WPA_GET_BE32((u8 *) dst));
4980
4981 return 0;
4982}
4983#endif /* CONFIG_P2P */
4984
4985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986static int wpa_config_process_country(const struct global_parse_data *data,
4987 struct wpa_config *config, int line,
4988 const char *pos)
4989{
4990 if (!pos[0] || !pos[1]) {
4991 wpa_printf(MSG_DEBUG, "Invalid country set");
4992 return -1;
4993 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004994 if (pos[0] == config->country[0] && pos[1] == config->country[1])
4995 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 config->country[0] = pos[0];
4997 config->country[1] = pos[1];
4998 wpa_printf(MSG_DEBUG, "country='%c%c'",
4999 config->country[0], config->country[1]);
5000 return 0;
5001}
5002
5003
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005004#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005static int wpa_config_process_load_dynamic_eap(
5006 const struct global_parse_data *data, struct wpa_config *config,
5007 int line, const char *so)
5008{
5009 int ret;
5010 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
5011 ret = eap_peer_method_load(so);
5012 if (ret == -2) {
5013 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
5014 "reloading.");
5015 } else if (ret) {
5016 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
5017 "method '%s'.", line, so);
5018 return -1;
5019 }
5020
5021 return 0;
5022}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005023#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024
5025
5026#ifdef CONFIG_WPS
5027
5028static int wpa_config_process_uuid(const struct global_parse_data *data,
5029 struct wpa_config *config, int line,
5030 const char *pos)
5031{
5032 char buf[40];
5033 if (uuid_str2bin(pos, config->uuid)) {
5034 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
5035 return -1;
5036 }
5037 uuid_bin2str(config->uuid, buf, sizeof(buf));
5038 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
5039 return 0;
5040}
5041
5042
5043static int wpa_config_process_device_type(
5044 const struct global_parse_data *data,
5045 struct wpa_config *config, int line, const char *pos)
5046{
5047 return wps_dev_type_str2bin(pos, config->device_type);
5048}
5049
5050
5051static int wpa_config_process_os_version(const struct global_parse_data *data,
5052 struct wpa_config *config, int line,
5053 const char *pos)
5054{
5055 if (hexstr2bin(pos, config->os_version, 4)) {
5056 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
5057 return -1;
5058 }
5059 wpa_printf(MSG_DEBUG, "os_version=%08x",
5060 WPA_GET_BE32(config->os_version));
5061 return 0;
5062}
5063
Dmitry Shmidt04949592012-07-19 12:16:46 -07005064
5065static int wpa_config_process_wps_vendor_ext_m1(
5066 const struct global_parse_data *data,
5067 struct wpa_config *config, int line, const char *pos)
5068{
5069 struct wpabuf *tmp;
5070 int len = os_strlen(pos) / 2;
5071 u8 *p;
5072
5073 if (!len) {
5074 wpa_printf(MSG_ERROR, "Line %d: "
5075 "invalid wps_vendor_ext_m1", line);
5076 return -1;
5077 }
5078
5079 tmp = wpabuf_alloc(len);
5080 if (tmp) {
5081 p = wpabuf_put(tmp, len);
5082
5083 if (hexstr2bin(pos, p, len)) {
5084 wpa_printf(MSG_ERROR, "Line %d: "
5085 "invalid wps_vendor_ext_m1", line);
5086 wpabuf_free(tmp);
5087 return -1;
5088 }
5089
5090 wpabuf_free(config->wps_vendor_ext_m1);
5091 config->wps_vendor_ext_m1 = tmp;
5092 } else {
5093 wpa_printf(MSG_ERROR, "Can not allocate "
5094 "memory for wps_vendor_ext_m1");
5095 return -1;
5096 }
5097
5098 return 0;
5099}
5100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101#endif /* CONFIG_WPS */
5102
5103#ifdef CONFIG_P2P
5104static int wpa_config_process_sec_device_type(
5105 const struct global_parse_data *data,
5106 struct wpa_config *config, int line, const char *pos)
5107{
5108 int idx;
5109
5110 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
5111 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
5112 "items", line);
5113 return -1;
5114 }
5115
5116 idx = config->num_sec_device_types;
5117
5118 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
5119 return -1;
5120
5121 config->num_sec_device_types++;
5122 return 0;
5123}
Dmitry Shmidt04949592012-07-19 12:16:46 -07005124
5125
5126static int wpa_config_process_p2p_pref_chan(
5127 const struct global_parse_data *data,
5128 struct wpa_config *config, int line, const char *pos)
5129{
5130 struct p2p_channel *pref = NULL, *n;
Hai Shalomfdcde762020-04-02 11:19:20 -07005131 size_t num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 const char *pos2;
5133 u8 op_class, chan;
5134
5135 /* format: class:chan,class:chan,... */
5136
5137 while (*pos) {
5138 op_class = atoi(pos);
5139 pos2 = os_strchr(pos, ':');
5140 if (pos2 == NULL)
5141 goto fail;
5142 pos2++;
5143 chan = atoi(pos2);
5144
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005145 n = os_realloc_array(pref, num + 1,
5146 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005147 if (n == NULL)
5148 goto fail;
5149 pref = n;
5150 pref[num].op_class = op_class;
5151 pref[num].chan = chan;
5152 num++;
5153
5154 pos = os_strchr(pos2, ',');
5155 if (pos == NULL)
5156 break;
5157 pos++;
5158 }
5159
5160 os_free(config->p2p_pref_chan);
5161 config->p2p_pref_chan = pref;
5162 config->num_p2p_pref_chan = num;
5163 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
5164 (u8 *) config->p2p_pref_chan,
5165 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
5166
5167 return 0;
5168
5169fail:
5170 os_free(pref);
5171 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
5172 return -1;
5173}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005174
5175
5176static int wpa_config_process_p2p_no_go_freq(
5177 const struct global_parse_data *data,
5178 struct wpa_config *config, int line, const char *pos)
5179{
5180 int ret;
5181
5182 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
5183 if (ret < 0) {
5184 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
5185 return -1;
5186 }
5187
5188 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
5189 config->p2p_no_go_freq.num);
5190
5191 return 0;
5192}
5193
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005194static int wpa_config_process_p2p_device_persistent_mac_addr(
5195 const struct global_parse_data *data,
5196 struct wpa_config *config, int line, const char *pos)
5197{
5198 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
5199 wpa_printf(MSG_ERROR,
5200 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
5201 line, pos);
5202 return -1;
5203 }
5204
5205 return 0;
5206}
5207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208#endif /* CONFIG_P2P */
5209
5210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005211static int wpa_config_process_hessid(
5212 const struct global_parse_data *data,
5213 struct wpa_config *config, int line, const char *pos)
5214{
5215 if (hwaddr_aton2(pos, config->hessid) < 0) {
5216 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
5217 line, pos);
5218 return -1;
5219 }
5220
5221 return 0;
5222}
5223
5224
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005225static int wpa_config_process_sae_groups(
5226 const struct global_parse_data *data,
5227 struct wpa_config *config, int line, const char *pos)
5228{
5229 int *groups = wpa_config_parse_int_array(pos);
5230 if (groups == NULL) {
5231 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5232 line, pos);
5233 return -1;
5234 }
5235
5236 os_free(config->sae_groups);
5237 config->sae_groups = groups;
5238
5239 return 0;
5240}
5241
5242
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005243static int wpa_config_process_ap_vendor_elements(
5244 const struct global_parse_data *data,
5245 struct wpa_config *config, int line, const char *pos)
5246{
5247 struct wpabuf *tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005248
Hai Shaloma20dcd72022-02-04 13:43:00 -08005249 if (!*pos) {
5250 wpabuf_free(config->ap_vendor_elements);
5251 config->ap_vendor_elements = NULL;
5252 return 0;
5253 }
5254
5255 tmp = wpabuf_parse_bin(pos);
5256 if (!tmp) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005257 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5258 line);
5259 return -1;
5260 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005261 wpabuf_free(config->ap_vendor_elements);
5262 config->ap_vendor_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005263
Hai Shaloma20dcd72022-02-04 13:43:00 -08005264 return 0;
5265}
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005266
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005267
Hai Shaloma20dcd72022-02-04 13:43:00 -08005268static int wpa_config_process_ap_assocresp_elements(
5269 const struct global_parse_data *data,
5270 struct wpa_config *config, int line, const char *pos)
5271{
5272 struct wpabuf *tmp;
5273
5274 if (!*pos) {
5275 wpabuf_free(config->ap_assocresp_elements);
5276 config->ap_assocresp_elements = NULL;
5277 return 0;
5278 }
5279
5280 tmp = wpabuf_parse_bin(pos);
5281 if (!tmp) {
5282 wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5283 line);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005284 return -1;
5285 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005286 wpabuf_free(config->ap_assocresp_elements);
5287 config->ap_assocresp_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005288
5289 return 0;
5290}
5291
5292
Dmitry Shmidt56052862013-10-04 10:23:25 -07005293#ifdef CONFIG_CTRL_IFACE
5294static int wpa_config_process_no_ctrl_interface(
5295 const struct global_parse_data *data,
5296 struct wpa_config *config, int line, const char *pos)
5297{
5298 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5299 os_free(config->ctrl_interface);
5300 config->ctrl_interface = NULL;
5301 return 0;
5302}
5303#endif /* CONFIG_CTRL_IFACE */
5304
5305
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005306static int wpa_config_get_int(const char *name, struct wpa_config *config,
5307 long offset, char *buf, size_t buflen,
5308 int pretty_print)
5309{
5310 int *val = (int *) (((u8 *) config) + (long) offset);
5311
5312 if (pretty_print)
5313 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5314 return os_snprintf(buf, buflen, "%d", *val);
5315}
5316
5317
5318static int wpa_config_get_str(const char *name, struct wpa_config *config,
5319 long offset, char *buf, size_t buflen,
5320 int pretty_print)
5321{
5322 char **val = (char **) (((u8 *) config) + (long) offset);
5323 int res;
5324
5325 if (pretty_print)
5326 res = os_snprintf(buf, buflen, "%s=%s\n", name,
5327 *val ? *val : "null");
5328 else if (!*val)
5329 return -1;
5330 else
5331 res = os_snprintf(buf, buflen, "%s", *val);
5332 if (os_snprintf_error(buflen, res))
5333 res = -1;
5334
5335 return res;
5336}
5337
5338
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005339#ifdef CONFIG_P2P
5340static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5341 long offset, char *buf, size_t buflen,
5342 int pretty_print)
5343{
5344 void *val = ((u8 *) config) + (long) offset;
5345 int res;
5346 char addr[INET_ADDRSTRLEN];
5347
5348 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5349 return -1;
5350
5351 if (pretty_print)
5352 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5353 else
5354 res = os_snprintf(buf, buflen, "%s", addr);
5355
5356 if (os_snprintf_error(buflen, res))
5357 res = -1;
5358
5359 return res;
5360}
5361#endif /* CONFIG_P2P */
5362
5363
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005364#ifdef CONFIG_TESTING_OPTIONS
5365static int wpa_config_process_mld_connect_bssid_pref(
5366 const struct global_parse_data *data,
5367 struct wpa_config *config, int line, const char *pos)
5368{
5369 if (hwaddr_aton2(pos, config->mld_connect_bssid_pref) < 0) {
5370 wpa_printf(MSG_ERROR,
5371 "Line %d: Invalid mld_connect_bssid_pref '%s'",
5372 line, pos);
5373 return -1;
5374 }
5375
5376 return 0;
5377}
5378#endif /* CONFIG_TESTING_OPTIONS */
5379
5380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381#ifdef OFFSET
5382#undef OFFSET
5383#endif /* OFFSET */
5384/* OFFSET: Get offset of a variable within the wpa_config structure */
5385#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5386
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005387#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5388#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5389#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390#define INT(f) _INT(f), NULL, NULL
Sunil Ravi99c035e2024-07-12 01:42:03 +00005391#define INT_RANGE(f, min, max) #f, wpa_global_config_parse_int_range, \
5392 wpa_config_get_int, OFFSET(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005393#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394#define STR(f) _STR(f), NULL, NULL
5395#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005396#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005397#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
5398 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399
5400static const struct global_parse_data global_fields[] = {
5401#ifdef CONFIG_CTRL_IFACE
5402 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005403 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005404 { STR(ctrl_interface_group), 0 } /* deprecated */,
5405#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005406#ifdef CONFIG_MACSEC
5407 { INT_RANGE(eapol_version, 1, 3), 0 },
5408#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005410#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 { INT(ap_scan), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005412 { FUNC(bgscan), CFG_CHANGED_BGSCAN },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005413#ifdef CONFIG_MESH
5414 { INT(user_mpm), 0 },
5415 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005416 { INT(mesh_max_inactivity), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005417 { INT_RANGE(mesh_fwding, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005418 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005419#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005420 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005421 { INT(fast_reauth), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005422#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 { STR(opensc_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005424#endif /* CONFIG_OPENSC_ENGINE_PATH */
5425#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005426 { STR(pkcs11_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005427#endif /* CONFIG_PKCS11_ENGINE_PATH */
5428#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429 { STR(pkcs11_module_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005430#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005431 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005432 { STR(pcsc_reader), 0 },
5433 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07005434 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 { STR(driver_param), 0 },
5436 { INT(dot11RSNAConfigPMKLifetime), 0 },
5437 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5438 { INT(dot11RSNAConfigSATimeout), 0 },
5439#ifndef CONFIG_NO_CONFIG_WRITE
5440 { INT(update_config), 0 },
5441#endif /* CONFIG_NO_CONFIG_WRITE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005442#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443 { FUNC_NO_VAR(load_dynamic_eap), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005444#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445#ifdef CONFIG_WPS
5446 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005447 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005448 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5449 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5451 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5452 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5453 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5454 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5455 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
5456 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5457 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07005458 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005459 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460#endif /* CONFIG_WPS */
5461#ifdef CONFIG_P2P
5462 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005463 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5464 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005465 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5466 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
5468 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5469 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
5470 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5471 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005472 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005473 { INT_RANGE(p2p_passphrase_len, 8, 63),
5474 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005475 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005476 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5477 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005478 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005479 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005480 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08005481 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005482 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005483 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005484 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005485 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005486 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005487 { IPV4(ip_addr_go), 0 },
5488 { IPV4(ip_addr_mask), 0 },
5489 { IPV4(ip_addr_start), 0 },
5490 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005491 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005492 { INT(p2p_device_random_mac_addr), 0 },
5493 { FUNC(p2p_device_persistent_mac_addr), 0 },
5494 { INT(p2p_interface_random_mac_addr), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005495 { INT(p2p_6ghz_disable), 0 },
Shuibing Dai64a8a892023-03-08 10:26:22 -08005496 { INT(p2p_dfs_chan_enable), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497#endif /* CONFIG_P2P */
5498 { FUNC(country), CFG_CHANGED_COUNTRY },
5499 { INT(bss_max_count), 0 },
5500 { INT(bss_expiration_age), 0 },
5501 { INT(bss_expiration_scan_count), 0 },
5502 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005503 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07005505 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005506 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005507#ifdef CONFIG_HS20
5508 { INT_RANGE(hs20, 0, 1), 0 },
5509#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005510 { INT_RANGE(interworking, 0, 1), 0 },
5511 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005512 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005513 { INT_RANGE(go_interworking, 0, 1), 0 },
5514 { INT_RANGE(go_access_network_type, 0, 15), 0 },
5515 { INT_RANGE(go_internet, 0, 1), 0 },
5516 { INT_RANGE(go_venue_group, 0, 255), 0 },
5517 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005518 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
5519 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005520 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5521 CFG_CHANGED_NFC_PASSWORD_TOKEN },
5522 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5523 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5524 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005525 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5526 { INT(p2p_go_max_inactivity), 0 },
5527 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005528 { INT(okc), 0 },
5529 { INT(pmf), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005530 { INT_RANGE(sae_check_mfp, 0, 1), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005531 { FUNC(sae_groups), 0 },
Hai Shalomfdcde762020-04-02 11:19:20 -07005532 { INT_RANGE(sae_pwe, 0, 3), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005533 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005534 { INT(dtim_period), 0 },
5535 { INT(beacon_int), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005536 { FUNC(ap_assocresp_elements), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005537 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005538 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005539 { FUNC(freq_list), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005540 { FUNC(initial_freq_list), 0},
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005541 { INT(scan_cur_freq), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005542 { INT(scan_res_valid_for_connect), 0},
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005543 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005544 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005545 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005546 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005547 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005548 { INT(p2p_search_delay), 0},
Sunil Ravi77d572f2023-01-17 23:58:31 +00005549 { INT_RANGE(mac_addr, 0, 2), 0 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005550 { INT(rand_addr_lifetime), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005551 { INT_RANGE(preassoc_mac_addr, 0, 2), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005552 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005553 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005554 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005555 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005556#ifdef CONFIG_FST
5557 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5558 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5559 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5560#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005561 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005562 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005563 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005564#ifdef CONFIG_MBO
5565 { STR(non_pref_chan), 0 },
5566 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5567 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005568 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5569 { INT_RANGE(oce, 0, 3), 0 },
5570#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07005571 { INT_RANGE(btm_offload, 0, 1), CFG_CHANGED_DISABLE_BTM_NOTIFY },
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005572 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005573 { INT_RANGE(ftm_responder, 0, 1), 0 },
5574 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005575 { INT(gas_rand_addr_lifetime), 0 },
5576 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005577#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005578 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005579 { STR(dpp_name), 0 },
5580 { STR(dpp_mud_url), 0 },
Sunil Ravi89eba102022-09-13 21:04:37 -07005581 { STR(dpp_extra_conf_req_name), 0 },
5582 { STR(dpp_extra_conf_req_value), 0 },
5583 { INT_RANGE(dpp_connector_privacy_default, 0, 1), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005584#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005585 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005586 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005587#ifdef CONFIG_WNM
5588 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
Hai Shalomfdcde762020-04-02 11:19:20 -07005589 { INT_RANGE(extended_key_id, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005590#endif /* CONFIG_WNM */
Hai Shalom60840252021-02-19 19:02:11 -08005591 { INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
5592#ifdef CONFIG_PASN
5593#ifdef CONFIG_TESTING_OPTIONS
5594 { INT_RANGE(force_kdk_derivation, 0, 1), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005595 { INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005596#endif /* CONFIG_TESTING_OPTIONS */
5597#endif /* CONFIG_PASN */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005598#ifdef CONFIG_TESTING_OPTIONS
5599 { INT_RANGE(mld_force_single_link, 0, 1), 0 },
5600 { INT_RANGE(mld_connect_band_pref, 0, MLD_CONNECT_BAND_PREF_MAX), 0 },
5601 { FUNC(mld_connect_bssid_pref), 0 },
5602#endif /* CONFIG_TESTING_OPTIONS */
5603 { INT_RANGE(ft_prepend_pmkid, 0, 1), CFG_CHANGED_FT_PREPEND_PMKID },
5604 /* NOTE: When adding new parameters here, add_interface() in
5605 * wpa_supplicant/dbus_new_introspect.c may need to be modified to
5606 * increase the size of the iface->xml buffer. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607};
5608
5609#undef FUNC
5610#undef _INT
5611#undef INT
5612#undef INT_RANGE
5613#undef _STR
5614#undef STR
5615#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005616#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005617#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005618#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619
5620
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005621int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5622{
5623 int result = 0;
5624 size_t i;
5625
5626 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5627 const struct global_parse_data *field = &global_fields[i];
5628 int tmp;
5629
5630 if (!field->get)
5631 continue;
5632
5633 tmp = field->get(field->name, config, (long) field->param1,
5634 buf, buflen, 1);
5635 if (tmp < 0)
5636 return -1;
5637 buf += tmp;
5638 buflen -= tmp;
5639 result += tmp;
5640 }
5641 return result;
5642}
5643
5644
5645int wpa_config_get_value(const char *name, struct wpa_config *config,
5646 char *buf, size_t buflen)
5647{
5648 size_t i;
5649
5650 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5651 const struct global_parse_data *field = &global_fields[i];
5652
5653 if (os_strcmp(name, field->name) != 0)
5654 continue;
5655 if (!field->get)
5656 break;
5657 return field->get(name, config, (long) field->param1,
5658 buf, buflen, 0);
5659 }
5660
5661 return -1;
5662}
5663
5664
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005665int wpa_config_get_num_global_field_names(void)
5666{
5667 return NUM_GLOBAL_FIELDS;
5668}
5669
5670
5671const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5672{
5673 if (i >= NUM_GLOBAL_FIELDS)
5674 return NULL;
5675
5676 if (no_var)
5677 *no_var = !global_fields[i].param1;
5678 return global_fields[i].name;
5679}
5680
5681
Hai Shalom899fcc72020-10-19 14:38:18 -07005682/**
5683 * wpa_config_process_global - Set a variable in global configuration
5684 * @config: Pointer to global configuration data
5685 * @pos: Name and value in the format "{name}={value}"
5686 * @line: Line number in configuration file or 0 if not used
5687 * Returns: 0 on success with a possible change in value, 1 on success with no
5688 * change to previously configured value, or -1 on failure
5689 *
5690 * This function can be used to set global configuration variables based on
5691 * both the configuration file and management interface input. The value
5692 * parameter must be in the same format as the text-based configuration file is
5693 * using. For example, strings are using double quotation marks.
5694 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5696{
5697 size_t i;
5698 int ret = 0;
5699
5700 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5701 const struct global_parse_data *field = &global_fields[i];
5702 size_t flen = os_strlen(field->name);
5703 if (os_strncmp(pos, field->name, flen) != 0 ||
5704 pos[flen] != '=')
5705 continue;
5706
Hai Shalom899fcc72020-10-19 14:38:18 -07005707 ret = field->parser(field, config, line, pos + flen + 1);
5708 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709 wpa_printf(MSG_ERROR, "Line %d: failed to "
5710 "parse '%s'.", line, pos);
5711 ret = -1;
5712 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005713 if (ret == 1)
5714 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005715 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5716 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717 config->changed_parameters |= field->changed_flag;
5718 break;
5719 }
5720 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005721#ifdef CONFIG_AP
Hai Shalom899fcc72020-10-19 14:38:18 -07005722 if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5723 char *tmp = os_strchr(pos, '=');
5724
5725 if (!tmp) {
5726 if (line < 0)
5727 wpa_printf(MSG_ERROR,
5728 "Line %d: invalid line %s",
5729 line, pos);
5730 return -1;
5731 }
5732 *tmp++ = '\0';
5733 if (hostapd_config_tx_queue(config->tx_queue, pos,
5734 tmp)) {
5735 wpa_printf(MSG_ERROR,
5736 "Line %d: invalid TX queue item",
5737 line);
5738 return -1;
5739 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005740 return ret;
Hai Shalom899fcc72020-10-19 14:38:18 -07005741 }
5742
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005743 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5744 char *tmp = os_strchr(pos, '=');
5745 if (tmp == NULL) {
5746 if (line < 0)
5747 return -1;
5748 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5749 "'%s'", line, pos);
5750 return -1;
5751 }
5752 *tmp++ = '\0';
5753 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5754 tmp)) {
5755 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5756 "AC item", line);
5757 return -1;
5758 }
Hai Shalomc3565922019-10-28 11:58:20 -07005759 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005760 }
5761#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 if (line < 0)
5763 return -1;
5764 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5765 line, pos);
5766 ret = -1;
5767 }
5768
5769 return ret;
5770}