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