blob: c6905422264636644077e9feff53ffc2c3571009 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/uuid.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/sha1.h"
15#include "rsn_supp/wpa.h"
16#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "config.h"
19
20
21#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
22#define NO_CONFIG_WRITE
23#endif
24
25/*
26 * Structure for network configuration parsing. This data is used to implement
27 * a generic parser for each network block variable. The table of configuration
28 * variables is defined below in this file (ssid_fields[]).
29 */
30struct parse_data {
31 /* Configuration variable name */
32 char *name;
33
34 /* Parser function for this variable */
35 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
36 int line, const char *value);
37
38#ifndef NO_CONFIG_WRITE
39 /* Writer function (i.e., to get the variable in text format from
40 * internal presentation). */
41 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
42#endif /* NO_CONFIG_WRITE */
43
44 /* Variable specific parameters for the parser. */
45 void *param1, *param2, *param3, *param4;
46
47 /* 0 = this variable can be included in debug output and ctrl_iface
48 * 1 = this variable contains key/private data and it must not be
49 * included in debug output unless explicitly requested. In
50 * addition, this variable will not be readable through the
51 * ctrl_iface.
52 */
53 int key_data;
54};
55
56
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057static int wpa_config_parse_str(const struct parse_data *data,
58 struct wpa_ssid *ssid,
59 int line, const char *value)
60{
61 size_t res_len, *dst_len;
62 char **dst, *tmp;
63
64 if (os_strcmp(value, "NULL") == 0) {
65 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
66 data->name);
67 tmp = NULL;
68 res_len = 0;
69 goto set;
70 }
71
72 tmp = wpa_config_parse_string(value, &res_len);
73 if (tmp == NULL) {
74 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
75 line, data->name,
76 data->key_data ? "[KEY DATA REMOVED]" : value);
77 return -1;
78 }
79
80 if (data->key_data) {
81 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
82 (u8 *) tmp, res_len);
83 } else {
84 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
85 (u8 *) tmp, res_len);
86 }
87
88 if (data->param3 && res_len < (size_t) data->param3) {
89 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
90 "min_len=%ld)", line, data->name,
91 (unsigned long) res_len, (long) data->param3);
92 os_free(tmp);
93 return -1;
94 }
95
96 if (data->param4 && res_len > (size_t) data->param4) {
97 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
98 "max_len=%ld)", line, data->name,
99 (unsigned long) res_len, (long) data->param4);
100 os_free(tmp);
101 return -1;
102 }
103
104set:
105 dst = (char **) (((u8 *) ssid) + (long) data->param1);
106 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
107 os_free(*dst);
108 *dst = tmp;
109 if (data->param2)
110 *dst_len = res_len;
111
112 return 0;
113}
114
115
116#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
118{
119 char *buf;
120
121 buf = os_malloc(len + 3);
122 if (buf == NULL)
123 return NULL;
124 buf[0] = '"';
125 os_memcpy(buf + 1, value, len);
126 buf[len + 1] = '"';
127 buf[len + 2] = '\0';
128
129 return buf;
130}
131
132
133static char * wpa_config_write_string_hex(const u8 *value, size_t len)
134{
135 char *buf;
136
137 buf = os_zalloc(2 * len + 1);
138 if (buf == NULL)
139 return NULL;
140 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
141
142 return buf;
143}
144
145
146static char * wpa_config_write_string(const u8 *value, size_t len)
147{
148 if (value == NULL)
149 return NULL;
150
151 if (is_hex(value, len))
152 return wpa_config_write_string_hex(value, len);
153 else
154 return wpa_config_write_string_ascii(value, len);
155}
156
157
158static char * wpa_config_write_str(const struct parse_data *data,
159 struct wpa_ssid *ssid)
160{
161 size_t len;
162 char **src;
163
164 src = (char **) (((u8 *) ssid) + (long) data->param1);
165 if (*src == NULL)
166 return NULL;
167
168 if (data->param2)
169 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
170 else
171 len = os_strlen(*src);
172
173 return wpa_config_write_string((const u8 *) *src, len);
174}
175#endif /* NO_CONFIG_WRITE */
176
177
178static int wpa_config_parse_int(const struct parse_data *data,
179 struct wpa_ssid *ssid,
180 int line, const char *value)
181{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700182 int val, *dst;
183 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
185 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700186 val = strtol(value, &end, 0);
187 if (*end) {
188 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
189 line, value);
190 return -1;
191 }
192 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
194
195 if (data->param3 && *dst < (long) data->param3) {
196 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
197 "min_value=%ld)", line, data->name, *dst,
198 (long) data->param3);
199 *dst = (long) data->param3;
200 return -1;
201 }
202
203 if (data->param4 && *dst > (long) data->param4) {
204 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
205 "max_value=%ld)", line, data->name, *dst,
206 (long) data->param4);
207 *dst = (long) data->param4;
208 return -1;
209 }
210
211 return 0;
212}
213
214
215#ifndef NO_CONFIG_WRITE
216static char * wpa_config_write_int(const struct parse_data *data,
217 struct wpa_ssid *ssid)
218{
219 int *src, res;
220 char *value;
221
222 src = (int *) (((u8 *) ssid) + (long) data->param1);
223
224 value = os_malloc(20);
225 if (value == NULL)
226 return NULL;
227 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 os_free(value);
230 return NULL;
231 }
232 value[20 - 1] = '\0';
233 return value;
234}
235#endif /* NO_CONFIG_WRITE */
236
237
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800238static int wpa_config_parse_addr_list(const struct parse_data *data,
239 int line, const char *value,
240 u8 **list, size_t *num, char *name,
241 u8 abort_on_error, u8 masked)
242{
243 const char *pos;
244 u8 *buf, *n, addr[2 * ETH_ALEN];
245 size_t count;
246
247 buf = NULL;
248 count = 0;
249
250 pos = value;
251 while (pos && *pos) {
252 while (*pos == ' ')
253 pos++;
254
255 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
256 if (abort_on_error || count == 0) {
257 wpa_printf(MSG_ERROR,
258 "Line %d: Invalid %s address '%s'",
259 line, name, value);
260 os_free(buf);
261 return -1;
262 }
263 /* continue anyway since this could have been from a
264 * truncated configuration file line */
265 wpa_printf(MSG_INFO,
266 "Line %d: Ignore likely truncated %s address '%s'",
267 line, name, pos);
268 } else {
269 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
270 if (n == NULL) {
271 os_free(buf);
272 return -1;
273 }
274 buf = n;
275 os_memmove(buf + 2 * ETH_ALEN, buf,
276 count * 2 * ETH_ALEN);
277 os_memcpy(buf, addr, 2 * ETH_ALEN);
278 count++;
279 wpa_printf(MSG_MSGDUMP,
280 "%s: addr=" MACSTR " mask=" MACSTR,
281 name, MAC2STR(addr),
282 MAC2STR(&addr[ETH_ALEN]));
283 }
284
285 pos = os_strchr(pos, ' ');
286 }
287
288 os_free(*list);
289 *list = buf;
290 *num = count;
291
292 return 0;
293}
294
295
296#ifndef NO_CONFIG_WRITE
297static char * wpa_config_write_addr_list(const struct parse_data *data,
298 const u8 *list, size_t num, char *name)
299{
300 char *value, *end, *pos;
301 int res;
302 size_t i;
303
304 if (list == NULL || num == 0)
305 return NULL;
306
307 value = os_malloc(2 * 20 * num);
308 if (value == NULL)
309 return NULL;
310 pos = value;
311 end = value + 2 * 20 * num;
312
313 for (i = num; i > 0; i--) {
314 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
315 const u8 *m = a + ETH_ALEN;
316
317 if (i < num)
318 *pos++ = ' ';
319 res = hwaddr_mask_txt(pos, end - pos, a, m);
320 if (res < 0) {
321 os_free(value);
322 return NULL;
323 }
324 pos += res;
325 }
326
327 return value;
328}
329#endif /* NO_CONFIG_WRITE */
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331static int wpa_config_parse_bssid(const struct parse_data *data,
332 struct wpa_ssid *ssid, int line,
333 const char *value)
334{
335 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
336 os_strcmp(value, "any") == 0) {
337 ssid->bssid_set = 0;
338 wpa_printf(MSG_MSGDUMP, "BSSID any");
339 return 0;
340 }
341 if (hwaddr_aton(value, ssid->bssid)) {
342 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
343 line, value);
344 return -1;
345 }
346 ssid->bssid_set = 1;
347 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
348 return 0;
349}
350
351
352#ifndef NO_CONFIG_WRITE
353static char * wpa_config_write_bssid(const struct parse_data *data,
354 struct wpa_ssid *ssid)
355{
356 char *value;
357 int res;
358
359 if (!ssid->bssid_set)
360 return NULL;
361
362 value = os_malloc(20);
363 if (value == NULL)
364 return NULL;
365 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800366 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 os_free(value);
368 return NULL;
369 }
370 value[20 - 1] = '\0';
371 return value;
372}
373#endif /* NO_CONFIG_WRITE */
374
375
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800376static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
377 struct wpa_ssid *ssid, int line,
378 const char *value)
379{
380 return wpa_config_parse_addr_list(data, line, value,
381 &ssid->bssid_blacklist,
382 &ssid->num_bssid_blacklist,
383 "bssid_blacklist", 1, 1);
384}
385
386
387#ifndef NO_CONFIG_WRITE
388static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
389 struct wpa_ssid *ssid)
390{
391 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
392 ssid->num_bssid_blacklist,
393 "bssid_blacklist");
394}
395#endif /* NO_CONFIG_WRITE */
396
397
398static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
399 struct wpa_ssid *ssid, int line,
400 const char *value)
401{
402 return wpa_config_parse_addr_list(data, line, value,
403 &ssid->bssid_whitelist,
404 &ssid->num_bssid_whitelist,
405 "bssid_whitelist", 1, 1);
406}
407
408
409#ifndef NO_CONFIG_WRITE
410static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
411 struct wpa_ssid *ssid)
412{
413 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
414 ssid->num_bssid_whitelist,
415 "bssid_whitelist");
416}
417#endif /* NO_CONFIG_WRITE */
418
419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420static int wpa_config_parse_psk(const struct parse_data *data,
421 struct wpa_ssid *ssid, int line,
422 const char *value)
423{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424#ifdef CONFIG_EXT_PASSWORD
425 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700426 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427 ssid->passphrase = NULL;
428 ssid->psk_set = 0;
429 os_free(ssid->ext_psk);
430 ssid->ext_psk = os_strdup(value + 4);
431 if (ssid->ext_psk == NULL)
432 return -1;
433 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
434 ssid->ext_psk);
435 return 0;
436 }
437#endif /* CONFIG_EXT_PASSWORD */
438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (*value == '"') {
440#ifndef CONFIG_NO_PBKDF2
441 const char *pos;
442 size_t len;
443
444 value++;
445 pos = os_strrchr(value, '"');
446 if (pos)
447 len = pos - value;
448 else
449 len = os_strlen(value);
450 if (len < 8 || len > 63) {
451 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
452 "length %lu (expected: 8..63) '%s'.",
453 line, (unsigned long) len, value);
454 return -1;
455 }
456 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
457 (u8 *) value, len);
458 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
459 os_memcmp(ssid->passphrase, value, len) == 0)
460 return 0;
461 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700462 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700463 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 if (ssid->passphrase == NULL)
465 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 return 0;
467#else /* CONFIG_NO_PBKDF2 */
468 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
469 "supported.", line);
470 return -1;
471#endif /* CONFIG_NO_PBKDF2 */
472 }
473
474 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
475 value[PMK_LEN * 2] != '\0') {
476 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
477 line, value);
478 return -1;
479 }
480
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700481 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482 ssid->passphrase = NULL;
483
484 ssid->psk_set = 1;
485 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
486 return 0;
487}
488
489
490#ifndef NO_CONFIG_WRITE
491static char * wpa_config_write_psk(const struct parse_data *data,
492 struct wpa_ssid *ssid)
493{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700494#ifdef CONFIG_EXT_PASSWORD
495 if (ssid->ext_psk) {
496 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
497 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800498 int res;
499
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700500 if (buf == NULL)
501 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800502 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
503 if (os_snprintf_error(len, res)) {
504 os_free(buf);
505 buf = NULL;
506 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700507 return buf;
508 }
509#endif /* CONFIG_EXT_PASSWORD */
510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 if (ssid->passphrase)
512 return wpa_config_write_string_ascii(
513 (const u8 *) ssid->passphrase,
514 os_strlen(ssid->passphrase));
515
516 if (ssid->psk_set)
517 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
518
519 return NULL;
520}
521#endif /* NO_CONFIG_WRITE */
522
523
524static int wpa_config_parse_proto(const struct parse_data *data,
525 struct wpa_ssid *ssid, int line,
526 const char *value)
527{
528 int val = 0, last, errors = 0;
529 char *start, *end, *buf;
530
531 buf = os_strdup(value);
532 if (buf == NULL)
533 return -1;
534 start = buf;
535
536 while (*start != '\0') {
537 while (*start == ' ' || *start == '\t')
538 start++;
539 if (*start == '\0')
540 break;
541 end = start;
542 while (*end != ' ' && *end != '\t' && *end != '\0')
543 end++;
544 last = *end == '\0';
545 *end = '\0';
546 if (os_strcmp(start, "WPA") == 0)
547 val |= WPA_PROTO_WPA;
548 else if (os_strcmp(start, "RSN") == 0 ||
549 os_strcmp(start, "WPA2") == 0)
550 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800551 else if (os_strcmp(start, "OSEN") == 0)
552 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 else {
554 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
555 line, start);
556 errors++;
557 }
558
559 if (last)
560 break;
561 start = end + 1;
562 }
563 os_free(buf);
564
565 if (val == 0) {
566 wpa_printf(MSG_ERROR,
567 "Line %d: no proto values configured.", line);
568 errors++;
569 }
570
571 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
572 ssid->proto = val;
573 return errors ? -1 : 0;
574}
575
576
577#ifndef NO_CONFIG_WRITE
578static char * wpa_config_write_proto(const struct parse_data *data,
579 struct wpa_ssid *ssid)
580{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700581 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 char *buf, *pos, *end;
583
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800584 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 if (buf == NULL)
586 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800587 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588
589 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700590 ret = os_snprintf(pos, end - pos, "%sWPA",
591 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800592 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 return buf;
594 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 }
596
597 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700598 ret = os_snprintf(pos, end - pos, "%sRSN",
599 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800600 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 return buf;
602 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 }
604
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800605 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700606 ret = os_snprintf(pos, end - pos, "%sOSEN",
607 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800609 return buf;
610 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700611 }
612
613 if (pos == buf) {
614 os_free(buf);
615 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800616 }
617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 return buf;
619}
620#endif /* NO_CONFIG_WRITE */
621
622
623static int wpa_config_parse_key_mgmt(const struct parse_data *data,
624 struct wpa_ssid *ssid, int line,
625 const char *value)
626{
627 int val = 0, last, errors = 0;
628 char *start, *end, *buf;
629
630 buf = os_strdup(value);
631 if (buf == NULL)
632 return -1;
633 start = buf;
634
635 while (*start != '\0') {
636 while (*start == ' ' || *start == '\t')
637 start++;
638 if (*start == '\0')
639 break;
640 end = start;
641 while (*end != ' ' && *end != '\t' && *end != '\0')
642 end++;
643 last = *end == '\0';
644 *end = '\0';
645 if (os_strcmp(start, "WPA-PSK") == 0)
646 val |= WPA_KEY_MGMT_PSK;
647 else if (os_strcmp(start, "WPA-EAP") == 0)
648 val |= WPA_KEY_MGMT_IEEE8021X;
649 else if (os_strcmp(start, "IEEE8021X") == 0)
650 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
651 else if (os_strcmp(start, "NONE") == 0)
652 val |= WPA_KEY_MGMT_NONE;
653 else if (os_strcmp(start, "WPA-NONE") == 0)
654 val |= WPA_KEY_MGMT_WPA_NONE;
655#ifdef CONFIG_IEEE80211R
656 else if (os_strcmp(start, "FT-PSK") == 0)
657 val |= WPA_KEY_MGMT_FT_PSK;
658 else if (os_strcmp(start, "FT-EAP") == 0)
659 val |= WPA_KEY_MGMT_FT_IEEE8021X;
660#endif /* CONFIG_IEEE80211R */
661#ifdef CONFIG_IEEE80211W
662 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
663 val |= WPA_KEY_MGMT_PSK_SHA256;
664 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
665 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
666#endif /* CONFIG_IEEE80211W */
667#ifdef CONFIG_WPS
668 else if (os_strcmp(start, "WPS") == 0)
669 val |= WPA_KEY_MGMT_WPS;
670#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800671#ifdef CONFIG_SAE
672 else if (os_strcmp(start, "SAE") == 0)
673 val |= WPA_KEY_MGMT_SAE;
674 else if (os_strcmp(start, "FT-SAE") == 0)
675 val |= WPA_KEY_MGMT_FT_SAE;
676#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800677#ifdef CONFIG_HS20
678 else if (os_strcmp(start, "OSEN") == 0)
679 val |= WPA_KEY_MGMT_OSEN;
680#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800681#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800682 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
683 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800684#endif /* CONFIG_SUITEB */
685#ifdef CONFIG_SUITEB192
686 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
687 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
688#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 else {
690 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
691 line, start);
692 errors++;
693 }
694
695 if (last)
696 break;
697 start = end + 1;
698 }
699 os_free(buf);
700
701 if (val == 0) {
702 wpa_printf(MSG_ERROR,
703 "Line %d: no key_mgmt values configured.", line);
704 errors++;
705 }
706
707 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
708 ssid->key_mgmt = val;
709 return errors ? -1 : 0;
710}
711
712
713#ifndef NO_CONFIG_WRITE
714static char * wpa_config_write_key_mgmt(const struct parse_data *data,
715 struct wpa_ssid *ssid)
716{
717 char *buf, *pos, *end;
718 int ret;
719
Dmitry Shmidt96571392013-10-14 12:54:46 -0700720 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 if (buf == NULL)
722 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700723 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724
725 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
726 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
727 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 end[-1] = '\0';
730 return buf;
731 }
732 pos += ret;
733 }
734
735 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
736 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
737 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800738 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 end[-1] = '\0';
740 return buf;
741 }
742 pos += ret;
743 }
744
745 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
746 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
747 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800748 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 end[-1] = '\0';
750 return buf;
751 }
752 pos += ret;
753 }
754
755 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
756 ret = os_snprintf(pos, end - pos, "%sNONE",
757 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800758 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 end[-1] = '\0';
760 return buf;
761 }
762 pos += ret;
763 }
764
765 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
766 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
767 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800768 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 end[-1] = '\0';
770 return buf;
771 }
772 pos += ret;
773 }
774
775#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700776 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
777 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
778 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800779 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700780 end[-1] = '\0';
781 return buf;
782 }
783 pos += ret;
784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785
Dmitry Shmidt96571392013-10-14 12:54:46 -0700786 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
787 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
788 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800789 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700790 end[-1] = '\0';
791 return buf;
792 }
793 pos += ret;
794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795#endif /* CONFIG_IEEE80211R */
796
797#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700798 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
799 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
800 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800801 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700802 end[-1] = '\0';
803 return buf;
804 }
805 pos += ret;
806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807
Dmitry Shmidt96571392013-10-14 12:54:46 -0700808 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
809 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
810 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800811 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700812 end[-1] = '\0';
813 return buf;
814 }
815 pos += ret;
816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817#endif /* CONFIG_IEEE80211W */
818
819#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700820 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
821 ret = os_snprintf(pos, end - pos, "%sWPS",
822 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800823 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700824 end[-1] = '\0';
825 return buf;
826 }
827 pos += ret;
828 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829#endif /* CONFIG_WPS */
830
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800831#ifdef CONFIG_SAE
832 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
833 ret = os_snprintf(pos, end - pos, "%sSAE",
834 pos == buf ? "" : " ");
835 if (os_snprintf_error(end - pos, ret)) {
836 end[-1] = '\0';
837 return buf;
838 }
839 pos += ret;
840 }
841
842 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
843 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
844 pos == buf ? "" : " ");
845 if (os_snprintf_error(end - pos, ret)) {
846 end[-1] = '\0';
847 return buf;
848 }
849 pos += ret;
850 }
851#endif /* CONFIG_SAE */
852
853#ifdef CONFIG_HS20
854 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
855 ret = os_snprintf(pos, end - pos, "%sOSEN",
856 pos == buf ? "" : " ");
857 if (os_snprintf_error(end - pos, ret)) {
858 end[-1] = '\0';
859 return buf;
860 }
861 pos += ret;
862 }
863#endif /* CONFIG_HS20 */
864
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800865#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800866 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
867 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
868 pos == buf ? "" : " ");
869 if (os_snprintf_error(end - pos, ret)) {
870 end[-1] = '\0';
871 return buf;
872 }
873 pos += ret;
874 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800875#endif /* CONFIG_SUITEB */
876
877#ifdef CONFIG_SUITEB192
878 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
879 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
880 pos == buf ? "" : " ");
881 if (os_snprintf_error(end - pos, ret)) {
882 end[-1] = '\0';
883 return buf;
884 }
885 pos += ret;
886 }
887#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700889 if (pos == buf) {
890 os_free(buf);
891 buf = NULL;
892 }
893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 return buf;
895}
896#endif /* NO_CONFIG_WRITE */
897
898
899static int wpa_config_parse_cipher(int line, const char *value)
900{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800901 int val = wpa_parse_cipher(value);
902 if (val < 0) {
903 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
904 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 if (val == 0) {
908 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
909 line);
910 return -1;
911 }
912 return val;
913}
914
915
916#ifndef NO_CONFIG_WRITE
917static char * wpa_config_write_cipher(int cipher)
918{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800919 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 if (buf == NULL)
921 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
924 os_free(buf);
925 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 }
927
928 return buf;
929}
930#endif /* NO_CONFIG_WRITE */
931
932
933static int wpa_config_parse_pairwise(const struct parse_data *data,
934 struct wpa_ssid *ssid, int line,
935 const char *value)
936{
937 int val;
938 val = wpa_config_parse_cipher(line, value);
939 if (val == -1)
940 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800941 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
943 "(0x%x).", line, val);
944 return -1;
945 }
946
947 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
948 ssid->pairwise_cipher = val;
949 return 0;
950}
951
952
953#ifndef NO_CONFIG_WRITE
954static char * wpa_config_write_pairwise(const struct parse_data *data,
955 struct wpa_ssid *ssid)
956{
957 return wpa_config_write_cipher(ssid->pairwise_cipher);
958}
959#endif /* NO_CONFIG_WRITE */
960
961
962static int wpa_config_parse_group(const struct parse_data *data,
963 struct wpa_ssid *ssid, int line,
964 const char *value)
965{
966 int val;
967 val = wpa_config_parse_cipher(line, value);
968 if (val == -1)
969 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800970 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
972 "(0x%x).", line, val);
973 return -1;
974 }
975
976 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
977 ssid->group_cipher = val;
978 return 0;
979}
980
981
982#ifndef NO_CONFIG_WRITE
983static char * wpa_config_write_group(const struct parse_data *data,
984 struct wpa_ssid *ssid)
985{
986 return wpa_config_write_cipher(ssid->group_cipher);
987}
988#endif /* NO_CONFIG_WRITE */
989
990
991static int wpa_config_parse_auth_alg(const struct parse_data *data,
992 struct wpa_ssid *ssid, int line,
993 const char *value)
994{
995 int val = 0, last, errors = 0;
996 char *start, *end, *buf;
997
998 buf = os_strdup(value);
999 if (buf == NULL)
1000 return -1;
1001 start = buf;
1002
1003 while (*start != '\0') {
1004 while (*start == ' ' || *start == '\t')
1005 start++;
1006 if (*start == '\0')
1007 break;
1008 end = start;
1009 while (*end != ' ' && *end != '\t' && *end != '\0')
1010 end++;
1011 last = *end == '\0';
1012 *end = '\0';
1013 if (os_strcmp(start, "OPEN") == 0)
1014 val |= WPA_AUTH_ALG_OPEN;
1015 else if (os_strcmp(start, "SHARED") == 0)
1016 val |= WPA_AUTH_ALG_SHARED;
1017 else if (os_strcmp(start, "LEAP") == 0)
1018 val |= WPA_AUTH_ALG_LEAP;
1019 else {
1020 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1021 line, start);
1022 errors++;
1023 }
1024
1025 if (last)
1026 break;
1027 start = end + 1;
1028 }
1029 os_free(buf);
1030
1031 if (val == 0) {
1032 wpa_printf(MSG_ERROR,
1033 "Line %d: no auth_alg values configured.", line);
1034 errors++;
1035 }
1036
1037 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1038 ssid->auth_alg = val;
1039 return errors ? -1 : 0;
1040}
1041
1042
1043#ifndef NO_CONFIG_WRITE
1044static char * wpa_config_write_auth_alg(const struct parse_data *data,
1045 struct wpa_ssid *ssid)
1046{
1047 char *buf, *pos, *end;
1048 int ret;
1049
1050 pos = buf = os_zalloc(30);
1051 if (buf == NULL)
1052 return NULL;
1053 end = buf + 30;
1054
1055 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1056 ret = os_snprintf(pos, end - pos, "%sOPEN",
1057 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001058 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 end[-1] = '\0';
1060 return buf;
1061 }
1062 pos += ret;
1063 }
1064
1065 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1066 ret = os_snprintf(pos, end - pos, "%sSHARED",
1067 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001068 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 end[-1] = '\0';
1070 return buf;
1071 }
1072 pos += ret;
1073 }
1074
1075 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1076 ret = os_snprintf(pos, end - pos, "%sLEAP",
1077 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001078 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 end[-1] = '\0';
1080 return buf;
1081 }
1082 pos += ret;
1083 }
1084
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001085 if (pos == buf) {
1086 os_free(buf);
1087 buf = NULL;
1088 }
1089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 return buf;
1091}
1092#endif /* NO_CONFIG_WRITE */
1093
1094
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001095static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096{
1097 int *freqs;
1098 size_t used, len;
1099 const char *pos;
1100
1101 used = 0;
1102 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 if (freqs == NULL)
1105 return NULL;
1106
1107 pos = value;
1108 while (pos) {
1109 while (*pos == ' ')
1110 pos++;
1111 if (used == len) {
1112 int *n;
1113 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001114 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 if (n == NULL) {
1116 os_free(freqs);
1117 return NULL;
1118 }
1119 for (i = len; i <= len * 2; i++)
1120 n[i] = 0;
1121 freqs = n;
1122 len *= 2;
1123 }
1124
1125 freqs[used] = atoi(pos);
1126 if (freqs[used] == 0)
1127 break;
1128 used++;
1129 pos = os_strchr(pos + 1, ' ');
1130 }
1131
1132 return freqs;
1133}
1134
1135
1136static int wpa_config_parse_scan_freq(const struct parse_data *data,
1137 struct wpa_ssid *ssid, int line,
1138 const char *value)
1139{
1140 int *freqs;
1141
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001142 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 if (freqs == NULL)
1144 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001145 if (freqs[0] == 0) {
1146 os_free(freqs);
1147 freqs = NULL;
1148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 os_free(ssid->scan_freq);
1150 ssid->scan_freq = freqs;
1151
1152 return 0;
1153}
1154
1155
1156static int wpa_config_parse_freq_list(const struct parse_data *data,
1157 struct wpa_ssid *ssid, int line,
1158 const char *value)
1159{
1160 int *freqs;
1161
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001162 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 if (freqs == NULL)
1164 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001165 if (freqs[0] == 0) {
1166 os_free(freqs);
1167 freqs = NULL;
1168 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 os_free(ssid->freq_list);
1170 ssid->freq_list = freqs;
1171
1172 return 0;
1173}
1174
1175
1176#ifndef NO_CONFIG_WRITE
1177static char * wpa_config_write_freqs(const struct parse_data *data,
1178 const int *freqs)
1179{
1180 char *buf, *pos, *end;
1181 int i, ret;
1182 size_t count;
1183
1184 if (freqs == NULL)
1185 return NULL;
1186
1187 count = 0;
1188 for (i = 0; freqs[i]; i++)
1189 count++;
1190
1191 pos = buf = os_zalloc(10 * count + 1);
1192 if (buf == NULL)
1193 return NULL;
1194 end = buf + 10 * count + 1;
1195
1196 for (i = 0; freqs[i]; i++) {
1197 ret = os_snprintf(pos, end - pos, "%s%u",
1198 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001199 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 end[-1] = '\0';
1201 return buf;
1202 }
1203 pos += ret;
1204 }
1205
1206 return buf;
1207}
1208
1209
1210static char * wpa_config_write_scan_freq(const struct parse_data *data,
1211 struct wpa_ssid *ssid)
1212{
1213 return wpa_config_write_freqs(data, ssid->scan_freq);
1214}
1215
1216
1217static char * wpa_config_write_freq_list(const struct parse_data *data,
1218 struct wpa_ssid *ssid)
1219{
1220 return wpa_config_write_freqs(data, ssid->freq_list);
1221}
1222#endif /* NO_CONFIG_WRITE */
1223
1224
1225#ifdef IEEE8021X_EAPOL
1226static int wpa_config_parse_eap(const struct parse_data *data,
1227 struct wpa_ssid *ssid, int line,
1228 const char *value)
1229{
1230 int last, errors = 0;
1231 char *start, *end, *buf;
1232 struct eap_method_type *methods = NULL, *tmp;
1233 size_t num_methods = 0;
1234
1235 buf = os_strdup(value);
1236 if (buf == NULL)
1237 return -1;
1238 start = buf;
1239
1240 while (*start != '\0') {
1241 while (*start == ' ' || *start == '\t')
1242 start++;
1243 if (*start == '\0')
1244 break;
1245 end = start;
1246 while (*end != ' ' && *end != '\t' && *end != '\0')
1247 end++;
1248 last = *end == '\0';
1249 *end = '\0';
1250 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001251 methods = os_realloc_array(methods, num_methods + 1,
1252 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 if (methods == NULL) {
1254 os_free(tmp);
1255 os_free(buf);
1256 return -1;
1257 }
1258 methods[num_methods].method = eap_peer_get_type(
1259 start, &methods[num_methods].vendor);
1260 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1261 methods[num_methods].method == EAP_TYPE_NONE) {
1262 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1263 "'%s'", line, start);
1264 wpa_printf(MSG_ERROR, "You may need to add support for"
1265 " this EAP method during wpa_supplicant\n"
1266 "build time configuration.\n"
1267 "See README for more information.");
1268 errors++;
1269 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1270 methods[num_methods].method == EAP_TYPE_LEAP)
1271 ssid->leap++;
1272 else
1273 ssid->non_leap++;
1274 num_methods++;
1275 if (last)
1276 break;
1277 start = end + 1;
1278 }
1279 os_free(buf);
1280
1281 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001282 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 if (methods == NULL) {
1284 os_free(tmp);
1285 return -1;
1286 }
1287 methods[num_methods].vendor = EAP_VENDOR_IETF;
1288 methods[num_methods].method = EAP_TYPE_NONE;
1289 num_methods++;
1290
1291 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1292 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001293 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 ssid->eap.eap_methods = methods;
1295 return errors ? -1 : 0;
1296}
1297
1298
1299static char * wpa_config_write_eap(const struct parse_data *data,
1300 struct wpa_ssid *ssid)
1301{
1302 int i, ret;
1303 char *buf, *pos, *end;
1304 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1305 const char *name;
1306
1307 if (eap_methods == NULL)
1308 return NULL;
1309
1310 pos = buf = os_zalloc(100);
1311 if (buf == NULL)
1312 return NULL;
1313 end = buf + 100;
1314
1315 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1316 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1317 name = eap_get_name(eap_methods[i].vendor,
1318 eap_methods[i].method);
1319 if (name) {
1320 ret = os_snprintf(pos, end - pos, "%s%s",
1321 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001322 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 break;
1324 pos += ret;
1325 }
1326 }
1327
1328 end[-1] = '\0';
1329
1330 return buf;
1331}
1332
1333
1334static int wpa_config_parse_password(const struct parse_data *data,
1335 struct wpa_ssid *ssid, int line,
1336 const char *value)
1337{
1338 u8 *hash;
1339
1340 if (os_strcmp(value, "NULL") == 0) {
1341 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001342 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 ssid->eap.password = NULL;
1344 ssid->eap.password_len = 0;
1345 return 0;
1346 }
1347
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001348#ifdef CONFIG_EXT_PASSWORD
1349 if (os_strncmp(value, "ext:", 4) == 0) {
1350 char *name = os_strdup(value + 4);
1351 if (name == NULL)
1352 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001353 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001354 ssid->eap.password = (u8 *) name;
1355 ssid->eap.password_len = os_strlen(name);
1356 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1357 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1358 return 0;
1359 }
1360#endif /* CONFIG_EXT_PASSWORD */
1361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 if (os_strncmp(value, "hash:", 5) != 0) {
1363 char *tmp;
1364 size_t res_len;
1365
1366 tmp = wpa_config_parse_string(value, &res_len);
1367 if (tmp == NULL) {
1368 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1369 "password.", line);
1370 return -1;
1371 }
1372 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1373 (u8 *) tmp, res_len);
1374
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001375 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376 ssid->eap.password = (u8 *) tmp;
1377 ssid->eap.password_len = res_len;
1378 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001379 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380
1381 return 0;
1382 }
1383
1384
1385 /* NtPasswordHash: hash:<32 hex digits> */
1386 if (os_strlen(value + 5) != 2 * 16) {
1387 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1388 "(expected 32 hex digits)", line);
1389 return -1;
1390 }
1391
1392 hash = os_malloc(16);
1393 if (hash == NULL)
1394 return -1;
1395
1396 if (hexstr2bin(value + 5, hash, 16)) {
1397 os_free(hash);
1398 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1399 return -1;
1400 }
1401
1402 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1403
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001404 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 ssid->eap.password = hash;
1406 ssid->eap.password_len = 16;
1407 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001408 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409
1410 return 0;
1411}
1412
1413
1414static char * wpa_config_write_password(const struct parse_data *data,
1415 struct wpa_ssid *ssid)
1416{
1417 char *buf;
1418
1419 if (ssid->eap.password == NULL)
1420 return NULL;
1421
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001422#ifdef CONFIG_EXT_PASSWORD
1423 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1424 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1425 if (buf == NULL)
1426 return NULL;
1427 os_memcpy(buf, "ext:", 4);
1428 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1429 return buf;
1430 }
1431#endif /* CONFIG_EXT_PASSWORD */
1432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1434 return wpa_config_write_string(
1435 ssid->eap.password, ssid->eap.password_len);
1436 }
1437
1438 buf = os_malloc(5 + 32 + 1);
1439 if (buf == NULL)
1440 return NULL;
1441
1442 os_memcpy(buf, "hash:", 5);
1443 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1444
1445 return buf;
1446}
1447#endif /* IEEE8021X_EAPOL */
1448
1449
1450static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1451 const char *value, int idx)
1452{
1453 char *buf, title[20];
1454 int res;
1455
1456 buf = wpa_config_parse_string(value, len);
1457 if (buf == NULL) {
1458 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1459 line, idx, value);
1460 return -1;
1461 }
1462 if (*len > MAX_WEP_KEY_LEN) {
1463 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1464 line, idx, value);
1465 os_free(buf);
1466 return -1;
1467 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001468 if (*len && *len != 5 && *len != 13 && *len != 16) {
1469 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1470 "this network block will be ignored",
1471 line, (unsigned int) *len);
1472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001474 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001476 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1478 return 0;
1479}
1480
1481
1482static int wpa_config_parse_wep_key0(const struct parse_data *data,
1483 struct wpa_ssid *ssid, int line,
1484 const char *value)
1485{
1486 return wpa_config_parse_wep_key(ssid->wep_key[0],
1487 &ssid->wep_key_len[0], line,
1488 value, 0);
1489}
1490
1491
1492static int wpa_config_parse_wep_key1(const struct parse_data *data,
1493 struct wpa_ssid *ssid, int line,
1494 const char *value)
1495{
1496 return wpa_config_parse_wep_key(ssid->wep_key[1],
1497 &ssid->wep_key_len[1], line,
1498 value, 1);
1499}
1500
1501
1502static int wpa_config_parse_wep_key2(const struct parse_data *data,
1503 struct wpa_ssid *ssid, int line,
1504 const char *value)
1505{
1506 return wpa_config_parse_wep_key(ssid->wep_key[2],
1507 &ssid->wep_key_len[2], line,
1508 value, 2);
1509}
1510
1511
1512static int wpa_config_parse_wep_key3(const struct parse_data *data,
1513 struct wpa_ssid *ssid, int line,
1514 const char *value)
1515{
1516 return wpa_config_parse_wep_key(ssid->wep_key[3],
1517 &ssid->wep_key_len[3], line,
1518 value, 3);
1519}
1520
1521
1522#ifndef NO_CONFIG_WRITE
1523static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1524{
1525 if (ssid->wep_key_len[idx] == 0)
1526 return NULL;
1527 return wpa_config_write_string(ssid->wep_key[idx],
1528 ssid->wep_key_len[idx]);
1529}
1530
1531
1532static char * wpa_config_write_wep_key0(const struct parse_data *data,
1533 struct wpa_ssid *ssid)
1534{
1535 return wpa_config_write_wep_key(ssid, 0);
1536}
1537
1538
1539static char * wpa_config_write_wep_key1(const struct parse_data *data,
1540 struct wpa_ssid *ssid)
1541{
1542 return wpa_config_write_wep_key(ssid, 1);
1543}
1544
1545
1546static char * wpa_config_write_wep_key2(const struct parse_data *data,
1547 struct wpa_ssid *ssid)
1548{
1549 return wpa_config_write_wep_key(ssid, 2);
1550}
1551
1552
1553static char * wpa_config_write_wep_key3(const struct parse_data *data,
1554 struct wpa_ssid *ssid)
1555{
1556 return wpa_config_write_wep_key(ssid, 3);
1557}
1558#endif /* NO_CONFIG_WRITE */
1559
1560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561#ifdef CONFIG_P2P
1562
Dmitry Shmidt54605472013-11-08 11:10:19 -08001563static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1564 struct wpa_ssid *ssid, int line,
1565 const char *value)
1566{
1567 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1568 os_strcmp(value, "any") == 0) {
1569 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1570 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1571 return 0;
1572 }
1573 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1574 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1575 line, value);
1576 return -1;
1577 }
1578 ssid->bssid_set = 1;
1579 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1580 MAC2STR(ssid->go_p2p_dev_addr));
1581 return 0;
1582}
1583
1584
1585#ifndef NO_CONFIG_WRITE
1586static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1587 struct wpa_ssid *ssid)
1588{
1589 char *value;
1590 int res;
1591
1592 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1593 return NULL;
1594
1595 value = os_malloc(20);
1596 if (value == NULL)
1597 return NULL;
1598 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001599 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001600 os_free(value);
1601 return NULL;
1602 }
1603 value[20 - 1] = '\0';
1604 return value;
1605}
1606#endif /* NO_CONFIG_WRITE */
1607
1608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1610 struct wpa_ssid *ssid, int line,
1611 const char *value)
1612{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001613 return wpa_config_parse_addr_list(data, line, value,
1614 &ssid->p2p_client_list,
1615 &ssid->num_p2p_clients,
1616 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617}
1618
1619
1620#ifndef NO_CONFIG_WRITE
1621static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1622 struct wpa_ssid *ssid)
1623{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001624 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1625 ssid->num_p2p_clients,
1626 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001627}
1628#endif /* NO_CONFIG_WRITE */
1629
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001630
1631static int wpa_config_parse_psk_list(const struct parse_data *data,
1632 struct wpa_ssid *ssid, int line,
1633 const char *value)
1634{
1635 struct psk_list_entry *p;
1636 const char *pos;
1637
1638 p = os_zalloc(sizeof(*p));
1639 if (p == NULL)
1640 return -1;
1641
1642 pos = value;
1643 if (os_strncmp(pos, "P2P-", 4) == 0) {
1644 p->p2p = 1;
1645 pos += 4;
1646 }
1647
1648 if (hwaddr_aton(pos, p->addr)) {
1649 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1650 line, pos);
1651 os_free(p);
1652 return -1;
1653 }
1654 pos += 17;
1655 if (*pos != '-') {
1656 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1657 line, pos);
1658 os_free(p);
1659 return -1;
1660 }
1661 pos++;
1662
1663 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1664 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1665 line, pos);
1666 os_free(p);
1667 return -1;
1668 }
1669
1670 dl_list_add(&ssid->psk_list, &p->list);
1671
1672 return 0;
1673}
1674
1675
1676#ifndef NO_CONFIG_WRITE
1677static char * wpa_config_write_psk_list(const struct parse_data *data,
1678 struct wpa_ssid *ssid)
1679{
1680 return NULL;
1681}
1682#endif /* NO_CONFIG_WRITE */
1683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684#endif /* CONFIG_P2P */
1685
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001686
1687#ifdef CONFIG_MESH
1688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001689static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1690 struct wpa_ssid *ssid, int line,
1691 const char *value)
1692{
1693 int *rates = wpa_config_parse_int_array(value);
1694
1695 if (rates == NULL) {
1696 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1697 line, value);
1698 return -1;
1699 }
1700 if (rates[0] == 0) {
1701 os_free(rates);
1702 rates = NULL;
1703 }
1704
1705 os_free(ssid->mesh_basic_rates);
1706 ssid->mesh_basic_rates = rates;
1707
1708 return 0;
1709}
1710
1711
1712#ifndef NO_CONFIG_WRITE
1713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001714static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1715 struct wpa_ssid *ssid)
1716{
1717 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1718}
1719
1720#endif /* NO_CONFIG_WRITE */
1721
1722#endif /* CONFIG_MESH */
1723
1724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725/* Helper macros for network block parser */
1726
1727#ifdef OFFSET
1728#undef OFFSET
1729#endif /* OFFSET */
1730/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1731#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1732
1733/* STR: Define a string variable for an ASCII string; f = field name */
1734#ifdef NO_CONFIG_WRITE
1735#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1736#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1737#else /* NO_CONFIG_WRITE */
1738#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1739#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1740#endif /* NO_CONFIG_WRITE */
1741#define STR(f) _STR(f), NULL, NULL, NULL, 0
1742#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1743#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1744#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1745
1746/* STR_LEN: Define a string variable with a separate variable for storing the
1747 * data length. Unlike STR(), this can be used to store arbitrary binary data
1748 * (i.e., even nul termination character). */
1749#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1750#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1751#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1752#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1753#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1754
1755/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1756 * explicitly specified. */
1757#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1758#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1759#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1760
1761#ifdef NO_CONFIG_WRITE
1762#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1763#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1764#else /* NO_CONFIG_WRITE */
1765#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1766 OFFSET(f), (void *) 0
1767#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1768 OFFSET(eap.f), (void *) 0
1769#endif /* NO_CONFIG_WRITE */
1770
1771/* INT: Define an integer variable */
1772#define INT(f) _INT(f), NULL, NULL, 0
1773#define INTe(f) _INTe(f), NULL, NULL, 0
1774
1775/* INT_RANGE: Define an integer variable with allowed value range */
1776#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1777
1778/* FUNC: Define a configuration variable that uses a custom function for
1779 * parsing and writing the value. */
1780#ifdef NO_CONFIG_WRITE
1781#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1782#else /* NO_CONFIG_WRITE */
1783#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1784 NULL, NULL, NULL, NULL
1785#endif /* NO_CONFIG_WRITE */
1786#define FUNC(f) _FUNC(f), 0
1787#define FUNC_KEY(f) _FUNC(f), 1
1788
1789/*
1790 * Table of network configuration variables. This table is used to parse each
1791 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1792 * that is inside a network block.
1793 *
1794 * This table is generated using the helper macros defined above and with
1795 * generous help from the C pre-processor. The field name is stored as a string
1796 * into .name and for STR and INT types, the offset of the target buffer within
1797 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1798 * offset to the field containing the length of the configuration variable.
1799 * .param3 and .param4 can be used to mark the allowed range (length for STR
1800 * and value for INT).
1801 *
1802 * For each configuration line in wpa_supplicant.conf, the parser goes through
1803 * this table and select the entry that matches with the field name. The parser
1804 * function (.parser) is then called to parse the actual value of the field.
1805 *
1806 * This kind of mechanism makes it easy to add new configuration parameters,
1807 * since only one line needs to be added into this table and into the
1808 * struct wpa_ssid definition if the new variable is either a string or
1809 * integer. More complex types will need to use their own parser and writer
1810 * functions.
1811 */
1812static const struct parse_data ssid_fields[] = {
1813 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1814 { INT_RANGE(scan_ssid, 0, 1) },
1815 { FUNC(bssid) },
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001816 { FUNC(bssid_blacklist) },
1817 { FUNC(bssid_whitelist) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001819 { INT(mem_only_psk) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 { FUNC(proto) },
1821 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001822 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 { FUNC(pairwise) },
1824 { FUNC(group) },
1825 { FUNC(auth_alg) },
1826 { FUNC(scan_freq) },
1827 { FUNC(freq_list) },
1828#ifdef IEEE8021X_EAPOL
1829 { FUNC(eap) },
1830 { STR_LENe(identity) },
1831 { STR_LENe(anonymous_identity) },
1832 { FUNC_KEY(password) },
1833 { STRe(ca_cert) },
1834 { STRe(ca_path) },
1835 { STRe(client_cert) },
1836 { STRe(private_key) },
1837 { STR_KEYe(private_key_passwd) },
1838 { STRe(dh_file) },
1839 { STRe(subject_match) },
1840 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001841 { STRe(domain_suffix_match) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001842 { STRe(domain_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 { STRe(ca_cert2) },
1844 { STRe(ca_path2) },
1845 { STRe(client_cert2) },
1846 { STRe(private_key2) },
1847 { STR_KEYe(private_key2_passwd) },
1848 { STRe(dh_file2) },
1849 { STRe(subject_match2) },
1850 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001851 { STRe(domain_suffix_match2) },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001852 { STRe(domain_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 { STRe(phase1) },
1854 { STRe(phase2) },
1855 { STRe(pcsc) },
1856 { STR_KEYe(pin) },
1857 { STRe(engine_id) },
1858 { STRe(key_id) },
1859 { STRe(cert_id) },
1860 { STRe(ca_cert_id) },
1861 { STR_KEYe(pin2) },
1862 { STRe(engine2_id) },
1863 { STRe(key2_id) },
1864 { STRe(cert2_id) },
1865 { STRe(ca_cert2_id) },
1866 { INTe(engine) },
1867 { INTe(engine2) },
1868 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001869 { INTe(sim_num) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001870 { STRe(openssl_ciphers) },
1871 { INTe(erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872#endif /* IEEE8021X_EAPOL */
1873 { FUNC_KEY(wep_key0) },
1874 { FUNC_KEY(wep_key1) },
1875 { FUNC_KEY(wep_key2) },
1876 { FUNC_KEY(wep_key3) },
1877 { INT(wep_tx_keyidx) },
1878 { INT(priority) },
1879#ifdef IEEE8021X_EAPOL
1880 { INT(eap_workaround) },
1881 { STRe(pac_file) },
1882 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001883 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885#ifdef CONFIG_MESH
1886 { INT_RANGE(mode, 0, 5) },
1887 { INT_RANGE(no_auto_peer, 0, 1) },
1888#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001890#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 { INT_RANGE(proactive_key_caching, 0, 1) },
1892 { INT_RANGE(disabled, 0, 2) },
1893 { STR(id_str) },
1894#ifdef CONFIG_IEEE80211W
1895 { INT_RANGE(ieee80211w, 0, 2) },
1896#endif /* CONFIG_IEEE80211W */
1897 { INT_RANGE(peerkey, 0, 1) },
1898 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001899 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001900 { INT_RANGE(fixed_freq, 0, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001901#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001902 { FUNC(mesh_basic_rates) },
1903 { INT(dot11MeshMaxRetries) },
1904 { INT(dot11MeshRetryTimeout) },
1905 { INT(dot11MeshConfirmTimeout) },
1906 { INT(dot11MeshHoldingTimeout) },
1907#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 { INT(wpa_ptk_rekey) },
1909 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001910 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001911#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001912 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001914 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001915#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001916#ifdef CONFIG_HT_OVERRIDES
1917 { INT_RANGE(disable_ht, 0, 1) },
1918 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001919 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001920 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001921 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001922 { INT_RANGE(disable_max_amsdu, -1, 1) },
1923 { INT_RANGE(ampdu_factor, -1, 3) },
1924 { INT_RANGE(ampdu_density, -1, 7) },
1925 { STR(ht_mcs) },
1926#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001927#ifdef CONFIG_VHT_OVERRIDES
1928 { INT_RANGE(disable_vht, 0, 1) },
1929 { INT(vht_capa) },
1930 { INT(vht_capa_mask) },
1931 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1932 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1933 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1934 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1935 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1936 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1937 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1938 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1939 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1940 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1941 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1942 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1943 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1944 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1945 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1946 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1947#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001948 { INT(ap_max_inactivity) },
1949 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001950 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001951#ifdef CONFIG_MACSEC
1952 { INT_RANGE(macsec_policy, 0, 1) },
1953#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001954#ifdef CONFIG_HS20
1955 { INT(update_identifier) },
1956#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001957 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958};
1959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960#undef OFFSET
1961#undef _STR
1962#undef STR
1963#undef STR_KEY
1964#undef _STR_LEN
1965#undef STR_LEN
1966#undef STR_LEN_KEY
1967#undef _STR_RANGE
1968#undef STR_RANGE
1969#undef STR_RANGE_KEY
1970#undef _INT
1971#undef INT
1972#undef INT_RANGE
1973#undef _FUNC
1974#undef FUNC
1975#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001976#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977
1978
1979/**
1980 * wpa_config_add_prio_network - Add a network to priority lists
1981 * @config: Configuration data from wpa_config_read()
1982 * @ssid: Pointer to the network configuration to be added to the list
1983 * Returns: 0 on success, -1 on failure
1984 *
1985 * This function is used to add a network block to the priority list of
1986 * networks. This must be called for each network when reading in the full
1987 * configuration. In addition, this can be used indirectly when updating
1988 * priorities by calling wpa_config_update_prio_list().
1989 */
1990int wpa_config_add_prio_network(struct wpa_config *config,
1991 struct wpa_ssid *ssid)
1992{
1993 int prio;
1994 struct wpa_ssid *prev, **nlist;
1995
1996 /*
1997 * Add to an existing priority list if one is available for the
1998 * configured priority level for this network.
1999 */
2000 for (prio = 0; prio < config->num_prio; prio++) {
2001 prev = config->pssid[prio];
2002 if (prev->priority == ssid->priority) {
2003 while (prev->pnext)
2004 prev = prev->pnext;
2005 prev->pnext = ssid;
2006 return 0;
2007 }
2008 }
2009
2010 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002011 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2012 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 if (nlist == NULL)
2014 return -1;
2015
2016 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002017 if (nlist[prio]->priority < ssid->priority) {
2018 os_memmove(&nlist[prio + 1], &nlist[prio],
2019 (config->num_prio - prio) *
2020 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002022 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 }
2024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 nlist[prio] = ssid;
2026 config->num_prio++;
2027 config->pssid = nlist;
2028
2029 return 0;
2030}
2031
2032
2033/**
2034 * wpa_config_update_prio_list - Update network priority list
2035 * @config: Configuration data from wpa_config_read()
2036 * Returns: 0 on success, -1 on failure
2037 *
2038 * This function is called to update the priority list of networks in the
2039 * configuration when a network is being added or removed. This is also called
2040 * if a priority for a network is changed.
2041 */
2042int wpa_config_update_prio_list(struct wpa_config *config)
2043{
2044 struct wpa_ssid *ssid;
2045 int ret = 0;
2046
2047 os_free(config->pssid);
2048 config->pssid = NULL;
2049 config->num_prio = 0;
2050
2051 ssid = config->ssid;
2052 while (ssid) {
2053 ssid->pnext = NULL;
2054 if (wpa_config_add_prio_network(config, ssid) < 0)
2055 ret = -1;
2056 ssid = ssid->next;
2057 }
2058
2059 return ret;
2060}
2061
2062
2063#ifdef IEEE8021X_EAPOL
2064static void eap_peer_config_free(struct eap_peer_config *eap)
2065{
2066 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002067 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002069 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 os_free(eap->ca_cert);
2071 os_free(eap->ca_path);
2072 os_free(eap->client_cert);
2073 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002074 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 os_free(eap->dh_file);
2076 os_free(eap->subject_match);
2077 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002078 os_free(eap->domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002079 os_free(eap->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 os_free(eap->ca_cert2);
2081 os_free(eap->ca_path2);
2082 os_free(eap->client_cert2);
2083 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002084 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 os_free(eap->dh_file2);
2086 os_free(eap->subject_match2);
2087 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002088 os_free(eap->domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002089 os_free(eap->domain_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 os_free(eap->phase1);
2091 os_free(eap->phase2);
2092 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002093 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 os_free(eap->engine_id);
2095 os_free(eap->key_id);
2096 os_free(eap->cert_id);
2097 os_free(eap->ca_cert_id);
2098 os_free(eap->key2_id);
2099 os_free(eap->cert2_id);
2100 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002101 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 os_free(eap->engine2_id);
2103 os_free(eap->otp);
2104 os_free(eap->pending_req_otp);
2105 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002106 bin_clear_free(eap->new_password, eap->new_password_len);
2107 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002108 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109}
2110#endif /* IEEE8021X_EAPOL */
2111
2112
2113/**
2114 * wpa_config_free_ssid - Free network/ssid configuration data
2115 * @ssid: Configuration data for the network
2116 *
2117 * This function frees all resources allocated for the network configuration
2118 * data.
2119 */
2120void wpa_config_free_ssid(struct wpa_ssid *ssid)
2121{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002122 struct psk_list_entry *psk;
2123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002125 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002126 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127#ifdef IEEE8021X_EAPOL
2128 eap_peer_config_free(&ssid->eap);
2129#endif /* IEEE8021X_EAPOL */
2130 os_free(ssid->id_str);
2131 os_free(ssid->scan_freq);
2132 os_free(ssid->freq_list);
2133 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002134 os_free(ssid->p2p_client_list);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002135 os_free(ssid->bssid_blacklist);
2136 os_free(ssid->bssid_whitelist);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002137#ifdef CONFIG_HT_OVERRIDES
2138 os_free(ssid->ht_mcs);
2139#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002140#ifdef CONFIG_MESH
2141 os_free(ssid->mesh_basic_rates);
2142#endif /* CONFIG_MESH */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002143 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2144 list))) {
2145 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002146 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002147 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002148 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149}
2150
2151
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152void wpa_config_free_cred(struct wpa_cred *cred)
2153{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002154 size_t i;
2155
Dmitry Shmidt04949592012-07-19 12:16:46 -07002156 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002157 str_clear_free(cred->username);
2158 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 os_free(cred->ca_cert);
2160 os_free(cred->client_cert);
2161 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002162 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002163 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002164 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002165 for (i = 0; i < cred->num_domain; i++)
2166 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002167 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002168 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002169 os_free(cred->eap_method);
2170 os_free(cred->phase1);
2171 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002172 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002173 os_free(cred->roaming_partner);
2174 os_free(cred->provisioning_sp);
2175 for (i = 0; i < cred->num_req_conn_capab; i++)
2176 os_free(cred->req_conn_capab_port[i]);
2177 os_free(cred->req_conn_capab_port);
2178 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002179 os_free(cred);
2180}
2181
2182
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002183void wpa_config_flush_blobs(struct wpa_config *config)
2184{
2185#ifndef CONFIG_NO_CONFIG_BLOBS
2186 struct wpa_config_blob *blob, *prev;
2187
2188 blob = config->blobs;
2189 config->blobs = NULL;
2190 while (blob) {
2191 prev = blob;
2192 blob = blob->next;
2193 wpa_config_free_blob(prev);
2194 }
2195#endif /* CONFIG_NO_CONFIG_BLOBS */
2196}
2197
2198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199/**
2200 * wpa_config_free - Free configuration data
2201 * @config: Configuration data from wpa_config_read()
2202 *
2203 * This function frees all resources allocated for the configuration data by
2204 * wpa_config_read().
2205 */
2206void wpa_config_free(struct wpa_config *config)
2207{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002209 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
2212 ssid = config->ssid;
2213 while (ssid) {
2214 prev = ssid;
2215 ssid = ssid->next;
2216 wpa_config_free_ssid(prev);
2217 }
2218
Dmitry Shmidt04949592012-07-19 12:16:46 -07002219 cred = config->cred;
2220 while (cred) {
2221 cprev = cred;
2222 cred = cred->next;
2223 wpa_config_free_cred(cprev);
2224 }
2225
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002226 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002229 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2230 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 os_free(config->ctrl_interface);
2232 os_free(config->ctrl_interface_group);
2233 os_free(config->opensc_engine_path);
2234 os_free(config->pkcs11_engine_path);
2235 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002236 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002237 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002238 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 os_free(config->driver_param);
2240 os_free(config->device_name);
2241 os_free(config->manufacturer);
2242 os_free(config->model_name);
2243 os_free(config->model_number);
2244 os_free(config->serial_number);
2245 os_free(config->config_methods);
2246 os_free(config->p2p_ssid_postfix);
2247 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002248 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002249 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002250 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002251 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002252 wpabuf_free(config->wps_nfc_dh_pubkey);
2253 wpabuf_free(config->wps_nfc_dh_privkey);
2254 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002255 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002256 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002257 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002258 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002259 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002260 os_free(config->wowlan_triggers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 os_free(config);
2262}
2263
2264
2265/**
2266 * wpa_config_foreach_network - Iterate over each configured network
2267 * @config: Configuration data from wpa_config_read()
2268 * @func: Callback function to process each network
2269 * @arg: Opaque argument to pass to callback function
2270 *
2271 * Iterate over the set of configured networks calling the specified
2272 * function for each item. We guard against callbacks removing the
2273 * supplied network.
2274 */
2275void wpa_config_foreach_network(struct wpa_config *config,
2276 void (*func)(void *, struct wpa_ssid *),
2277 void *arg)
2278{
2279 struct wpa_ssid *ssid, *next;
2280
2281 ssid = config->ssid;
2282 while (ssid) {
2283 next = ssid->next;
2284 func(arg, ssid);
2285 ssid = next;
2286 }
2287}
2288
2289
2290/**
2291 * wpa_config_get_network - Get configured network based on id
2292 * @config: Configuration data from wpa_config_read()
2293 * @id: Unique network id to search for
2294 * Returns: Network configuration or %NULL if not found
2295 */
2296struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2297{
2298 struct wpa_ssid *ssid;
2299
2300 ssid = config->ssid;
2301 while (ssid) {
2302 if (id == ssid->id)
2303 break;
2304 ssid = ssid->next;
2305 }
2306
2307 return ssid;
2308}
2309
2310
2311/**
2312 * wpa_config_add_network - Add a new network with empty configuration
2313 * @config: Configuration data from wpa_config_read()
2314 * Returns: The new network configuration or %NULL if operation failed
2315 */
2316struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2317{
2318 int id;
2319 struct wpa_ssid *ssid, *last = NULL;
2320
2321 id = -1;
2322 ssid = config->ssid;
2323 while (ssid) {
2324 if (ssid->id > id)
2325 id = ssid->id;
2326 last = ssid;
2327 ssid = ssid->next;
2328 }
2329 id++;
2330
2331 ssid = os_zalloc(sizeof(*ssid));
2332 if (ssid == NULL)
2333 return NULL;
2334 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002335 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 if (last)
2337 last->next = ssid;
2338 else
2339 config->ssid = ssid;
2340
2341 wpa_config_update_prio_list(config);
2342
2343 return ssid;
2344}
2345
2346
2347/**
2348 * wpa_config_remove_network - Remove a configured network based on id
2349 * @config: Configuration data from wpa_config_read()
2350 * @id: Unique network id to search for
2351 * Returns: 0 on success, or -1 if the network was not found
2352 */
2353int wpa_config_remove_network(struct wpa_config *config, int id)
2354{
2355 struct wpa_ssid *ssid, *prev = NULL;
2356
2357 ssid = config->ssid;
2358 while (ssid) {
2359 if (id == ssid->id)
2360 break;
2361 prev = ssid;
2362 ssid = ssid->next;
2363 }
2364
2365 if (ssid == NULL)
2366 return -1;
2367
2368 if (prev)
2369 prev->next = ssid->next;
2370 else
2371 config->ssid = ssid->next;
2372
2373 wpa_config_update_prio_list(config);
2374 wpa_config_free_ssid(ssid);
2375 return 0;
2376}
2377
2378
2379/**
2380 * wpa_config_set_network_defaults - Set network default values
2381 * @ssid: Pointer to network configuration data
2382 */
2383void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2384{
2385 ssid->proto = DEFAULT_PROTO;
2386 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2387 ssid->group_cipher = DEFAULT_GROUP;
2388 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002389 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390#ifdef IEEE8021X_EAPOL
2391 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2392 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2393 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002394 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002396#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002397 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2398 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2399 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2400 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
2401#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002402#ifdef CONFIG_HT_OVERRIDES
2403 ssid->disable_ht = DEFAULT_DISABLE_HT;
2404 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002405 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002406 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002407 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2408 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2409 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2410#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002411#ifdef CONFIG_VHT_OVERRIDES
2412 ssid->vht_rx_mcs_nss_1 = -1;
2413 ssid->vht_rx_mcs_nss_2 = -1;
2414 ssid->vht_rx_mcs_nss_3 = -1;
2415 ssid->vht_rx_mcs_nss_4 = -1;
2416 ssid->vht_rx_mcs_nss_5 = -1;
2417 ssid->vht_rx_mcs_nss_6 = -1;
2418 ssid->vht_rx_mcs_nss_7 = -1;
2419 ssid->vht_rx_mcs_nss_8 = -1;
2420 ssid->vht_tx_mcs_nss_1 = -1;
2421 ssid->vht_tx_mcs_nss_2 = -1;
2422 ssid->vht_tx_mcs_nss_3 = -1;
2423 ssid->vht_tx_mcs_nss_4 = -1;
2424 ssid->vht_tx_mcs_nss_5 = -1;
2425 ssid->vht_tx_mcs_nss_6 = -1;
2426 ssid->vht_tx_mcs_nss_7 = -1;
2427 ssid->vht_tx_mcs_nss_8 = -1;
2428#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002429 ssid->proactive_key_caching = -1;
2430#ifdef CONFIG_IEEE80211W
2431 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2432#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002433 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434}
2435
2436
2437/**
2438 * wpa_config_set - Set a variable in network configuration
2439 * @ssid: Pointer to network configuration data
2440 * @var: Variable name, e.g., "ssid"
2441 * @value: Variable value
2442 * @line: Line number in configuration file or 0 if not used
2443 * Returns: 0 on success, -1 on failure
2444 *
2445 * This function can be used to set network configuration variables based on
2446 * both the configuration file and management interface input. The value
2447 * parameter must be in the same format as the text-based configuration file is
2448 * using. For example, strings are using double quotation marks.
2449 */
2450int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2451 int line)
2452{
2453 size_t i;
2454 int ret = 0;
2455
2456 if (ssid == NULL || var == NULL || value == NULL)
2457 return -1;
2458
2459 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2460 const struct parse_data *field = &ssid_fields[i];
2461 if (os_strcmp(var, field->name) != 0)
2462 continue;
2463
2464 if (field->parser(field, ssid, line, value)) {
2465 if (line) {
2466 wpa_printf(MSG_ERROR, "Line %d: failed to "
2467 "parse %s '%s'.", line, var, value);
2468 }
2469 ret = -1;
2470 }
2471 break;
2472 }
2473 if (i == NUM_SSID_FIELDS) {
2474 if (line) {
2475 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2476 "'%s'.", line, var);
2477 }
2478 ret = -1;
2479 }
2480
2481 return ret;
2482}
2483
2484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2486 const char *value)
2487{
2488 size_t len;
2489 char *buf;
2490 int ret;
2491
2492 len = os_strlen(value);
2493 buf = os_malloc(len + 3);
2494 if (buf == NULL)
2495 return -1;
2496 buf[0] = '"';
2497 os_memcpy(buf + 1, value, len);
2498 buf[len + 1] = '"';
2499 buf[len + 2] = '\0';
2500 ret = wpa_config_set(ssid, var, buf, 0);
2501 os_free(buf);
2502 return ret;
2503}
2504
2505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506/**
2507 * wpa_config_get_all - Get all options from network configuration
2508 * @ssid: Pointer to network configuration data
2509 * @get_keys: Determines if keys/passwords will be included in returned list
2510 * (if they may be exported)
2511 * Returns: %NULL terminated list of all set keys and their values in the form
2512 * of [key1, val1, key2, val2, ... , NULL]
2513 *
2514 * This function can be used to get list of all configured network properties.
2515 * The caller is responsible for freeing the returned list and all its
2516 * elements.
2517 */
2518char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2519{
2520 const struct parse_data *field;
2521 char *key, *value;
2522 size_t i;
2523 char **props;
2524 int fields_num;
2525
2526 get_keys = get_keys && ssid->export_keys;
2527
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002528 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 if (!props)
2530 return NULL;
2531
2532 fields_num = 0;
2533 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2534 field = &ssid_fields[i];
2535 if (field->key_data && !get_keys)
2536 continue;
2537 value = field->writer(field, ssid);
2538 if (value == NULL)
2539 continue;
2540 if (os_strlen(value) == 0) {
2541 os_free(value);
2542 continue;
2543 }
2544
2545 key = os_strdup(field->name);
2546 if (key == NULL) {
2547 os_free(value);
2548 goto err;
2549 }
2550
2551 props[fields_num * 2] = key;
2552 props[fields_num * 2 + 1] = value;
2553
2554 fields_num++;
2555 }
2556
2557 return props;
2558
2559err:
2560 value = *props;
2561 while (value)
2562 os_free(value++);
2563 os_free(props);
2564 return NULL;
2565}
2566
2567
2568#ifndef NO_CONFIG_WRITE
2569/**
2570 * wpa_config_get - Get a variable in network configuration
2571 * @ssid: Pointer to network configuration data
2572 * @var: Variable name, e.g., "ssid"
2573 * Returns: Value of the variable or %NULL on failure
2574 *
2575 * This function can be used to get network configuration variables. The
2576 * returned value is a copy of the configuration variable in text format, i.e,.
2577 * the same format that the text-based configuration file and wpa_config_set()
2578 * are using for the value. The caller is responsible for freeing the returned
2579 * value.
2580 */
2581char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2582{
2583 size_t i;
2584
2585 if (ssid == NULL || var == NULL)
2586 return NULL;
2587
2588 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2589 const struct parse_data *field = &ssid_fields[i];
2590 if (os_strcmp(var, field->name) == 0)
2591 return field->writer(field, ssid);
2592 }
2593
2594 return NULL;
2595}
2596
2597
2598/**
2599 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2600 * @ssid: Pointer to network configuration data
2601 * @var: Variable name, e.g., "ssid"
2602 * Returns: Value of the variable or %NULL on failure
2603 *
2604 * This function can be used to get network configuration variable like
2605 * wpa_config_get(). The only difference is that this functions does not expose
2606 * key/password material from the configuration. In case a key/password field
2607 * is requested, the returned value is an empty string or %NULL if the variable
2608 * is not set or "*" if the variable is set (regardless of its value). The
2609 * returned value is a copy of the configuration variable in text format, i.e,.
2610 * the same format that the text-based configuration file and wpa_config_set()
2611 * are using for the value. The caller is responsible for freeing the returned
2612 * value.
2613 */
2614char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2615{
2616 size_t i;
2617
2618 if (ssid == NULL || var == NULL)
2619 return NULL;
2620
2621 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2622 const struct parse_data *field = &ssid_fields[i];
2623 if (os_strcmp(var, field->name) == 0) {
2624 char *res = field->writer(field, ssid);
2625 if (field->key_data) {
2626 if (res && res[0]) {
2627 wpa_printf(MSG_DEBUG, "Do not allow "
2628 "key_data field to be "
2629 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002630 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 return os_strdup("*");
2632 }
2633
2634 os_free(res);
2635 return NULL;
2636 }
2637 return res;
2638 }
2639 }
2640
2641 return NULL;
2642}
2643#endif /* NO_CONFIG_WRITE */
2644
2645
2646/**
2647 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2648 * @ssid: Pointer to network configuration data
2649 *
2650 * This function must be called to update WPA PSK when either SSID or the
2651 * passphrase has changed for the network configuration.
2652 */
2653void wpa_config_update_psk(struct wpa_ssid *ssid)
2654{
2655#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002656 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 ssid->psk, PMK_LEN);
2658 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2659 ssid->psk, PMK_LEN);
2660 ssid->psk_set = 1;
2661#endif /* CONFIG_NO_PBKDF2 */
2662}
2663
2664
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002665static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2666 const char *value)
2667{
2668 u8 *proto;
2669 int **port;
2670 int *ports, *nports;
2671 const char *pos;
2672 unsigned int num_ports;
2673
2674 proto = os_realloc_array(cred->req_conn_capab_proto,
2675 cred->num_req_conn_capab + 1, sizeof(u8));
2676 if (proto == NULL)
2677 return -1;
2678 cred->req_conn_capab_proto = proto;
2679
2680 port = os_realloc_array(cred->req_conn_capab_port,
2681 cred->num_req_conn_capab + 1, sizeof(int *));
2682 if (port == NULL)
2683 return -1;
2684 cred->req_conn_capab_port = port;
2685
2686 proto[cred->num_req_conn_capab] = atoi(value);
2687
2688 pos = os_strchr(value, ':');
2689 if (pos == NULL) {
2690 port[cred->num_req_conn_capab] = NULL;
2691 cred->num_req_conn_capab++;
2692 return 0;
2693 }
2694 pos++;
2695
2696 ports = NULL;
2697 num_ports = 0;
2698
2699 while (*pos) {
2700 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2701 if (nports == NULL) {
2702 os_free(ports);
2703 return -1;
2704 }
2705 ports = nports;
2706 ports[num_ports++] = atoi(pos);
2707
2708 pos = os_strchr(pos, ',');
2709 if (pos == NULL)
2710 break;
2711 pos++;
2712 }
2713
2714 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2715 if (nports == NULL) {
2716 os_free(ports);
2717 return -1;
2718 }
2719 ports = nports;
2720 ports[num_ports] = -1;
2721
2722 port[cred->num_req_conn_capab] = ports;
2723 cred->num_req_conn_capab++;
2724 return 0;
2725}
2726
2727
Dmitry Shmidt04949592012-07-19 12:16:46 -07002728int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2729 const char *value, int line)
2730{
2731 char *val;
2732 size_t len;
2733
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002734 if (os_strcmp(var, "temporary") == 0) {
2735 cred->temporary = atoi(value);
2736 return 0;
2737 }
2738
Dmitry Shmidt04949592012-07-19 12:16:46 -07002739 if (os_strcmp(var, "priority") == 0) {
2740 cred->priority = atoi(value);
2741 return 0;
2742 }
2743
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002744 if (os_strcmp(var, "sp_priority") == 0) {
2745 int prio = atoi(value);
2746 if (prio < 0 || prio > 255)
2747 return -1;
2748 cred->sp_priority = prio;
2749 return 0;
2750 }
2751
Dmitry Shmidt04949592012-07-19 12:16:46 -07002752 if (os_strcmp(var, "pcsc") == 0) {
2753 cred->pcsc = atoi(value);
2754 return 0;
2755 }
2756
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757 if (os_strcmp(var, "eap") == 0) {
2758 struct eap_method_type method;
2759 method.method = eap_peer_get_type(value, &method.vendor);
2760 if (method.vendor == EAP_VENDOR_IETF &&
2761 method.method == EAP_TYPE_NONE) {
2762 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2763 "for a credential", line, value);
2764 return -1;
2765 }
2766 os_free(cred->eap_method);
2767 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2768 if (cred->eap_method == NULL)
2769 return -1;
2770 os_memcpy(cred->eap_method, &method, sizeof(method));
2771 return 0;
2772 }
2773
2774 if (os_strcmp(var, "password") == 0 &&
2775 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002776 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002777 cred->password = os_strdup(value);
2778 cred->ext_password = 1;
2779 return 0;
2780 }
2781
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002782 if (os_strcmp(var, "update_identifier") == 0) {
2783 cred->update_identifier = atoi(value);
2784 return 0;
2785 }
2786
2787 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2788 cred->min_dl_bandwidth_home = atoi(value);
2789 return 0;
2790 }
2791
2792 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2793 cred->min_ul_bandwidth_home = atoi(value);
2794 return 0;
2795 }
2796
2797 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2798 cred->min_dl_bandwidth_roaming = atoi(value);
2799 return 0;
2800 }
2801
2802 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2803 cred->min_ul_bandwidth_roaming = atoi(value);
2804 return 0;
2805 }
2806
2807 if (os_strcmp(var, "max_bss_load") == 0) {
2808 cred->max_bss_load = atoi(value);
2809 return 0;
2810 }
2811
2812 if (os_strcmp(var, "req_conn_capab") == 0)
2813 return wpa_config_set_cred_req_conn_capab(cred, value);
2814
2815 if (os_strcmp(var, "ocsp") == 0) {
2816 cred->ocsp = atoi(value);
2817 return 0;
2818 }
2819
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002820 if (os_strcmp(var, "sim_num") == 0) {
2821 cred->sim_num = atoi(value);
2822 return 0;
2823 }
2824
Dmitry Shmidt04949592012-07-19 12:16:46 -07002825 val = wpa_config_parse_string(value, &len);
2826 if (val == NULL) {
2827 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2828 "value '%s'.", line, var, value);
2829 return -1;
2830 }
2831
2832 if (os_strcmp(var, "realm") == 0) {
2833 os_free(cred->realm);
2834 cred->realm = val;
2835 return 0;
2836 }
2837
2838 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002839 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002840 cred->username = val;
2841 return 0;
2842 }
2843
2844 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002845 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002846 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002847 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002848 return 0;
2849 }
2850
2851 if (os_strcmp(var, "ca_cert") == 0) {
2852 os_free(cred->ca_cert);
2853 cred->ca_cert = val;
2854 return 0;
2855 }
2856
2857 if (os_strcmp(var, "client_cert") == 0) {
2858 os_free(cred->client_cert);
2859 cred->client_cert = val;
2860 return 0;
2861 }
2862
2863 if (os_strcmp(var, "private_key") == 0) {
2864 os_free(cred->private_key);
2865 cred->private_key = val;
2866 return 0;
2867 }
2868
2869 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002870 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002871 cred->private_key_passwd = val;
2872 return 0;
2873 }
2874
2875 if (os_strcmp(var, "imsi") == 0) {
2876 os_free(cred->imsi);
2877 cred->imsi = val;
2878 return 0;
2879 }
2880
2881 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002882 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002883 cred->milenage = val;
2884 return 0;
2885 }
2886
Dmitry Shmidt051af732013-10-22 13:52:46 -07002887 if (os_strcmp(var, "domain_suffix_match") == 0) {
2888 os_free(cred->domain_suffix_match);
2889 cred->domain_suffix_match = val;
2890 return 0;
2891 }
2892
Dmitry Shmidt04949592012-07-19 12:16:46 -07002893 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002894 char **new_domain;
2895 new_domain = os_realloc_array(cred->domain,
2896 cred->num_domain + 1,
2897 sizeof(char *));
2898 if (new_domain == NULL) {
2899 os_free(val);
2900 return -1;
2901 }
2902 new_domain[cred->num_domain++] = val;
2903 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002904 return 0;
2905 }
2906
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002907 if (os_strcmp(var, "phase1") == 0) {
2908 os_free(cred->phase1);
2909 cred->phase1 = val;
2910 return 0;
2911 }
2912
2913 if (os_strcmp(var, "phase2") == 0) {
2914 os_free(cred->phase2);
2915 cred->phase2 = val;
2916 return 0;
2917 }
2918
2919 if (os_strcmp(var, "roaming_consortium") == 0) {
2920 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2921 wpa_printf(MSG_ERROR, "Line %d: invalid "
2922 "roaming_consortium length %d (3..15 "
2923 "expected)", line, (int) len);
2924 os_free(val);
2925 return -1;
2926 }
2927 os_memcpy(cred->roaming_consortium, val, len);
2928 cred->roaming_consortium_len = len;
2929 os_free(val);
2930 return 0;
2931 }
2932
Dmitry Shmidt051af732013-10-22 13:52:46 -07002933 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2934 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2935 {
2936 wpa_printf(MSG_ERROR, "Line %d: invalid "
2937 "required_roaming_consortium length %d "
2938 "(3..15 expected)", line, (int) len);
2939 os_free(val);
2940 return -1;
2941 }
2942 os_memcpy(cred->required_roaming_consortium, val, len);
2943 cred->required_roaming_consortium_len = len;
2944 os_free(val);
2945 return 0;
2946 }
2947
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002948 if (os_strcmp(var, "excluded_ssid") == 0) {
2949 struct excluded_ssid *e;
2950
2951 if (len > MAX_SSID_LEN) {
2952 wpa_printf(MSG_ERROR, "Line %d: invalid "
2953 "excluded_ssid length %d", line, (int) len);
2954 os_free(val);
2955 return -1;
2956 }
2957
2958 e = os_realloc_array(cred->excluded_ssid,
2959 cred->num_excluded_ssid + 1,
2960 sizeof(struct excluded_ssid));
2961 if (e == NULL) {
2962 os_free(val);
2963 return -1;
2964 }
2965 cred->excluded_ssid = e;
2966
2967 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2968 os_memcpy(e->ssid, val, len);
2969 e->ssid_len = len;
2970
2971 os_free(val);
2972
2973 return 0;
2974 }
2975
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002976 if (os_strcmp(var, "roaming_partner") == 0) {
2977 struct roaming_partner *p;
2978 char *pos;
2979
2980 p = os_realloc_array(cred->roaming_partner,
2981 cred->num_roaming_partner + 1,
2982 sizeof(struct roaming_partner));
2983 if (p == NULL) {
2984 os_free(val);
2985 return -1;
2986 }
2987 cred->roaming_partner = p;
2988
2989 p = &cred->roaming_partner[cred->num_roaming_partner];
2990
2991 pos = os_strchr(val, ',');
2992 if (pos == NULL) {
2993 os_free(val);
2994 return -1;
2995 }
2996 *pos++ = '\0';
2997 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2998 os_free(val);
2999 return -1;
3000 }
3001 os_memcpy(p->fqdn, val, pos - val);
3002
3003 p->exact_match = atoi(pos);
3004
3005 pos = os_strchr(pos, ',');
3006 if (pos == NULL) {
3007 os_free(val);
3008 return -1;
3009 }
3010 *pos++ = '\0';
3011
3012 p->priority = atoi(pos);
3013
3014 pos = os_strchr(pos, ',');
3015 if (pos == NULL) {
3016 os_free(val);
3017 return -1;
3018 }
3019 *pos++ = '\0';
3020
3021 if (os_strlen(pos) >= sizeof(p->country)) {
3022 os_free(val);
3023 return -1;
3024 }
3025 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3026
3027 cred->num_roaming_partner++;
3028 os_free(val);
3029
3030 return 0;
3031 }
3032
3033 if (os_strcmp(var, "provisioning_sp") == 0) {
3034 os_free(cred->provisioning_sp);
3035 cred->provisioning_sp = val;
3036 return 0;
3037 }
3038
Dmitry Shmidt04949592012-07-19 12:16:46 -07003039 if (line) {
3040 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3041 line, var);
3042 }
3043
3044 os_free(val);
3045
3046 return -1;
3047}
3048
3049
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003050static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003051{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003052 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003053 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003054 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003056 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003057 if (buf == NULL)
3058 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003059 res = os_snprintf(buf, bufsize, "%d", val);
3060 if (os_snprintf_error(bufsize, res)) {
3061 os_free(buf);
3062 buf = NULL;
3063 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003064 return buf;
3065}
3066
3067
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003068static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003069{
3070 if (str == NULL)
3071 return NULL;
3072 return os_strdup(str);
3073}
3074
3075
3076char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3077{
3078 if (os_strcmp(var, "temporary") == 0)
3079 return alloc_int_str(cred->temporary);
3080
3081 if (os_strcmp(var, "priority") == 0)
3082 return alloc_int_str(cred->priority);
3083
3084 if (os_strcmp(var, "sp_priority") == 0)
3085 return alloc_int_str(cred->sp_priority);
3086
3087 if (os_strcmp(var, "pcsc") == 0)
3088 return alloc_int_str(cred->pcsc);
3089
3090 if (os_strcmp(var, "eap") == 0) {
3091 if (!cred->eap_method)
3092 return NULL;
3093 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3094 cred->eap_method[0].method));
3095 }
3096
3097 if (os_strcmp(var, "update_identifier") == 0)
3098 return alloc_int_str(cred->update_identifier);
3099
3100 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3101 return alloc_int_str(cred->min_dl_bandwidth_home);
3102
3103 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3104 return alloc_int_str(cred->min_ul_bandwidth_home);
3105
3106 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3107 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3108
3109 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3110 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3111
3112 if (os_strcmp(var, "max_bss_load") == 0)
3113 return alloc_int_str(cred->max_bss_load);
3114
3115 if (os_strcmp(var, "req_conn_capab") == 0) {
3116 unsigned int i;
3117 char *buf, *end, *pos;
3118 int ret;
3119
3120 if (!cred->num_req_conn_capab)
3121 return NULL;
3122
3123 buf = os_malloc(4000);
3124 if (buf == NULL)
3125 return NULL;
3126 pos = buf;
3127 end = pos + 4000;
3128 for (i = 0; i < cred->num_req_conn_capab; i++) {
3129 int *ports;
3130
3131 ret = os_snprintf(pos, end - pos, "%s%u",
3132 i > 0 ? "\n" : "",
3133 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003134 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003135 return buf;
3136 pos += ret;
3137
3138 ports = cred->req_conn_capab_port[i];
3139 if (ports) {
3140 int j;
3141 for (j = 0; ports[j] != -1; j++) {
3142 ret = os_snprintf(pos, end - pos,
3143 "%s%d",
3144 j > 0 ? "," : ":",
3145 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003146 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003147 return buf;
3148 pos += ret;
3149 }
3150 }
3151 }
3152
3153 return buf;
3154 }
3155
3156 if (os_strcmp(var, "ocsp") == 0)
3157 return alloc_int_str(cred->ocsp);
3158
3159 if (os_strcmp(var, "realm") == 0)
3160 return alloc_strdup(cred->realm);
3161
3162 if (os_strcmp(var, "username") == 0)
3163 return alloc_strdup(cred->username);
3164
3165 if (os_strcmp(var, "password") == 0) {
3166 if (!cred->password)
3167 return NULL;
3168 return alloc_strdup("*");
3169 }
3170
3171 if (os_strcmp(var, "ca_cert") == 0)
3172 return alloc_strdup(cred->ca_cert);
3173
3174 if (os_strcmp(var, "client_cert") == 0)
3175 return alloc_strdup(cred->client_cert);
3176
3177 if (os_strcmp(var, "private_key") == 0)
3178 return alloc_strdup(cred->private_key);
3179
3180 if (os_strcmp(var, "private_key_passwd") == 0) {
3181 if (!cred->private_key_passwd)
3182 return NULL;
3183 return alloc_strdup("*");
3184 }
3185
3186 if (os_strcmp(var, "imsi") == 0)
3187 return alloc_strdup(cred->imsi);
3188
3189 if (os_strcmp(var, "milenage") == 0) {
3190 if (!(cred->milenage))
3191 return NULL;
3192 return alloc_strdup("*");
3193 }
3194
3195 if (os_strcmp(var, "domain_suffix_match") == 0)
3196 return alloc_strdup(cred->domain_suffix_match);
3197
3198 if (os_strcmp(var, "domain") == 0) {
3199 unsigned int i;
3200 char *buf, *end, *pos;
3201 int ret;
3202
3203 if (!cred->num_domain)
3204 return NULL;
3205
3206 buf = os_malloc(4000);
3207 if (buf == NULL)
3208 return NULL;
3209 pos = buf;
3210 end = pos + 4000;
3211
3212 for (i = 0; i < cred->num_domain; i++) {
3213 ret = os_snprintf(pos, end - pos, "%s%s",
3214 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003215 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003216 return buf;
3217 pos += ret;
3218 }
3219
3220 return buf;
3221 }
3222
3223 if (os_strcmp(var, "phase1") == 0)
3224 return alloc_strdup(cred->phase1);
3225
3226 if (os_strcmp(var, "phase2") == 0)
3227 return alloc_strdup(cred->phase2);
3228
3229 if (os_strcmp(var, "roaming_consortium") == 0) {
3230 size_t buflen;
3231 char *buf;
3232
3233 if (!cred->roaming_consortium_len)
3234 return NULL;
3235 buflen = cred->roaming_consortium_len * 2 + 1;
3236 buf = os_malloc(buflen);
3237 if (buf == NULL)
3238 return NULL;
3239 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3240 cred->roaming_consortium_len);
3241 return buf;
3242 }
3243
3244 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3245 size_t buflen;
3246 char *buf;
3247
3248 if (!cred->required_roaming_consortium_len)
3249 return NULL;
3250 buflen = cred->required_roaming_consortium_len * 2 + 1;
3251 buf = os_malloc(buflen);
3252 if (buf == NULL)
3253 return NULL;
3254 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3255 cred->required_roaming_consortium_len);
3256 return buf;
3257 }
3258
3259 if (os_strcmp(var, "excluded_ssid") == 0) {
3260 unsigned int i;
3261 char *buf, *end, *pos;
3262
3263 if (!cred->num_excluded_ssid)
3264 return NULL;
3265
3266 buf = os_malloc(4000);
3267 if (buf == NULL)
3268 return NULL;
3269 pos = buf;
3270 end = pos + 4000;
3271
3272 for (i = 0; i < cred->num_excluded_ssid; i++) {
3273 struct excluded_ssid *e;
3274 int ret;
3275
3276 e = &cred->excluded_ssid[i];
3277 ret = os_snprintf(pos, end - pos, "%s%s",
3278 i > 0 ? "\n" : "",
3279 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003280 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003281 return buf;
3282 pos += ret;
3283 }
3284
3285 return buf;
3286 }
3287
3288 if (os_strcmp(var, "roaming_partner") == 0) {
3289 unsigned int i;
3290 char *buf, *end, *pos;
3291
3292 if (!cred->num_roaming_partner)
3293 return NULL;
3294
3295 buf = os_malloc(4000);
3296 if (buf == NULL)
3297 return NULL;
3298 pos = buf;
3299 end = pos + 4000;
3300
3301 for (i = 0; i < cred->num_roaming_partner; i++) {
3302 struct roaming_partner *p;
3303 int ret;
3304
3305 p = &cred->roaming_partner[i];
3306 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3307 i > 0 ? "\n" : "",
3308 p->fqdn, p->exact_match, p->priority,
3309 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003310 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003311 return buf;
3312 pos += ret;
3313 }
3314
3315 return buf;
3316 }
3317
3318 if (os_strcmp(var, "provisioning_sp") == 0)
3319 return alloc_strdup(cred->provisioning_sp);
3320
3321 return NULL;
3322}
3323
3324
Dmitry Shmidt04949592012-07-19 12:16:46 -07003325struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3326{
3327 struct wpa_cred *cred;
3328
3329 cred = config->cred;
3330 while (cred) {
3331 if (id == cred->id)
3332 break;
3333 cred = cred->next;
3334 }
3335
3336 return cred;
3337}
3338
3339
3340struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3341{
3342 int id;
3343 struct wpa_cred *cred, *last = NULL;
3344
3345 id = -1;
3346 cred = config->cred;
3347 while (cred) {
3348 if (cred->id > id)
3349 id = cred->id;
3350 last = cred;
3351 cred = cred->next;
3352 }
3353 id++;
3354
3355 cred = os_zalloc(sizeof(*cred));
3356 if (cred == NULL)
3357 return NULL;
3358 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003359 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003360 if (last)
3361 last->next = cred;
3362 else
3363 config->cred = cred;
3364
3365 return cred;
3366}
3367
3368
3369int wpa_config_remove_cred(struct wpa_config *config, int id)
3370{
3371 struct wpa_cred *cred, *prev = NULL;
3372
3373 cred = config->cred;
3374 while (cred) {
3375 if (id == cred->id)
3376 break;
3377 prev = cred;
3378 cred = cred->next;
3379 }
3380
3381 if (cred == NULL)
3382 return -1;
3383
3384 if (prev)
3385 prev->next = cred->next;
3386 else
3387 config->cred = cred->next;
3388
3389 wpa_config_free_cred(cred);
3390 return 0;
3391}
3392
3393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394#ifndef CONFIG_NO_CONFIG_BLOBS
3395/**
3396 * wpa_config_get_blob - Get a named configuration blob
3397 * @config: Configuration data from wpa_config_read()
3398 * @name: Name of the blob
3399 * Returns: Pointer to blob data or %NULL if not found
3400 */
3401const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3402 const char *name)
3403{
3404 struct wpa_config_blob *blob = config->blobs;
3405
3406 while (blob) {
3407 if (os_strcmp(blob->name, name) == 0)
3408 return blob;
3409 blob = blob->next;
3410 }
3411 return NULL;
3412}
3413
3414
3415/**
3416 * wpa_config_set_blob - Set or add a named configuration blob
3417 * @config: Configuration data from wpa_config_read()
3418 * @blob: New value for the blob
3419 *
3420 * Adds a new configuration blob or replaces the current value of an existing
3421 * blob.
3422 */
3423void wpa_config_set_blob(struct wpa_config *config,
3424 struct wpa_config_blob *blob)
3425{
3426 wpa_config_remove_blob(config, blob->name);
3427 blob->next = config->blobs;
3428 config->blobs = blob;
3429}
3430
3431
3432/**
3433 * wpa_config_free_blob - Free blob data
3434 * @blob: Pointer to blob to be freed
3435 */
3436void wpa_config_free_blob(struct wpa_config_blob *blob)
3437{
3438 if (blob) {
3439 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003440 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 os_free(blob);
3442 }
3443}
3444
3445
3446/**
3447 * wpa_config_remove_blob - Remove a named configuration blob
3448 * @config: Configuration data from wpa_config_read()
3449 * @name: Name of the blob to remove
3450 * Returns: 0 if blob was removed or -1 if blob was not found
3451 */
3452int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3453{
3454 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3455
3456 while (pos) {
3457 if (os_strcmp(pos->name, name) == 0) {
3458 if (prev)
3459 prev->next = pos->next;
3460 else
3461 config->blobs = pos->next;
3462 wpa_config_free_blob(pos);
3463 return 0;
3464 }
3465 prev = pos;
3466 pos = pos->next;
3467 }
3468
3469 return -1;
3470}
3471#endif /* CONFIG_NO_CONFIG_BLOBS */
3472
3473
3474/**
3475 * wpa_config_alloc_empty - Allocate an empty configuration
3476 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3477 * socket
3478 * @driver_param: Driver parameters
3479 * Returns: Pointer to allocated configuration data or %NULL on failure
3480 */
3481struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3482 const char *driver_param)
3483{
3484 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003485 const int aCWmin = 4, aCWmax = 10;
3486 const struct hostapd_wmm_ac_params ac_bk =
3487 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3488 const struct hostapd_wmm_ac_params ac_be =
3489 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3490 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3491 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3492 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3493 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494
3495 config = os_zalloc(sizeof(*config));
3496 if (config == NULL)
3497 return NULL;
3498 config->eapol_version = DEFAULT_EAPOL_VERSION;
3499 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003500 config->user_mpm = DEFAULT_USER_MPM;
3501 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003502 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503 config->fast_reauth = DEFAULT_FAST_REAUTH;
3504 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3505 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003506 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003507 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003508 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3510 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3511 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3512 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003514 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003515 config->wmm_ac_params[0] = ac_be;
3516 config->wmm_ac_params[1] = ac_bk;
3517 config->wmm_ac_params[2] = ac_vi;
3518 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003519 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003520 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003521 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003522 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
3524 if (ctrl_interface)
3525 config->ctrl_interface = os_strdup(ctrl_interface);
3526 if (driver_param)
3527 config->driver_param = os_strdup(driver_param);
3528
3529 return config;
3530}
3531
3532
3533#ifndef CONFIG_NO_STDOUT_DEBUG
3534/**
3535 * wpa_config_debug_dump_networks - Debug dump of configured networks
3536 * @config: Configuration data from wpa_config_read()
3537 */
3538void wpa_config_debug_dump_networks(struct wpa_config *config)
3539{
3540 int prio;
3541 struct wpa_ssid *ssid;
3542
3543 for (prio = 0; prio < config->num_prio; prio++) {
3544 ssid = config->pssid[prio];
3545 wpa_printf(MSG_DEBUG, "Priority group %d",
3546 ssid->priority);
3547 while (ssid) {
3548 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3549 ssid->id,
3550 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3551 ssid = ssid->pnext;
3552 }
3553 }
3554}
3555#endif /* CONFIG_NO_STDOUT_DEBUG */
3556
3557
3558struct global_parse_data {
3559 char *name;
3560 int (*parser)(const struct global_parse_data *data,
3561 struct wpa_config *config, int line, const char *value);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003562 int (*get)(const char *name, struct wpa_config *config, long offset,
3563 char *buf, size_t buflen, int pretty_print);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564 void *param1, *param2, *param3;
3565 unsigned int changed_flag;
3566};
3567
3568
3569static int wpa_global_config_parse_int(const struct global_parse_data *data,
3570 struct wpa_config *config, int line,
3571 const char *pos)
3572{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003573 int val, *dst;
3574 char *end;
3575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003577 val = strtol(pos, &end, 0);
3578 if (*end) {
3579 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3580 line, pos);
3581 return -1;
3582 }
3583 *dst = val;
3584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3586
3587 if (data->param2 && *dst < (long) data->param2) {
3588 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3589 "min_value=%ld)", line, data->name, *dst,
3590 (long) data->param2);
3591 *dst = (long) data->param2;
3592 return -1;
3593 }
3594
3595 if (data->param3 && *dst > (long) data->param3) {
3596 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3597 "max_value=%ld)", line, data->name, *dst,
3598 (long) data->param3);
3599 *dst = (long) data->param3;
3600 return -1;
3601 }
3602
3603 return 0;
3604}
3605
3606
3607static int wpa_global_config_parse_str(const struct global_parse_data *data,
3608 struct wpa_config *config, int line,
3609 const char *pos)
3610{
3611 size_t len;
3612 char **dst, *tmp;
3613
3614 len = os_strlen(pos);
3615 if (data->param2 && len < (size_t) data->param2) {
3616 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3617 "min_len=%ld)", line, data->name,
3618 (unsigned long) len, (long) data->param2);
3619 return -1;
3620 }
3621
3622 if (data->param3 && len > (size_t) data->param3) {
3623 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3624 "max_len=%ld)", line, data->name,
3625 (unsigned long) len, (long) data->param3);
3626 return -1;
3627 }
3628
3629 tmp = os_strdup(pos);
3630 if (tmp == NULL)
3631 return -1;
3632
3633 dst = (char **) (((u8 *) config) + (long) data->param1);
3634 os_free(*dst);
3635 *dst = tmp;
3636 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3637
3638 return 0;
3639}
3640
3641
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003642static int wpa_config_process_bgscan(const struct global_parse_data *data,
3643 struct wpa_config *config, int line,
3644 const char *pos)
3645{
3646 size_t len;
3647 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003648 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003649
3650 tmp = wpa_config_parse_string(pos, &len);
3651 if (tmp == NULL) {
3652 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3653 line, data->name);
3654 return -1;
3655 }
3656
Dmitry Shmidt97672262014-02-03 13:02:54 -08003657 res = wpa_global_config_parse_str(data, config, line, tmp);
3658 os_free(tmp);
3659 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003660}
3661
3662
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3664 struct wpa_config *config, int line,
3665 const char *pos)
3666{
3667 size_t len;
3668 struct wpabuf **dst, *tmp;
3669
3670 len = os_strlen(pos);
3671 if (len & 0x01)
3672 return -1;
3673
3674 tmp = wpabuf_alloc(len / 2);
3675 if (tmp == NULL)
3676 return -1;
3677
3678 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3679 wpabuf_free(tmp);
3680 return -1;
3681 }
3682
3683 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3684 wpabuf_free(*dst);
3685 *dst = tmp;
3686 wpa_printf(MSG_DEBUG, "%s", data->name);
3687
3688 return 0;
3689}
3690
3691
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003692static int wpa_config_process_freq_list(const struct global_parse_data *data,
3693 struct wpa_config *config, int line,
3694 const char *value)
3695{
3696 int *freqs;
3697
3698 freqs = wpa_config_parse_int_array(value);
3699 if (freqs == NULL)
3700 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003701 if (freqs[0] == 0) {
3702 os_free(freqs);
3703 freqs = NULL;
3704 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003705 os_free(config->freq_list);
3706 config->freq_list = freqs;
3707 return 0;
3708}
3709
3710
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003711#ifdef CONFIG_P2P
3712static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3713 struct wpa_config *config, int line,
3714 const char *pos)
3715{
3716 u32 *dst;
3717 struct hostapd_ip_addr addr;
3718
3719 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3720 return -1;
3721 if (addr.af != AF_INET)
3722 return -1;
3723
3724 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3725 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3726 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3727 WPA_GET_BE32((u8 *) dst));
3728
3729 return 0;
3730}
3731#endif /* CONFIG_P2P */
3732
3733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734static int wpa_config_process_country(const struct global_parse_data *data,
3735 struct wpa_config *config, int line,
3736 const char *pos)
3737{
3738 if (!pos[0] || !pos[1]) {
3739 wpa_printf(MSG_DEBUG, "Invalid country set");
3740 return -1;
3741 }
3742 config->country[0] = pos[0];
3743 config->country[1] = pos[1];
3744 wpa_printf(MSG_DEBUG, "country='%c%c'",
3745 config->country[0], config->country[1]);
3746 return 0;
3747}
3748
3749
3750static int wpa_config_process_load_dynamic_eap(
3751 const struct global_parse_data *data, struct wpa_config *config,
3752 int line, const char *so)
3753{
3754 int ret;
3755 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3756 ret = eap_peer_method_load(so);
3757 if (ret == -2) {
3758 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3759 "reloading.");
3760 } else if (ret) {
3761 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3762 "method '%s'.", line, so);
3763 return -1;
3764 }
3765
3766 return 0;
3767}
3768
3769
3770#ifdef CONFIG_WPS
3771
3772static int wpa_config_process_uuid(const struct global_parse_data *data,
3773 struct wpa_config *config, int line,
3774 const char *pos)
3775{
3776 char buf[40];
3777 if (uuid_str2bin(pos, config->uuid)) {
3778 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3779 return -1;
3780 }
3781 uuid_bin2str(config->uuid, buf, sizeof(buf));
3782 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3783 return 0;
3784}
3785
3786
3787static int wpa_config_process_device_type(
3788 const struct global_parse_data *data,
3789 struct wpa_config *config, int line, const char *pos)
3790{
3791 return wps_dev_type_str2bin(pos, config->device_type);
3792}
3793
3794
3795static int wpa_config_process_os_version(const struct global_parse_data *data,
3796 struct wpa_config *config, int line,
3797 const char *pos)
3798{
3799 if (hexstr2bin(pos, config->os_version, 4)) {
3800 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3801 return -1;
3802 }
3803 wpa_printf(MSG_DEBUG, "os_version=%08x",
3804 WPA_GET_BE32(config->os_version));
3805 return 0;
3806}
3807
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808
3809static int wpa_config_process_wps_vendor_ext_m1(
3810 const struct global_parse_data *data,
3811 struct wpa_config *config, int line, const char *pos)
3812{
3813 struct wpabuf *tmp;
3814 int len = os_strlen(pos) / 2;
3815 u8 *p;
3816
3817 if (!len) {
3818 wpa_printf(MSG_ERROR, "Line %d: "
3819 "invalid wps_vendor_ext_m1", line);
3820 return -1;
3821 }
3822
3823 tmp = wpabuf_alloc(len);
3824 if (tmp) {
3825 p = wpabuf_put(tmp, len);
3826
3827 if (hexstr2bin(pos, p, len)) {
3828 wpa_printf(MSG_ERROR, "Line %d: "
3829 "invalid wps_vendor_ext_m1", line);
3830 wpabuf_free(tmp);
3831 return -1;
3832 }
3833
3834 wpabuf_free(config->wps_vendor_ext_m1);
3835 config->wps_vendor_ext_m1 = tmp;
3836 } else {
3837 wpa_printf(MSG_ERROR, "Can not allocate "
3838 "memory for wps_vendor_ext_m1");
3839 return -1;
3840 }
3841
3842 return 0;
3843}
3844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845#endif /* CONFIG_WPS */
3846
3847#ifdef CONFIG_P2P
3848static int wpa_config_process_sec_device_type(
3849 const struct global_parse_data *data,
3850 struct wpa_config *config, int line, const char *pos)
3851{
3852 int idx;
3853
3854 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3855 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3856 "items", line);
3857 return -1;
3858 }
3859
3860 idx = config->num_sec_device_types;
3861
3862 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3863 return -1;
3864
3865 config->num_sec_device_types++;
3866 return 0;
3867}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003868
3869
3870static int wpa_config_process_p2p_pref_chan(
3871 const struct global_parse_data *data,
3872 struct wpa_config *config, int line, const char *pos)
3873{
3874 struct p2p_channel *pref = NULL, *n;
3875 unsigned int num = 0;
3876 const char *pos2;
3877 u8 op_class, chan;
3878
3879 /* format: class:chan,class:chan,... */
3880
3881 while (*pos) {
3882 op_class = atoi(pos);
3883 pos2 = os_strchr(pos, ':');
3884 if (pos2 == NULL)
3885 goto fail;
3886 pos2++;
3887 chan = atoi(pos2);
3888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003889 n = os_realloc_array(pref, num + 1,
3890 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003891 if (n == NULL)
3892 goto fail;
3893 pref = n;
3894 pref[num].op_class = op_class;
3895 pref[num].chan = chan;
3896 num++;
3897
3898 pos = os_strchr(pos2, ',');
3899 if (pos == NULL)
3900 break;
3901 pos++;
3902 }
3903
3904 os_free(config->p2p_pref_chan);
3905 config->p2p_pref_chan = pref;
3906 config->num_p2p_pref_chan = num;
3907 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3908 (u8 *) config->p2p_pref_chan,
3909 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3910
3911 return 0;
3912
3913fail:
3914 os_free(pref);
3915 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3916 return -1;
3917}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003918
3919
3920static int wpa_config_process_p2p_no_go_freq(
3921 const struct global_parse_data *data,
3922 struct wpa_config *config, int line, const char *pos)
3923{
3924 int ret;
3925
3926 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3927 if (ret < 0) {
3928 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3929 return -1;
3930 }
3931
3932 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3933 config->p2p_no_go_freq.num);
3934
3935 return 0;
3936}
3937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938#endif /* CONFIG_P2P */
3939
3940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003941static int wpa_config_process_hessid(
3942 const struct global_parse_data *data,
3943 struct wpa_config *config, int line, const char *pos)
3944{
3945 if (hwaddr_aton2(pos, config->hessid) < 0) {
3946 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3947 line, pos);
3948 return -1;
3949 }
3950
3951 return 0;
3952}
3953
3954
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003955static int wpa_config_process_sae_groups(
3956 const struct global_parse_data *data,
3957 struct wpa_config *config, int line, const char *pos)
3958{
3959 int *groups = wpa_config_parse_int_array(pos);
3960 if (groups == NULL) {
3961 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3962 line, pos);
3963 return -1;
3964 }
3965
3966 os_free(config->sae_groups);
3967 config->sae_groups = groups;
3968
3969 return 0;
3970}
3971
3972
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003973static int wpa_config_process_ap_vendor_elements(
3974 const struct global_parse_data *data,
3975 struct wpa_config *config, int line, const char *pos)
3976{
3977 struct wpabuf *tmp;
3978 int len = os_strlen(pos) / 2;
3979 u8 *p;
3980
3981 if (!len) {
3982 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3983 line);
3984 return -1;
3985 }
3986
3987 tmp = wpabuf_alloc(len);
3988 if (tmp) {
3989 p = wpabuf_put(tmp, len);
3990
3991 if (hexstr2bin(pos, p, len)) {
3992 wpa_printf(MSG_ERROR, "Line %d: invalid "
3993 "ap_vendor_elements", line);
3994 wpabuf_free(tmp);
3995 return -1;
3996 }
3997
3998 wpabuf_free(config->ap_vendor_elements);
3999 config->ap_vendor_elements = tmp;
4000 } else {
4001 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4002 "ap_vendor_elements");
4003 return -1;
4004 }
4005
4006 return 0;
4007}
4008
4009
Dmitry Shmidt56052862013-10-04 10:23:25 -07004010#ifdef CONFIG_CTRL_IFACE
4011static int wpa_config_process_no_ctrl_interface(
4012 const struct global_parse_data *data,
4013 struct wpa_config *config, int line, const char *pos)
4014{
4015 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4016 os_free(config->ctrl_interface);
4017 config->ctrl_interface = NULL;
4018 return 0;
4019}
4020#endif /* CONFIG_CTRL_IFACE */
4021
4022
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004023static int wpa_config_get_int(const char *name, struct wpa_config *config,
4024 long offset, char *buf, size_t buflen,
4025 int pretty_print)
4026{
4027 int *val = (int *) (((u8 *) config) + (long) offset);
4028
4029 if (pretty_print)
4030 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4031 return os_snprintf(buf, buflen, "%d", *val);
4032}
4033
4034
4035static int wpa_config_get_str(const char *name, struct wpa_config *config,
4036 long offset, char *buf, size_t buflen,
4037 int pretty_print)
4038{
4039 char **val = (char **) (((u8 *) config) + (long) offset);
4040 int res;
4041
4042 if (pretty_print)
4043 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4044 *val ? *val : "null");
4045 else if (!*val)
4046 return -1;
4047 else
4048 res = os_snprintf(buf, buflen, "%s", *val);
4049 if (os_snprintf_error(buflen, res))
4050 res = -1;
4051
4052 return res;
4053}
4054
4055
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004056#ifdef CONFIG_P2P
4057static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4058 long offset, char *buf, size_t buflen,
4059 int pretty_print)
4060{
4061 void *val = ((u8 *) config) + (long) offset;
4062 int res;
4063 char addr[INET_ADDRSTRLEN];
4064
4065 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4066 return -1;
4067
4068 if (pretty_print)
4069 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4070 else
4071 res = os_snprintf(buf, buflen, "%s", addr);
4072
4073 if (os_snprintf_error(buflen, res))
4074 res = -1;
4075
4076 return res;
4077}
4078#endif /* CONFIG_P2P */
4079
4080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081#ifdef OFFSET
4082#undef OFFSET
4083#endif /* OFFSET */
4084/* OFFSET: Get offset of a variable within the wpa_config structure */
4085#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4086
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004087#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4088#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4089#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090#define INT(f) _INT(f), NULL, NULL
4091#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004092#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093#define STR(f) _STR(f), NULL, NULL
4094#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004095#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07004096#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4097 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098
4099static const struct global_parse_data global_fields[] = {
4100#ifdef CONFIG_CTRL_IFACE
4101 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004102 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 { STR(ctrl_interface_group), 0 } /* deprecated */,
4104#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004105#ifdef CONFIG_MACSEC
4106 { INT_RANGE(eapol_version, 1, 3), 0 },
4107#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004109#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004111 { FUNC(bgscan), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004112#ifdef CONFIG_MESH
4113 { INT(user_mpm), 0 },
4114 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004115 { INT(mesh_max_inactivity), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004116#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004117 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 { INT(fast_reauth), 0 },
4119 { STR(opensc_engine_path), 0 },
4120 { STR(pkcs11_engine_path), 0 },
4121 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004122 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004123 { STR(pcsc_reader), 0 },
4124 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07004125 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 { STR(driver_param), 0 },
4127 { INT(dot11RSNAConfigPMKLifetime), 0 },
4128 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4129 { INT(dot11RSNAConfigSATimeout), 0 },
4130#ifndef CONFIG_NO_CONFIG_WRITE
4131 { INT(update_config), 0 },
4132#endif /* CONFIG_NO_CONFIG_WRITE */
4133 { FUNC_NO_VAR(load_dynamic_eap), 0 },
4134#ifdef CONFIG_WPS
4135 { FUNC(uuid), CFG_CHANGED_UUID },
4136 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
4137 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4138 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4139 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4140 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
4141 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
4142 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4143 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4144 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004145 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146#endif /* CONFIG_WPS */
4147#ifdef CONFIG_P2P
4148 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
4149 { INT(p2p_listen_reg_class), 0 },
4150 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07004151 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4152 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4154 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4155 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
4156 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
4157 { INT(p2p_group_idle), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004158 { INT_RANGE(p2p_passphrase_len, 8, 63),
4159 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004160 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004161 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
4162 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004163 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004164 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004165 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004166 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004167 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004168 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004169 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004170 { IPV4(ip_addr_go), 0 },
4171 { IPV4(ip_addr_mask), 0 },
4172 { IPV4(ip_addr_start), 0 },
4173 { IPV4(ip_addr_end), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174#endif /* CONFIG_P2P */
4175 { FUNC(country), CFG_CHANGED_COUNTRY },
4176 { INT(bss_max_count), 0 },
4177 { INT(bss_expiration_age), 0 },
4178 { INT(bss_expiration_scan_count), 0 },
4179 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004180 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004182 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183#ifdef CONFIG_HS20
4184 { INT_RANGE(hs20, 0, 1), 0 },
4185#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186 { INT_RANGE(interworking, 0, 1), 0 },
4187 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07004188 { INT_RANGE(access_network_type, 0, 15), 0 },
4189 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
4190 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004191 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4192 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4193 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4194 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4195 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004196 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4197 { INT(p2p_go_max_inactivity), 0 },
4198 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004199 { INT(okc), 0 },
4200 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004201 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004202 { INT(dtim_period), 0 },
4203 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004204 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004205 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004206 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004207 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004208 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004209 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004210 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004211 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004212 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004213 { INT(mac_addr), 0 },
4214 { INT(rand_addr_lifetime), 0 },
4215 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004216 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004217 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004218 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219};
4220
4221#undef FUNC
4222#undef _INT
4223#undef INT
4224#undef INT_RANGE
4225#undef _STR
4226#undef STR
4227#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07004228#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004229#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004230#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231
4232
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004233int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4234{
4235 int result = 0;
4236 size_t i;
4237
4238 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4239 const struct global_parse_data *field = &global_fields[i];
4240 int tmp;
4241
4242 if (!field->get)
4243 continue;
4244
4245 tmp = field->get(field->name, config, (long) field->param1,
4246 buf, buflen, 1);
4247 if (tmp < 0)
4248 return -1;
4249 buf += tmp;
4250 buflen -= tmp;
4251 result += tmp;
4252 }
4253 return result;
4254}
4255
4256
4257int wpa_config_get_value(const char *name, struct wpa_config *config,
4258 char *buf, size_t buflen)
4259{
4260 size_t i;
4261
4262 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4263 const struct global_parse_data *field = &global_fields[i];
4264
4265 if (os_strcmp(name, field->name) != 0)
4266 continue;
4267 if (!field->get)
4268 break;
4269 return field->get(name, config, (long) field->param1,
4270 buf, buflen, 0);
4271 }
4272
4273 return -1;
4274}
4275
4276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4278{
4279 size_t i;
4280 int ret = 0;
4281
4282 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4283 const struct global_parse_data *field = &global_fields[i];
4284 size_t flen = os_strlen(field->name);
4285 if (os_strncmp(pos, field->name, flen) != 0 ||
4286 pos[flen] != '=')
4287 continue;
4288
4289 if (field->parser(field, config, line, pos + flen + 1)) {
4290 wpa_printf(MSG_ERROR, "Line %d: failed to "
4291 "parse '%s'.", line, pos);
4292 ret = -1;
4293 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004294 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4295 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296 config->changed_parameters |= field->changed_flag;
4297 break;
4298 }
4299 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004300#ifdef CONFIG_AP
4301 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4302 char *tmp = os_strchr(pos, '=');
4303 if (tmp == NULL) {
4304 if (line < 0)
4305 return -1;
4306 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4307 "'%s'", line, pos);
4308 return -1;
4309 }
4310 *tmp++ = '\0';
4311 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4312 tmp)) {
4313 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4314 "AC item", line);
4315 return -1;
4316 }
4317 }
4318#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004319 if (line < 0)
4320 return -1;
4321 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4322 line, pos);
4323 ret = -1;
4324 }
4325
4326 return ret;
4327}