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