blob: 467f586f21b9adf54333841830b1c931fbf0f745 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2003-2012, 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);
228 if (res < 0 || res >= 20) {
229 os_free(value);
230 return NULL;
231 }
232 value[20 - 1] = '\0';
233 return value;
234}
235#endif /* NO_CONFIG_WRITE */
236
237
238static int wpa_config_parse_bssid(const struct parse_data *data,
239 struct wpa_ssid *ssid, int line,
240 const char *value)
241{
242 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
243 os_strcmp(value, "any") == 0) {
244 ssid->bssid_set = 0;
245 wpa_printf(MSG_MSGDUMP, "BSSID any");
246 return 0;
247 }
248 if (hwaddr_aton(value, ssid->bssid)) {
249 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
250 line, value);
251 return -1;
252 }
253 ssid->bssid_set = 1;
254 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
255 return 0;
256}
257
258
259#ifndef NO_CONFIG_WRITE
260static char * wpa_config_write_bssid(const struct parse_data *data,
261 struct wpa_ssid *ssid)
262{
263 char *value;
264 int res;
265
266 if (!ssid->bssid_set)
267 return NULL;
268
269 value = os_malloc(20);
270 if (value == NULL)
271 return NULL;
272 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
273 if (res < 0 || res >= 20) {
274 os_free(value);
275 return NULL;
276 }
277 value[20 - 1] = '\0';
278 return value;
279}
280#endif /* NO_CONFIG_WRITE */
281
282
283static int wpa_config_parse_psk(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
285 const char *value)
286{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287#ifdef CONFIG_EXT_PASSWORD
288 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700289 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700290 ssid->passphrase = NULL;
291 ssid->psk_set = 0;
292 os_free(ssid->ext_psk);
293 ssid->ext_psk = os_strdup(value + 4);
294 if (ssid->ext_psk == NULL)
295 return -1;
296 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
297 ssid->ext_psk);
298 return 0;
299 }
300#endif /* CONFIG_EXT_PASSWORD */
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 if (*value == '"') {
303#ifndef CONFIG_NO_PBKDF2
304 const char *pos;
305 size_t len;
306
307 value++;
308 pos = os_strrchr(value, '"');
309 if (pos)
310 len = pos - value;
311 else
312 len = os_strlen(value);
313 if (len < 8 || len > 63) {
314 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
315 "length %lu (expected: 8..63) '%s'.",
316 line, (unsigned long) len, value);
317 return -1;
318 }
319 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
320 (u8 *) value, len);
321 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
322 os_memcmp(ssid->passphrase, value, len) == 0)
323 return 0;
324 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700325 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700326 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 if (ssid->passphrase == NULL)
328 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 return 0;
330#else /* CONFIG_NO_PBKDF2 */
331 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
332 "supported.", line);
333 return -1;
334#endif /* CONFIG_NO_PBKDF2 */
335 }
336
337 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
338 value[PMK_LEN * 2] != '\0') {
339 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
340 line, value);
341 return -1;
342 }
343
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700344 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 ssid->passphrase = NULL;
346
347 ssid->psk_set = 1;
348 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
349 return 0;
350}
351
352
353#ifndef NO_CONFIG_WRITE
354static char * wpa_config_write_psk(const struct parse_data *data,
355 struct wpa_ssid *ssid)
356{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700357#ifdef CONFIG_EXT_PASSWORD
358 if (ssid->ext_psk) {
359 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
360 char *buf = os_malloc(len);
361 if (buf == NULL)
362 return NULL;
363 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
364 return buf;
365 }
366#endif /* CONFIG_EXT_PASSWORD */
367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 if (ssid->passphrase)
369 return wpa_config_write_string_ascii(
370 (const u8 *) ssid->passphrase,
371 os_strlen(ssid->passphrase));
372
373 if (ssid->psk_set)
374 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
375
376 return NULL;
377}
378#endif /* NO_CONFIG_WRITE */
379
380
381static int wpa_config_parse_proto(const struct parse_data *data,
382 struct wpa_ssid *ssid, int line,
383 const char *value)
384{
385 int val = 0, last, errors = 0;
386 char *start, *end, *buf;
387
388 buf = os_strdup(value);
389 if (buf == NULL)
390 return -1;
391 start = buf;
392
393 while (*start != '\0') {
394 while (*start == ' ' || *start == '\t')
395 start++;
396 if (*start == '\0')
397 break;
398 end = start;
399 while (*end != ' ' && *end != '\t' && *end != '\0')
400 end++;
401 last = *end == '\0';
402 *end = '\0';
403 if (os_strcmp(start, "WPA") == 0)
404 val |= WPA_PROTO_WPA;
405 else if (os_strcmp(start, "RSN") == 0 ||
406 os_strcmp(start, "WPA2") == 0)
407 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800408 else if (os_strcmp(start, "OSEN") == 0)
409 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 else {
411 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
412 line, start);
413 errors++;
414 }
415
416 if (last)
417 break;
418 start = end + 1;
419 }
420 os_free(buf);
421
422 if (val == 0) {
423 wpa_printf(MSG_ERROR,
424 "Line %d: no proto values configured.", line);
425 errors++;
426 }
427
428 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
429 ssid->proto = val;
430 return errors ? -1 : 0;
431}
432
433
434#ifndef NO_CONFIG_WRITE
435static char * wpa_config_write_proto(const struct parse_data *data,
436 struct wpa_ssid *ssid)
437{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700438 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 char *buf, *pos, *end;
440
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800441 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 if (buf == NULL)
443 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800444 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445
446 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700447 ret = os_snprintf(pos, end - pos, "%sWPA",
448 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 if (ret < 0 || ret >= end - pos)
450 return buf;
451 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 }
453
454 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700455 ret = os_snprintf(pos, end - pos, "%sRSN",
456 pos == buf ? "" : " ");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 if (ret < 0 || ret >= end - pos)
458 return buf;
459 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 }
461
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800462 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700463 ret = os_snprintf(pos, end - pos, "%sOSEN",
464 pos == buf ? "" : " ");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800465 if (ret < 0 || ret >= end - pos)
466 return buf;
467 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700468 }
469
470 if (pos == buf) {
471 os_free(buf);
472 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800473 }
474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 return buf;
476}
477#endif /* NO_CONFIG_WRITE */
478
479
480static int wpa_config_parse_key_mgmt(const struct parse_data *data,
481 struct wpa_ssid *ssid, int line,
482 const char *value)
483{
484 int val = 0, last, errors = 0;
485 char *start, *end, *buf;
486
487 buf = os_strdup(value);
488 if (buf == NULL)
489 return -1;
490 start = buf;
491
492 while (*start != '\0') {
493 while (*start == ' ' || *start == '\t')
494 start++;
495 if (*start == '\0')
496 break;
497 end = start;
498 while (*end != ' ' && *end != '\t' && *end != '\0')
499 end++;
500 last = *end == '\0';
501 *end = '\0';
502 if (os_strcmp(start, "WPA-PSK") == 0)
503 val |= WPA_KEY_MGMT_PSK;
504 else if (os_strcmp(start, "WPA-EAP") == 0)
505 val |= WPA_KEY_MGMT_IEEE8021X;
506 else if (os_strcmp(start, "IEEE8021X") == 0)
507 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
508 else if (os_strcmp(start, "NONE") == 0)
509 val |= WPA_KEY_MGMT_NONE;
510 else if (os_strcmp(start, "WPA-NONE") == 0)
511 val |= WPA_KEY_MGMT_WPA_NONE;
512#ifdef CONFIG_IEEE80211R
513 else if (os_strcmp(start, "FT-PSK") == 0)
514 val |= WPA_KEY_MGMT_FT_PSK;
515 else if (os_strcmp(start, "FT-EAP") == 0)
516 val |= WPA_KEY_MGMT_FT_IEEE8021X;
517#endif /* CONFIG_IEEE80211R */
518#ifdef CONFIG_IEEE80211W
519 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
520 val |= WPA_KEY_MGMT_PSK_SHA256;
521 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
522 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
523#endif /* CONFIG_IEEE80211W */
524#ifdef CONFIG_WPS
525 else if (os_strcmp(start, "WPS") == 0)
526 val |= WPA_KEY_MGMT_WPS;
527#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800528#ifdef CONFIG_SAE
529 else if (os_strcmp(start, "SAE") == 0)
530 val |= WPA_KEY_MGMT_SAE;
531 else if (os_strcmp(start, "FT-SAE") == 0)
532 val |= WPA_KEY_MGMT_FT_SAE;
533#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800534#ifdef CONFIG_HS20
535 else if (os_strcmp(start, "OSEN") == 0)
536 val |= WPA_KEY_MGMT_OSEN;
537#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 else {
539 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
540 line, start);
541 errors++;
542 }
543
544 if (last)
545 break;
546 start = end + 1;
547 }
548 os_free(buf);
549
550 if (val == 0) {
551 wpa_printf(MSG_ERROR,
552 "Line %d: no key_mgmt values configured.", line);
553 errors++;
554 }
555
556 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
557 ssid->key_mgmt = val;
558 return errors ? -1 : 0;
559}
560
561
562#ifndef NO_CONFIG_WRITE
563static char * wpa_config_write_key_mgmt(const struct parse_data *data,
564 struct wpa_ssid *ssid)
565{
566 char *buf, *pos, *end;
567 int ret;
568
Dmitry Shmidt96571392013-10-14 12:54:46 -0700569 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 if (buf == NULL)
571 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700572 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573
574 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
575 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
576 pos == buf ? "" : " ");
577 if (ret < 0 || ret >= end - pos) {
578 end[-1] = '\0';
579 return buf;
580 }
581 pos += ret;
582 }
583
584 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
585 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
586 pos == buf ? "" : " ");
587 if (ret < 0 || ret >= end - pos) {
588 end[-1] = '\0';
589 return buf;
590 }
591 pos += ret;
592 }
593
594 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
595 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
596 pos == buf ? "" : " ");
597 if (ret < 0 || ret >= end - pos) {
598 end[-1] = '\0';
599 return buf;
600 }
601 pos += ret;
602 }
603
604 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
605 ret = os_snprintf(pos, end - pos, "%sNONE",
606 pos == buf ? "" : " ");
607 if (ret < 0 || ret >= end - pos) {
608 end[-1] = '\0';
609 return buf;
610 }
611 pos += ret;
612 }
613
614 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
615 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
616 pos == buf ? "" : " ");
617 if (ret < 0 || ret >= end - pos) {
618 end[-1] = '\0';
619 return buf;
620 }
621 pos += ret;
622 }
623
624#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700625 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
626 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
627 pos == buf ? "" : " ");
628 if (ret < 0 || ret >= end - pos) {
629 end[-1] = '\0';
630 return buf;
631 }
632 pos += ret;
633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634
Dmitry Shmidt96571392013-10-14 12:54:46 -0700635 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
636 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
637 pos == buf ? "" : " ");
638 if (ret < 0 || ret >= end - pos) {
639 end[-1] = '\0';
640 return buf;
641 }
642 pos += ret;
643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644#endif /* CONFIG_IEEE80211R */
645
646#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700647 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
648 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
649 pos == buf ? "" : " ");
650 if (ret < 0 || ret >= end - pos) {
651 end[-1] = '\0';
652 return buf;
653 }
654 pos += ret;
655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656
Dmitry Shmidt96571392013-10-14 12:54:46 -0700657 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
658 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
659 pos == buf ? "" : " ");
660 if (ret < 0 || ret >= end - pos) {
661 end[-1] = '\0';
662 return buf;
663 }
664 pos += ret;
665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666#endif /* CONFIG_IEEE80211W */
667
668#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700669 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
670 ret = os_snprintf(pos, end - pos, "%sWPS",
671 pos == buf ? "" : " ");
672 if (ret < 0 || ret >= end - pos) {
673 end[-1] = '\0';
674 return buf;
675 }
676 pos += ret;
677 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678#endif /* CONFIG_WPS */
679
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700680 if (pos == buf) {
681 os_free(buf);
682 buf = NULL;
683 }
684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 return buf;
686}
687#endif /* NO_CONFIG_WRITE */
688
689
690static int wpa_config_parse_cipher(int line, const char *value)
691{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800692 int val = wpa_parse_cipher(value);
693 if (val < 0) {
694 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
695 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 if (val == 0) {
699 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
700 line);
701 return -1;
702 }
703 return val;
704}
705
706
707#ifndef NO_CONFIG_WRITE
708static char * wpa_config_write_cipher(int cipher)
709{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800710 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 if (buf == NULL)
712 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800714 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
715 os_free(buf);
716 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 }
718
719 return buf;
720}
721#endif /* NO_CONFIG_WRITE */
722
723
724static int wpa_config_parse_pairwise(const struct parse_data *data,
725 struct wpa_ssid *ssid, int line,
726 const char *value)
727{
728 int val;
729 val = wpa_config_parse_cipher(line, value);
730 if (val == -1)
731 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800732 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
734 "(0x%x).", line, val);
735 return -1;
736 }
737
738 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
739 ssid->pairwise_cipher = val;
740 return 0;
741}
742
743
744#ifndef NO_CONFIG_WRITE
745static char * wpa_config_write_pairwise(const struct parse_data *data,
746 struct wpa_ssid *ssid)
747{
748 return wpa_config_write_cipher(ssid->pairwise_cipher);
749}
750#endif /* NO_CONFIG_WRITE */
751
752
753static int wpa_config_parse_group(const struct parse_data *data,
754 struct wpa_ssid *ssid, int line,
755 const char *value)
756{
757 int val;
758 val = wpa_config_parse_cipher(line, value);
759 if (val == -1)
760 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800761 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
763 "(0x%x).", line, val);
764 return -1;
765 }
766
767 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
768 ssid->group_cipher = val;
769 return 0;
770}
771
772
773#ifndef NO_CONFIG_WRITE
774static char * wpa_config_write_group(const struct parse_data *data,
775 struct wpa_ssid *ssid)
776{
777 return wpa_config_write_cipher(ssid->group_cipher);
778}
779#endif /* NO_CONFIG_WRITE */
780
781
782static int wpa_config_parse_auth_alg(const struct parse_data *data,
783 struct wpa_ssid *ssid, int line,
784 const char *value)
785{
786 int val = 0, last, errors = 0;
787 char *start, *end, *buf;
788
789 buf = os_strdup(value);
790 if (buf == NULL)
791 return -1;
792 start = buf;
793
794 while (*start != '\0') {
795 while (*start == ' ' || *start == '\t')
796 start++;
797 if (*start == '\0')
798 break;
799 end = start;
800 while (*end != ' ' && *end != '\t' && *end != '\0')
801 end++;
802 last = *end == '\0';
803 *end = '\0';
804 if (os_strcmp(start, "OPEN") == 0)
805 val |= WPA_AUTH_ALG_OPEN;
806 else if (os_strcmp(start, "SHARED") == 0)
807 val |= WPA_AUTH_ALG_SHARED;
808 else if (os_strcmp(start, "LEAP") == 0)
809 val |= WPA_AUTH_ALG_LEAP;
810 else {
811 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
812 line, start);
813 errors++;
814 }
815
816 if (last)
817 break;
818 start = end + 1;
819 }
820 os_free(buf);
821
822 if (val == 0) {
823 wpa_printf(MSG_ERROR,
824 "Line %d: no auth_alg values configured.", line);
825 errors++;
826 }
827
828 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
829 ssid->auth_alg = val;
830 return errors ? -1 : 0;
831}
832
833
834#ifndef NO_CONFIG_WRITE
835static char * wpa_config_write_auth_alg(const struct parse_data *data,
836 struct wpa_ssid *ssid)
837{
838 char *buf, *pos, *end;
839 int ret;
840
841 pos = buf = os_zalloc(30);
842 if (buf == NULL)
843 return NULL;
844 end = buf + 30;
845
846 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
847 ret = os_snprintf(pos, end - pos, "%sOPEN",
848 pos == buf ? "" : " ");
849 if (ret < 0 || ret >= end - pos) {
850 end[-1] = '\0';
851 return buf;
852 }
853 pos += ret;
854 }
855
856 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
857 ret = os_snprintf(pos, end - pos, "%sSHARED",
858 pos == buf ? "" : " ");
859 if (ret < 0 || ret >= end - pos) {
860 end[-1] = '\0';
861 return buf;
862 }
863 pos += ret;
864 }
865
866 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
867 ret = os_snprintf(pos, end - pos, "%sLEAP",
868 pos == buf ? "" : " ");
869 if (ret < 0 || ret >= end - pos) {
870 end[-1] = '\0';
871 return buf;
872 }
873 pos += ret;
874 }
875
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700876 if (pos == buf) {
877 os_free(buf);
878 buf = NULL;
879 }
880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 return buf;
882}
883#endif /* NO_CONFIG_WRITE */
884
885
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800886static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887{
888 int *freqs;
889 size_t used, len;
890 const char *pos;
891
892 used = 0;
893 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700894 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 if (freqs == NULL)
896 return NULL;
897
898 pos = value;
899 while (pos) {
900 while (*pos == ' ')
901 pos++;
902 if (used == len) {
903 int *n;
904 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700905 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 if (n == NULL) {
907 os_free(freqs);
908 return NULL;
909 }
910 for (i = len; i <= len * 2; i++)
911 n[i] = 0;
912 freqs = n;
913 len *= 2;
914 }
915
916 freqs[used] = atoi(pos);
917 if (freqs[used] == 0)
918 break;
919 used++;
920 pos = os_strchr(pos + 1, ' ');
921 }
922
923 return freqs;
924}
925
926
927static int wpa_config_parse_scan_freq(const struct parse_data *data,
928 struct wpa_ssid *ssid, int line,
929 const char *value)
930{
931 int *freqs;
932
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800933 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 if (freqs == NULL)
935 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700936 if (freqs[0] == 0) {
937 os_free(freqs);
938 freqs = NULL;
939 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 os_free(ssid->scan_freq);
941 ssid->scan_freq = freqs;
942
943 return 0;
944}
945
946
947static int wpa_config_parse_freq_list(const struct parse_data *data,
948 struct wpa_ssid *ssid, int line,
949 const char *value)
950{
951 int *freqs;
952
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800953 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 if (freqs == NULL)
955 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700956 if (freqs[0] == 0) {
957 os_free(freqs);
958 freqs = NULL;
959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 os_free(ssid->freq_list);
961 ssid->freq_list = freqs;
962
963 return 0;
964}
965
966
967#ifndef NO_CONFIG_WRITE
968static char * wpa_config_write_freqs(const struct parse_data *data,
969 const int *freqs)
970{
971 char *buf, *pos, *end;
972 int i, ret;
973 size_t count;
974
975 if (freqs == NULL)
976 return NULL;
977
978 count = 0;
979 for (i = 0; freqs[i]; i++)
980 count++;
981
982 pos = buf = os_zalloc(10 * count + 1);
983 if (buf == NULL)
984 return NULL;
985 end = buf + 10 * count + 1;
986
987 for (i = 0; freqs[i]; i++) {
988 ret = os_snprintf(pos, end - pos, "%s%u",
989 i == 0 ? "" : " ", freqs[i]);
990 if (ret < 0 || ret >= end - pos) {
991 end[-1] = '\0';
992 return buf;
993 }
994 pos += ret;
995 }
996
997 return buf;
998}
999
1000
1001static char * wpa_config_write_scan_freq(const struct parse_data *data,
1002 struct wpa_ssid *ssid)
1003{
1004 return wpa_config_write_freqs(data, ssid->scan_freq);
1005}
1006
1007
1008static char * wpa_config_write_freq_list(const struct parse_data *data,
1009 struct wpa_ssid *ssid)
1010{
1011 return wpa_config_write_freqs(data, ssid->freq_list);
1012}
1013#endif /* NO_CONFIG_WRITE */
1014
1015
1016#ifdef IEEE8021X_EAPOL
1017static int wpa_config_parse_eap(const struct parse_data *data,
1018 struct wpa_ssid *ssid, int line,
1019 const char *value)
1020{
1021 int last, errors = 0;
1022 char *start, *end, *buf;
1023 struct eap_method_type *methods = NULL, *tmp;
1024 size_t num_methods = 0;
1025
1026 buf = os_strdup(value);
1027 if (buf == NULL)
1028 return -1;
1029 start = buf;
1030
1031 while (*start != '\0') {
1032 while (*start == ' ' || *start == '\t')
1033 start++;
1034 if (*start == '\0')
1035 break;
1036 end = start;
1037 while (*end != ' ' && *end != '\t' && *end != '\0')
1038 end++;
1039 last = *end == '\0';
1040 *end = '\0';
1041 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001042 methods = os_realloc_array(methods, num_methods + 1,
1043 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 if (methods == NULL) {
1045 os_free(tmp);
1046 os_free(buf);
1047 return -1;
1048 }
1049 methods[num_methods].method = eap_peer_get_type(
1050 start, &methods[num_methods].vendor);
1051 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1052 methods[num_methods].method == EAP_TYPE_NONE) {
1053 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1054 "'%s'", line, start);
1055 wpa_printf(MSG_ERROR, "You may need to add support for"
1056 " this EAP method during wpa_supplicant\n"
1057 "build time configuration.\n"
1058 "See README for more information.");
1059 errors++;
1060 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1061 methods[num_methods].method == EAP_TYPE_LEAP)
1062 ssid->leap++;
1063 else
1064 ssid->non_leap++;
1065 num_methods++;
1066 if (last)
1067 break;
1068 start = end + 1;
1069 }
1070 os_free(buf);
1071
1072 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001073 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 if (methods == NULL) {
1075 os_free(tmp);
1076 return -1;
1077 }
1078 methods[num_methods].vendor = EAP_VENDOR_IETF;
1079 methods[num_methods].method = EAP_TYPE_NONE;
1080 num_methods++;
1081
1082 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1083 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001084 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 ssid->eap.eap_methods = methods;
1086 return errors ? -1 : 0;
1087}
1088
1089
1090static char * wpa_config_write_eap(const struct parse_data *data,
1091 struct wpa_ssid *ssid)
1092{
1093 int i, ret;
1094 char *buf, *pos, *end;
1095 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1096 const char *name;
1097
1098 if (eap_methods == NULL)
1099 return NULL;
1100
1101 pos = buf = os_zalloc(100);
1102 if (buf == NULL)
1103 return NULL;
1104 end = buf + 100;
1105
1106 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1107 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1108 name = eap_get_name(eap_methods[i].vendor,
1109 eap_methods[i].method);
1110 if (name) {
1111 ret = os_snprintf(pos, end - pos, "%s%s",
1112 pos == buf ? "" : " ", name);
1113 if (ret < 0 || ret >= end - pos)
1114 break;
1115 pos += ret;
1116 }
1117 }
1118
1119 end[-1] = '\0';
1120
1121 return buf;
1122}
1123
1124
1125static int wpa_config_parse_password(const struct parse_data *data,
1126 struct wpa_ssid *ssid, int line,
1127 const char *value)
1128{
1129 u8 *hash;
1130
1131 if (os_strcmp(value, "NULL") == 0) {
1132 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001133 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 ssid->eap.password = NULL;
1135 ssid->eap.password_len = 0;
1136 return 0;
1137 }
1138
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001139#ifdef CONFIG_EXT_PASSWORD
1140 if (os_strncmp(value, "ext:", 4) == 0) {
1141 char *name = os_strdup(value + 4);
1142 if (name == NULL)
1143 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001144 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001145 ssid->eap.password = (u8 *) name;
1146 ssid->eap.password_len = os_strlen(name);
1147 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1148 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1149 return 0;
1150 }
1151#endif /* CONFIG_EXT_PASSWORD */
1152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 if (os_strncmp(value, "hash:", 5) != 0) {
1154 char *tmp;
1155 size_t res_len;
1156
1157 tmp = wpa_config_parse_string(value, &res_len);
1158 if (tmp == NULL) {
1159 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1160 "password.", line);
1161 return -1;
1162 }
1163 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1164 (u8 *) tmp, res_len);
1165
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001166 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 ssid->eap.password = (u8 *) tmp;
1168 ssid->eap.password_len = res_len;
1169 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001170 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171
1172 return 0;
1173 }
1174
1175
1176 /* NtPasswordHash: hash:<32 hex digits> */
1177 if (os_strlen(value + 5) != 2 * 16) {
1178 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1179 "(expected 32 hex digits)", line);
1180 return -1;
1181 }
1182
1183 hash = os_malloc(16);
1184 if (hash == NULL)
1185 return -1;
1186
1187 if (hexstr2bin(value + 5, hash, 16)) {
1188 os_free(hash);
1189 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1190 return -1;
1191 }
1192
1193 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1194
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001195 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 ssid->eap.password = hash;
1197 ssid->eap.password_len = 16;
1198 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001199 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
1201 return 0;
1202}
1203
1204
1205static char * wpa_config_write_password(const struct parse_data *data,
1206 struct wpa_ssid *ssid)
1207{
1208 char *buf;
1209
1210 if (ssid->eap.password == NULL)
1211 return NULL;
1212
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213#ifdef CONFIG_EXT_PASSWORD
1214 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1215 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1216 if (buf == NULL)
1217 return NULL;
1218 os_memcpy(buf, "ext:", 4);
1219 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1220 return buf;
1221 }
1222#endif /* CONFIG_EXT_PASSWORD */
1223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1225 return wpa_config_write_string(
1226 ssid->eap.password, ssid->eap.password_len);
1227 }
1228
1229 buf = os_malloc(5 + 32 + 1);
1230 if (buf == NULL)
1231 return NULL;
1232
1233 os_memcpy(buf, "hash:", 5);
1234 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1235
1236 return buf;
1237}
1238#endif /* IEEE8021X_EAPOL */
1239
1240
1241static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1242 const char *value, int idx)
1243{
1244 char *buf, title[20];
1245 int res;
1246
1247 buf = wpa_config_parse_string(value, len);
1248 if (buf == NULL) {
1249 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1250 line, idx, value);
1251 return -1;
1252 }
1253 if (*len > MAX_WEP_KEY_LEN) {
1254 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1255 line, idx, value);
1256 os_free(buf);
1257 return -1;
1258 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001259 if (*len && *len != 5 && *len != 13 && *len != 16) {
1260 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1261 "this network block will be ignored",
1262 line, (unsigned int) *len);
1263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001265 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1267 if (res >= 0 && (size_t) res < sizeof(title))
1268 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1269 return 0;
1270}
1271
1272
1273static int wpa_config_parse_wep_key0(const struct parse_data *data,
1274 struct wpa_ssid *ssid, int line,
1275 const char *value)
1276{
1277 return wpa_config_parse_wep_key(ssid->wep_key[0],
1278 &ssid->wep_key_len[0], line,
1279 value, 0);
1280}
1281
1282
1283static int wpa_config_parse_wep_key1(const struct parse_data *data,
1284 struct wpa_ssid *ssid, int line,
1285 const char *value)
1286{
1287 return wpa_config_parse_wep_key(ssid->wep_key[1],
1288 &ssid->wep_key_len[1], line,
1289 value, 1);
1290}
1291
1292
1293static int wpa_config_parse_wep_key2(const struct parse_data *data,
1294 struct wpa_ssid *ssid, int line,
1295 const char *value)
1296{
1297 return wpa_config_parse_wep_key(ssid->wep_key[2],
1298 &ssid->wep_key_len[2], line,
1299 value, 2);
1300}
1301
1302
1303static int wpa_config_parse_wep_key3(const struct parse_data *data,
1304 struct wpa_ssid *ssid, int line,
1305 const char *value)
1306{
1307 return wpa_config_parse_wep_key(ssid->wep_key[3],
1308 &ssid->wep_key_len[3], line,
1309 value, 3);
1310}
1311
1312
1313#ifndef NO_CONFIG_WRITE
1314static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1315{
1316 if (ssid->wep_key_len[idx] == 0)
1317 return NULL;
1318 return wpa_config_write_string(ssid->wep_key[idx],
1319 ssid->wep_key_len[idx]);
1320}
1321
1322
1323static char * wpa_config_write_wep_key0(const struct parse_data *data,
1324 struct wpa_ssid *ssid)
1325{
1326 return wpa_config_write_wep_key(ssid, 0);
1327}
1328
1329
1330static char * wpa_config_write_wep_key1(const struct parse_data *data,
1331 struct wpa_ssid *ssid)
1332{
1333 return wpa_config_write_wep_key(ssid, 1);
1334}
1335
1336
1337static char * wpa_config_write_wep_key2(const struct parse_data *data,
1338 struct wpa_ssid *ssid)
1339{
1340 return wpa_config_write_wep_key(ssid, 2);
1341}
1342
1343
1344static char * wpa_config_write_wep_key3(const struct parse_data *data,
1345 struct wpa_ssid *ssid)
1346{
1347 return wpa_config_write_wep_key(ssid, 3);
1348}
1349#endif /* NO_CONFIG_WRITE */
1350
1351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001352#ifdef CONFIG_P2P
1353
Dmitry Shmidt54605472013-11-08 11:10:19 -08001354static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1355 struct wpa_ssid *ssid, int line,
1356 const char *value)
1357{
1358 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1359 os_strcmp(value, "any") == 0) {
1360 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1361 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1362 return 0;
1363 }
1364 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1365 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1366 line, value);
1367 return -1;
1368 }
1369 ssid->bssid_set = 1;
1370 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1371 MAC2STR(ssid->go_p2p_dev_addr));
1372 return 0;
1373}
1374
1375
1376#ifndef NO_CONFIG_WRITE
1377static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1378 struct wpa_ssid *ssid)
1379{
1380 char *value;
1381 int res;
1382
1383 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1384 return NULL;
1385
1386 value = os_malloc(20);
1387 if (value == NULL)
1388 return NULL;
1389 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1390 if (res < 0 || res >= 20) {
1391 os_free(value);
1392 return NULL;
1393 }
1394 value[20 - 1] = '\0';
1395 return value;
1396}
1397#endif /* NO_CONFIG_WRITE */
1398
1399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1401 struct wpa_ssid *ssid, int line,
1402 const char *value)
1403{
1404 const char *pos;
1405 u8 *buf, *n, addr[ETH_ALEN];
1406 size_t count;
1407
1408 buf = NULL;
1409 count = 0;
1410
1411 pos = value;
1412 while (pos && *pos) {
1413 while (*pos == ' ')
1414 pos++;
1415
1416 if (hwaddr_aton(pos, addr)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001417 if (count == 0) {
1418 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1419 "p2p_client_list address '%s'.",
1420 line, value);
1421 os_free(buf);
1422 return -1;
1423 }
1424 /* continue anyway since this could have been from a
1425 * truncated configuration file line */
1426 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1427 "truncated p2p_client_list address '%s'",
1428 line, pos);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001429 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001430 n = os_realloc_array(buf, count + 1, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001431 if (n == NULL) {
1432 os_free(buf);
1433 return -1;
1434 }
1435 buf = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001436 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1437 os_memcpy(buf, addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001438 count++;
1439 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1440 addr, ETH_ALEN);
1441 }
1442
1443 pos = os_strchr(pos, ' ');
1444 }
1445
1446 os_free(ssid->p2p_client_list);
1447 ssid->p2p_client_list = buf;
1448 ssid->num_p2p_clients = count;
1449
1450 return 0;
1451}
1452
1453
1454#ifndef NO_CONFIG_WRITE
1455static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1456 struct wpa_ssid *ssid)
1457{
1458 char *value, *end, *pos;
1459 int res;
1460 size_t i;
1461
1462 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1463 return NULL;
1464
1465 value = os_malloc(20 * ssid->num_p2p_clients);
1466 if (value == NULL)
1467 return NULL;
1468 pos = value;
1469 end = value + 20 * ssid->num_p2p_clients;
1470
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001471 for (i = ssid->num_p2p_clients; i > 0; i--) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 res = os_snprintf(pos, end - pos, MACSTR " ",
1473 MAC2STR(ssid->p2p_client_list +
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001474 (i - 1) * ETH_ALEN));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001475 if (res < 0 || res >= end - pos) {
1476 os_free(value);
1477 return NULL;
1478 }
1479 pos += res;
1480 }
1481
1482 if (pos > value)
1483 pos[-1] = '\0';
1484
1485 return value;
1486}
1487#endif /* NO_CONFIG_WRITE */
1488
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001489
1490static int wpa_config_parse_psk_list(const struct parse_data *data,
1491 struct wpa_ssid *ssid, int line,
1492 const char *value)
1493{
1494 struct psk_list_entry *p;
1495 const char *pos;
1496
1497 p = os_zalloc(sizeof(*p));
1498 if (p == NULL)
1499 return -1;
1500
1501 pos = value;
1502 if (os_strncmp(pos, "P2P-", 4) == 0) {
1503 p->p2p = 1;
1504 pos += 4;
1505 }
1506
1507 if (hwaddr_aton(pos, p->addr)) {
1508 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1509 line, pos);
1510 os_free(p);
1511 return -1;
1512 }
1513 pos += 17;
1514 if (*pos != '-') {
1515 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1516 line, pos);
1517 os_free(p);
1518 return -1;
1519 }
1520 pos++;
1521
1522 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1523 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1524 line, pos);
1525 os_free(p);
1526 return -1;
1527 }
1528
1529 dl_list_add(&ssid->psk_list, &p->list);
1530
1531 return 0;
1532}
1533
1534
1535#ifndef NO_CONFIG_WRITE
1536static char * wpa_config_write_psk_list(const struct parse_data *data,
1537 struct wpa_ssid *ssid)
1538{
1539 return NULL;
1540}
1541#endif /* NO_CONFIG_WRITE */
1542
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001543#endif /* CONFIG_P2P */
1544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545/* Helper macros for network block parser */
1546
1547#ifdef OFFSET
1548#undef OFFSET
1549#endif /* OFFSET */
1550/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1551#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1552
1553/* STR: Define a string variable for an ASCII string; f = field name */
1554#ifdef NO_CONFIG_WRITE
1555#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1556#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1557#else /* NO_CONFIG_WRITE */
1558#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1559#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1560#endif /* NO_CONFIG_WRITE */
1561#define STR(f) _STR(f), NULL, NULL, NULL, 0
1562#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1563#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1564#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1565
1566/* STR_LEN: Define a string variable with a separate variable for storing the
1567 * data length. Unlike STR(), this can be used to store arbitrary binary data
1568 * (i.e., even nul termination character). */
1569#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1570#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1571#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1572#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1573#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1574
1575/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1576 * explicitly specified. */
1577#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1578#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1579#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1580
1581#ifdef NO_CONFIG_WRITE
1582#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1583#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1584#else /* NO_CONFIG_WRITE */
1585#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1586 OFFSET(f), (void *) 0
1587#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1588 OFFSET(eap.f), (void *) 0
1589#endif /* NO_CONFIG_WRITE */
1590
1591/* INT: Define an integer variable */
1592#define INT(f) _INT(f), NULL, NULL, 0
1593#define INTe(f) _INTe(f), NULL, NULL, 0
1594
1595/* INT_RANGE: Define an integer variable with allowed value range */
1596#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1597
1598/* FUNC: Define a configuration variable that uses a custom function for
1599 * parsing and writing the value. */
1600#ifdef NO_CONFIG_WRITE
1601#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1602#else /* NO_CONFIG_WRITE */
1603#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1604 NULL, NULL, NULL, NULL
1605#endif /* NO_CONFIG_WRITE */
1606#define FUNC(f) _FUNC(f), 0
1607#define FUNC_KEY(f) _FUNC(f), 1
1608
1609/*
1610 * Table of network configuration variables. This table is used to parse each
1611 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1612 * that is inside a network block.
1613 *
1614 * This table is generated using the helper macros defined above and with
1615 * generous help from the C pre-processor. The field name is stored as a string
1616 * into .name and for STR and INT types, the offset of the target buffer within
1617 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1618 * offset to the field containing the length of the configuration variable.
1619 * .param3 and .param4 can be used to mark the allowed range (length for STR
1620 * and value for INT).
1621 *
1622 * For each configuration line in wpa_supplicant.conf, the parser goes through
1623 * this table and select the entry that matches with the field name. The parser
1624 * function (.parser) is then called to parse the actual value of the field.
1625 *
1626 * This kind of mechanism makes it easy to add new configuration parameters,
1627 * since only one line needs to be added into this table and into the
1628 * struct wpa_ssid definition if the new variable is either a string or
1629 * integer. More complex types will need to use their own parser and writer
1630 * functions.
1631 */
1632static const struct parse_data ssid_fields[] = {
1633 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1634 { INT_RANGE(scan_ssid, 0, 1) },
1635 { FUNC(bssid) },
1636 { FUNC_KEY(psk) },
1637 { FUNC(proto) },
1638 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001639 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 { FUNC(pairwise) },
1641 { FUNC(group) },
1642 { FUNC(auth_alg) },
1643 { FUNC(scan_freq) },
1644 { FUNC(freq_list) },
1645#ifdef IEEE8021X_EAPOL
1646 { FUNC(eap) },
1647 { STR_LENe(identity) },
1648 { STR_LENe(anonymous_identity) },
1649 { FUNC_KEY(password) },
1650 { STRe(ca_cert) },
1651 { STRe(ca_path) },
1652 { STRe(client_cert) },
1653 { STRe(private_key) },
1654 { STR_KEYe(private_key_passwd) },
1655 { STRe(dh_file) },
1656 { STRe(subject_match) },
1657 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001658 { STRe(domain_suffix_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 { STRe(ca_cert2) },
1660 { STRe(ca_path2) },
1661 { STRe(client_cert2) },
1662 { STRe(private_key2) },
1663 { STR_KEYe(private_key2_passwd) },
1664 { STRe(dh_file2) },
1665 { STRe(subject_match2) },
1666 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001667 { STRe(domain_suffix_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 { STRe(phase1) },
1669 { STRe(phase2) },
1670 { STRe(pcsc) },
1671 { STR_KEYe(pin) },
1672 { STRe(engine_id) },
1673 { STRe(key_id) },
1674 { STRe(cert_id) },
1675 { STRe(ca_cert_id) },
1676 { STR_KEYe(pin2) },
1677 { STRe(engine2_id) },
1678 { STRe(key2_id) },
1679 { STRe(cert2_id) },
1680 { STRe(ca_cert2_id) },
1681 { INTe(engine) },
1682 { INTe(engine2) },
1683 { INT(eapol_flags) },
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001684 { INTe(sim_num) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685#endif /* IEEE8021X_EAPOL */
1686 { FUNC_KEY(wep_key0) },
1687 { FUNC_KEY(wep_key1) },
1688 { FUNC_KEY(wep_key2) },
1689 { FUNC_KEY(wep_key3) },
1690 { INT(wep_tx_keyidx) },
1691 { INT(priority) },
1692#ifdef IEEE8021X_EAPOL
1693 { INT(eap_workaround) },
1694 { STRe(pac_file) },
1695 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001696 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697#endif /* IEEE8021X_EAPOL */
1698 { INT_RANGE(mode, 0, 4) },
1699 { INT_RANGE(proactive_key_caching, 0, 1) },
1700 { INT_RANGE(disabled, 0, 2) },
1701 { STR(id_str) },
1702#ifdef CONFIG_IEEE80211W
1703 { INT_RANGE(ieee80211w, 0, 2) },
1704#endif /* CONFIG_IEEE80211W */
1705 { INT_RANGE(peerkey, 0, 1) },
1706 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001707 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 { INT(wpa_ptk_rekey) },
1709 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001710 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001712 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001714 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001716#ifdef CONFIG_HT_OVERRIDES
1717 { INT_RANGE(disable_ht, 0, 1) },
1718 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001719 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001720 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001721 { INT_RANGE(ht40_intolerant, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001722 { INT_RANGE(disable_max_amsdu, -1, 1) },
1723 { INT_RANGE(ampdu_factor, -1, 3) },
1724 { INT_RANGE(ampdu_density, -1, 7) },
1725 { STR(ht_mcs) },
1726#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001727#ifdef CONFIG_VHT_OVERRIDES
1728 { INT_RANGE(disable_vht, 0, 1) },
1729 { INT(vht_capa) },
1730 { INT(vht_capa_mask) },
1731 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1732 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1733 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1734 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1735 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1736 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1737 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1738 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1739 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1740 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1741 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1742 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1743 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1744 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1745 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1746 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1747#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001748 { INT(ap_max_inactivity) },
1749 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001750 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001751#ifdef CONFIG_MACSEC
1752 { INT_RANGE(macsec_policy, 0, 1) },
1753#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001754#ifdef CONFIG_HS20
1755 { INT(update_identifier) },
1756#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001757 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758};
1759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760#undef OFFSET
1761#undef _STR
1762#undef STR
1763#undef STR_KEY
1764#undef _STR_LEN
1765#undef STR_LEN
1766#undef STR_LEN_KEY
1767#undef _STR_RANGE
1768#undef STR_RANGE
1769#undef STR_RANGE_KEY
1770#undef _INT
1771#undef INT
1772#undef INT_RANGE
1773#undef _FUNC
1774#undef FUNC
1775#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001776#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777
1778
1779/**
1780 * wpa_config_add_prio_network - Add a network to priority lists
1781 * @config: Configuration data from wpa_config_read()
1782 * @ssid: Pointer to the network configuration to be added to the list
1783 * Returns: 0 on success, -1 on failure
1784 *
1785 * This function is used to add a network block to the priority list of
1786 * networks. This must be called for each network when reading in the full
1787 * configuration. In addition, this can be used indirectly when updating
1788 * priorities by calling wpa_config_update_prio_list().
1789 */
1790int wpa_config_add_prio_network(struct wpa_config *config,
1791 struct wpa_ssid *ssid)
1792{
1793 int prio;
1794 struct wpa_ssid *prev, **nlist;
1795
1796 /*
1797 * Add to an existing priority list if one is available for the
1798 * configured priority level for this network.
1799 */
1800 for (prio = 0; prio < config->num_prio; prio++) {
1801 prev = config->pssid[prio];
1802 if (prev->priority == ssid->priority) {
1803 while (prev->pnext)
1804 prev = prev->pnext;
1805 prev->pnext = ssid;
1806 return 0;
1807 }
1808 }
1809
1810 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001811 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1812 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 if (nlist == NULL)
1814 return -1;
1815
1816 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001817 if (nlist[prio]->priority < ssid->priority) {
1818 os_memmove(&nlist[prio + 1], &nlist[prio],
1819 (config->num_prio - prio) *
1820 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 }
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 nlist[prio] = ssid;
1826 config->num_prio++;
1827 config->pssid = nlist;
1828
1829 return 0;
1830}
1831
1832
1833/**
1834 * wpa_config_update_prio_list - Update network priority list
1835 * @config: Configuration data from wpa_config_read()
1836 * Returns: 0 on success, -1 on failure
1837 *
1838 * This function is called to update the priority list of networks in the
1839 * configuration when a network is being added or removed. This is also called
1840 * if a priority for a network is changed.
1841 */
1842int wpa_config_update_prio_list(struct wpa_config *config)
1843{
1844 struct wpa_ssid *ssid;
1845 int ret = 0;
1846
1847 os_free(config->pssid);
1848 config->pssid = NULL;
1849 config->num_prio = 0;
1850
1851 ssid = config->ssid;
1852 while (ssid) {
1853 ssid->pnext = NULL;
1854 if (wpa_config_add_prio_network(config, ssid) < 0)
1855 ret = -1;
1856 ssid = ssid->next;
1857 }
1858
1859 return ret;
1860}
1861
1862
1863#ifdef IEEE8021X_EAPOL
1864static void eap_peer_config_free(struct eap_peer_config *eap)
1865{
1866 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001867 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868 os_free(eap->anonymous_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001869 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 os_free(eap->ca_cert);
1871 os_free(eap->ca_path);
1872 os_free(eap->client_cert);
1873 os_free(eap->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001874 str_clear_free(eap->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 os_free(eap->dh_file);
1876 os_free(eap->subject_match);
1877 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001878 os_free(eap->domain_suffix_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 os_free(eap->ca_cert2);
1880 os_free(eap->ca_path2);
1881 os_free(eap->client_cert2);
1882 os_free(eap->private_key2);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001883 str_clear_free(eap->private_key2_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 os_free(eap->dh_file2);
1885 os_free(eap->subject_match2);
1886 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001887 os_free(eap->domain_suffix_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 os_free(eap->phase1);
1889 os_free(eap->phase2);
1890 os_free(eap->pcsc);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001891 str_clear_free(eap->pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892 os_free(eap->engine_id);
1893 os_free(eap->key_id);
1894 os_free(eap->cert_id);
1895 os_free(eap->ca_cert_id);
1896 os_free(eap->key2_id);
1897 os_free(eap->cert2_id);
1898 os_free(eap->ca_cert2_id);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001899 str_clear_free(eap->pin2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 os_free(eap->engine2_id);
1901 os_free(eap->otp);
1902 os_free(eap->pending_req_otp);
1903 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001904 bin_clear_free(eap->new_password, eap->new_password_len);
1905 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906}
1907#endif /* IEEE8021X_EAPOL */
1908
1909
1910/**
1911 * wpa_config_free_ssid - Free network/ssid configuration data
1912 * @ssid: Configuration data for the network
1913 *
1914 * This function frees all resources allocated for the network configuration
1915 * data.
1916 */
1917void wpa_config_free_ssid(struct wpa_ssid *ssid)
1918{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001919 struct psk_list_entry *psk;
1920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001922 os_memset(ssid->psk, 0, sizeof(ssid->psk));
1923 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001924 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925#ifdef IEEE8021X_EAPOL
1926 eap_peer_config_free(&ssid->eap);
1927#endif /* IEEE8021X_EAPOL */
1928 os_free(ssid->id_str);
1929 os_free(ssid->scan_freq);
1930 os_free(ssid->freq_list);
1931 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001932 os_free(ssid->p2p_client_list);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001933#ifdef CONFIG_HT_OVERRIDES
1934 os_free(ssid->ht_mcs);
1935#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001936 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1937 list))) {
1938 dl_list_del(&psk->list);
1939 os_free(psk);
1940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 os_free(ssid);
1942}
1943
1944
Dmitry Shmidt04949592012-07-19 12:16:46 -07001945void wpa_config_free_cred(struct wpa_cred *cred)
1946{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001947 size_t i;
1948
Dmitry Shmidt04949592012-07-19 12:16:46 -07001949 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001950 str_clear_free(cred->username);
1951 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001952 os_free(cred->ca_cert);
1953 os_free(cred->client_cert);
1954 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001955 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001956 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001957 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001958 for (i = 0; i < cred->num_domain; i++)
1959 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001960 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001961 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001962 os_free(cred->eap_method);
1963 os_free(cred->phase1);
1964 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001965 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001966 os_free(cred->roaming_partner);
1967 os_free(cred->provisioning_sp);
1968 for (i = 0; i < cred->num_req_conn_capab; i++)
1969 os_free(cred->req_conn_capab_port[i]);
1970 os_free(cred->req_conn_capab_port);
1971 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001972 os_free(cred);
1973}
1974
1975
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001976void wpa_config_flush_blobs(struct wpa_config *config)
1977{
1978#ifndef CONFIG_NO_CONFIG_BLOBS
1979 struct wpa_config_blob *blob, *prev;
1980
1981 blob = config->blobs;
1982 config->blobs = NULL;
1983 while (blob) {
1984 prev = blob;
1985 blob = blob->next;
1986 wpa_config_free_blob(prev);
1987 }
1988#endif /* CONFIG_NO_CONFIG_BLOBS */
1989}
1990
1991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992/**
1993 * wpa_config_free - Free configuration data
1994 * @config: Configuration data from wpa_config_read()
1995 *
1996 * This function frees all resources allocated for the configuration data by
1997 * wpa_config_read().
1998 */
1999void wpa_config_free(struct wpa_config *config)
2000{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002002 struct wpa_cred *cred, *cprev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003
2004 ssid = config->ssid;
2005 while (ssid) {
2006 prev = ssid;
2007 ssid = ssid->next;
2008 wpa_config_free_ssid(prev);
2009 }
2010
Dmitry Shmidt04949592012-07-19 12:16:46 -07002011 cred = config->cred;
2012 while (cred) {
2013 cprev = cred;
2014 cred = cred->next;
2015 wpa_config_free_cred(cprev);
2016 }
2017
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002018 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019
Dmitry Shmidt04949592012-07-19 12:16:46 -07002020 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 os_free(config->ctrl_interface);
2022 os_free(config->ctrl_interface_group);
2023 os_free(config->opensc_engine_path);
2024 os_free(config->pkcs11_engine_path);
2025 os_free(config->pkcs11_module_path);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002026 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002027 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 os_free(config->driver_param);
2029 os_free(config->device_name);
2030 os_free(config->manufacturer);
2031 os_free(config->model_name);
2032 os_free(config->model_number);
2033 os_free(config->serial_number);
2034 os_free(config->config_methods);
2035 os_free(config->p2p_ssid_postfix);
2036 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002037 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002038 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002039 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002040 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002041 wpabuf_free(config->wps_nfc_dh_pubkey);
2042 wpabuf_free(config->wps_nfc_dh_privkey);
2043 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002044 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002045 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002046 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002047 os_free(config->osu_dir);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002048 os_free(config->wowlan_triggers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 os_free(config);
2050}
2051
2052
2053/**
2054 * wpa_config_foreach_network - Iterate over each configured network
2055 * @config: Configuration data from wpa_config_read()
2056 * @func: Callback function to process each network
2057 * @arg: Opaque argument to pass to callback function
2058 *
2059 * Iterate over the set of configured networks calling the specified
2060 * function for each item. We guard against callbacks removing the
2061 * supplied network.
2062 */
2063void wpa_config_foreach_network(struct wpa_config *config,
2064 void (*func)(void *, struct wpa_ssid *),
2065 void *arg)
2066{
2067 struct wpa_ssid *ssid, *next;
2068
2069 ssid = config->ssid;
2070 while (ssid) {
2071 next = ssid->next;
2072 func(arg, ssid);
2073 ssid = next;
2074 }
2075}
2076
2077
2078/**
2079 * wpa_config_get_network - Get configured network based on id
2080 * @config: Configuration data from wpa_config_read()
2081 * @id: Unique network id to search for
2082 * Returns: Network configuration or %NULL if not found
2083 */
2084struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2085{
2086 struct wpa_ssid *ssid;
2087
2088 ssid = config->ssid;
2089 while (ssid) {
2090 if (id == ssid->id)
2091 break;
2092 ssid = ssid->next;
2093 }
2094
2095 return ssid;
2096}
2097
2098
2099/**
2100 * wpa_config_add_network - Add a new network with empty configuration
2101 * @config: Configuration data from wpa_config_read()
2102 * Returns: The new network configuration or %NULL if operation failed
2103 */
2104struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2105{
2106 int id;
2107 struct wpa_ssid *ssid, *last = NULL;
2108
2109 id = -1;
2110 ssid = config->ssid;
2111 while (ssid) {
2112 if (ssid->id > id)
2113 id = ssid->id;
2114 last = ssid;
2115 ssid = ssid->next;
2116 }
2117 id++;
2118
2119 ssid = os_zalloc(sizeof(*ssid));
2120 if (ssid == NULL)
2121 return NULL;
2122 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002123 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 if (last)
2125 last->next = ssid;
2126 else
2127 config->ssid = ssid;
2128
2129 wpa_config_update_prio_list(config);
2130
2131 return ssid;
2132}
2133
2134
2135/**
2136 * wpa_config_remove_network - Remove a configured network based on id
2137 * @config: Configuration data from wpa_config_read()
2138 * @id: Unique network id to search for
2139 * Returns: 0 on success, or -1 if the network was not found
2140 */
2141int wpa_config_remove_network(struct wpa_config *config, int id)
2142{
2143 struct wpa_ssid *ssid, *prev = NULL;
2144
2145 ssid = config->ssid;
2146 while (ssid) {
2147 if (id == ssid->id)
2148 break;
2149 prev = ssid;
2150 ssid = ssid->next;
2151 }
2152
2153 if (ssid == NULL)
2154 return -1;
2155
2156 if (prev)
2157 prev->next = ssid->next;
2158 else
2159 config->ssid = ssid->next;
2160
2161 wpa_config_update_prio_list(config);
2162 wpa_config_free_ssid(ssid);
2163 return 0;
2164}
2165
2166
2167/**
2168 * wpa_config_set_network_defaults - Set network default values
2169 * @ssid: Pointer to network configuration data
2170 */
2171void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2172{
2173 ssid->proto = DEFAULT_PROTO;
2174 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2175 ssid->group_cipher = DEFAULT_GROUP;
2176 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002177 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178#ifdef IEEE8021X_EAPOL
2179 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2180 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2181 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002182 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183#endif /* IEEE8021X_EAPOL */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002184#ifdef CONFIG_HT_OVERRIDES
2185 ssid->disable_ht = DEFAULT_DISABLE_HT;
2186 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002187 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002188 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002189 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2190 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2191 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2192#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002193#ifdef CONFIG_VHT_OVERRIDES
2194 ssid->vht_rx_mcs_nss_1 = -1;
2195 ssid->vht_rx_mcs_nss_2 = -1;
2196 ssid->vht_rx_mcs_nss_3 = -1;
2197 ssid->vht_rx_mcs_nss_4 = -1;
2198 ssid->vht_rx_mcs_nss_5 = -1;
2199 ssid->vht_rx_mcs_nss_6 = -1;
2200 ssid->vht_rx_mcs_nss_7 = -1;
2201 ssid->vht_rx_mcs_nss_8 = -1;
2202 ssid->vht_tx_mcs_nss_1 = -1;
2203 ssid->vht_tx_mcs_nss_2 = -1;
2204 ssid->vht_tx_mcs_nss_3 = -1;
2205 ssid->vht_tx_mcs_nss_4 = -1;
2206 ssid->vht_tx_mcs_nss_5 = -1;
2207 ssid->vht_tx_mcs_nss_6 = -1;
2208 ssid->vht_tx_mcs_nss_7 = -1;
2209 ssid->vht_tx_mcs_nss_8 = -1;
2210#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002211 ssid->proactive_key_caching = -1;
2212#ifdef CONFIG_IEEE80211W
2213 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2214#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002215 ssid->mac_addr = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216}
2217
2218
2219/**
2220 * wpa_config_set - Set a variable in network configuration
2221 * @ssid: Pointer to network configuration data
2222 * @var: Variable name, e.g., "ssid"
2223 * @value: Variable value
2224 * @line: Line number in configuration file or 0 if not used
2225 * Returns: 0 on success, -1 on failure
2226 *
2227 * This function can be used to set network configuration variables based on
2228 * both the configuration file and management interface input. The value
2229 * parameter must be in the same format as the text-based configuration file is
2230 * using. For example, strings are using double quotation marks.
2231 */
2232int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2233 int line)
2234{
2235 size_t i;
2236 int ret = 0;
2237
2238 if (ssid == NULL || var == NULL || value == NULL)
2239 return -1;
2240
2241 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2242 const struct parse_data *field = &ssid_fields[i];
2243 if (os_strcmp(var, field->name) != 0)
2244 continue;
2245
2246 if (field->parser(field, ssid, line, value)) {
2247 if (line) {
2248 wpa_printf(MSG_ERROR, "Line %d: failed to "
2249 "parse %s '%s'.", line, var, value);
2250 }
2251 ret = -1;
2252 }
2253 break;
2254 }
2255 if (i == NUM_SSID_FIELDS) {
2256 if (line) {
2257 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2258 "'%s'.", line, var);
2259 }
2260 ret = -1;
2261 }
2262
2263 return ret;
2264}
2265
2266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2268 const char *value)
2269{
2270 size_t len;
2271 char *buf;
2272 int ret;
2273
2274 len = os_strlen(value);
2275 buf = os_malloc(len + 3);
2276 if (buf == NULL)
2277 return -1;
2278 buf[0] = '"';
2279 os_memcpy(buf + 1, value, len);
2280 buf[len + 1] = '"';
2281 buf[len + 2] = '\0';
2282 ret = wpa_config_set(ssid, var, buf, 0);
2283 os_free(buf);
2284 return ret;
2285}
2286
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288/**
2289 * wpa_config_get_all - Get all options from network configuration
2290 * @ssid: Pointer to network configuration data
2291 * @get_keys: Determines if keys/passwords will be included in returned list
2292 * (if they may be exported)
2293 * Returns: %NULL terminated list of all set keys and their values in the form
2294 * of [key1, val1, key2, val2, ... , NULL]
2295 *
2296 * This function can be used to get list of all configured network properties.
2297 * The caller is responsible for freeing the returned list and all its
2298 * elements.
2299 */
2300char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2301{
2302 const struct parse_data *field;
2303 char *key, *value;
2304 size_t i;
2305 char **props;
2306 int fields_num;
2307
2308 get_keys = get_keys && ssid->export_keys;
2309
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002310 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 if (!props)
2312 return NULL;
2313
2314 fields_num = 0;
2315 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2316 field = &ssid_fields[i];
2317 if (field->key_data && !get_keys)
2318 continue;
2319 value = field->writer(field, ssid);
2320 if (value == NULL)
2321 continue;
2322 if (os_strlen(value) == 0) {
2323 os_free(value);
2324 continue;
2325 }
2326
2327 key = os_strdup(field->name);
2328 if (key == NULL) {
2329 os_free(value);
2330 goto err;
2331 }
2332
2333 props[fields_num * 2] = key;
2334 props[fields_num * 2 + 1] = value;
2335
2336 fields_num++;
2337 }
2338
2339 return props;
2340
2341err:
2342 value = *props;
2343 while (value)
2344 os_free(value++);
2345 os_free(props);
2346 return NULL;
2347}
2348
2349
2350#ifndef NO_CONFIG_WRITE
2351/**
2352 * wpa_config_get - Get a variable in network configuration
2353 * @ssid: Pointer to network configuration data
2354 * @var: Variable name, e.g., "ssid"
2355 * Returns: Value of the variable or %NULL on failure
2356 *
2357 * This function can be used to get network configuration variables. The
2358 * returned value is a copy of the configuration variable in text format, i.e,.
2359 * the same format that the text-based configuration file and wpa_config_set()
2360 * are using for the value. The caller is responsible for freeing the returned
2361 * value.
2362 */
2363char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2364{
2365 size_t i;
2366
2367 if (ssid == NULL || var == NULL)
2368 return NULL;
2369
2370 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2371 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08002372 if (os_strcmp(var, field->name) == 0) {
2373 char *ret = field->writer(field, ssid);
Paul Stewart9c42c0b2016-03-18 18:14:26 -07002374 if (ret != NULL && (os_strchr(ret, '\r') != NULL ||
2375 os_strchr(ret, '\n') != NULL)) {
2376 wpa_printf(MSG_ERROR,
2377 "Found newline in value for %s; "
Paul Stewart85c72c62016-03-03 15:33:47 -08002378 "not returning it", var);
2379 os_free(ret);
2380 ret = NULL;
2381 }
2382 return ret;
2383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384 }
2385
2386 return NULL;
2387}
2388
2389
2390/**
2391 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2392 * @ssid: Pointer to network configuration data
2393 * @var: Variable name, e.g., "ssid"
2394 * Returns: Value of the variable or %NULL on failure
2395 *
2396 * This function can be used to get network configuration variable like
2397 * wpa_config_get(). The only difference is that this functions does not expose
2398 * key/password material from the configuration. In case a key/password field
2399 * is requested, the returned value is an empty string or %NULL if the variable
2400 * is not set or "*" if the variable is set (regardless of its value). The
2401 * returned value is a copy of the configuration variable in text format, i.e,.
2402 * the same format that the text-based configuration file and wpa_config_set()
2403 * are using for the value. The caller is responsible for freeing the returned
2404 * value.
2405 */
2406char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2407{
2408 size_t i;
2409
2410 if (ssid == NULL || var == NULL)
2411 return NULL;
2412
2413 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2414 const struct parse_data *field = &ssid_fields[i];
2415 if (os_strcmp(var, field->name) == 0) {
2416 char *res = field->writer(field, ssid);
2417 if (field->key_data) {
2418 if (res && res[0]) {
2419 wpa_printf(MSG_DEBUG, "Do not allow "
2420 "key_data field to be "
2421 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002422 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 return os_strdup("*");
2424 }
2425
2426 os_free(res);
2427 return NULL;
2428 }
2429 return res;
2430 }
2431 }
2432
2433 return NULL;
2434}
2435#endif /* NO_CONFIG_WRITE */
2436
2437
2438/**
2439 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2440 * @ssid: Pointer to network configuration data
2441 *
2442 * This function must be called to update WPA PSK when either SSID or the
2443 * passphrase has changed for the network configuration.
2444 */
2445void wpa_config_update_psk(struct wpa_ssid *ssid)
2446{
2447#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002448 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 ssid->psk, PMK_LEN);
2450 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2451 ssid->psk, PMK_LEN);
2452 ssid->psk_set = 1;
2453#endif /* CONFIG_NO_PBKDF2 */
2454}
2455
2456
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002457static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2458 const char *value)
2459{
2460 u8 *proto;
2461 int **port;
2462 int *ports, *nports;
2463 const char *pos;
2464 unsigned int num_ports;
2465
2466 proto = os_realloc_array(cred->req_conn_capab_proto,
2467 cred->num_req_conn_capab + 1, sizeof(u8));
2468 if (proto == NULL)
2469 return -1;
2470 cred->req_conn_capab_proto = proto;
2471
2472 port = os_realloc_array(cred->req_conn_capab_port,
2473 cred->num_req_conn_capab + 1, sizeof(int *));
2474 if (port == NULL)
2475 return -1;
2476 cred->req_conn_capab_port = port;
2477
2478 proto[cred->num_req_conn_capab] = atoi(value);
2479
2480 pos = os_strchr(value, ':');
2481 if (pos == NULL) {
2482 port[cred->num_req_conn_capab] = NULL;
2483 cred->num_req_conn_capab++;
2484 return 0;
2485 }
2486 pos++;
2487
2488 ports = NULL;
2489 num_ports = 0;
2490
2491 while (*pos) {
2492 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2493 if (nports == NULL) {
2494 os_free(ports);
2495 return -1;
2496 }
2497 ports = nports;
2498 ports[num_ports++] = atoi(pos);
2499
2500 pos = os_strchr(pos, ',');
2501 if (pos == NULL)
2502 break;
2503 pos++;
2504 }
2505
2506 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2507 if (nports == NULL) {
2508 os_free(ports);
2509 return -1;
2510 }
2511 ports = nports;
2512 ports[num_ports] = -1;
2513
2514 port[cred->num_req_conn_capab] = ports;
2515 cred->num_req_conn_capab++;
2516 return 0;
2517}
2518
2519
Dmitry Shmidt04949592012-07-19 12:16:46 -07002520int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2521 const char *value, int line)
2522{
2523 char *val;
2524 size_t len;
2525
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002526 if (os_strcmp(var, "temporary") == 0) {
2527 cred->temporary = atoi(value);
2528 return 0;
2529 }
2530
Dmitry Shmidt04949592012-07-19 12:16:46 -07002531 if (os_strcmp(var, "priority") == 0) {
2532 cred->priority = atoi(value);
2533 return 0;
2534 }
2535
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002536 if (os_strcmp(var, "sp_priority") == 0) {
2537 int prio = atoi(value);
2538 if (prio < 0 || prio > 255)
2539 return -1;
2540 cred->sp_priority = prio;
2541 return 0;
2542 }
2543
Dmitry Shmidt04949592012-07-19 12:16:46 -07002544 if (os_strcmp(var, "pcsc") == 0) {
2545 cred->pcsc = atoi(value);
2546 return 0;
2547 }
2548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002549 if (os_strcmp(var, "eap") == 0) {
2550 struct eap_method_type method;
2551 method.method = eap_peer_get_type(value, &method.vendor);
2552 if (method.vendor == EAP_VENDOR_IETF &&
2553 method.method == EAP_TYPE_NONE) {
2554 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2555 "for a credential", line, value);
2556 return -1;
2557 }
2558 os_free(cred->eap_method);
2559 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2560 if (cred->eap_method == NULL)
2561 return -1;
2562 os_memcpy(cred->eap_method, &method, sizeof(method));
2563 return 0;
2564 }
2565
2566 if (os_strcmp(var, "password") == 0 &&
2567 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002568 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002569 cred->password = os_strdup(value);
2570 cred->ext_password = 1;
2571 return 0;
2572 }
2573
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002574 if (os_strcmp(var, "update_identifier") == 0) {
2575 cred->update_identifier = atoi(value);
2576 return 0;
2577 }
2578
2579 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2580 cred->min_dl_bandwidth_home = atoi(value);
2581 return 0;
2582 }
2583
2584 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2585 cred->min_ul_bandwidth_home = atoi(value);
2586 return 0;
2587 }
2588
2589 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2590 cred->min_dl_bandwidth_roaming = atoi(value);
2591 return 0;
2592 }
2593
2594 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2595 cred->min_ul_bandwidth_roaming = atoi(value);
2596 return 0;
2597 }
2598
2599 if (os_strcmp(var, "max_bss_load") == 0) {
2600 cred->max_bss_load = atoi(value);
2601 return 0;
2602 }
2603
2604 if (os_strcmp(var, "req_conn_capab") == 0)
2605 return wpa_config_set_cred_req_conn_capab(cred, value);
2606
2607 if (os_strcmp(var, "ocsp") == 0) {
2608 cred->ocsp = atoi(value);
2609 return 0;
2610 }
2611
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07002612 if (os_strcmp(var, "sim_num") == 0) {
2613 cred->sim_num = atoi(value);
2614 return 0;
2615 }
2616
Dmitry Shmidt04949592012-07-19 12:16:46 -07002617 val = wpa_config_parse_string(value, &len);
2618 if (val == NULL) {
2619 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2620 "value '%s'.", line, var, value);
2621 return -1;
2622 }
2623
2624 if (os_strcmp(var, "realm") == 0) {
2625 os_free(cred->realm);
2626 cred->realm = val;
2627 return 0;
2628 }
2629
2630 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002631 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002632 cred->username = val;
2633 return 0;
2634 }
2635
2636 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002637 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002638 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002639 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002640 return 0;
2641 }
2642
2643 if (os_strcmp(var, "ca_cert") == 0) {
2644 os_free(cred->ca_cert);
2645 cred->ca_cert = val;
2646 return 0;
2647 }
2648
2649 if (os_strcmp(var, "client_cert") == 0) {
2650 os_free(cred->client_cert);
2651 cred->client_cert = val;
2652 return 0;
2653 }
2654
2655 if (os_strcmp(var, "private_key") == 0) {
2656 os_free(cred->private_key);
2657 cred->private_key = val;
2658 return 0;
2659 }
2660
2661 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002662 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002663 cred->private_key_passwd = val;
2664 return 0;
2665 }
2666
2667 if (os_strcmp(var, "imsi") == 0) {
2668 os_free(cred->imsi);
2669 cred->imsi = val;
2670 return 0;
2671 }
2672
2673 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002674 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002675 cred->milenage = val;
2676 return 0;
2677 }
2678
Dmitry Shmidt051af732013-10-22 13:52:46 -07002679 if (os_strcmp(var, "domain_suffix_match") == 0) {
2680 os_free(cred->domain_suffix_match);
2681 cred->domain_suffix_match = val;
2682 return 0;
2683 }
2684
Dmitry Shmidt04949592012-07-19 12:16:46 -07002685 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002686 char **new_domain;
2687 new_domain = os_realloc_array(cred->domain,
2688 cred->num_domain + 1,
2689 sizeof(char *));
2690 if (new_domain == NULL) {
2691 os_free(val);
2692 return -1;
2693 }
2694 new_domain[cred->num_domain++] = val;
2695 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002696 return 0;
2697 }
2698
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002699 if (os_strcmp(var, "phase1") == 0) {
2700 os_free(cred->phase1);
2701 cred->phase1 = val;
2702 return 0;
2703 }
2704
2705 if (os_strcmp(var, "phase2") == 0) {
2706 os_free(cred->phase2);
2707 cred->phase2 = val;
2708 return 0;
2709 }
2710
2711 if (os_strcmp(var, "roaming_consortium") == 0) {
2712 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2713 wpa_printf(MSG_ERROR, "Line %d: invalid "
2714 "roaming_consortium length %d (3..15 "
2715 "expected)", line, (int) len);
2716 os_free(val);
2717 return -1;
2718 }
2719 os_memcpy(cred->roaming_consortium, val, len);
2720 cred->roaming_consortium_len = len;
2721 os_free(val);
2722 return 0;
2723 }
2724
Dmitry Shmidt051af732013-10-22 13:52:46 -07002725 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2726 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2727 {
2728 wpa_printf(MSG_ERROR, "Line %d: invalid "
2729 "required_roaming_consortium length %d "
2730 "(3..15 expected)", line, (int) len);
2731 os_free(val);
2732 return -1;
2733 }
2734 os_memcpy(cred->required_roaming_consortium, val, len);
2735 cred->required_roaming_consortium_len = len;
2736 os_free(val);
2737 return 0;
2738 }
2739
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002740 if (os_strcmp(var, "excluded_ssid") == 0) {
2741 struct excluded_ssid *e;
2742
2743 if (len > MAX_SSID_LEN) {
2744 wpa_printf(MSG_ERROR, "Line %d: invalid "
2745 "excluded_ssid length %d", line, (int) len);
2746 os_free(val);
2747 return -1;
2748 }
2749
2750 e = os_realloc_array(cred->excluded_ssid,
2751 cred->num_excluded_ssid + 1,
2752 sizeof(struct excluded_ssid));
2753 if (e == NULL) {
2754 os_free(val);
2755 return -1;
2756 }
2757 cred->excluded_ssid = e;
2758
2759 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2760 os_memcpy(e->ssid, val, len);
2761 e->ssid_len = len;
2762
2763 os_free(val);
2764
2765 return 0;
2766 }
2767
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002768 if (os_strcmp(var, "roaming_partner") == 0) {
2769 struct roaming_partner *p;
2770 char *pos;
2771
2772 p = os_realloc_array(cred->roaming_partner,
2773 cred->num_roaming_partner + 1,
2774 sizeof(struct roaming_partner));
2775 if (p == NULL) {
2776 os_free(val);
2777 return -1;
2778 }
2779 cred->roaming_partner = p;
2780
2781 p = &cred->roaming_partner[cred->num_roaming_partner];
2782
2783 pos = os_strchr(val, ',');
2784 if (pos == NULL) {
2785 os_free(val);
2786 return -1;
2787 }
2788 *pos++ = '\0';
2789 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2790 os_free(val);
2791 return -1;
2792 }
2793 os_memcpy(p->fqdn, val, pos - val);
2794
2795 p->exact_match = atoi(pos);
2796
2797 pos = os_strchr(pos, ',');
2798 if (pos == NULL) {
2799 os_free(val);
2800 return -1;
2801 }
2802 *pos++ = '\0';
2803
2804 p->priority = atoi(pos);
2805
2806 pos = os_strchr(pos, ',');
2807 if (pos == NULL) {
2808 os_free(val);
2809 return -1;
2810 }
2811 *pos++ = '\0';
2812
2813 if (os_strlen(pos) >= sizeof(p->country)) {
2814 os_free(val);
2815 return -1;
2816 }
2817 os_memcpy(p->country, pos, os_strlen(pos) + 1);
2818
2819 cred->num_roaming_partner++;
2820 os_free(val);
2821
2822 return 0;
2823 }
2824
2825 if (os_strcmp(var, "provisioning_sp") == 0) {
2826 os_free(cred->provisioning_sp);
2827 cred->provisioning_sp = val;
2828 return 0;
2829 }
2830
Dmitry Shmidt04949592012-07-19 12:16:46 -07002831 if (line) {
2832 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2833 line, var);
2834 }
2835
2836 os_free(val);
2837
2838 return -1;
2839}
2840
2841
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002842static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002843{
2844 char *buf;
2845
2846 buf = os_malloc(20);
2847 if (buf == NULL)
2848 return NULL;
2849 os_snprintf(buf, 20, "%d", val);
2850 return buf;
2851}
2852
2853
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002854static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002855{
2856 if (str == NULL)
2857 return NULL;
2858 return os_strdup(str);
2859}
2860
2861
2862char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
2863{
2864 if (os_strcmp(var, "temporary") == 0)
2865 return alloc_int_str(cred->temporary);
2866
2867 if (os_strcmp(var, "priority") == 0)
2868 return alloc_int_str(cred->priority);
2869
2870 if (os_strcmp(var, "sp_priority") == 0)
2871 return alloc_int_str(cred->sp_priority);
2872
2873 if (os_strcmp(var, "pcsc") == 0)
2874 return alloc_int_str(cred->pcsc);
2875
2876 if (os_strcmp(var, "eap") == 0) {
2877 if (!cred->eap_method)
2878 return NULL;
2879 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
2880 cred->eap_method[0].method));
2881 }
2882
2883 if (os_strcmp(var, "update_identifier") == 0)
2884 return alloc_int_str(cred->update_identifier);
2885
2886 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
2887 return alloc_int_str(cred->min_dl_bandwidth_home);
2888
2889 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
2890 return alloc_int_str(cred->min_ul_bandwidth_home);
2891
2892 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
2893 return alloc_int_str(cred->min_dl_bandwidth_roaming);
2894
2895 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
2896 return alloc_int_str(cred->min_ul_bandwidth_roaming);
2897
2898 if (os_strcmp(var, "max_bss_load") == 0)
2899 return alloc_int_str(cred->max_bss_load);
2900
2901 if (os_strcmp(var, "req_conn_capab") == 0) {
2902 unsigned int i;
2903 char *buf, *end, *pos;
2904 int ret;
2905
2906 if (!cred->num_req_conn_capab)
2907 return NULL;
2908
2909 buf = os_malloc(4000);
2910 if (buf == NULL)
2911 return NULL;
2912 pos = buf;
2913 end = pos + 4000;
2914 for (i = 0; i < cred->num_req_conn_capab; i++) {
2915 int *ports;
2916
2917 ret = os_snprintf(pos, end - pos, "%s%u",
2918 i > 0 ? "\n" : "",
2919 cred->req_conn_capab_proto[i]);
2920 if (ret < 0 || ret >= end - pos)
2921 return buf;
2922 pos += ret;
2923
2924 ports = cred->req_conn_capab_port[i];
2925 if (ports) {
2926 int j;
2927 for (j = 0; ports[j] != -1; j++) {
2928 ret = os_snprintf(pos, end - pos,
2929 "%s%d",
2930 j > 0 ? "," : ":",
2931 ports[j]);
2932 if (ret < 0 || ret >= end - pos)
2933 return buf;
2934 pos += ret;
2935 }
2936 }
2937 }
2938
2939 return buf;
2940 }
2941
2942 if (os_strcmp(var, "ocsp") == 0)
2943 return alloc_int_str(cred->ocsp);
2944
2945 if (os_strcmp(var, "realm") == 0)
2946 return alloc_strdup(cred->realm);
2947
2948 if (os_strcmp(var, "username") == 0)
2949 return alloc_strdup(cred->username);
2950
2951 if (os_strcmp(var, "password") == 0) {
2952 if (!cred->password)
2953 return NULL;
2954 return alloc_strdup("*");
2955 }
2956
2957 if (os_strcmp(var, "ca_cert") == 0)
2958 return alloc_strdup(cred->ca_cert);
2959
2960 if (os_strcmp(var, "client_cert") == 0)
2961 return alloc_strdup(cred->client_cert);
2962
2963 if (os_strcmp(var, "private_key") == 0)
2964 return alloc_strdup(cred->private_key);
2965
2966 if (os_strcmp(var, "private_key_passwd") == 0) {
2967 if (!cred->private_key_passwd)
2968 return NULL;
2969 return alloc_strdup("*");
2970 }
2971
2972 if (os_strcmp(var, "imsi") == 0)
2973 return alloc_strdup(cred->imsi);
2974
2975 if (os_strcmp(var, "milenage") == 0) {
2976 if (!(cred->milenage))
2977 return NULL;
2978 return alloc_strdup("*");
2979 }
2980
2981 if (os_strcmp(var, "domain_suffix_match") == 0)
2982 return alloc_strdup(cred->domain_suffix_match);
2983
2984 if (os_strcmp(var, "domain") == 0) {
2985 unsigned int i;
2986 char *buf, *end, *pos;
2987 int ret;
2988
2989 if (!cred->num_domain)
2990 return NULL;
2991
2992 buf = os_malloc(4000);
2993 if (buf == NULL)
2994 return NULL;
2995 pos = buf;
2996 end = pos + 4000;
2997
2998 for (i = 0; i < cred->num_domain; i++) {
2999 ret = os_snprintf(pos, end - pos, "%s%s",
3000 i > 0 ? "\n" : "", cred->domain[i]);
3001 if (ret < 0 || ret >= end - pos)
3002 return buf;
3003 pos += ret;
3004 }
3005
3006 return buf;
3007 }
3008
3009 if (os_strcmp(var, "phase1") == 0)
3010 return alloc_strdup(cred->phase1);
3011
3012 if (os_strcmp(var, "phase2") == 0)
3013 return alloc_strdup(cred->phase2);
3014
3015 if (os_strcmp(var, "roaming_consortium") == 0) {
3016 size_t buflen;
3017 char *buf;
3018
3019 if (!cred->roaming_consortium_len)
3020 return NULL;
3021 buflen = cred->roaming_consortium_len * 2 + 1;
3022 buf = os_malloc(buflen);
3023 if (buf == NULL)
3024 return NULL;
3025 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3026 cred->roaming_consortium_len);
3027 return buf;
3028 }
3029
3030 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3031 size_t buflen;
3032 char *buf;
3033
3034 if (!cred->required_roaming_consortium_len)
3035 return NULL;
3036 buflen = cred->required_roaming_consortium_len * 2 + 1;
3037 buf = os_malloc(buflen);
3038 if (buf == NULL)
3039 return NULL;
3040 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3041 cred->required_roaming_consortium_len);
3042 return buf;
3043 }
3044
3045 if (os_strcmp(var, "excluded_ssid") == 0) {
3046 unsigned int i;
3047 char *buf, *end, *pos;
3048
3049 if (!cred->num_excluded_ssid)
3050 return NULL;
3051
3052 buf = os_malloc(4000);
3053 if (buf == NULL)
3054 return NULL;
3055 pos = buf;
3056 end = pos + 4000;
3057
3058 for (i = 0; i < cred->num_excluded_ssid; i++) {
3059 struct excluded_ssid *e;
3060 int ret;
3061
3062 e = &cred->excluded_ssid[i];
3063 ret = os_snprintf(pos, end - pos, "%s%s",
3064 i > 0 ? "\n" : "",
3065 wpa_ssid_txt(e->ssid, e->ssid_len));
3066 if (ret < 0 || ret >= end - pos)
3067 return buf;
3068 pos += ret;
3069 }
3070
3071 return buf;
3072 }
3073
3074 if (os_strcmp(var, "roaming_partner") == 0) {
3075 unsigned int i;
3076 char *buf, *end, *pos;
3077
3078 if (!cred->num_roaming_partner)
3079 return NULL;
3080
3081 buf = os_malloc(4000);
3082 if (buf == NULL)
3083 return NULL;
3084 pos = buf;
3085 end = pos + 4000;
3086
3087 for (i = 0; i < cred->num_roaming_partner; i++) {
3088 struct roaming_partner *p;
3089 int ret;
3090
3091 p = &cred->roaming_partner[i];
3092 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3093 i > 0 ? "\n" : "",
3094 p->fqdn, p->exact_match, p->priority,
3095 p->country);
3096 if (ret < 0 || ret >= end - pos)
3097 return buf;
3098 pos += ret;
3099 }
3100
3101 return buf;
3102 }
3103
3104 if (os_strcmp(var, "provisioning_sp") == 0)
3105 return alloc_strdup(cred->provisioning_sp);
3106
3107 return NULL;
3108}
3109
3110
Dmitry Shmidt04949592012-07-19 12:16:46 -07003111struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3112{
3113 struct wpa_cred *cred;
3114
3115 cred = config->cred;
3116 while (cred) {
3117 if (id == cred->id)
3118 break;
3119 cred = cred->next;
3120 }
3121
3122 return cred;
3123}
3124
3125
3126struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3127{
3128 int id;
3129 struct wpa_cred *cred, *last = NULL;
3130
3131 id = -1;
3132 cred = config->cred;
3133 while (cred) {
3134 if (cred->id > id)
3135 id = cred->id;
3136 last = cred;
3137 cred = cred->next;
3138 }
3139 id++;
3140
3141 cred = os_zalloc(sizeof(*cred));
3142 if (cred == NULL)
3143 return NULL;
3144 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003145 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003146 if (last)
3147 last->next = cred;
3148 else
3149 config->cred = cred;
3150
3151 return cred;
3152}
3153
3154
3155int wpa_config_remove_cred(struct wpa_config *config, int id)
3156{
3157 struct wpa_cred *cred, *prev = NULL;
3158
3159 cred = config->cred;
3160 while (cred) {
3161 if (id == cred->id)
3162 break;
3163 prev = cred;
3164 cred = cred->next;
3165 }
3166
3167 if (cred == NULL)
3168 return -1;
3169
3170 if (prev)
3171 prev->next = cred->next;
3172 else
3173 config->cred = cred->next;
3174
3175 wpa_config_free_cred(cred);
3176 return 0;
3177}
3178
3179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180#ifndef CONFIG_NO_CONFIG_BLOBS
3181/**
3182 * wpa_config_get_blob - Get a named configuration blob
3183 * @config: Configuration data from wpa_config_read()
3184 * @name: Name of the blob
3185 * Returns: Pointer to blob data or %NULL if not found
3186 */
3187const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3188 const char *name)
3189{
3190 struct wpa_config_blob *blob = config->blobs;
3191
3192 while (blob) {
3193 if (os_strcmp(blob->name, name) == 0)
3194 return blob;
3195 blob = blob->next;
3196 }
3197 return NULL;
3198}
3199
3200
3201/**
3202 * wpa_config_set_blob - Set or add a named configuration blob
3203 * @config: Configuration data from wpa_config_read()
3204 * @blob: New value for the blob
3205 *
3206 * Adds a new configuration blob or replaces the current value of an existing
3207 * blob.
3208 */
3209void wpa_config_set_blob(struct wpa_config *config,
3210 struct wpa_config_blob *blob)
3211{
3212 wpa_config_remove_blob(config, blob->name);
3213 blob->next = config->blobs;
3214 config->blobs = blob;
3215}
3216
3217
3218/**
3219 * wpa_config_free_blob - Free blob data
3220 * @blob: Pointer to blob to be freed
3221 */
3222void wpa_config_free_blob(struct wpa_config_blob *blob)
3223{
3224 if (blob) {
3225 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003226 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 os_free(blob);
3228 }
3229}
3230
3231
3232/**
3233 * wpa_config_remove_blob - Remove a named configuration blob
3234 * @config: Configuration data from wpa_config_read()
3235 * @name: Name of the blob to remove
3236 * Returns: 0 if blob was removed or -1 if blob was not found
3237 */
3238int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3239{
3240 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3241
3242 while (pos) {
3243 if (os_strcmp(pos->name, name) == 0) {
3244 if (prev)
3245 prev->next = pos->next;
3246 else
3247 config->blobs = pos->next;
3248 wpa_config_free_blob(pos);
3249 return 0;
3250 }
3251 prev = pos;
3252 pos = pos->next;
3253 }
3254
3255 return -1;
3256}
3257#endif /* CONFIG_NO_CONFIG_BLOBS */
3258
3259
3260/**
3261 * wpa_config_alloc_empty - Allocate an empty configuration
3262 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3263 * socket
3264 * @driver_param: Driver parameters
3265 * Returns: Pointer to allocated configuration data or %NULL on failure
3266 */
3267struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3268 const char *driver_param)
3269{
3270 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003271 const int aCWmin = 4, aCWmax = 10;
3272 const struct hostapd_wmm_ac_params ac_bk =
3273 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3274 const struct hostapd_wmm_ac_params ac_be =
3275 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3276 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3277 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3278 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3279 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280
3281 config = os_zalloc(sizeof(*config));
3282 if (config == NULL)
3283 return NULL;
3284 config->eapol_version = DEFAULT_EAPOL_VERSION;
3285 config->ap_scan = DEFAULT_AP_SCAN;
3286 config->fast_reauth = DEFAULT_FAST_REAUTH;
3287 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3288 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003289 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003290 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3292 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3293 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3294 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003295 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003296 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003297 config->wmm_ac_params[0] = ac_be;
3298 config->wmm_ac_params[1] = ac_bk;
3299 config->wmm_ac_params[2] = ac_vi;
3300 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003301 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003302 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303
3304 if (ctrl_interface)
3305 config->ctrl_interface = os_strdup(ctrl_interface);
3306 if (driver_param)
3307 config->driver_param = os_strdup(driver_param);
3308
3309 return config;
3310}
3311
3312
3313#ifndef CONFIG_NO_STDOUT_DEBUG
3314/**
3315 * wpa_config_debug_dump_networks - Debug dump of configured networks
3316 * @config: Configuration data from wpa_config_read()
3317 */
3318void wpa_config_debug_dump_networks(struct wpa_config *config)
3319{
3320 int prio;
3321 struct wpa_ssid *ssid;
3322
3323 for (prio = 0; prio < config->num_prio; prio++) {
3324 ssid = config->pssid[prio];
3325 wpa_printf(MSG_DEBUG, "Priority group %d",
3326 ssid->priority);
3327 while (ssid) {
3328 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3329 ssid->id,
3330 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3331 ssid = ssid->pnext;
3332 }
3333 }
3334}
3335#endif /* CONFIG_NO_STDOUT_DEBUG */
3336
3337
3338struct global_parse_data {
3339 char *name;
3340 int (*parser)(const struct global_parse_data *data,
3341 struct wpa_config *config, int line, const char *value);
3342 void *param1, *param2, *param3;
3343 unsigned int changed_flag;
3344};
3345
3346
3347static int wpa_global_config_parse_int(const struct global_parse_data *data,
3348 struct wpa_config *config, int line,
3349 const char *pos)
3350{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003351 int val, *dst;
3352 char *end;
3353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003355 val = strtol(pos, &end, 0);
3356 if (*end) {
3357 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3358 line, pos);
3359 return -1;
3360 }
3361 *dst = val;
3362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3364
3365 if (data->param2 && *dst < (long) data->param2) {
3366 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3367 "min_value=%ld)", line, data->name, *dst,
3368 (long) data->param2);
3369 *dst = (long) data->param2;
3370 return -1;
3371 }
3372
3373 if (data->param3 && *dst > (long) data->param3) {
3374 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3375 "max_value=%ld)", line, data->name, *dst,
3376 (long) data->param3);
3377 *dst = (long) data->param3;
3378 return -1;
3379 }
3380
3381 return 0;
3382}
3383
3384
3385static int wpa_global_config_parse_str(const struct global_parse_data *data,
3386 struct wpa_config *config, int line,
3387 const char *pos)
3388{
3389 size_t len;
3390 char **dst, *tmp;
3391
3392 len = os_strlen(pos);
3393 if (data->param2 && len < (size_t) data->param2) {
3394 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3395 "min_len=%ld)", line, data->name,
3396 (unsigned long) len, (long) data->param2);
3397 return -1;
3398 }
3399
3400 if (data->param3 && len > (size_t) data->param3) {
3401 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3402 "max_len=%ld)", line, data->name,
3403 (unsigned long) len, (long) data->param3);
3404 return -1;
3405 }
3406
3407 tmp = os_strdup(pos);
3408 if (tmp == NULL)
3409 return -1;
3410
3411 dst = (char **) (((u8 *) config) + (long) data->param1);
3412 os_free(*dst);
3413 *dst = tmp;
3414 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3415
3416 return 0;
3417}
3418
3419
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003420static int wpa_config_process_bgscan(const struct global_parse_data *data,
3421 struct wpa_config *config, int line,
3422 const char *pos)
3423{
3424 size_t len;
3425 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003426 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003427
3428 tmp = wpa_config_parse_string(pos, &len);
3429 if (tmp == NULL) {
3430 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3431 line, data->name);
3432 return -1;
3433 }
3434
Dmitry Shmidt97672262014-02-03 13:02:54 -08003435 res = wpa_global_config_parse_str(data, config, line, tmp);
3436 os_free(tmp);
3437 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003438}
3439
3440
Dmitry Shmidt04949592012-07-19 12:16:46 -07003441static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3442 struct wpa_config *config, int line,
3443 const char *pos)
3444{
3445 size_t len;
3446 struct wpabuf **dst, *tmp;
3447
3448 len = os_strlen(pos);
3449 if (len & 0x01)
3450 return -1;
3451
3452 tmp = wpabuf_alloc(len / 2);
3453 if (tmp == NULL)
3454 return -1;
3455
3456 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3457 wpabuf_free(tmp);
3458 return -1;
3459 }
3460
3461 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3462 wpabuf_free(*dst);
3463 *dst = tmp;
3464 wpa_printf(MSG_DEBUG, "%s", data->name);
3465
3466 return 0;
3467}
3468
3469
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003470static int wpa_config_process_freq_list(const struct global_parse_data *data,
3471 struct wpa_config *config, int line,
3472 const char *value)
3473{
3474 int *freqs;
3475
3476 freqs = wpa_config_parse_int_array(value);
3477 if (freqs == NULL)
3478 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003479 if (freqs[0] == 0) {
3480 os_free(freqs);
3481 freqs = NULL;
3482 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003483 os_free(config->freq_list);
3484 config->freq_list = freqs;
3485 return 0;
3486}
3487
3488
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003489#ifdef CONFIG_P2P
3490static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3491 struct wpa_config *config, int line,
3492 const char *pos)
3493{
3494 u32 *dst;
3495 struct hostapd_ip_addr addr;
3496
3497 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3498 return -1;
3499 if (addr.af != AF_INET)
3500 return -1;
3501
3502 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3503 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3504 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3505 WPA_GET_BE32((u8 *) dst));
3506
3507 return 0;
3508}
3509#endif /* CONFIG_P2P */
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512static int wpa_config_process_country(const struct global_parse_data *data,
3513 struct wpa_config *config, int line,
3514 const char *pos)
3515{
3516 if (!pos[0] || !pos[1]) {
3517 wpa_printf(MSG_DEBUG, "Invalid country set");
3518 return -1;
3519 }
3520 config->country[0] = pos[0];
3521 config->country[1] = pos[1];
3522 wpa_printf(MSG_DEBUG, "country='%c%c'",
3523 config->country[0], config->country[1]);
3524 return 0;
3525}
3526
3527
3528static int wpa_config_process_load_dynamic_eap(
3529 const struct global_parse_data *data, struct wpa_config *config,
3530 int line, const char *so)
3531{
3532 int ret;
3533 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3534 ret = eap_peer_method_load(so);
3535 if (ret == -2) {
3536 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3537 "reloading.");
3538 } else if (ret) {
3539 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3540 "method '%s'.", line, so);
3541 return -1;
3542 }
3543
3544 return 0;
3545}
3546
3547
3548#ifdef CONFIG_WPS
3549
3550static int wpa_config_process_uuid(const struct global_parse_data *data,
3551 struct wpa_config *config, int line,
3552 const char *pos)
3553{
3554 char buf[40];
3555 if (uuid_str2bin(pos, config->uuid)) {
3556 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3557 return -1;
3558 }
3559 uuid_bin2str(config->uuid, buf, sizeof(buf));
3560 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3561 return 0;
3562}
3563
3564
3565static int wpa_config_process_device_type(
3566 const struct global_parse_data *data,
3567 struct wpa_config *config, int line, const char *pos)
3568{
3569 return wps_dev_type_str2bin(pos, config->device_type);
3570}
3571
3572
3573static int wpa_config_process_os_version(const struct global_parse_data *data,
3574 struct wpa_config *config, int line,
3575 const char *pos)
3576{
3577 if (hexstr2bin(pos, config->os_version, 4)) {
3578 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3579 return -1;
3580 }
3581 wpa_printf(MSG_DEBUG, "os_version=%08x",
3582 WPA_GET_BE32(config->os_version));
3583 return 0;
3584}
3585
Dmitry Shmidt04949592012-07-19 12:16:46 -07003586
3587static int wpa_config_process_wps_vendor_ext_m1(
3588 const struct global_parse_data *data,
3589 struct wpa_config *config, int line, const char *pos)
3590{
3591 struct wpabuf *tmp;
3592 int len = os_strlen(pos) / 2;
3593 u8 *p;
3594
3595 if (!len) {
3596 wpa_printf(MSG_ERROR, "Line %d: "
3597 "invalid wps_vendor_ext_m1", line);
3598 return -1;
3599 }
3600
3601 tmp = wpabuf_alloc(len);
3602 if (tmp) {
3603 p = wpabuf_put(tmp, len);
3604
3605 if (hexstr2bin(pos, p, len)) {
3606 wpa_printf(MSG_ERROR, "Line %d: "
3607 "invalid wps_vendor_ext_m1", line);
3608 wpabuf_free(tmp);
3609 return -1;
3610 }
3611
3612 wpabuf_free(config->wps_vendor_ext_m1);
3613 config->wps_vendor_ext_m1 = tmp;
3614 } else {
3615 wpa_printf(MSG_ERROR, "Can not allocate "
3616 "memory for wps_vendor_ext_m1");
3617 return -1;
3618 }
3619
3620 return 0;
3621}
3622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623#endif /* CONFIG_WPS */
3624
3625#ifdef CONFIG_P2P
3626static int wpa_config_process_sec_device_type(
3627 const struct global_parse_data *data,
3628 struct wpa_config *config, int line, const char *pos)
3629{
3630 int idx;
3631
3632 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3633 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3634 "items", line);
3635 return -1;
3636 }
3637
3638 idx = config->num_sec_device_types;
3639
3640 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3641 return -1;
3642
3643 config->num_sec_device_types++;
3644 return 0;
3645}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003646
3647
3648static int wpa_config_process_p2p_pref_chan(
3649 const struct global_parse_data *data,
3650 struct wpa_config *config, int line, const char *pos)
3651{
3652 struct p2p_channel *pref = NULL, *n;
3653 unsigned int num = 0;
3654 const char *pos2;
3655 u8 op_class, chan;
3656
3657 /* format: class:chan,class:chan,... */
3658
3659 while (*pos) {
3660 op_class = atoi(pos);
3661 pos2 = os_strchr(pos, ':');
3662 if (pos2 == NULL)
3663 goto fail;
3664 pos2++;
3665 chan = atoi(pos2);
3666
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003667 n = os_realloc_array(pref, num + 1,
3668 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003669 if (n == NULL)
3670 goto fail;
3671 pref = n;
3672 pref[num].op_class = op_class;
3673 pref[num].chan = chan;
3674 num++;
3675
3676 pos = os_strchr(pos2, ',');
3677 if (pos == NULL)
3678 break;
3679 pos++;
3680 }
3681
3682 os_free(config->p2p_pref_chan);
3683 config->p2p_pref_chan = pref;
3684 config->num_p2p_pref_chan = num;
3685 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3686 (u8 *) config->p2p_pref_chan,
3687 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3688
3689 return 0;
3690
3691fail:
3692 os_free(pref);
3693 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3694 return -1;
3695}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003696
3697
3698static int wpa_config_process_p2p_no_go_freq(
3699 const struct global_parse_data *data,
3700 struct wpa_config *config, int line, const char *pos)
3701{
3702 int ret;
3703
3704 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3705 if (ret < 0) {
3706 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3707 return -1;
3708 }
3709
3710 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3711 config->p2p_no_go_freq.num);
3712
3713 return 0;
3714}
3715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716#endif /* CONFIG_P2P */
3717
3718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003719static int wpa_config_process_hessid(
3720 const struct global_parse_data *data,
3721 struct wpa_config *config, int line, const char *pos)
3722{
3723 if (hwaddr_aton2(pos, config->hessid) < 0) {
3724 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3725 line, pos);
3726 return -1;
3727 }
3728
3729 return 0;
3730}
3731
3732
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003733static int wpa_config_process_sae_groups(
3734 const struct global_parse_data *data,
3735 struct wpa_config *config, int line, const char *pos)
3736{
3737 int *groups = wpa_config_parse_int_array(pos);
3738 if (groups == NULL) {
3739 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3740 line, pos);
3741 return -1;
3742 }
3743
3744 os_free(config->sae_groups);
3745 config->sae_groups = groups;
3746
3747 return 0;
3748}
3749
3750
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003751static int wpa_config_process_ap_vendor_elements(
3752 const struct global_parse_data *data,
3753 struct wpa_config *config, int line, const char *pos)
3754{
3755 struct wpabuf *tmp;
3756 int len = os_strlen(pos) / 2;
3757 u8 *p;
3758
3759 if (!len) {
3760 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3761 line);
3762 return -1;
3763 }
3764
3765 tmp = wpabuf_alloc(len);
3766 if (tmp) {
3767 p = wpabuf_put(tmp, len);
3768
3769 if (hexstr2bin(pos, p, len)) {
3770 wpa_printf(MSG_ERROR, "Line %d: invalid "
3771 "ap_vendor_elements", line);
3772 wpabuf_free(tmp);
3773 return -1;
3774 }
3775
3776 wpabuf_free(config->ap_vendor_elements);
3777 config->ap_vendor_elements = tmp;
3778 } else {
3779 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3780 "ap_vendor_elements");
3781 return -1;
3782 }
3783
3784 return 0;
3785}
3786
3787
Dmitry Shmidt56052862013-10-04 10:23:25 -07003788#ifdef CONFIG_CTRL_IFACE
3789static int wpa_config_process_no_ctrl_interface(
3790 const struct global_parse_data *data,
3791 struct wpa_config *config, int line, const char *pos)
3792{
3793 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3794 os_free(config->ctrl_interface);
3795 config->ctrl_interface = NULL;
3796 return 0;
3797}
3798#endif /* CONFIG_CTRL_IFACE */
3799
3800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801#ifdef OFFSET
3802#undef OFFSET
3803#endif /* OFFSET */
3804/* OFFSET: Get offset of a variable within the wpa_config structure */
3805#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3806
3807#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3808#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3809#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3810#define INT(f) _INT(f), NULL, NULL
3811#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3812#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3813#define STR(f) _STR(f), NULL, NULL
3814#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07003815#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003816#define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817
3818static const struct global_parse_data global_fields[] = {
3819#ifdef CONFIG_CTRL_IFACE
3820 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003821 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822 { STR(ctrl_interface_group), 0 } /* deprecated */,
3823#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003824#ifdef CONFIG_MACSEC
3825 { INT_RANGE(eapol_version, 1, 3), 0 },
3826#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003828#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003830 { FUNC(bgscan), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003831 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 { INT(fast_reauth), 0 },
3833 { STR(opensc_engine_path), 0 },
3834 { STR(pkcs11_engine_path), 0 },
3835 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003836 { STR(pcsc_reader), 0 },
3837 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07003838 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 { STR(driver_param), 0 },
3840 { INT(dot11RSNAConfigPMKLifetime), 0 },
3841 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3842 { INT(dot11RSNAConfigSATimeout), 0 },
3843#ifndef CONFIG_NO_CONFIG_WRITE
3844 { INT(update_config), 0 },
3845#endif /* CONFIG_NO_CONFIG_WRITE */
3846 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3847#ifdef CONFIG_WPS
3848 { FUNC(uuid), CFG_CHANGED_UUID },
3849 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3850 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3851 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3852 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3853 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3854 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3855 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3856 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3857 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003858 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859#endif /* CONFIG_WPS */
3860#ifdef CONFIG_P2P
3861 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3862 { INT(p2p_listen_reg_class), 0 },
3863 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003864 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3865 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3867 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3868 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3869 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3870 { INT(p2p_group_idle), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003871 { INT_RANGE(p2p_passphrase_len, 8, 63),
3872 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003873 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003874 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
3875 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003876 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003877 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003878 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003879 { INT(p2p_disabled), 0 },
3880 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003881 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003882 { IPV4(ip_addr_go), 0 },
3883 { IPV4(ip_addr_mask), 0 },
3884 { IPV4(ip_addr_start), 0 },
3885 { IPV4(ip_addr_end), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886#endif /* CONFIG_P2P */
3887 { FUNC(country), CFG_CHANGED_COUNTRY },
3888 { INT(bss_max_count), 0 },
3889 { INT(bss_expiration_age), 0 },
3890 { INT(bss_expiration_scan_count), 0 },
3891 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003894 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895#ifdef CONFIG_HS20
3896 { INT_RANGE(hs20, 0, 1), 0 },
3897#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003898 { INT_RANGE(interworking, 0, 1), 0 },
3899 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003900 { INT_RANGE(access_network_type, 0, 15), 0 },
3901 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3902 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003903 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3904 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3905 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3906 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3907 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003908 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3909 { INT(p2p_go_max_inactivity), 0 },
3910 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003911 { INT(okc), 0 },
3912 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003913 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003914 { INT(dtim_period), 0 },
3915 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003916 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003917 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003918 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003919 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003920 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003921 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003922 { STR(osu_dir), 0 },
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003923 { STR(wowlan_triggers), 0 },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07003924 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003925 { INT(mac_addr), 0 },
3926 { INT(rand_addr_lifetime), 0 },
3927 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928};
3929
3930#undef FUNC
3931#undef _INT
3932#undef INT
3933#undef INT_RANGE
3934#undef _STR
3935#undef STR
3936#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07003937#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003938#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003939#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940
3941
3942int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3943{
3944 size_t i;
3945 int ret = 0;
3946
3947 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3948 const struct global_parse_data *field = &global_fields[i];
3949 size_t flen = os_strlen(field->name);
3950 if (os_strncmp(pos, field->name, flen) != 0 ||
3951 pos[flen] != '=')
3952 continue;
3953
3954 if (field->parser(field, config, line, pos + flen + 1)) {
3955 wpa_printf(MSG_ERROR, "Line %d: failed to "
3956 "parse '%s'.", line, pos);
3957 ret = -1;
3958 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003959 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3960 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961 config->changed_parameters |= field->changed_flag;
3962 break;
3963 }
3964 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003965#ifdef CONFIG_AP
3966 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3967 char *tmp = os_strchr(pos, '=');
3968 if (tmp == NULL) {
3969 if (line < 0)
3970 return -1;
3971 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3972 "'%s'", line, pos);
3973 return -1;
3974 }
3975 *tmp++ = '\0';
3976 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3977 tmp)) {
3978 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3979 "AC item", line);
3980 return -1;
3981 }
3982 }
3983#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 if (line < 0)
3985 return -1;
3986 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3987 line, pos);
3988 ret = -1;
3989 }
3990
3991 return ret;
3992}