blob: 74283eb756dbf583bdf141cae980a9b304cb5abb [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) {
289 os_free(ssid->passphrase);
290 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;
325 os_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
344 os_free(ssid->passphrase);
345 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{
438 int first = 1, ret;
439 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) {
447 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
448 if (ret < 0 || ret >= end - pos)
449 return buf;
450 pos += ret;
451 first = 0;
452 }
453
454 if (ssid->proto & WPA_PROTO_RSN) {
455 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
456 if (ret < 0 || ret >= end - pos)
457 return buf;
458 pos += ret;
459 first = 0;
460 }
461
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800462 if (ssid->proto & WPA_PROTO_OSEN) {
463 ret = os_snprintf(pos, end - pos, "%sOSEN", first ? "" : " ");
464 if (ret < 0 || ret >= end - pos)
465 return buf;
466 pos += ret;
467 first = 0;
468 }
469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 return buf;
471}
472#endif /* NO_CONFIG_WRITE */
473
474
475static int wpa_config_parse_key_mgmt(const struct parse_data *data,
476 struct wpa_ssid *ssid, int line,
477 const char *value)
478{
479 int val = 0, last, errors = 0;
480 char *start, *end, *buf;
481
482 buf = os_strdup(value);
483 if (buf == NULL)
484 return -1;
485 start = buf;
486
487 while (*start != '\0') {
488 while (*start == ' ' || *start == '\t')
489 start++;
490 if (*start == '\0')
491 break;
492 end = start;
493 while (*end != ' ' && *end != '\t' && *end != '\0')
494 end++;
495 last = *end == '\0';
496 *end = '\0';
497 if (os_strcmp(start, "WPA-PSK") == 0)
498 val |= WPA_KEY_MGMT_PSK;
499 else if (os_strcmp(start, "WPA-EAP") == 0)
500 val |= WPA_KEY_MGMT_IEEE8021X;
501 else if (os_strcmp(start, "IEEE8021X") == 0)
502 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
503 else if (os_strcmp(start, "NONE") == 0)
504 val |= WPA_KEY_MGMT_NONE;
505 else if (os_strcmp(start, "WPA-NONE") == 0)
506 val |= WPA_KEY_MGMT_WPA_NONE;
507#ifdef CONFIG_IEEE80211R
508 else if (os_strcmp(start, "FT-PSK") == 0)
509 val |= WPA_KEY_MGMT_FT_PSK;
510 else if (os_strcmp(start, "FT-EAP") == 0)
511 val |= WPA_KEY_MGMT_FT_IEEE8021X;
512#endif /* CONFIG_IEEE80211R */
513#ifdef CONFIG_IEEE80211W
514 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
515 val |= WPA_KEY_MGMT_PSK_SHA256;
516 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
517 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
518#endif /* CONFIG_IEEE80211W */
519#ifdef CONFIG_WPS
520 else if (os_strcmp(start, "WPS") == 0)
521 val |= WPA_KEY_MGMT_WPS;
522#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800523#ifdef CONFIG_SAE
524 else if (os_strcmp(start, "SAE") == 0)
525 val |= WPA_KEY_MGMT_SAE;
526 else if (os_strcmp(start, "FT-SAE") == 0)
527 val |= WPA_KEY_MGMT_FT_SAE;
528#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800529#ifdef CONFIG_HS20
530 else if (os_strcmp(start, "OSEN") == 0)
531 val |= WPA_KEY_MGMT_OSEN;
532#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 else {
534 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
535 line, start);
536 errors++;
537 }
538
539 if (last)
540 break;
541 start = end + 1;
542 }
543 os_free(buf);
544
545 if (val == 0) {
546 wpa_printf(MSG_ERROR,
547 "Line %d: no key_mgmt values configured.", line);
548 errors++;
549 }
550
551 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
552 ssid->key_mgmt = val;
553 return errors ? -1 : 0;
554}
555
556
557#ifndef NO_CONFIG_WRITE
558static char * wpa_config_write_key_mgmt(const struct parse_data *data,
559 struct wpa_ssid *ssid)
560{
561 char *buf, *pos, *end;
562 int ret;
563
Dmitry Shmidt96571392013-10-14 12:54:46 -0700564 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 if (buf == NULL)
566 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700567 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568
569 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
570 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
571 pos == buf ? "" : " ");
572 if (ret < 0 || ret >= end - pos) {
573 end[-1] = '\0';
574 return buf;
575 }
576 pos += ret;
577 }
578
579 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
580 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
581 pos == buf ? "" : " ");
582 if (ret < 0 || ret >= end - pos) {
583 end[-1] = '\0';
584 return buf;
585 }
586 pos += ret;
587 }
588
589 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
590 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
591 pos == buf ? "" : " ");
592 if (ret < 0 || ret >= end - pos) {
593 end[-1] = '\0';
594 return buf;
595 }
596 pos += ret;
597 }
598
599 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
600 ret = os_snprintf(pos, end - pos, "%sNONE",
601 pos == buf ? "" : " ");
602 if (ret < 0 || ret >= end - pos) {
603 end[-1] = '\0';
604 return buf;
605 }
606 pos += ret;
607 }
608
609 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
610 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
611 pos == buf ? "" : " ");
612 if (ret < 0 || ret >= end - pos) {
613 end[-1] = '\0';
614 return buf;
615 }
616 pos += ret;
617 }
618
619#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700620 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
621 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
622 pos == buf ? "" : " ");
623 if (ret < 0 || ret >= end - pos) {
624 end[-1] = '\0';
625 return buf;
626 }
627 pos += ret;
628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629
Dmitry Shmidt96571392013-10-14 12:54:46 -0700630 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
631 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
632 pos == buf ? "" : " ");
633 if (ret < 0 || ret >= end - pos) {
634 end[-1] = '\0';
635 return buf;
636 }
637 pos += ret;
638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639#endif /* CONFIG_IEEE80211R */
640
641#ifdef CONFIG_IEEE80211W
Dmitry Shmidt96571392013-10-14 12:54:46 -0700642 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
643 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
644 pos == buf ? "" : " ");
645 if (ret < 0 || ret >= end - pos) {
646 end[-1] = '\0';
647 return buf;
648 }
649 pos += ret;
650 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651
Dmitry Shmidt96571392013-10-14 12:54:46 -0700652 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
653 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
654 pos == buf ? "" : " ");
655 if (ret < 0 || ret >= end - pos) {
656 end[-1] = '\0';
657 return buf;
658 }
659 pos += ret;
660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661#endif /* CONFIG_IEEE80211W */
662
663#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700664 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
665 ret = os_snprintf(pos, end - pos, "%sWPS",
666 pos == buf ? "" : " ");
667 if (ret < 0 || ret >= end - pos) {
668 end[-1] = '\0';
669 return buf;
670 }
671 pos += ret;
672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673#endif /* CONFIG_WPS */
674
675 return buf;
676}
677#endif /* NO_CONFIG_WRITE */
678
679
680static int wpa_config_parse_cipher(int line, const char *value)
681{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800682 int val = wpa_parse_cipher(value);
683 if (val < 0) {
684 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
685 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 if (val == 0) {
689 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
690 line);
691 return -1;
692 }
693 return val;
694}
695
696
697#ifndef NO_CONFIG_WRITE
698static char * wpa_config_write_cipher(int cipher)
699{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800700 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 if (buf == NULL)
702 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800704 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
705 os_free(buf);
706 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707 }
708
709 return buf;
710}
711#endif /* NO_CONFIG_WRITE */
712
713
714static int wpa_config_parse_pairwise(const struct parse_data *data,
715 struct wpa_ssid *ssid, int line,
716 const char *value)
717{
718 int val;
719 val = wpa_config_parse_cipher(line, value);
720 if (val == -1)
721 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800722 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
724 "(0x%x).", line, val);
725 return -1;
726 }
727
728 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
729 ssid->pairwise_cipher = val;
730 return 0;
731}
732
733
734#ifndef NO_CONFIG_WRITE
735static char * wpa_config_write_pairwise(const struct parse_data *data,
736 struct wpa_ssid *ssid)
737{
738 return wpa_config_write_cipher(ssid->pairwise_cipher);
739}
740#endif /* NO_CONFIG_WRITE */
741
742
743static int wpa_config_parse_group(const struct parse_data *data,
744 struct wpa_ssid *ssid, int line,
745 const char *value)
746{
747 int val;
748 val = wpa_config_parse_cipher(line, value);
749 if (val == -1)
750 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800751 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
753 "(0x%x).", line, val);
754 return -1;
755 }
756
757 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
758 ssid->group_cipher = val;
759 return 0;
760}
761
762
763#ifndef NO_CONFIG_WRITE
764static char * wpa_config_write_group(const struct parse_data *data,
765 struct wpa_ssid *ssid)
766{
767 return wpa_config_write_cipher(ssid->group_cipher);
768}
769#endif /* NO_CONFIG_WRITE */
770
771
772static int wpa_config_parse_auth_alg(const struct parse_data *data,
773 struct wpa_ssid *ssid, int line,
774 const char *value)
775{
776 int val = 0, last, errors = 0;
777 char *start, *end, *buf;
778
779 buf = os_strdup(value);
780 if (buf == NULL)
781 return -1;
782 start = buf;
783
784 while (*start != '\0') {
785 while (*start == ' ' || *start == '\t')
786 start++;
787 if (*start == '\0')
788 break;
789 end = start;
790 while (*end != ' ' && *end != '\t' && *end != '\0')
791 end++;
792 last = *end == '\0';
793 *end = '\0';
794 if (os_strcmp(start, "OPEN") == 0)
795 val |= WPA_AUTH_ALG_OPEN;
796 else if (os_strcmp(start, "SHARED") == 0)
797 val |= WPA_AUTH_ALG_SHARED;
798 else if (os_strcmp(start, "LEAP") == 0)
799 val |= WPA_AUTH_ALG_LEAP;
800 else {
801 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
802 line, start);
803 errors++;
804 }
805
806 if (last)
807 break;
808 start = end + 1;
809 }
810 os_free(buf);
811
812 if (val == 0) {
813 wpa_printf(MSG_ERROR,
814 "Line %d: no auth_alg values configured.", line);
815 errors++;
816 }
817
818 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
819 ssid->auth_alg = val;
820 return errors ? -1 : 0;
821}
822
823
824#ifndef NO_CONFIG_WRITE
825static char * wpa_config_write_auth_alg(const struct parse_data *data,
826 struct wpa_ssid *ssid)
827{
828 char *buf, *pos, *end;
829 int ret;
830
831 pos = buf = os_zalloc(30);
832 if (buf == NULL)
833 return NULL;
834 end = buf + 30;
835
836 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
837 ret = os_snprintf(pos, end - pos, "%sOPEN",
838 pos == buf ? "" : " ");
839 if (ret < 0 || ret >= end - pos) {
840 end[-1] = '\0';
841 return buf;
842 }
843 pos += ret;
844 }
845
846 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
847 ret = os_snprintf(pos, end - pos, "%sSHARED",
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_LEAP) {
857 ret = os_snprintf(pos, end - pos, "%sLEAP",
858 pos == buf ? "" : " ");
859 if (ret < 0 || ret >= end - pos) {
860 end[-1] = '\0';
861 return buf;
862 }
863 pos += ret;
864 }
865
866 return buf;
867}
868#endif /* NO_CONFIG_WRITE */
869
870
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800871static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872{
873 int *freqs;
874 size_t used, len;
875 const char *pos;
876
877 used = 0;
878 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700879 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (freqs == NULL)
881 return NULL;
882
883 pos = value;
884 while (pos) {
885 while (*pos == ' ')
886 pos++;
887 if (used == len) {
888 int *n;
889 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700890 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 if (n == NULL) {
892 os_free(freqs);
893 return NULL;
894 }
895 for (i = len; i <= len * 2; i++)
896 n[i] = 0;
897 freqs = n;
898 len *= 2;
899 }
900
901 freqs[used] = atoi(pos);
902 if (freqs[used] == 0)
903 break;
904 used++;
905 pos = os_strchr(pos + 1, ' ');
906 }
907
908 return freqs;
909}
910
911
912static int wpa_config_parse_scan_freq(const struct parse_data *data,
913 struct wpa_ssid *ssid, int line,
914 const char *value)
915{
916 int *freqs;
917
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800918 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 if (freqs == NULL)
920 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700921 if (freqs[0] == 0) {
922 os_free(freqs);
923 freqs = NULL;
924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 os_free(ssid->scan_freq);
926 ssid->scan_freq = freqs;
927
928 return 0;
929}
930
931
932static int wpa_config_parse_freq_list(const struct parse_data *data,
933 struct wpa_ssid *ssid, int line,
934 const char *value)
935{
936 int *freqs;
937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800938 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 if (freqs == NULL)
940 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700941 if (freqs[0] == 0) {
942 os_free(freqs);
943 freqs = NULL;
944 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 os_free(ssid->freq_list);
946 ssid->freq_list = freqs;
947
948 return 0;
949}
950
951
952#ifndef NO_CONFIG_WRITE
953static char * wpa_config_write_freqs(const struct parse_data *data,
954 const int *freqs)
955{
956 char *buf, *pos, *end;
957 int i, ret;
958 size_t count;
959
960 if (freqs == NULL)
961 return NULL;
962
963 count = 0;
964 for (i = 0; freqs[i]; i++)
965 count++;
966
967 pos = buf = os_zalloc(10 * count + 1);
968 if (buf == NULL)
969 return NULL;
970 end = buf + 10 * count + 1;
971
972 for (i = 0; freqs[i]; i++) {
973 ret = os_snprintf(pos, end - pos, "%s%u",
974 i == 0 ? "" : " ", freqs[i]);
975 if (ret < 0 || ret >= end - pos) {
976 end[-1] = '\0';
977 return buf;
978 }
979 pos += ret;
980 }
981
982 return buf;
983}
984
985
986static char * wpa_config_write_scan_freq(const struct parse_data *data,
987 struct wpa_ssid *ssid)
988{
989 return wpa_config_write_freqs(data, ssid->scan_freq);
990}
991
992
993static char * wpa_config_write_freq_list(const struct parse_data *data,
994 struct wpa_ssid *ssid)
995{
996 return wpa_config_write_freqs(data, ssid->freq_list);
997}
998#endif /* NO_CONFIG_WRITE */
999
1000
1001#ifdef IEEE8021X_EAPOL
1002static int wpa_config_parse_eap(const struct parse_data *data,
1003 struct wpa_ssid *ssid, int line,
1004 const char *value)
1005{
1006 int last, errors = 0;
1007 char *start, *end, *buf;
1008 struct eap_method_type *methods = NULL, *tmp;
1009 size_t num_methods = 0;
1010
1011 buf = os_strdup(value);
1012 if (buf == NULL)
1013 return -1;
1014 start = buf;
1015
1016 while (*start != '\0') {
1017 while (*start == ' ' || *start == '\t')
1018 start++;
1019 if (*start == '\0')
1020 break;
1021 end = start;
1022 while (*end != ' ' && *end != '\t' && *end != '\0')
1023 end++;
1024 last = *end == '\0';
1025 *end = '\0';
1026 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001027 methods = os_realloc_array(methods, num_methods + 1,
1028 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 if (methods == NULL) {
1030 os_free(tmp);
1031 os_free(buf);
1032 return -1;
1033 }
1034 methods[num_methods].method = eap_peer_get_type(
1035 start, &methods[num_methods].vendor);
1036 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1037 methods[num_methods].method == EAP_TYPE_NONE) {
1038 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1039 "'%s'", line, start);
1040 wpa_printf(MSG_ERROR, "You may need to add support for"
1041 " this EAP method during wpa_supplicant\n"
1042 "build time configuration.\n"
1043 "See README for more information.");
1044 errors++;
1045 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1046 methods[num_methods].method == EAP_TYPE_LEAP)
1047 ssid->leap++;
1048 else
1049 ssid->non_leap++;
1050 num_methods++;
1051 if (last)
1052 break;
1053 start = end + 1;
1054 }
1055 os_free(buf);
1056
1057 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001058 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 if (methods == NULL) {
1060 os_free(tmp);
1061 return -1;
1062 }
1063 methods[num_methods].vendor = EAP_VENDOR_IETF;
1064 methods[num_methods].method = EAP_TYPE_NONE;
1065 num_methods++;
1066
1067 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1068 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001069 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 ssid->eap.eap_methods = methods;
1071 return errors ? -1 : 0;
1072}
1073
1074
1075static char * wpa_config_write_eap(const struct parse_data *data,
1076 struct wpa_ssid *ssid)
1077{
1078 int i, ret;
1079 char *buf, *pos, *end;
1080 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1081 const char *name;
1082
1083 if (eap_methods == NULL)
1084 return NULL;
1085
1086 pos = buf = os_zalloc(100);
1087 if (buf == NULL)
1088 return NULL;
1089 end = buf + 100;
1090
1091 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1092 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1093 name = eap_get_name(eap_methods[i].vendor,
1094 eap_methods[i].method);
1095 if (name) {
1096 ret = os_snprintf(pos, end - pos, "%s%s",
1097 pos == buf ? "" : " ", name);
1098 if (ret < 0 || ret >= end - pos)
1099 break;
1100 pos += ret;
1101 }
1102 }
1103
1104 end[-1] = '\0';
1105
1106 return buf;
1107}
1108
1109
1110static int wpa_config_parse_password(const struct parse_data *data,
1111 struct wpa_ssid *ssid, int line,
1112 const char *value)
1113{
1114 u8 *hash;
1115
1116 if (os_strcmp(value, "NULL") == 0) {
1117 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1118 os_free(ssid->eap.password);
1119 ssid->eap.password = NULL;
1120 ssid->eap.password_len = 0;
1121 return 0;
1122 }
1123
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001124#ifdef CONFIG_EXT_PASSWORD
1125 if (os_strncmp(value, "ext:", 4) == 0) {
1126 char *name = os_strdup(value + 4);
1127 if (name == NULL)
1128 return -1;
1129 os_free(ssid->eap.password);
1130 ssid->eap.password = (u8 *) name;
1131 ssid->eap.password_len = os_strlen(name);
1132 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1133 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1134 return 0;
1135 }
1136#endif /* CONFIG_EXT_PASSWORD */
1137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 if (os_strncmp(value, "hash:", 5) != 0) {
1139 char *tmp;
1140 size_t res_len;
1141
1142 tmp = wpa_config_parse_string(value, &res_len);
1143 if (tmp == NULL) {
1144 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1145 "password.", line);
1146 return -1;
1147 }
1148 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1149 (u8 *) tmp, res_len);
1150
1151 os_free(ssid->eap.password);
1152 ssid->eap.password = (u8 *) tmp;
1153 ssid->eap.password_len = res_len;
1154 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156
1157 return 0;
1158 }
1159
1160
1161 /* NtPasswordHash: hash:<32 hex digits> */
1162 if (os_strlen(value + 5) != 2 * 16) {
1163 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1164 "(expected 32 hex digits)", line);
1165 return -1;
1166 }
1167
1168 hash = os_malloc(16);
1169 if (hash == NULL)
1170 return -1;
1171
1172 if (hexstr2bin(value + 5, hash, 16)) {
1173 os_free(hash);
1174 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1175 return -1;
1176 }
1177
1178 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1179
1180 os_free(ssid->eap.password);
1181 ssid->eap.password = hash;
1182 ssid->eap.password_len = 16;
1183 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001184 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185
1186 return 0;
1187}
1188
1189
1190static char * wpa_config_write_password(const struct parse_data *data,
1191 struct wpa_ssid *ssid)
1192{
1193 char *buf;
1194
1195 if (ssid->eap.password == NULL)
1196 return NULL;
1197
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001198#ifdef CONFIG_EXT_PASSWORD
1199 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1200 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1201 if (buf == NULL)
1202 return NULL;
1203 os_memcpy(buf, "ext:", 4);
1204 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1205 return buf;
1206 }
1207#endif /* CONFIG_EXT_PASSWORD */
1208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1210 return wpa_config_write_string(
1211 ssid->eap.password, ssid->eap.password_len);
1212 }
1213
1214 buf = os_malloc(5 + 32 + 1);
1215 if (buf == NULL)
1216 return NULL;
1217
1218 os_memcpy(buf, "hash:", 5);
1219 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1220
1221 return buf;
1222}
1223#endif /* IEEE8021X_EAPOL */
1224
1225
1226static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1227 const char *value, int idx)
1228{
1229 char *buf, title[20];
1230 int res;
1231
1232 buf = wpa_config_parse_string(value, len);
1233 if (buf == NULL) {
1234 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1235 line, idx, value);
1236 return -1;
1237 }
1238 if (*len > MAX_WEP_KEY_LEN) {
1239 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1240 line, idx, value);
1241 os_free(buf);
1242 return -1;
1243 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001244 if (*len && *len != 5 && *len != 13 && *len != 16) {
1245 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1246 "this network block will be ignored",
1247 line, (unsigned int) *len);
1248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 os_memcpy(key, buf, *len);
1250 os_free(buf);
1251 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1252 if (res >= 0 && (size_t) res < sizeof(title))
1253 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1254 return 0;
1255}
1256
1257
1258static int wpa_config_parse_wep_key0(const struct parse_data *data,
1259 struct wpa_ssid *ssid, int line,
1260 const char *value)
1261{
1262 return wpa_config_parse_wep_key(ssid->wep_key[0],
1263 &ssid->wep_key_len[0], line,
1264 value, 0);
1265}
1266
1267
1268static int wpa_config_parse_wep_key1(const struct parse_data *data,
1269 struct wpa_ssid *ssid, int line,
1270 const char *value)
1271{
1272 return wpa_config_parse_wep_key(ssid->wep_key[1],
1273 &ssid->wep_key_len[1], line,
1274 value, 1);
1275}
1276
1277
1278static int wpa_config_parse_wep_key2(const struct parse_data *data,
1279 struct wpa_ssid *ssid, int line,
1280 const char *value)
1281{
1282 return wpa_config_parse_wep_key(ssid->wep_key[2],
1283 &ssid->wep_key_len[2], line,
1284 value, 2);
1285}
1286
1287
1288static int wpa_config_parse_wep_key3(const struct parse_data *data,
1289 struct wpa_ssid *ssid, int line,
1290 const char *value)
1291{
1292 return wpa_config_parse_wep_key(ssid->wep_key[3],
1293 &ssid->wep_key_len[3], line,
1294 value, 3);
1295}
1296
1297
1298#ifndef NO_CONFIG_WRITE
1299static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1300{
1301 if (ssid->wep_key_len[idx] == 0)
1302 return NULL;
1303 return wpa_config_write_string(ssid->wep_key[idx],
1304 ssid->wep_key_len[idx]);
1305}
1306
1307
1308static char * wpa_config_write_wep_key0(const struct parse_data *data,
1309 struct wpa_ssid *ssid)
1310{
1311 return wpa_config_write_wep_key(ssid, 0);
1312}
1313
1314
1315static char * wpa_config_write_wep_key1(const struct parse_data *data,
1316 struct wpa_ssid *ssid)
1317{
1318 return wpa_config_write_wep_key(ssid, 1);
1319}
1320
1321
1322static char * wpa_config_write_wep_key2(const struct parse_data *data,
1323 struct wpa_ssid *ssid)
1324{
1325 return wpa_config_write_wep_key(ssid, 2);
1326}
1327
1328
1329static char * wpa_config_write_wep_key3(const struct parse_data *data,
1330 struct wpa_ssid *ssid)
1331{
1332 return wpa_config_write_wep_key(ssid, 3);
1333}
1334#endif /* NO_CONFIG_WRITE */
1335
1336
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001337#ifdef CONFIG_P2P
1338
Dmitry Shmidt54605472013-11-08 11:10:19 -08001339static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1340 struct wpa_ssid *ssid, int line,
1341 const char *value)
1342{
1343 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1344 os_strcmp(value, "any") == 0) {
1345 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1346 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1347 return 0;
1348 }
1349 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1350 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1351 line, value);
1352 return -1;
1353 }
1354 ssid->bssid_set = 1;
1355 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1356 MAC2STR(ssid->go_p2p_dev_addr));
1357 return 0;
1358}
1359
1360
1361#ifndef NO_CONFIG_WRITE
1362static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1363 struct wpa_ssid *ssid)
1364{
1365 char *value;
1366 int res;
1367
1368 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1369 return NULL;
1370
1371 value = os_malloc(20);
1372 if (value == NULL)
1373 return NULL;
1374 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1375 if (res < 0 || res >= 20) {
1376 os_free(value);
1377 return NULL;
1378 }
1379 value[20 - 1] = '\0';
1380 return value;
1381}
1382#endif /* NO_CONFIG_WRITE */
1383
1384
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001385static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1386 struct wpa_ssid *ssid, int line,
1387 const char *value)
1388{
1389 const char *pos;
1390 u8 *buf, *n, addr[ETH_ALEN];
1391 size_t count;
1392
1393 buf = NULL;
1394 count = 0;
1395
1396 pos = value;
1397 while (pos && *pos) {
1398 while (*pos == ' ')
1399 pos++;
1400
1401 if (hwaddr_aton(pos, addr)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402 if (count == 0) {
1403 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1404 "p2p_client_list address '%s'.",
1405 line, value);
1406 os_free(buf);
1407 return -1;
1408 }
1409 /* continue anyway since this could have been from a
1410 * truncated configuration file line */
1411 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1412 "truncated p2p_client_list address '%s'",
1413 line, pos);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001414 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001415 n = os_realloc_array(buf, count + 1, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001416 if (n == NULL) {
1417 os_free(buf);
1418 return -1;
1419 }
1420 buf = n;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001421 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1422 os_memcpy(buf, addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001423 count++;
1424 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1425 addr, ETH_ALEN);
1426 }
1427
1428 pos = os_strchr(pos, ' ');
1429 }
1430
1431 os_free(ssid->p2p_client_list);
1432 ssid->p2p_client_list = buf;
1433 ssid->num_p2p_clients = count;
1434
1435 return 0;
1436}
1437
1438
1439#ifndef NO_CONFIG_WRITE
1440static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1441 struct wpa_ssid *ssid)
1442{
1443 char *value, *end, *pos;
1444 int res;
1445 size_t i;
1446
1447 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1448 return NULL;
1449
1450 value = os_malloc(20 * ssid->num_p2p_clients);
1451 if (value == NULL)
1452 return NULL;
1453 pos = value;
1454 end = value + 20 * ssid->num_p2p_clients;
1455
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001456 for (i = ssid->num_p2p_clients; i > 0; i--) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001457 res = os_snprintf(pos, end - pos, MACSTR " ",
1458 MAC2STR(ssid->p2p_client_list +
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001459 (i - 1) * ETH_ALEN));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460 if (res < 0 || res >= end - pos) {
1461 os_free(value);
1462 return NULL;
1463 }
1464 pos += res;
1465 }
1466
1467 if (pos > value)
1468 pos[-1] = '\0';
1469
1470 return value;
1471}
1472#endif /* NO_CONFIG_WRITE */
1473
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001474
1475static int wpa_config_parse_psk_list(const struct parse_data *data,
1476 struct wpa_ssid *ssid, int line,
1477 const char *value)
1478{
1479 struct psk_list_entry *p;
1480 const char *pos;
1481
1482 p = os_zalloc(sizeof(*p));
1483 if (p == NULL)
1484 return -1;
1485
1486 pos = value;
1487 if (os_strncmp(pos, "P2P-", 4) == 0) {
1488 p->p2p = 1;
1489 pos += 4;
1490 }
1491
1492 if (hwaddr_aton(pos, p->addr)) {
1493 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1494 line, pos);
1495 os_free(p);
1496 return -1;
1497 }
1498 pos += 17;
1499 if (*pos != '-') {
1500 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1501 line, pos);
1502 os_free(p);
1503 return -1;
1504 }
1505 pos++;
1506
1507 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1508 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1509 line, pos);
1510 os_free(p);
1511 return -1;
1512 }
1513
1514 dl_list_add(&ssid->psk_list, &p->list);
1515
1516 return 0;
1517}
1518
1519
1520#ifndef NO_CONFIG_WRITE
1521static char * wpa_config_write_psk_list(const struct parse_data *data,
1522 struct wpa_ssid *ssid)
1523{
1524 return NULL;
1525}
1526#endif /* NO_CONFIG_WRITE */
1527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001528#endif /* CONFIG_P2P */
1529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530/* Helper macros for network block parser */
1531
1532#ifdef OFFSET
1533#undef OFFSET
1534#endif /* OFFSET */
1535/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1536#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1537
1538/* STR: Define a string variable for an ASCII string; f = field name */
1539#ifdef NO_CONFIG_WRITE
1540#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1541#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1542#else /* NO_CONFIG_WRITE */
1543#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1544#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1545#endif /* NO_CONFIG_WRITE */
1546#define STR(f) _STR(f), NULL, NULL, NULL, 0
1547#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1548#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1549#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1550
1551/* STR_LEN: Define a string variable with a separate variable for storing the
1552 * data length. Unlike STR(), this can be used to store arbitrary binary data
1553 * (i.e., even nul termination character). */
1554#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1555#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1556#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1557#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1558#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1559
1560/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1561 * explicitly specified. */
1562#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1563#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1564#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1565
1566#ifdef NO_CONFIG_WRITE
1567#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1568#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1569#else /* NO_CONFIG_WRITE */
1570#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1571 OFFSET(f), (void *) 0
1572#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1573 OFFSET(eap.f), (void *) 0
1574#endif /* NO_CONFIG_WRITE */
1575
1576/* INT: Define an integer variable */
1577#define INT(f) _INT(f), NULL, NULL, 0
1578#define INTe(f) _INTe(f), NULL, NULL, 0
1579
1580/* INT_RANGE: Define an integer variable with allowed value range */
1581#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1582
1583/* FUNC: Define a configuration variable that uses a custom function for
1584 * parsing and writing the value. */
1585#ifdef NO_CONFIG_WRITE
1586#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1587#else /* NO_CONFIG_WRITE */
1588#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1589 NULL, NULL, NULL, NULL
1590#endif /* NO_CONFIG_WRITE */
1591#define FUNC(f) _FUNC(f), 0
1592#define FUNC_KEY(f) _FUNC(f), 1
1593
1594/*
1595 * Table of network configuration variables. This table is used to parse each
1596 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1597 * that is inside a network block.
1598 *
1599 * This table is generated using the helper macros defined above and with
1600 * generous help from the C pre-processor. The field name is stored as a string
1601 * into .name and for STR and INT types, the offset of the target buffer within
1602 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1603 * offset to the field containing the length of the configuration variable.
1604 * .param3 and .param4 can be used to mark the allowed range (length for STR
1605 * and value for INT).
1606 *
1607 * For each configuration line in wpa_supplicant.conf, the parser goes through
1608 * this table and select the entry that matches with the field name. The parser
1609 * function (.parser) is then called to parse the actual value of the field.
1610 *
1611 * This kind of mechanism makes it easy to add new configuration parameters,
1612 * since only one line needs to be added into this table and into the
1613 * struct wpa_ssid definition if the new variable is either a string or
1614 * integer. More complex types will need to use their own parser and writer
1615 * functions.
1616 */
1617static const struct parse_data ssid_fields[] = {
1618 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1619 { INT_RANGE(scan_ssid, 0, 1) },
1620 { FUNC(bssid) },
1621 { FUNC_KEY(psk) },
1622 { FUNC(proto) },
1623 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001624 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 { FUNC(pairwise) },
1626 { FUNC(group) },
1627 { FUNC(auth_alg) },
1628 { FUNC(scan_freq) },
1629 { FUNC(freq_list) },
1630#ifdef IEEE8021X_EAPOL
1631 { FUNC(eap) },
1632 { STR_LENe(identity) },
1633 { STR_LENe(anonymous_identity) },
1634 { FUNC_KEY(password) },
1635 { STRe(ca_cert) },
1636 { STRe(ca_path) },
1637 { STRe(client_cert) },
1638 { STRe(private_key) },
1639 { STR_KEYe(private_key_passwd) },
1640 { STRe(dh_file) },
1641 { STRe(subject_match) },
1642 { STRe(altsubject_match) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001643 { STRe(domain_suffix_match) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 { STRe(ca_cert2) },
1645 { STRe(ca_path2) },
1646 { STRe(client_cert2) },
1647 { STRe(private_key2) },
1648 { STR_KEYe(private_key2_passwd) },
1649 { STRe(dh_file2) },
1650 { STRe(subject_match2) },
1651 { STRe(altsubject_match2) },
Dmitry Shmidt051af732013-10-22 13:52:46 -07001652 { STRe(domain_suffix_match2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 { STRe(phase1) },
1654 { STRe(phase2) },
1655 { STRe(pcsc) },
1656 { STR_KEYe(pin) },
1657 { STRe(engine_id) },
1658 { STRe(key_id) },
1659 { STRe(cert_id) },
1660 { STRe(ca_cert_id) },
1661 { STR_KEYe(pin2) },
1662 { STRe(engine2_id) },
1663 { STRe(key2_id) },
1664 { STRe(cert2_id) },
1665 { STRe(ca_cert2_id) },
1666 { INTe(engine) },
1667 { INTe(engine2) },
1668 { INT(eapol_flags) },
1669#endif /* IEEE8021X_EAPOL */
1670 { FUNC_KEY(wep_key0) },
1671 { FUNC_KEY(wep_key1) },
1672 { FUNC_KEY(wep_key2) },
1673 { FUNC_KEY(wep_key3) },
1674 { INT(wep_tx_keyidx) },
1675 { INT(priority) },
1676#ifdef IEEE8021X_EAPOL
1677 { INT(eap_workaround) },
1678 { STRe(pac_file) },
1679 { INTe(fragment_size) },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001680 { INTe(ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681#endif /* IEEE8021X_EAPOL */
1682 { INT_RANGE(mode, 0, 4) },
1683 { INT_RANGE(proactive_key_caching, 0, 1) },
1684 { INT_RANGE(disabled, 0, 2) },
1685 { STR(id_str) },
1686#ifdef CONFIG_IEEE80211W
1687 { INT_RANGE(ieee80211w, 0, 2) },
1688#endif /* CONFIG_IEEE80211W */
1689 { INT_RANGE(peerkey, 0, 1) },
1690 { INT_RANGE(mixed_cell, 0, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001691 { INT_RANGE(frequency, 0, 65000) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 { INT(wpa_ptk_rekey) },
1693 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07001694 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08001696 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001698 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001699#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001700#ifdef CONFIG_HT_OVERRIDES
1701 { INT_RANGE(disable_ht, 0, 1) },
1702 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001703 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001704 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001705 { INT_RANGE(disable_max_amsdu, -1, 1) },
1706 { INT_RANGE(ampdu_factor, -1, 3) },
1707 { INT_RANGE(ampdu_density, -1, 7) },
1708 { STR(ht_mcs) },
1709#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001710#ifdef CONFIG_VHT_OVERRIDES
1711 { INT_RANGE(disable_vht, 0, 1) },
1712 { INT(vht_capa) },
1713 { INT(vht_capa_mask) },
1714 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1715 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1716 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1717 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1718 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1719 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1720 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1721 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1722 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1723 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1724 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1725 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1726 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1727 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1728 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1729 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1730#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001731 { INT(ap_max_inactivity) },
1732 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001733 { INT(beacon_int) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734};
1735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736#undef OFFSET
1737#undef _STR
1738#undef STR
1739#undef STR_KEY
1740#undef _STR_LEN
1741#undef STR_LEN
1742#undef STR_LEN_KEY
1743#undef _STR_RANGE
1744#undef STR_RANGE
1745#undef STR_RANGE_KEY
1746#undef _INT
1747#undef INT
1748#undef INT_RANGE
1749#undef _FUNC
1750#undef FUNC
1751#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001752#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753
1754
1755/**
1756 * wpa_config_add_prio_network - Add a network to priority lists
1757 * @config: Configuration data from wpa_config_read()
1758 * @ssid: Pointer to the network configuration to be added to the list
1759 * Returns: 0 on success, -1 on failure
1760 *
1761 * This function is used to add a network block to the priority list of
1762 * networks. This must be called for each network when reading in the full
1763 * configuration. In addition, this can be used indirectly when updating
1764 * priorities by calling wpa_config_update_prio_list().
1765 */
1766int wpa_config_add_prio_network(struct wpa_config *config,
1767 struct wpa_ssid *ssid)
1768{
1769 int prio;
1770 struct wpa_ssid *prev, **nlist;
1771
1772 /*
1773 * Add to an existing priority list if one is available for the
1774 * configured priority level for this network.
1775 */
1776 for (prio = 0; prio < config->num_prio; prio++) {
1777 prev = config->pssid[prio];
1778 if (prev->priority == ssid->priority) {
1779 while (prev->pnext)
1780 prev = prev->pnext;
1781 prev->pnext = ssid;
1782 return 0;
1783 }
1784 }
1785
1786 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001787 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1788 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 if (nlist == NULL)
1790 return -1;
1791
1792 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001793 if (nlist[prio]->priority < ssid->priority) {
1794 os_memmove(&nlist[prio + 1], &nlist[prio],
1795 (config->num_prio - prio) *
1796 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 }
1800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 nlist[prio] = ssid;
1802 config->num_prio++;
1803 config->pssid = nlist;
1804
1805 return 0;
1806}
1807
1808
1809/**
1810 * wpa_config_update_prio_list - Update network priority list
1811 * @config: Configuration data from wpa_config_read()
1812 * Returns: 0 on success, -1 on failure
1813 *
1814 * This function is called to update the priority list of networks in the
1815 * configuration when a network is being added or removed. This is also called
1816 * if a priority for a network is changed.
1817 */
1818int wpa_config_update_prio_list(struct wpa_config *config)
1819{
1820 struct wpa_ssid *ssid;
1821 int ret = 0;
1822
1823 os_free(config->pssid);
1824 config->pssid = NULL;
1825 config->num_prio = 0;
1826
1827 ssid = config->ssid;
1828 while (ssid) {
1829 ssid->pnext = NULL;
1830 if (wpa_config_add_prio_network(config, ssid) < 0)
1831 ret = -1;
1832 ssid = ssid->next;
1833 }
1834
1835 return ret;
1836}
1837
1838
1839#ifdef IEEE8021X_EAPOL
1840static void eap_peer_config_free(struct eap_peer_config *eap)
1841{
1842 os_free(eap->eap_methods);
1843 os_free(eap->identity);
1844 os_free(eap->anonymous_identity);
1845 os_free(eap->password);
1846 os_free(eap->ca_cert);
1847 os_free(eap->ca_path);
1848 os_free(eap->client_cert);
1849 os_free(eap->private_key);
1850 os_free(eap->private_key_passwd);
1851 os_free(eap->dh_file);
1852 os_free(eap->subject_match);
1853 os_free(eap->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001854 os_free(eap->domain_suffix_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 os_free(eap->ca_cert2);
1856 os_free(eap->ca_path2);
1857 os_free(eap->client_cert2);
1858 os_free(eap->private_key2);
1859 os_free(eap->private_key2_passwd);
1860 os_free(eap->dh_file2);
1861 os_free(eap->subject_match2);
1862 os_free(eap->altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001863 os_free(eap->domain_suffix_match2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 os_free(eap->phase1);
1865 os_free(eap->phase2);
1866 os_free(eap->pcsc);
1867 os_free(eap->pin);
1868 os_free(eap->engine_id);
1869 os_free(eap->key_id);
1870 os_free(eap->cert_id);
1871 os_free(eap->ca_cert_id);
1872 os_free(eap->key2_id);
1873 os_free(eap->cert2_id);
1874 os_free(eap->ca_cert2_id);
1875 os_free(eap->pin2);
1876 os_free(eap->engine2_id);
1877 os_free(eap->otp);
1878 os_free(eap->pending_req_otp);
1879 os_free(eap->pac_file);
1880 os_free(eap->new_password);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001881 os_free(eap->external_sim_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882}
1883#endif /* IEEE8021X_EAPOL */
1884
1885
1886/**
1887 * wpa_config_free_ssid - Free network/ssid configuration data
1888 * @ssid: Configuration data for the network
1889 *
1890 * This function frees all resources allocated for the network configuration
1891 * data.
1892 */
1893void wpa_config_free_ssid(struct wpa_ssid *ssid)
1894{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001895 struct psk_list_entry *psk;
1896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 os_free(ssid->ssid);
1898 os_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001899 os_free(ssid->ext_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900#ifdef IEEE8021X_EAPOL
1901 eap_peer_config_free(&ssid->eap);
1902#endif /* IEEE8021X_EAPOL */
1903 os_free(ssid->id_str);
1904 os_free(ssid->scan_freq);
1905 os_free(ssid->freq_list);
1906 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 os_free(ssid->p2p_client_list);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001908#ifdef CONFIG_HT_OVERRIDES
1909 os_free(ssid->ht_mcs);
1910#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001911 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1912 list))) {
1913 dl_list_del(&psk->list);
1914 os_free(psk);
1915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 os_free(ssid);
1917}
1918
1919
Dmitry Shmidt04949592012-07-19 12:16:46 -07001920void wpa_config_free_cred(struct wpa_cred *cred)
1921{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001922 size_t i;
1923
Dmitry Shmidt04949592012-07-19 12:16:46 -07001924 os_free(cred->realm);
1925 os_free(cred->username);
1926 os_free(cred->password);
1927 os_free(cred->ca_cert);
1928 os_free(cred->client_cert);
1929 os_free(cred->private_key);
1930 os_free(cred->private_key_passwd);
1931 os_free(cred->imsi);
1932 os_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001933 for (i = 0; i < cred->num_domain; i++)
1934 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001935 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001936 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001937 os_free(cred->eap_method);
1938 os_free(cred->phase1);
1939 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001940 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001941 os_free(cred->roaming_partner);
1942 os_free(cred->provisioning_sp);
1943 for (i = 0; i < cred->num_req_conn_capab; i++)
1944 os_free(cred->req_conn_capab_port[i]);
1945 os_free(cred->req_conn_capab_port);
1946 os_free(cred->req_conn_capab_proto);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001947 os_free(cred);
1948}
1949
1950
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001951void wpa_config_flush_blobs(struct wpa_config *config)
1952{
1953#ifndef CONFIG_NO_CONFIG_BLOBS
1954 struct wpa_config_blob *blob, *prev;
1955
1956 blob = config->blobs;
1957 config->blobs = NULL;
1958 while (blob) {
1959 prev = blob;
1960 blob = blob->next;
1961 wpa_config_free_blob(prev);
1962 }
1963#endif /* CONFIG_NO_CONFIG_BLOBS */
1964}
1965
1966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967/**
1968 * wpa_config_free - Free configuration data
1969 * @config: Configuration data from wpa_config_read()
1970 *
1971 * This function frees all resources allocated for the configuration data by
1972 * wpa_config_read().
1973 */
1974void wpa_config_free(struct wpa_config *config)
1975{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001977 struct wpa_cred *cred, *cprev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978
1979 ssid = config->ssid;
1980 while (ssid) {
1981 prev = ssid;
1982 ssid = ssid->next;
1983 wpa_config_free_ssid(prev);
1984 }
1985
Dmitry Shmidt04949592012-07-19 12:16:46 -07001986 cred = config->cred;
1987 while (cred) {
1988 cprev = cred;
1989 cred = cred->next;
1990 wpa_config_free_cred(cprev);
1991 }
1992
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001993 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994
Dmitry Shmidt04949592012-07-19 12:16:46 -07001995 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 os_free(config->ctrl_interface);
1997 os_free(config->ctrl_interface_group);
1998 os_free(config->opensc_engine_path);
1999 os_free(config->pkcs11_engine_path);
2000 os_free(config->pkcs11_module_path);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002001 os_free(config->pcsc_reader);
2002 os_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 os_free(config->driver_param);
2004 os_free(config->device_name);
2005 os_free(config->manufacturer);
2006 os_free(config->model_name);
2007 os_free(config->model_number);
2008 os_free(config->serial_number);
2009 os_free(config->config_methods);
2010 os_free(config->p2p_ssid_postfix);
2011 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002012 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002013 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002014 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002015 os_free(config->freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002016 wpabuf_free(config->wps_nfc_dh_pubkey);
2017 wpabuf_free(config->wps_nfc_dh_privkey);
2018 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002019 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002020 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002021 wpabuf_free(config->ap_vendor_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002022 os_free(config->osu_dir);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 os_free(config);
2024}
2025
2026
2027/**
2028 * wpa_config_foreach_network - Iterate over each configured network
2029 * @config: Configuration data from wpa_config_read()
2030 * @func: Callback function to process each network
2031 * @arg: Opaque argument to pass to callback function
2032 *
2033 * Iterate over the set of configured networks calling the specified
2034 * function for each item. We guard against callbacks removing the
2035 * supplied network.
2036 */
2037void wpa_config_foreach_network(struct wpa_config *config,
2038 void (*func)(void *, struct wpa_ssid *),
2039 void *arg)
2040{
2041 struct wpa_ssid *ssid, *next;
2042
2043 ssid = config->ssid;
2044 while (ssid) {
2045 next = ssid->next;
2046 func(arg, ssid);
2047 ssid = next;
2048 }
2049}
2050
2051
2052/**
2053 * wpa_config_get_network - Get configured network based on id
2054 * @config: Configuration data from wpa_config_read()
2055 * @id: Unique network id to search for
2056 * Returns: Network configuration or %NULL if not found
2057 */
2058struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2059{
2060 struct wpa_ssid *ssid;
2061
2062 ssid = config->ssid;
2063 while (ssid) {
2064 if (id == ssid->id)
2065 break;
2066 ssid = ssid->next;
2067 }
2068
2069 return ssid;
2070}
2071
2072
2073/**
2074 * wpa_config_add_network - Add a new network with empty configuration
2075 * @config: Configuration data from wpa_config_read()
2076 * Returns: The new network configuration or %NULL if operation failed
2077 */
2078struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2079{
2080 int id;
2081 struct wpa_ssid *ssid, *last = NULL;
2082
2083 id = -1;
2084 ssid = config->ssid;
2085 while (ssid) {
2086 if (ssid->id > id)
2087 id = ssid->id;
2088 last = ssid;
2089 ssid = ssid->next;
2090 }
2091 id++;
2092
2093 ssid = os_zalloc(sizeof(*ssid));
2094 if (ssid == NULL)
2095 return NULL;
2096 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002097 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 if (last)
2099 last->next = ssid;
2100 else
2101 config->ssid = ssid;
2102
2103 wpa_config_update_prio_list(config);
2104
2105 return ssid;
2106}
2107
2108
2109/**
2110 * wpa_config_remove_network - Remove a configured network based on id
2111 * @config: Configuration data from wpa_config_read()
2112 * @id: Unique network id to search for
2113 * Returns: 0 on success, or -1 if the network was not found
2114 */
2115int wpa_config_remove_network(struct wpa_config *config, int id)
2116{
2117 struct wpa_ssid *ssid, *prev = NULL;
2118
2119 ssid = config->ssid;
2120 while (ssid) {
2121 if (id == ssid->id)
2122 break;
2123 prev = ssid;
2124 ssid = ssid->next;
2125 }
2126
2127 if (ssid == NULL)
2128 return -1;
2129
2130 if (prev)
2131 prev->next = ssid->next;
2132 else
2133 config->ssid = ssid->next;
2134
2135 wpa_config_update_prio_list(config);
2136 wpa_config_free_ssid(ssid);
2137 return 0;
2138}
2139
2140
2141/**
2142 * wpa_config_set_network_defaults - Set network default values
2143 * @ssid: Pointer to network configuration data
2144 */
2145void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2146{
2147 ssid->proto = DEFAULT_PROTO;
2148 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2149 ssid->group_cipher = DEFAULT_GROUP;
2150 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002151 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152#ifdef IEEE8021X_EAPOL
2153 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2154 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2155 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2156#endif /* IEEE8021X_EAPOL */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002157#ifdef CONFIG_HT_OVERRIDES
2158 ssid->disable_ht = DEFAULT_DISABLE_HT;
2159 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002160 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002161 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002162 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2163 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2164 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2165#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002166#ifdef CONFIG_VHT_OVERRIDES
2167 ssid->vht_rx_mcs_nss_1 = -1;
2168 ssid->vht_rx_mcs_nss_2 = -1;
2169 ssid->vht_rx_mcs_nss_3 = -1;
2170 ssid->vht_rx_mcs_nss_4 = -1;
2171 ssid->vht_rx_mcs_nss_5 = -1;
2172 ssid->vht_rx_mcs_nss_6 = -1;
2173 ssid->vht_rx_mcs_nss_7 = -1;
2174 ssid->vht_rx_mcs_nss_8 = -1;
2175 ssid->vht_tx_mcs_nss_1 = -1;
2176 ssid->vht_tx_mcs_nss_2 = -1;
2177 ssid->vht_tx_mcs_nss_3 = -1;
2178 ssid->vht_tx_mcs_nss_4 = -1;
2179 ssid->vht_tx_mcs_nss_5 = -1;
2180 ssid->vht_tx_mcs_nss_6 = -1;
2181 ssid->vht_tx_mcs_nss_7 = -1;
2182 ssid->vht_tx_mcs_nss_8 = -1;
2183#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002184 ssid->proactive_key_caching = -1;
2185#ifdef CONFIG_IEEE80211W
2186 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2187#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188}
2189
2190
2191/**
2192 * wpa_config_set - Set a variable in network configuration
2193 * @ssid: Pointer to network configuration data
2194 * @var: Variable name, e.g., "ssid"
2195 * @value: Variable value
2196 * @line: Line number in configuration file or 0 if not used
2197 * Returns: 0 on success, -1 on failure
2198 *
2199 * This function can be used to set network configuration variables based on
2200 * both the configuration file and management interface input. The value
2201 * parameter must be in the same format as the text-based configuration file is
2202 * using. For example, strings are using double quotation marks.
2203 */
2204int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2205 int line)
2206{
2207 size_t i;
2208 int ret = 0;
2209
2210 if (ssid == NULL || var == NULL || value == NULL)
2211 return -1;
2212
2213 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2214 const struct parse_data *field = &ssid_fields[i];
2215 if (os_strcmp(var, field->name) != 0)
2216 continue;
2217
2218 if (field->parser(field, ssid, line, value)) {
2219 if (line) {
2220 wpa_printf(MSG_ERROR, "Line %d: failed to "
2221 "parse %s '%s'.", line, var, value);
2222 }
2223 ret = -1;
2224 }
2225 break;
2226 }
2227 if (i == NUM_SSID_FIELDS) {
2228 if (line) {
2229 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2230 "'%s'.", line, var);
2231 }
2232 ret = -1;
2233 }
2234
2235 return ret;
2236}
2237
2238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2240 const char *value)
2241{
2242 size_t len;
2243 char *buf;
2244 int ret;
2245
2246 len = os_strlen(value);
2247 buf = os_malloc(len + 3);
2248 if (buf == NULL)
2249 return -1;
2250 buf[0] = '"';
2251 os_memcpy(buf + 1, value, len);
2252 buf[len + 1] = '"';
2253 buf[len + 2] = '\0';
2254 ret = wpa_config_set(ssid, var, buf, 0);
2255 os_free(buf);
2256 return ret;
2257}
2258
2259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260/**
2261 * wpa_config_get_all - Get all options from network configuration
2262 * @ssid: Pointer to network configuration data
2263 * @get_keys: Determines if keys/passwords will be included in returned list
2264 * (if they may be exported)
2265 * Returns: %NULL terminated list of all set keys and their values in the form
2266 * of [key1, val1, key2, val2, ... , NULL]
2267 *
2268 * This function can be used to get list of all configured network properties.
2269 * The caller is responsible for freeing the returned list and all its
2270 * elements.
2271 */
2272char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2273{
2274 const struct parse_data *field;
2275 char *key, *value;
2276 size_t i;
2277 char **props;
2278 int fields_num;
2279
2280 get_keys = get_keys && ssid->export_keys;
2281
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002282 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 if (!props)
2284 return NULL;
2285
2286 fields_num = 0;
2287 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2288 field = &ssid_fields[i];
2289 if (field->key_data && !get_keys)
2290 continue;
2291 value = field->writer(field, ssid);
2292 if (value == NULL)
2293 continue;
2294 if (os_strlen(value) == 0) {
2295 os_free(value);
2296 continue;
2297 }
2298
2299 key = os_strdup(field->name);
2300 if (key == NULL) {
2301 os_free(value);
2302 goto err;
2303 }
2304
2305 props[fields_num * 2] = key;
2306 props[fields_num * 2 + 1] = value;
2307
2308 fields_num++;
2309 }
2310
2311 return props;
2312
2313err:
2314 value = *props;
2315 while (value)
2316 os_free(value++);
2317 os_free(props);
2318 return NULL;
2319}
2320
2321
2322#ifndef NO_CONFIG_WRITE
2323/**
2324 * wpa_config_get - Get a variable in network configuration
2325 * @ssid: Pointer to network configuration data
2326 * @var: Variable name, e.g., "ssid"
2327 * Returns: Value of the variable or %NULL on failure
2328 *
2329 * This function can be used to get network configuration variables. The
2330 * returned value is a copy of the configuration variable in text format, i.e,.
2331 * the same format that the text-based configuration file and wpa_config_set()
2332 * are using for the value. The caller is responsible for freeing the returned
2333 * value.
2334 */
2335char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2336{
2337 size_t i;
2338
2339 if (ssid == NULL || var == NULL)
2340 return NULL;
2341
2342 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2343 const struct parse_data *field = &ssid_fields[i];
2344 if (os_strcmp(var, field->name) == 0)
2345 return field->writer(field, ssid);
2346 }
2347
2348 return NULL;
2349}
2350
2351
2352/**
2353 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2354 * @ssid: Pointer to network configuration data
2355 * @var: Variable name, e.g., "ssid"
2356 * Returns: Value of the variable or %NULL on failure
2357 *
2358 * This function can be used to get network configuration variable like
2359 * wpa_config_get(). The only difference is that this functions does not expose
2360 * key/password material from the configuration. In case a key/password field
2361 * is requested, the returned value is an empty string or %NULL if the variable
2362 * is not set or "*" if the variable is set (regardless of its value). The
2363 * returned value is a copy of the configuration variable in text format, i.e,.
2364 * the same format that the text-based configuration file and wpa_config_set()
2365 * are using for the value. The caller is responsible for freeing the returned
2366 * value.
2367 */
2368char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2369{
2370 size_t i;
2371
2372 if (ssid == NULL || var == NULL)
2373 return NULL;
2374
2375 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2376 const struct parse_data *field = &ssid_fields[i];
2377 if (os_strcmp(var, field->name) == 0) {
2378 char *res = field->writer(field, ssid);
2379 if (field->key_data) {
2380 if (res && res[0]) {
2381 wpa_printf(MSG_DEBUG, "Do not allow "
2382 "key_data field to be "
2383 "exposed");
2384 os_free(res);
2385 return os_strdup("*");
2386 }
2387
2388 os_free(res);
2389 return NULL;
2390 }
2391 return res;
2392 }
2393 }
2394
2395 return NULL;
2396}
2397#endif /* NO_CONFIG_WRITE */
2398
2399
2400/**
2401 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2402 * @ssid: Pointer to network configuration data
2403 *
2404 * This function must be called to update WPA PSK when either SSID or the
2405 * passphrase has changed for the network configuration.
2406 */
2407void wpa_config_update_psk(struct wpa_ssid *ssid)
2408{
2409#ifndef CONFIG_NO_PBKDF2
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002410 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 ssid->psk, PMK_LEN);
2412 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2413 ssid->psk, PMK_LEN);
2414 ssid->psk_set = 1;
2415#endif /* CONFIG_NO_PBKDF2 */
2416}
2417
2418
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002419static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2420 const char *value)
2421{
2422 u8 *proto;
2423 int **port;
2424 int *ports, *nports;
2425 const char *pos;
2426 unsigned int num_ports;
2427
2428 proto = os_realloc_array(cred->req_conn_capab_proto,
2429 cred->num_req_conn_capab + 1, sizeof(u8));
2430 if (proto == NULL)
2431 return -1;
2432 cred->req_conn_capab_proto = proto;
2433
2434 port = os_realloc_array(cred->req_conn_capab_port,
2435 cred->num_req_conn_capab + 1, sizeof(int *));
2436 if (port == NULL)
2437 return -1;
2438 cred->req_conn_capab_port = port;
2439
2440 proto[cred->num_req_conn_capab] = atoi(value);
2441
2442 pos = os_strchr(value, ':');
2443 if (pos == NULL) {
2444 port[cred->num_req_conn_capab] = NULL;
2445 cred->num_req_conn_capab++;
2446 return 0;
2447 }
2448 pos++;
2449
2450 ports = NULL;
2451 num_ports = 0;
2452
2453 while (*pos) {
2454 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2455 if (nports == NULL) {
2456 os_free(ports);
2457 return -1;
2458 }
2459 ports = nports;
2460 ports[num_ports++] = atoi(pos);
2461
2462 pos = os_strchr(pos, ',');
2463 if (pos == NULL)
2464 break;
2465 pos++;
2466 }
2467
2468 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
2469 if (nports == NULL) {
2470 os_free(ports);
2471 return -1;
2472 }
2473 ports = nports;
2474 ports[num_ports] = -1;
2475
2476 port[cred->num_req_conn_capab] = ports;
2477 cred->num_req_conn_capab++;
2478 return 0;
2479}
2480
2481
Dmitry Shmidt04949592012-07-19 12:16:46 -07002482int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2483 const char *value, int line)
2484{
2485 char *val;
2486 size_t len;
2487
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002488 if (os_strcmp(var, "temporary") == 0) {
2489 cred->temporary = atoi(value);
2490 return 0;
2491 }
2492
Dmitry Shmidt04949592012-07-19 12:16:46 -07002493 if (os_strcmp(var, "priority") == 0) {
2494 cred->priority = atoi(value);
2495 return 0;
2496 }
2497
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002498 if (os_strcmp(var, "sp_priority") == 0) {
2499 int prio = atoi(value);
2500 if (prio < 0 || prio > 255)
2501 return -1;
2502 cred->sp_priority = prio;
2503 return 0;
2504 }
2505
Dmitry Shmidt04949592012-07-19 12:16:46 -07002506 if (os_strcmp(var, "pcsc") == 0) {
2507 cred->pcsc = atoi(value);
2508 return 0;
2509 }
2510
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002511 if (os_strcmp(var, "eap") == 0) {
2512 struct eap_method_type method;
2513 method.method = eap_peer_get_type(value, &method.vendor);
2514 if (method.vendor == EAP_VENDOR_IETF &&
2515 method.method == EAP_TYPE_NONE) {
2516 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2517 "for a credential", line, value);
2518 return -1;
2519 }
2520 os_free(cred->eap_method);
2521 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2522 if (cred->eap_method == NULL)
2523 return -1;
2524 os_memcpy(cred->eap_method, &method, sizeof(method));
2525 return 0;
2526 }
2527
2528 if (os_strcmp(var, "password") == 0 &&
2529 os_strncmp(value, "ext:", 4) == 0) {
2530 os_free(cred->password);
2531 cred->password = os_strdup(value);
2532 cred->ext_password = 1;
2533 return 0;
2534 }
2535
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002536 if (os_strcmp(var, "update_identifier") == 0) {
2537 cred->update_identifier = atoi(value);
2538 return 0;
2539 }
2540
2541 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
2542 cred->min_dl_bandwidth_home = atoi(value);
2543 return 0;
2544 }
2545
2546 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
2547 cred->min_ul_bandwidth_home = atoi(value);
2548 return 0;
2549 }
2550
2551 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
2552 cred->min_dl_bandwidth_roaming = atoi(value);
2553 return 0;
2554 }
2555
2556 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
2557 cred->min_ul_bandwidth_roaming = atoi(value);
2558 return 0;
2559 }
2560
2561 if (os_strcmp(var, "max_bss_load") == 0) {
2562 cred->max_bss_load = atoi(value);
2563 return 0;
2564 }
2565
2566 if (os_strcmp(var, "req_conn_capab") == 0)
2567 return wpa_config_set_cred_req_conn_capab(cred, value);
2568
2569 if (os_strcmp(var, "ocsp") == 0) {
2570 cred->ocsp = atoi(value);
2571 return 0;
2572 }
2573
Dmitry Shmidt04949592012-07-19 12:16:46 -07002574 val = wpa_config_parse_string(value, &len);
2575 if (val == NULL) {
2576 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2577 "value '%s'.", line, var, value);
2578 return -1;
2579 }
2580
2581 if (os_strcmp(var, "realm") == 0) {
2582 os_free(cred->realm);
2583 cred->realm = val;
2584 return 0;
2585 }
2586
2587 if (os_strcmp(var, "username") == 0) {
2588 os_free(cred->username);
2589 cred->username = val;
2590 return 0;
2591 }
2592
2593 if (os_strcmp(var, "password") == 0) {
2594 os_free(cred->password);
2595 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002596 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002597 return 0;
2598 }
2599
2600 if (os_strcmp(var, "ca_cert") == 0) {
2601 os_free(cred->ca_cert);
2602 cred->ca_cert = val;
2603 return 0;
2604 }
2605
2606 if (os_strcmp(var, "client_cert") == 0) {
2607 os_free(cred->client_cert);
2608 cred->client_cert = val;
2609 return 0;
2610 }
2611
2612 if (os_strcmp(var, "private_key") == 0) {
2613 os_free(cred->private_key);
2614 cred->private_key = val;
2615 return 0;
2616 }
2617
2618 if (os_strcmp(var, "private_key_passwd") == 0) {
2619 os_free(cred->private_key_passwd);
2620 cred->private_key_passwd = val;
2621 return 0;
2622 }
2623
2624 if (os_strcmp(var, "imsi") == 0) {
2625 os_free(cred->imsi);
2626 cred->imsi = val;
2627 return 0;
2628 }
2629
2630 if (os_strcmp(var, "milenage") == 0) {
2631 os_free(cred->milenage);
2632 cred->milenage = val;
2633 return 0;
2634 }
2635
Dmitry Shmidt051af732013-10-22 13:52:46 -07002636 if (os_strcmp(var, "domain_suffix_match") == 0) {
2637 os_free(cred->domain_suffix_match);
2638 cred->domain_suffix_match = val;
2639 return 0;
2640 }
2641
Dmitry Shmidt04949592012-07-19 12:16:46 -07002642 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002643 char **new_domain;
2644 new_domain = os_realloc_array(cred->domain,
2645 cred->num_domain + 1,
2646 sizeof(char *));
2647 if (new_domain == NULL) {
2648 os_free(val);
2649 return -1;
2650 }
2651 new_domain[cred->num_domain++] = val;
2652 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002653 return 0;
2654 }
2655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002656 if (os_strcmp(var, "phase1") == 0) {
2657 os_free(cred->phase1);
2658 cred->phase1 = val;
2659 return 0;
2660 }
2661
2662 if (os_strcmp(var, "phase2") == 0) {
2663 os_free(cred->phase2);
2664 cred->phase2 = val;
2665 return 0;
2666 }
2667
2668 if (os_strcmp(var, "roaming_consortium") == 0) {
2669 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2670 wpa_printf(MSG_ERROR, "Line %d: invalid "
2671 "roaming_consortium length %d (3..15 "
2672 "expected)", line, (int) len);
2673 os_free(val);
2674 return -1;
2675 }
2676 os_memcpy(cred->roaming_consortium, val, len);
2677 cred->roaming_consortium_len = len;
2678 os_free(val);
2679 return 0;
2680 }
2681
Dmitry Shmidt051af732013-10-22 13:52:46 -07002682 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2683 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2684 {
2685 wpa_printf(MSG_ERROR, "Line %d: invalid "
2686 "required_roaming_consortium length %d "
2687 "(3..15 expected)", line, (int) len);
2688 os_free(val);
2689 return -1;
2690 }
2691 os_memcpy(cred->required_roaming_consortium, val, len);
2692 cred->required_roaming_consortium_len = len;
2693 os_free(val);
2694 return 0;
2695 }
2696
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002697 if (os_strcmp(var, "excluded_ssid") == 0) {
2698 struct excluded_ssid *e;
2699
2700 if (len > MAX_SSID_LEN) {
2701 wpa_printf(MSG_ERROR, "Line %d: invalid "
2702 "excluded_ssid length %d", line, (int) len);
2703 os_free(val);
2704 return -1;
2705 }
2706
2707 e = os_realloc_array(cred->excluded_ssid,
2708 cred->num_excluded_ssid + 1,
2709 sizeof(struct excluded_ssid));
2710 if (e == NULL) {
2711 os_free(val);
2712 return -1;
2713 }
2714 cred->excluded_ssid = e;
2715
2716 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2717 os_memcpy(e->ssid, val, len);
2718 e->ssid_len = len;
2719
2720 os_free(val);
2721
2722 return 0;
2723 }
2724
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002725 if (os_strcmp(var, "roaming_partner") == 0) {
2726 struct roaming_partner *p;
2727 char *pos;
2728
2729 p = os_realloc_array(cred->roaming_partner,
2730 cred->num_roaming_partner + 1,
2731 sizeof(struct roaming_partner));
2732 if (p == NULL) {
2733 os_free(val);
2734 return -1;
2735 }
2736 cred->roaming_partner = p;
2737
2738 p = &cred->roaming_partner[cred->num_roaming_partner];
2739
2740 pos = os_strchr(val, ',');
2741 if (pos == NULL) {
2742 os_free(val);
2743 return -1;
2744 }
2745 *pos++ = '\0';
2746 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2747 os_free(val);
2748 return -1;
2749 }
2750 os_memcpy(p->fqdn, val, pos - val);
2751
2752 p->exact_match = atoi(pos);
2753
2754 pos = os_strchr(pos, ',');
2755 if (pos == NULL) {
2756 os_free(val);
2757 return -1;
2758 }
2759 *pos++ = '\0';
2760
2761 p->priority = atoi(pos);
2762
2763 pos = os_strchr(pos, ',');
2764 if (pos == NULL) {
2765 os_free(val);
2766 return -1;
2767 }
2768 *pos++ = '\0';
2769
2770 if (os_strlen(pos) >= sizeof(p->country)) {
2771 os_free(val);
2772 return -1;
2773 }
2774 os_memcpy(p->country, pos, os_strlen(pos) + 1);
2775
2776 cred->num_roaming_partner++;
2777 os_free(val);
2778
2779 return 0;
2780 }
2781
2782 if (os_strcmp(var, "provisioning_sp") == 0) {
2783 os_free(cred->provisioning_sp);
2784 cred->provisioning_sp = val;
2785 return 0;
2786 }
2787
Dmitry Shmidt04949592012-07-19 12:16:46 -07002788 if (line) {
2789 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2790 line, var);
2791 }
2792
2793 os_free(val);
2794
2795 return -1;
2796}
2797
2798
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07002799char * alloc_int_str(int val)
2800{
2801 char *buf;
2802
2803 buf = os_malloc(20);
2804 if (buf == NULL)
2805 return NULL;
2806 os_snprintf(buf, 20, "%d", val);
2807 return buf;
2808}
2809
2810
2811char * alloc_strdup(const char *str)
2812{
2813 if (str == NULL)
2814 return NULL;
2815 return os_strdup(str);
2816}
2817
2818
2819char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
2820{
2821 if (os_strcmp(var, "temporary") == 0)
2822 return alloc_int_str(cred->temporary);
2823
2824 if (os_strcmp(var, "priority") == 0)
2825 return alloc_int_str(cred->priority);
2826
2827 if (os_strcmp(var, "sp_priority") == 0)
2828 return alloc_int_str(cred->sp_priority);
2829
2830 if (os_strcmp(var, "pcsc") == 0)
2831 return alloc_int_str(cred->pcsc);
2832
2833 if (os_strcmp(var, "eap") == 0) {
2834 if (!cred->eap_method)
2835 return NULL;
2836 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
2837 cred->eap_method[0].method));
2838 }
2839
2840 if (os_strcmp(var, "update_identifier") == 0)
2841 return alloc_int_str(cred->update_identifier);
2842
2843 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
2844 return alloc_int_str(cred->min_dl_bandwidth_home);
2845
2846 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
2847 return alloc_int_str(cred->min_ul_bandwidth_home);
2848
2849 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
2850 return alloc_int_str(cred->min_dl_bandwidth_roaming);
2851
2852 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
2853 return alloc_int_str(cred->min_ul_bandwidth_roaming);
2854
2855 if (os_strcmp(var, "max_bss_load") == 0)
2856 return alloc_int_str(cred->max_bss_load);
2857
2858 if (os_strcmp(var, "req_conn_capab") == 0) {
2859 unsigned int i;
2860 char *buf, *end, *pos;
2861 int ret;
2862
2863 if (!cred->num_req_conn_capab)
2864 return NULL;
2865
2866 buf = os_malloc(4000);
2867 if (buf == NULL)
2868 return NULL;
2869 pos = buf;
2870 end = pos + 4000;
2871 for (i = 0; i < cred->num_req_conn_capab; i++) {
2872 int *ports;
2873
2874 ret = os_snprintf(pos, end - pos, "%s%u",
2875 i > 0 ? "\n" : "",
2876 cred->req_conn_capab_proto[i]);
2877 if (ret < 0 || ret >= end - pos)
2878 return buf;
2879 pos += ret;
2880
2881 ports = cred->req_conn_capab_port[i];
2882 if (ports) {
2883 int j;
2884 for (j = 0; ports[j] != -1; j++) {
2885 ret = os_snprintf(pos, end - pos,
2886 "%s%d",
2887 j > 0 ? "," : ":",
2888 ports[j]);
2889 if (ret < 0 || ret >= end - pos)
2890 return buf;
2891 pos += ret;
2892 }
2893 }
2894 }
2895
2896 return buf;
2897 }
2898
2899 if (os_strcmp(var, "ocsp") == 0)
2900 return alloc_int_str(cred->ocsp);
2901
2902 if (os_strcmp(var, "realm") == 0)
2903 return alloc_strdup(cred->realm);
2904
2905 if (os_strcmp(var, "username") == 0)
2906 return alloc_strdup(cred->username);
2907
2908 if (os_strcmp(var, "password") == 0) {
2909 if (!cred->password)
2910 return NULL;
2911 return alloc_strdup("*");
2912 }
2913
2914 if (os_strcmp(var, "ca_cert") == 0)
2915 return alloc_strdup(cred->ca_cert);
2916
2917 if (os_strcmp(var, "client_cert") == 0)
2918 return alloc_strdup(cred->client_cert);
2919
2920 if (os_strcmp(var, "private_key") == 0)
2921 return alloc_strdup(cred->private_key);
2922
2923 if (os_strcmp(var, "private_key_passwd") == 0) {
2924 if (!cred->private_key_passwd)
2925 return NULL;
2926 return alloc_strdup("*");
2927 }
2928
2929 if (os_strcmp(var, "imsi") == 0)
2930 return alloc_strdup(cred->imsi);
2931
2932 if (os_strcmp(var, "milenage") == 0) {
2933 if (!(cred->milenage))
2934 return NULL;
2935 return alloc_strdup("*");
2936 }
2937
2938 if (os_strcmp(var, "domain_suffix_match") == 0)
2939 return alloc_strdup(cred->domain_suffix_match);
2940
2941 if (os_strcmp(var, "domain") == 0) {
2942 unsigned int i;
2943 char *buf, *end, *pos;
2944 int ret;
2945
2946 if (!cred->num_domain)
2947 return NULL;
2948
2949 buf = os_malloc(4000);
2950 if (buf == NULL)
2951 return NULL;
2952 pos = buf;
2953 end = pos + 4000;
2954
2955 for (i = 0; i < cred->num_domain; i++) {
2956 ret = os_snprintf(pos, end - pos, "%s%s",
2957 i > 0 ? "\n" : "", cred->domain[i]);
2958 if (ret < 0 || ret >= end - pos)
2959 return buf;
2960 pos += ret;
2961 }
2962
2963 return buf;
2964 }
2965
2966 if (os_strcmp(var, "phase1") == 0)
2967 return alloc_strdup(cred->phase1);
2968
2969 if (os_strcmp(var, "phase2") == 0)
2970 return alloc_strdup(cred->phase2);
2971
2972 if (os_strcmp(var, "roaming_consortium") == 0) {
2973 size_t buflen;
2974 char *buf;
2975
2976 if (!cred->roaming_consortium_len)
2977 return NULL;
2978 buflen = cred->roaming_consortium_len * 2 + 1;
2979 buf = os_malloc(buflen);
2980 if (buf == NULL)
2981 return NULL;
2982 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
2983 cred->roaming_consortium_len);
2984 return buf;
2985 }
2986
2987 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2988 size_t buflen;
2989 char *buf;
2990
2991 if (!cred->required_roaming_consortium_len)
2992 return NULL;
2993 buflen = cred->required_roaming_consortium_len * 2 + 1;
2994 buf = os_malloc(buflen);
2995 if (buf == NULL)
2996 return NULL;
2997 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
2998 cred->required_roaming_consortium_len);
2999 return buf;
3000 }
3001
3002 if (os_strcmp(var, "excluded_ssid") == 0) {
3003 unsigned int i;
3004 char *buf, *end, *pos;
3005
3006 if (!cred->num_excluded_ssid)
3007 return NULL;
3008
3009 buf = os_malloc(4000);
3010 if (buf == NULL)
3011 return NULL;
3012 pos = buf;
3013 end = pos + 4000;
3014
3015 for (i = 0; i < cred->num_excluded_ssid; i++) {
3016 struct excluded_ssid *e;
3017 int ret;
3018
3019 e = &cred->excluded_ssid[i];
3020 ret = os_snprintf(pos, end - pos, "%s%s",
3021 i > 0 ? "\n" : "",
3022 wpa_ssid_txt(e->ssid, e->ssid_len));
3023 if (ret < 0 || ret >= end - pos)
3024 return buf;
3025 pos += ret;
3026 }
3027
3028 return buf;
3029 }
3030
3031 if (os_strcmp(var, "roaming_partner") == 0) {
3032 unsigned int i;
3033 char *buf, *end, *pos;
3034
3035 if (!cred->num_roaming_partner)
3036 return NULL;
3037
3038 buf = os_malloc(4000);
3039 if (buf == NULL)
3040 return NULL;
3041 pos = buf;
3042 end = pos + 4000;
3043
3044 for (i = 0; i < cred->num_roaming_partner; i++) {
3045 struct roaming_partner *p;
3046 int ret;
3047
3048 p = &cred->roaming_partner[i];
3049 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3050 i > 0 ? "\n" : "",
3051 p->fqdn, p->exact_match, p->priority,
3052 p->country);
3053 if (ret < 0 || ret >= end - pos)
3054 return buf;
3055 pos += ret;
3056 }
3057
3058 return buf;
3059 }
3060
3061 if (os_strcmp(var, "provisioning_sp") == 0)
3062 return alloc_strdup(cred->provisioning_sp);
3063
3064 return NULL;
3065}
3066
3067
Dmitry Shmidt04949592012-07-19 12:16:46 -07003068struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3069{
3070 struct wpa_cred *cred;
3071
3072 cred = config->cred;
3073 while (cred) {
3074 if (id == cred->id)
3075 break;
3076 cred = cred->next;
3077 }
3078
3079 return cred;
3080}
3081
3082
3083struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3084{
3085 int id;
3086 struct wpa_cred *cred, *last = NULL;
3087
3088 id = -1;
3089 cred = config->cred;
3090 while (cred) {
3091 if (cred->id > id)
3092 id = cred->id;
3093 last = cred;
3094 cred = cred->next;
3095 }
3096 id++;
3097
3098 cred = os_zalloc(sizeof(*cred));
3099 if (cred == NULL)
3100 return NULL;
3101 cred->id = id;
3102 if (last)
3103 last->next = cred;
3104 else
3105 config->cred = cred;
3106
3107 return cred;
3108}
3109
3110
3111int wpa_config_remove_cred(struct wpa_config *config, int id)
3112{
3113 struct wpa_cred *cred, *prev = NULL;
3114
3115 cred = config->cred;
3116 while (cred) {
3117 if (id == cred->id)
3118 break;
3119 prev = cred;
3120 cred = cred->next;
3121 }
3122
3123 if (cred == NULL)
3124 return -1;
3125
3126 if (prev)
3127 prev->next = cred->next;
3128 else
3129 config->cred = cred->next;
3130
3131 wpa_config_free_cred(cred);
3132 return 0;
3133}
3134
3135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136#ifndef CONFIG_NO_CONFIG_BLOBS
3137/**
3138 * wpa_config_get_blob - Get a named configuration blob
3139 * @config: Configuration data from wpa_config_read()
3140 * @name: Name of the blob
3141 * Returns: Pointer to blob data or %NULL if not found
3142 */
3143const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3144 const char *name)
3145{
3146 struct wpa_config_blob *blob = config->blobs;
3147
3148 while (blob) {
3149 if (os_strcmp(blob->name, name) == 0)
3150 return blob;
3151 blob = blob->next;
3152 }
3153 return NULL;
3154}
3155
3156
3157/**
3158 * wpa_config_set_blob - Set or add a named configuration blob
3159 * @config: Configuration data from wpa_config_read()
3160 * @blob: New value for the blob
3161 *
3162 * Adds a new configuration blob or replaces the current value of an existing
3163 * blob.
3164 */
3165void wpa_config_set_blob(struct wpa_config *config,
3166 struct wpa_config_blob *blob)
3167{
3168 wpa_config_remove_blob(config, blob->name);
3169 blob->next = config->blobs;
3170 config->blobs = blob;
3171}
3172
3173
3174/**
3175 * wpa_config_free_blob - Free blob data
3176 * @blob: Pointer to blob to be freed
3177 */
3178void wpa_config_free_blob(struct wpa_config_blob *blob)
3179{
3180 if (blob) {
3181 os_free(blob->name);
3182 os_free(blob->data);
3183 os_free(blob);
3184 }
3185}
3186
3187
3188/**
3189 * wpa_config_remove_blob - Remove a named configuration blob
3190 * @config: Configuration data from wpa_config_read()
3191 * @name: Name of the blob to remove
3192 * Returns: 0 if blob was removed or -1 if blob was not found
3193 */
3194int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3195{
3196 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3197
3198 while (pos) {
3199 if (os_strcmp(pos->name, name) == 0) {
3200 if (prev)
3201 prev->next = pos->next;
3202 else
3203 config->blobs = pos->next;
3204 wpa_config_free_blob(pos);
3205 return 0;
3206 }
3207 prev = pos;
3208 pos = pos->next;
3209 }
3210
3211 return -1;
3212}
3213#endif /* CONFIG_NO_CONFIG_BLOBS */
3214
3215
3216/**
3217 * wpa_config_alloc_empty - Allocate an empty configuration
3218 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3219 * socket
3220 * @driver_param: Driver parameters
3221 * Returns: Pointer to allocated configuration data or %NULL on failure
3222 */
3223struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3224 const char *driver_param)
3225{
3226 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003227 const int aCWmin = 4, aCWmax = 10;
3228 const struct hostapd_wmm_ac_params ac_bk =
3229 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3230 const struct hostapd_wmm_ac_params ac_be =
3231 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3232 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
3233 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
3234 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
3235 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236
3237 config = os_zalloc(sizeof(*config));
3238 if (config == NULL)
3239 return NULL;
3240 config->eapol_version = DEFAULT_EAPOL_VERSION;
3241 config->ap_scan = DEFAULT_AP_SCAN;
3242 config->fast_reauth = DEFAULT_FAST_REAUTH;
3243 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
3244 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003245 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
3247 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3248 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
3249 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003250 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003251 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003252 config->wmm_ac_params[0] = ac_be;
3253 config->wmm_ac_params[1] = ac_bk;
3254 config->wmm_ac_params[2] = ac_vi;
3255 config->wmm_ac_params[3] = ac_vo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256
3257 if (ctrl_interface)
3258 config->ctrl_interface = os_strdup(ctrl_interface);
3259 if (driver_param)
3260 config->driver_param = os_strdup(driver_param);
3261
3262 return config;
3263}
3264
3265
3266#ifndef CONFIG_NO_STDOUT_DEBUG
3267/**
3268 * wpa_config_debug_dump_networks - Debug dump of configured networks
3269 * @config: Configuration data from wpa_config_read()
3270 */
3271void wpa_config_debug_dump_networks(struct wpa_config *config)
3272{
3273 int prio;
3274 struct wpa_ssid *ssid;
3275
3276 for (prio = 0; prio < config->num_prio; prio++) {
3277 ssid = config->pssid[prio];
3278 wpa_printf(MSG_DEBUG, "Priority group %d",
3279 ssid->priority);
3280 while (ssid) {
3281 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3282 ssid->id,
3283 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3284 ssid = ssid->pnext;
3285 }
3286 }
3287}
3288#endif /* CONFIG_NO_STDOUT_DEBUG */
3289
3290
3291struct global_parse_data {
3292 char *name;
3293 int (*parser)(const struct global_parse_data *data,
3294 struct wpa_config *config, int line, const char *value);
3295 void *param1, *param2, *param3;
3296 unsigned int changed_flag;
3297};
3298
3299
3300static int wpa_global_config_parse_int(const struct global_parse_data *data,
3301 struct wpa_config *config, int line,
3302 const char *pos)
3303{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003304 int val, *dst;
3305 char *end;
3306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003308 val = strtol(pos, &end, 0);
3309 if (*end) {
3310 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3311 line, pos);
3312 return -1;
3313 }
3314 *dst = val;
3315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3317
3318 if (data->param2 && *dst < (long) data->param2) {
3319 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3320 "min_value=%ld)", line, data->name, *dst,
3321 (long) data->param2);
3322 *dst = (long) data->param2;
3323 return -1;
3324 }
3325
3326 if (data->param3 && *dst > (long) data->param3) {
3327 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3328 "max_value=%ld)", line, data->name, *dst,
3329 (long) data->param3);
3330 *dst = (long) data->param3;
3331 return -1;
3332 }
3333
3334 return 0;
3335}
3336
3337
3338static int wpa_global_config_parse_str(const struct global_parse_data *data,
3339 struct wpa_config *config, int line,
3340 const char *pos)
3341{
3342 size_t len;
3343 char **dst, *tmp;
3344
3345 len = os_strlen(pos);
3346 if (data->param2 && len < (size_t) data->param2) {
3347 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3348 "min_len=%ld)", line, data->name,
3349 (unsigned long) len, (long) data->param2);
3350 return -1;
3351 }
3352
3353 if (data->param3 && len > (size_t) data->param3) {
3354 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3355 "max_len=%ld)", line, data->name,
3356 (unsigned long) len, (long) data->param3);
3357 return -1;
3358 }
3359
3360 tmp = os_strdup(pos);
3361 if (tmp == NULL)
3362 return -1;
3363
3364 dst = (char **) (((u8 *) config) + (long) data->param1);
3365 os_free(*dst);
3366 *dst = tmp;
3367 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3368
3369 return 0;
3370}
3371
3372
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003373static int wpa_config_process_bgscan(const struct global_parse_data *data,
3374 struct wpa_config *config, int line,
3375 const char *pos)
3376{
3377 size_t len;
3378 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08003379 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003380
3381 tmp = wpa_config_parse_string(pos, &len);
3382 if (tmp == NULL) {
3383 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3384 line, data->name);
3385 return -1;
3386 }
3387
Dmitry Shmidt97672262014-02-03 13:02:54 -08003388 res = wpa_global_config_parse_str(data, config, line, tmp);
3389 os_free(tmp);
3390 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003391}
3392
3393
Dmitry Shmidt04949592012-07-19 12:16:46 -07003394static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3395 struct wpa_config *config, int line,
3396 const char *pos)
3397{
3398 size_t len;
3399 struct wpabuf **dst, *tmp;
3400
3401 len = os_strlen(pos);
3402 if (len & 0x01)
3403 return -1;
3404
3405 tmp = wpabuf_alloc(len / 2);
3406 if (tmp == NULL)
3407 return -1;
3408
3409 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3410 wpabuf_free(tmp);
3411 return -1;
3412 }
3413
3414 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3415 wpabuf_free(*dst);
3416 *dst = tmp;
3417 wpa_printf(MSG_DEBUG, "%s", data->name);
3418
3419 return 0;
3420}
3421
3422
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003423static int wpa_config_process_freq_list(const struct global_parse_data *data,
3424 struct wpa_config *config, int line,
3425 const char *value)
3426{
3427 int *freqs;
3428
3429 freqs = wpa_config_parse_int_array(value);
3430 if (freqs == NULL)
3431 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003432 if (freqs[0] == 0) {
3433 os_free(freqs);
3434 freqs = NULL;
3435 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003436 os_free(config->freq_list);
3437 config->freq_list = freqs;
3438 return 0;
3439}
3440
3441
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003442#ifdef CONFIG_P2P
3443static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3444 struct wpa_config *config, int line,
3445 const char *pos)
3446{
3447 u32 *dst;
3448 struct hostapd_ip_addr addr;
3449
3450 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3451 return -1;
3452 if (addr.af != AF_INET)
3453 return -1;
3454
3455 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3456 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3457 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3458 WPA_GET_BE32((u8 *) dst));
3459
3460 return 0;
3461}
3462#endif /* CONFIG_P2P */
3463
3464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465static int wpa_config_process_country(const struct global_parse_data *data,
3466 struct wpa_config *config, int line,
3467 const char *pos)
3468{
3469 if (!pos[0] || !pos[1]) {
3470 wpa_printf(MSG_DEBUG, "Invalid country set");
3471 return -1;
3472 }
3473 config->country[0] = pos[0];
3474 config->country[1] = pos[1];
3475 wpa_printf(MSG_DEBUG, "country='%c%c'",
3476 config->country[0], config->country[1]);
3477 return 0;
3478}
3479
3480
3481static int wpa_config_process_load_dynamic_eap(
3482 const struct global_parse_data *data, struct wpa_config *config,
3483 int line, const char *so)
3484{
3485 int ret;
3486 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3487 ret = eap_peer_method_load(so);
3488 if (ret == -2) {
3489 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3490 "reloading.");
3491 } else if (ret) {
3492 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3493 "method '%s'.", line, so);
3494 return -1;
3495 }
3496
3497 return 0;
3498}
3499
3500
3501#ifdef CONFIG_WPS
3502
3503static int wpa_config_process_uuid(const struct global_parse_data *data,
3504 struct wpa_config *config, int line,
3505 const char *pos)
3506{
3507 char buf[40];
3508 if (uuid_str2bin(pos, config->uuid)) {
3509 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3510 return -1;
3511 }
3512 uuid_bin2str(config->uuid, buf, sizeof(buf));
3513 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3514 return 0;
3515}
3516
3517
3518static int wpa_config_process_device_type(
3519 const struct global_parse_data *data,
3520 struct wpa_config *config, int line, const char *pos)
3521{
3522 return wps_dev_type_str2bin(pos, config->device_type);
3523}
3524
3525
3526static int wpa_config_process_os_version(const struct global_parse_data *data,
3527 struct wpa_config *config, int line,
3528 const char *pos)
3529{
3530 if (hexstr2bin(pos, config->os_version, 4)) {
3531 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3532 return -1;
3533 }
3534 wpa_printf(MSG_DEBUG, "os_version=%08x",
3535 WPA_GET_BE32(config->os_version));
3536 return 0;
3537}
3538
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539
3540static int wpa_config_process_wps_vendor_ext_m1(
3541 const struct global_parse_data *data,
3542 struct wpa_config *config, int line, const char *pos)
3543{
3544 struct wpabuf *tmp;
3545 int len = os_strlen(pos) / 2;
3546 u8 *p;
3547
3548 if (!len) {
3549 wpa_printf(MSG_ERROR, "Line %d: "
3550 "invalid wps_vendor_ext_m1", line);
3551 return -1;
3552 }
3553
3554 tmp = wpabuf_alloc(len);
3555 if (tmp) {
3556 p = wpabuf_put(tmp, len);
3557
3558 if (hexstr2bin(pos, p, len)) {
3559 wpa_printf(MSG_ERROR, "Line %d: "
3560 "invalid wps_vendor_ext_m1", line);
3561 wpabuf_free(tmp);
3562 return -1;
3563 }
3564
3565 wpabuf_free(config->wps_vendor_ext_m1);
3566 config->wps_vendor_ext_m1 = tmp;
3567 } else {
3568 wpa_printf(MSG_ERROR, "Can not allocate "
3569 "memory for wps_vendor_ext_m1");
3570 return -1;
3571 }
3572
3573 return 0;
3574}
3575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576#endif /* CONFIG_WPS */
3577
3578#ifdef CONFIG_P2P
3579static int wpa_config_process_sec_device_type(
3580 const struct global_parse_data *data,
3581 struct wpa_config *config, int line, const char *pos)
3582{
3583 int idx;
3584
3585 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3586 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3587 "items", line);
3588 return -1;
3589 }
3590
3591 idx = config->num_sec_device_types;
3592
3593 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3594 return -1;
3595
3596 config->num_sec_device_types++;
3597 return 0;
3598}
Dmitry Shmidt04949592012-07-19 12:16:46 -07003599
3600
3601static int wpa_config_process_p2p_pref_chan(
3602 const struct global_parse_data *data,
3603 struct wpa_config *config, int line, const char *pos)
3604{
3605 struct p2p_channel *pref = NULL, *n;
3606 unsigned int num = 0;
3607 const char *pos2;
3608 u8 op_class, chan;
3609
3610 /* format: class:chan,class:chan,... */
3611
3612 while (*pos) {
3613 op_class = atoi(pos);
3614 pos2 = os_strchr(pos, ':');
3615 if (pos2 == NULL)
3616 goto fail;
3617 pos2++;
3618 chan = atoi(pos2);
3619
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003620 n = os_realloc_array(pref, num + 1,
3621 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003622 if (n == NULL)
3623 goto fail;
3624 pref = n;
3625 pref[num].op_class = op_class;
3626 pref[num].chan = chan;
3627 num++;
3628
3629 pos = os_strchr(pos2, ',');
3630 if (pos == NULL)
3631 break;
3632 pos++;
3633 }
3634
3635 os_free(config->p2p_pref_chan);
3636 config->p2p_pref_chan = pref;
3637 config->num_p2p_pref_chan = num;
3638 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3639 (u8 *) config->p2p_pref_chan,
3640 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3641
3642 return 0;
3643
3644fail:
3645 os_free(pref);
3646 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3647 return -1;
3648}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003649
3650
3651static int wpa_config_process_p2p_no_go_freq(
3652 const struct global_parse_data *data,
3653 struct wpa_config *config, int line, const char *pos)
3654{
3655 int ret;
3656
3657 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3658 if (ret < 0) {
3659 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3660 return -1;
3661 }
3662
3663 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3664 config->p2p_no_go_freq.num);
3665
3666 return 0;
3667}
3668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669#endif /* CONFIG_P2P */
3670
3671
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003672static int wpa_config_process_hessid(
3673 const struct global_parse_data *data,
3674 struct wpa_config *config, int line, const char *pos)
3675{
3676 if (hwaddr_aton2(pos, config->hessid) < 0) {
3677 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3678 line, pos);
3679 return -1;
3680 }
3681
3682 return 0;
3683}
3684
3685
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003686static int wpa_config_process_sae_groups(
3687 const struct global_parse_data *data,
3688 struct wpa_config *config, int line, const char *pos)
3689{
3690 int *groups = wpa_config_parse_int_array(pos);
3691 if (groups == NULL) {
3692 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3693 line, pos);
3694 return -1;
3695 }
3696
3697 os_free(config->sae_groups);
3698 config->sae_groups = groups;
3699
3700 return 0;
3701}
3702
3703
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003704static int wpa_config_process_ap_vendor_elements(
3705 const struct global_parse_data *data,
3706 struct wpa_config *config, int line, const char *pos)
3707{
3708 struct wpabuf *tmp;
3709 int len = os_strlen(pos) / 2;
3710 u8 *p;
3711
3712 if (!len) {
3713 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3714 line);
3715 return -1;
3716 }
3717
3718 tmp = wpabuf_alloc(len);
3719 if (tmp) {
3720 p = wpabuf_put(tmp, len);
3721
3722 if (hexstr2bin(pos, p, len)) {
3723 wpa_printf(MSG_ERROR, "Line %d: invalid "
3724 "ap_vendor_elements", line);
3725 wpabuf_free(tmp);
3726 return -1;
3727 }
3728
3729 wpabuf_free(config->ap_vendor_elements);
3730 config->ap_vendor_elements = tmp;
3731 } else {
3732 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3733 "ap_vendor_elements");
3734 return -1;
3735 }
3736
3737 return 0;
3738}
3739
3740
Dmitry Shmidt56052862013-10-04 10:23:25 -07003741#ifdef CONFIG_CTRL_IFACE
3742static int wpa_config_process_no_ctrl_interface(
3743 const struct global_parse_data *data,
3744 struct wpa_config *config, int line, const char *pos)
3745{
3746 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3747 os_free(config->ctrl_interface);
3748 config->ctrl_interface = NULL;
3749 return 0;
3750}
3751#endif /* CONFIG_CTRL_IFACE */
3752
3753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754#ifdef OFFSET
3755#undef OFFSET
3756#endif /* OFFSET */
3757/* OFFSET: Get offset of a variable within the wpa_config structure */
3758#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3759
3760#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3761#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3762#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3763#define INT(f) _INT(f), NULL, NULL
3764#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3765#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3766#define STR(f) _STR(f), NULL, NULL
3767#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003769#define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770
3771static const struct global_parse_data global_fields[] = {
3772#ifdef CONFIG_CTRL_IFACE
3773 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003774 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 { STR(ctrl_interface_group), 0 } /* deprecated */,
3776#endif /* CONFIG_CTRL_IFACE */
3777 { INT_RANGE(eapol_version, 1, 2), 0 },
3778 { INT(ap_scan), 0 },
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08003779 { FUNC(bgscan), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003780 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 { INT(fast_reauth), 0 },
3782 { STR(opensc_engine_path), 0 },
3783 { STR(pkcs11_engine_path), 0 },
3784 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003785 { STR(pcsc_reader), 0 },
3786 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07003787 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 { STR(driver_param), 0 },
3789 { INT(dot11RSNAConfigPMKLifetime), 0 },
3790 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3791 { INT(dot11RSNAConfigSATimeout), 0 },
3792#ifndef CONFIG_NO_CONFIG_WRITE
3793 { INT(update_config), 0 },
3794#endif /* CONFIG_NO_CONFIG_WRITE */
3795 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3796#ifdef CONFIG_WPS
3797 { FUNC(uuid), CFG_CHANGED_UUID },
3798 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3799 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3800 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3801 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3802 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3803 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3804 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3805 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3806 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003807 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808#endif /* CONFIG_WPS */
3809#ifdef CONFIG_P2P
3810 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3811 { INT(p2p_listen_reg_class), 0 },
3812 { INT(p2p_listen_channel), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07003813 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3814 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3816 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3817 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3818 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3819 { INT(p2p_group_idle), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003820 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003821 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
3822 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003823 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003824 { INT(p2p_go_vht), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003825 { INT(p2p_disabled), 0 },
3826 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003827 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003828 { IPV4(ip_addr_go), 0 },
3829 { IPV4(ip_addr_mask), 0 },
3830 { IPV4(ip_addr_start), 0 },
3831 { IPV4(ip_addr_end), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832#endif /* CONFIG_P2P */
3833 { FUNC(country), CFG_CHANGED_COUNTRY },
3834 { INT(bss_max_count), 0 },
3835 { INT(bss_expiration_age), 0 },
3836 { INT(bss_expiration_scan_count), 0 },
3837 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003838 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 { INT(max_num_sta), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003840 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003841#ifdef CONFIG_HS20
3842 { INT_RANGE(hs20, 0, 1), 0 },
3843#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003844 { INT_RANGE(interworking, 0, 1), 0 },
3845 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07003846 { INT_RANGE(access_network_type, 0, 15), 0 },
3847 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3848 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003849 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3850 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3851 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3852 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3853 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003854 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3855 { INT(p2p_go_max_inactivity), 0 },
3856 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003857 { INT(okc), 0 },
3858 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003859 { FUNC(sae_groups), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003860 { INT(dtim_period), 0 },
3861 { INT(beacon_int), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003862 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003863 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003864 { FUNC(freq_list), 0 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003865 { INT(scan_cur_freq), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003866 { INT(sched_scan_interval), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003867 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003868 { STR(osu_dir), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869};
3870
3871#undef FUNC
3872#undef _INT
3873#undef INT
3874#undef INT_RANGE
3875#undef _STR
3876#undef STR
3877#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07003878#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003879#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003880#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881
3882
3883int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3884{
3885 size_t i;
3886 int ret = 0;
3887
3888 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3889 const struct global_parse_data *field = &global_fields[i];
3890 size_t flen = os_strlen(field->name);
3891 if (os_strncmp(pos, field->name, flen) != 0 ||
3892 pos[flen] != '=')
3893 continue;
3894
3895 if (field->parser(field, config, line, pos + flen + 1)) {
3896 wpa_printf(MSG_ERROR, "Line %d: failed to "
3897 "parse '%s'.", line, pos);
3898 ret = -1;
3899 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003900 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3901 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 config->changed_parameters |= field->changed_flag;
3903 break;
3904 }
3905 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003906#ifdef CONFIG_AP
3907 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3908 char *tmp = os_strchr(pos, '=');
3909 if (tmp == NULL) {
3910 if (line < 0)
3911 return -1;
3912 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3913 "'%s'", line, pos);
3914 return -1;
3915 }
3916 *tmp++ = '\0';
3917 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3918 tmp)) {
3919 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3920 "AC item", line);
3921 return -1;
3922 }
3923 }
3924#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 if (line < 0)
3926 return -1;
3927 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3928 line, pos);
3929 ret = -1;
3930 }
3931
3932 return ret;
3933}