Improve p2p_client_list updates in configuration file
[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 "crypto/sha1.h"
14 #include "rsn_supp/wpa.h"
15 #include "eap_peer/eap.h"
16 #include "p2p/p2p.h"
17 #include "config.h"
18
19
20 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
21 #define NO_CONFIG_WRITE
22 #endif
23
24 /*
25  * Structure for network configuration parsing. This data is used to implement
26  * a generic parser for each network block variable. The table of configuration
27  * variables is defined below in this file (ssid_fields[]).
28  */
29 struct parse_data {
30         /* Configuration variable name */
31         char *name;
32
33         /* Parser function for this variable */
34         int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
35                       int line, const char *value);
36
37 #ifndef NO_CONFIG_WRITE
38         /* Writer function (i.e., to get the variable in text format from
39          * internal presentation). */
40         char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
41 #endif /* NO_CONFIG_WRITE */
42
43         /* Variable specific parameters for the parser. */
44         void *param1, *param2, *param3, *param4;
45
46         /* 0 = this variable can be included in debug output and ctrl_iface
47          * 1 = this variable contains key/private data and it must not be
48          *     included in debug output unless explicitly requested. In
49          *     addition, this variable will not be readable through the
50          *     ctrl_iface.
51          */
52         int key_data;
53 };
54
55
56 static int wpa_config_parse_str(const struct parse_data *data,
57                                 struct wpa_ssid *ssid,
58                                 int line, const char *value)
59 {
60         size_t res_len, *dst_len;
61         char **dst, *tmp;
62
63         if (os_strcmp(value, "NULL") == 0) {
64                 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
65                            data->name);
66                 tmp = NULL;
67                 res_len = 0;
68                 goto set;
69         }
70
71         tmp = wpa_config_parse_string(value, &res_len);
72         if (tmp == NULL) {
73                 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
74                            line, data->name,
75                            data->key_data ? "[KEY DATA REMOVED]" : value);
76                 return -1;
77         }
78
79         if (data->key_data) {
80                 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
81                                       (u8 *) tmp, res_len);
82         } else {
83                 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
84                                   (u8 *) tmp, res_len);
85         }
86
87         if (data->param3 && res_len < (size_t) data->param3) {
88                 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
89                            "min_len=%ld)", line, data->name,
90                            (unsigned long) res_len, (long) data->param3);
91                 os_free(tmp);
92                 return -1;
93         }
94
95         if (data->param4 && res_len > (size_t) data->param4) {
96                 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
97                            "max_len=%ld)", line, data->name,
98                            (unsigned long) res_len, (long) data->param4);
99                 os_free(tmp);
100                 return -1;
101         }
102
103 set:
104         dst = (char **) (((u8 *) ssid) + (long) data->param1);
105         dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
106         os_free(*dst);
107         *dst = tmp;
108         if (data->param2)
109                 *dst_len = res_len;
110
111         return 0;
112 }
113
114
115 #ifndef NO_CONFIG_WRITE
116 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
117 {
118         char *buf;
119
120         buf = os_malloc(len + 3);
121         if (buf == NULL)
122                 return NULL;
123         buf[0] = '"';
124         os_memcpy(buf + 1, value, len);
125         buf[len + 1] = '"';
126         buf[len + 2] = '\0';
127
128         return buf;
129 }
130
131
132 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
133 {
134         char *buf;
135
136         buf = os_zalloc(2 * len + 1);
137         if (buf == NULL)
138                 return NULL;
139         wpa_snprintf_hex(buf, 2 * len + 1, value, len);
140
141         return buf;
142 }
143
144
145 static char * wpa_config_write_string(const u8 *value, size_t len)
146 {
147         if (value == NULL)
148                 return NULL;
149
150         if (is_hex(value, len))
151                 return wpa_config_write_string_hex(value, len);
152         else
153                 return wpa_config_write_string_ascii(value, len);
154 }
155
156
157 static char * wpa_config_write_str(const struct parse_data *data,
158                                    struct wpa_ssid *ssid)
159 {
160         size_t len;
161         char **src;
162
163         src = (char **) (((u8 *) ssid) + (long) data->param1);
164         if (*src == NULL)
165                 return NULL;
166
167         if (data->param2)
168                 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
169         else
170                 len = os_strlen(*src);
171
172         return wpa_config_write_string((const u8 *) *src, len);
173 }
174 #endif /* NO_CONFIG_WRITE */
175
176
177 static int wpa_config_parse_int(const struct parse_data *data,
178                                 struct wpa_ssid *ssid,
179                                 int line, const char *value)
180 {
181         int *dst;
182
183         dst = (int *) (((u8 *) ssid) + (long) data->param1);
184         *dst = atoi(value);
185         wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
186
187         if (data->param3 && *dst < (long) data->param3) {
188                 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
189                            "min_value=%ld)", line, data->name, *dst,
190                            (long) data->param3);
191                 *dst = (long) data->param3;
192                 return -1;
193         }
194
195         if (data->param4 && *dst > (long) data->param4) {
196                 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
197                            "max_value=%ld)", line, data->name, *dst,
198                            (long) data->param4);
199                 *dst = (long) data->param4;
200                 return -1;
201         }
202
203         return 0;
204 }
205
206
207 #ifndef NO_CONFIG_WRITE
208 static char * wpa_config_write_int(const struct parse_data *data,
209                                    struct wpa_ssid *ssid)
210 {
211         int *src, res;
212         char *value;
213
214         src = (int *) (((u8 *) ssid) + (long) data->param1);
215
216         value = os_malloc(20);
217         if (value == NULL)
218                 return NULL;
219         res = os_snprintf(value, 20, "%d", *src);
220         if (res < 0 || res >= 20) {
221                 os_free(value);
222                 return NULL;
223         }
224         value[20 - 1] = '\0';
225         return value;
226 }
227 #endif /* NO_CONFIG_WRITE */
228
229
230 static int wpa_config_parse_bssid(const struct parse_data *data,
231                                   struct wpa_ssid *ssid, int line,
232                                   const char *value)
233 {
234         if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
235             os_strcmp(value, "any") == 0) {
236                 ssid->bssid_set = 0;
237                 wpa_printf(MSG_MSGDUMP, "BSSID any");
238                 return 0;
239         }
240         if (hwaddr_aton(value, ssid->bssid)) {
241                 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
242                            line, value);
243                 return -1;
244         }
245         ssid->bssid_set = 1;
246         wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
247         return 0;
248 }
249
250
251 #ifndef NO_CONFIG_WRITE
252 static char * wpa_config_write_bssid(const struct parse_data *data,
253                                      struct wpa_ssid *ssid)
254 {
255         char *value;
256         int res;
257
258         if (!ssid->bssid_set)
259                 return NULL;
260
261         value = os_malloc(20);
262         if (value == NULL)
263                 return NULL;
264         res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
265         if (res < 0 || res >= 20) {
266                 os_free(value);
267                 return NULL;
268         }
269         value[20 - 1] = '\0';
270         return value;
271 }
272 #endif /* NO_CONFIG_WRITE */
273
274
275 static int wpa_config_parse_psk(const struct parse_data *data,
276                                 struct wpa_ssid *ssid, int line,
277                                 const char *value)
278 {
279 #ifdef CONFIG_EXT_PASSWORD
280         if (os_strncmp(value, "ext:", 4) == 0) {
281                 os_free(ssid->passphrase);
282                 ssid->passphrase = NULL;
283                 ssid->psk_set = 0;
284                 os_free(ssid->ext_psk);
285                 ssid->ext_psk = os_strdup(value + 4);
286                 if (ssid->ext_psk == NULL)
287                         return -1;
288                 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
289                            ssid->ext_psk);
290                 return 0;
291         }
292 #endif /* CONFIG_EXT_PASSWORD */
293
294         if (*value == '"') {
295 #ifndef CONFIG_NO_PBKDF2
296                 const char *pos;
297                 size_t len;
298
299                 value++;
300                 pos = os_strrchr(value, '"');
301                 if (pos)
302                         len = pos - value;
303                 else
304                         len = os_strlen(value);
305                 if (len < 8 || len > 63) {
306                         wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
307                                    "length %lu (expected: 8..63) '%s'.",
308                                    line, (unsigned long) len, value);
309                         return -1;
310                 }
311                 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
312                                       (u8 *) value, len);
313                 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
314                     os_memcmp(ssid->passphrase, value, len) == 0)
315                         return 0;
316                 ssid->psk_set = 0;
317                 os_free(ssid->passphrase);
318                 ssid->passphrase = os_malloc(len + 1);
319                 if (ssid->passphrase == NULL)
320                         return -1;
321                 os_memcpy(ssid->passphrase, value, len);
322                 ssid->passphrase[len] = '\0';
323                 return 0;
324 #else /* CONFIG_NO_PBKDF2 */
325                 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
326                            "supported.", line);
327                 return -1;
328 #endif /* CONFIG_NO_PBKDF2 */
329         }
330
331         if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
332             value[PMK_LEN * 2] != '\0') {
333                 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
334                            line, value);
335                 return -1;
336         }
337
338         os_free(ssid->passphrase);
339         ssid->passphrase = NULL;
340
341         ssid->psk_set = 1;
342         wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
343         return 0;
344 }
345
346
347 #ifndef NO_CONFIG_WRITE
348 static char * wpa_config_write_psk(const struct parse_data *data,
349                                    struct wpa_ssid *ssid)
350 {
351 #ifdef CONFIG_EXT_PASSWORD
352         if (ssid->ext_psk) {
353                 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
354                 char *buf = os_malloc(len);
355                 if (buf == NULL)
356                         return NULL;
357                 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
358                 return buf;
359         }
360 #endif /* CONFIG_EXT_PASSWORD */
361
362         if (ssid->passphrase)
363                 return wpa_config_write_string_ascii(
364                         (const u8 *) ssid->passphrase,
365                         os_strlen(ssid->passphrase));
366
367         if (ssid->psk_set)
368                 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
369
370         return NULL;
371 }
372 #endif /* NO_CONFIG_WRITE */
373
374
375 static int wpa_config_parse_proto(const struct parse_data *data,
376                                   struct wpa_ssid *ssid, int line,
377                                   const char *value)
378 {
379         int val = 0, last, errors = 0;
380         char *start, *end, *buf;
381
382         buf = os_strdup(value);
383         if (buf == NULL)
384                 return -1;
385         start = buf;
386
387         while (*start != '\0') {
388                 while (*start == ' ' || *start == '\t')
389                         start++;
390                 if (*start == '\0')
391                         break;
392                 end = start;
393                 while (*end != ' ' && *end != '\t' && *end != '\0')
394                         end++;
395                 last = *end == '\0';
396                 *end = '\0';
397                 if (os_strcmp(start, "WPA") == 0)
398                         val |= WPA_PROTO_WPA;
399                 else if (os_strcmp(start, "RSN") == 0 ||
400                          os_strcmp(start, "WPA2") == 0)
401                         val |= WPA_PROTO_RSN;
402                 else {
403                         wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
404                                    line, start);
405                         errors++;
406                 }
407
408                 if (last)
409                         break;
410                 start = end + 1;
411         }
412         os_free(buf);
413
414         if (val == 0) {
415                 wpa_printf(MSG_ERROR,
416                            "Line %d: no proto values configured.", line);
417                 errors++;
418         }
419
420         wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
421         ssid->proto = val;
422         return errors ? -1 : 0;
423 }
424
425
426 #ifndef NO_CONFIG_WRITE
427 static char * wpa_config_write_proto(const struct parse_data *data,
428                                      struct wpa_ssid *ssid)
429 {
430         int first = 1, ret;
431         char *buf, *pos, *end;
432
433         pos = buf = os_zalloc(10);
434         if (buf == NULL)
435                 return NULL;
436         end = buf + 10;
437
438         if (ssid->proto & WPA_PROTO_WPA) {
439                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
440                 if (ret < 0 || ret >= end - pos)
441                         return buf;
442                 pos += ret;
443                 first = 0;
444         }
445
446         if (ssid->proto & WPA_PROTO_RSN) {
447                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
448                 if (ret < 0 || ret >= end - pos)
449                         return buf;
450                 pos += ret;
451                 first = 0;
452         }
453
454         return buf;
455 }
456 #endif /* NO_CONFIG_WRITE */
457
458
459 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
460                                      struct wpa_ssid *ssid, int line,
461                                      const char *value)
462 {
463         int val = 0, last, errors = 0;
464         char *start, *end, *buf;
465
466         buf = os_strdup(value);
467         if (buf == NULL)
468                 return -1;
469         start = buf;
470
471         while (*start != '\0') {
472                 while (*start == ' ' || *start == '\t')
473                         start++;
474                 if (*start == '\0')
475                         break;
476                 end = start;
477                 while (*end != ' ' && *end != '\t' && *end != '\0')
478                         end++;
479                 last = *end == '\0';
480                 *end = '\0';
481                 if (os_strcmp(start, "WPA-PSK") == 0)
482                         val |= WPA_KEY_MGMT_PSK;
483                 else if (os_strcmp(start, "WPA-EAP") == 0)
484                         val |= WPA_KEY_MGMT_IEEE8021X;
485                 else if (os_strcmp(start, "IEEE8021X") == 0)
486                         val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
487                 else if (os_strcmp(start, "NONE") == 0)
488                         val |= WPA_KEY_MGMT_NONE;
489                 else if (os_strcmp(start, "WPA-NONE") == 0)
490                         val |= WPA_KEY_MGMT_WPA_NONE;
491 #ifdef CONFIG_IEEE80211R
492                 else if (os_strcmp(start, "FT-PSK") == 0)
493                         val |= WPA_KEY_MGMT_FT_PSK;
494                 else if (os_strcmp(start, "FT-EAP") == 0)
495                         val |= WPA_KEY_MGMT_FT_IEEE8021X;
496 #endif /* CONFIG_IEEE80211R */
497 #ifdef CONFIG_IEEE80211W
498                 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
499                         val |= WPA_KEY_MGMT_PSK_SHA256;
500                 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
501                         val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
502 #endif /* CONFIG_IEEE80211W */
503 #ifdef CONFIG_WPS
504                 else if (os_strcmp(start, "WPS") == 0)
505                         val |= WPA_KEY_MGMT_WPS;
506 #endif /* CONFIG_WPS */
507                 else {
508                         wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
509                                    line, start);
510                         errors++;
511                 }
512
513                 if (last)
514                         break;
515                 start = end + 1;
516         }
517         os_free(buf);
518
519         if (val == 0) {
520                 wpa_printf(MSG_ERROR,
521                            "Line %d: no key_mgmt values configured.", line);
522                 errors++;
523         }
524
525         wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
526         ssid->key_mgmt = val;
527         return errors ? -1 : 0;
528 }
529
530
531 #ifndef NO_CONFIG_WRITE
532 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
533                                         struct wpa_ssid *ssid)
534 {
535         char *buf, *pos, *end;
536         int ret;
537
538         pos = buf = os_zalloc(50);
539         if (buf == NULL)
540                 return NULL;
541         end = buf + 50;
542
543         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
544                 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
545                                   pos == buf ? "" : " ");
546                 if (ret < 0 || ret >= end - pos) {
547                         end[-1] = '\0';
548                         return buf;
549                 }
550                 pos += ret;
551         }
552
553         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
554                 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
555                                   pos == buf ? "" : " ");
556                 if (ret < 0 || ret >= end - pos) {
557                         end[-1] = '\0';
558                         return buf;
559                 }
560                 pos += ret;
561         }
562
563         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
564                 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
565                                   pos == buf ? "" : " ");
566                 if (ret < 0 || ret >= end - pos) {
567                         end[-1] = '\0';
568                         return buf;
569                 }
570                 pos += ret;
571         }
572
573         if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
574                 ret = os_snprintf(pos, end - pos, "%sNONE",
575                                   pos == buf ? "" : " ");
576                 if (ret < 0 || ret >= end - pos) {
577                         end[-1] = '\0';
578                         return buf;
579                 }
580                 pos += ret;
581         }
582
583         if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
584                 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
585                                   pos == buf ? "" : " ");
586                 if (ret < 0 || ret >= end - pos) {
587                         end[-1] = '\0';
588                         return buf;
589                 }
590                 pos += ret;
591         }
592
593 #ifdef CONFIG_IEEE80211R
594         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
595                 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
596                                    pos == buf ? "" : " ");
597
598         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
599                 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
600                                    pos == buf ? "" : " ");
601 #endif /* CONFIG_IEEE80211R */
602
603 #ifdef CONFIG_IEEE80211W
604         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
605                 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
606                                    pos == buf ? "" : " ");
607
608         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
609                 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
610                                    pos == buf ? "" : " ");
611 #endif /* CONFIG_IEEE80211W */
612
613 #ifdef CONFIG_WPS
614         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
615                 pos += os_snprintf(pos, end - pos, "%sWPS",
616                                    pos == buf ? "" : " ");
617 #endif /* CONFIG_WPS */
618
619         return buf;
620 }
621 #endif /* NO_CONFIG_WRITE */
622
623
624 static int wpa_config_parse_cipher(int line, const char *value)
625 {
626         int val = 0, last;
627         char *start, *end, *buf;
628
629         buf = os_strdup(value);
630         if (buf == NULL)
631                 return -1;
632         start = buf;
633
634         while (*start != '\0') {
635                 while (*start == ' ' || *start == '\t')
636                         start++;
637                 if (*start == '\0')
638                         break;
639                 end = start;
640                 while (*end != ' ' && *end != '\t' && *end != '\0')
641                         end++;
642                 last = *end == '\0';
643                 *end = '\0';
644                 if (os_strcmp(start, "CCMP") == 0)
645                         val |= WPA_CIPHER_CCMP;
646                 else if (os_strcmp(start, "TKIP") == 0)
647                         val |= WPA_CIPHER_TKIP;
648                 else if (os_strcmp(start, "WEP104") == 0)
649                         val |= WPA_CIPHER_WEP104;
650                 else if (os_strcmp(start, "WEP40") == 0)
651                         val |= WPA_CIPHER_WEP40;
652                 else if (os_strcmp(start, "NONE") == 0)
653                         val |= WPA_CIPHER_NONE;
654                 else {
655                         wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
656                                    line, start);
657                         os_free(buf);
658                         return -1;
659                 }
660
661                 if (last)
662                         break;
663                 start = end + 1;
664         }
665         os_free(buf);
666
667         if (val == 0) {
668                 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
669                            line);
670                 return -1;
671         }
672         return val;
673 }
674
675
676 #ifndef NO_CONFIG_WRITE
677 static char * wpa_config_write_cipher(int cipher)
678 {
679         char *buf, *pos, *end;
680         int ret;
681
682         pos = buf = os_zalloc(50);
683         if (buf == NULL)
684                 return NULL;
685         end = buf + 50;
686
687         if (cipher & WPA_CIPHER_CCMP) {
688                 ret = os_snprintf(pos, end - pos, "%sCCMP",
689                                   pos == buf ? "" : " ");
690                 if (ret < 0 || ret >= end - pos) {
691                         end[-1] = '\0';
692                         return buf;
693                 }
694                 pos += ret;
695         }
696
697         if (cipher & WPA_CIPHER_TKIP) {
698                 ret = os_snprintf(pos, end - pos, "%sTKIP",
699                                   pos == buf ? "" : " ");
700                 if (ret < 0 || ret >= end - pos) {
701                         end[-1] = '\0';
702                         return buf;
703                 }
704                 pos += ret;
705         }
706
707         if (cipher & WPA_CIPHER_WEP104) {
708                 ret = os_snprintf(pos, end - pos, "%sWEP104",
709                                   pos == buf ? "" : " ");
710                 if (ret < 0 || ret >= end - pos) {
711                         end[-1] = '\0';
712                         return buf;
713                 }
714                 pos += ret;
715         }
716
717         if (cipher & WPA_CIPHER_WEP40) {
718                 ret = os_snprintf(pos, end - pos, "%sWEP40",
719                                   pos == buf ? "" : " ");
720                 if (ret < 0 || ret >= end - pos) {
721                         end[-1] = '\0';
722                         return buf;
723                 }
724                 pos += ret;
725         }
726
727         if (cipher & WPA_CIPHER_NONE) {
728                 ret = os_snprintf(pos, end - pos, "%sNONE",
729                                   pos == buf ? "" : " ");
730                 if (ret < 0 || ret >= end - pos) {
731                         end[-1] = '\0';
732                         return buf;
733                 }
734                 pos += ret;
735         }
736
737         return buf;
738 }
739 #endif /* NO_CONFIG_WRITE */
740
741
742 static int wpa_config_parse_pairwise(const struct parse_data *data,
743                                      struct wpa_ssid *ssid, int line,
744                                      const char *value)
745 {
746         int val;
747         val = wpa_config_parse_cipher(line, value);
748         if (val == -1)
749                 return -1;
750         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
751                 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
752                            "(0x%x).", line, val);
753                 return -1;
754         }
755
756         wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
757         ssid->pairwise_cipher = val;
758         return 0;
759 }
760
761
762 #ifndef NO_CONFIG_WRITE
763 static char * wpa_config_write_pairwise(const struct parse_data *data,
764                                         struct wpa_ssid *ssid)
765 {
766         return wpa_config_write_cipher(ssid->pairwise_cipher);
767 }
768 #endif /* NO_CONFIG_WRITE */
769
770
771 static int wpa_config_parse_group(const struct parse_data *data,
772                                   struct wpa_ssid *ssid, int line,
773                                   const char *value)
774 {
775         int val;
776         val = wpa_config_parse_cipher(line, value);
777         if (val == -1)
778                 return -1;
779         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
780                     WPA_CIPHER_WEP40)) {
781                 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
782                            "(0x%x).", line, val);
783                 return -1;
784         }
785
786         wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
787         ssid->group_cipher = val;
788         return 0;
789 }
790
791
792 #ifndef NO_CONFIG_WRITE
793 static char * wpa_config_write_group(const struct parse_data *data,
794                                      struct wpa_ssid *ssid)
795 {
796         return wpa_config_write_cipher(ssid->group_cipher);
797 }
798 #endif /* NO_CONFIG_WRITE */
799
800
801 static int wpa_config_parse_auth_alg(const struct parse_data *data,
802                                      struct wpa_ssid *ssid, int line,
803                                      const char *value)
804 {
805         int val = 0, last, errors = 0;
806         char *start, *end, *buf;
807
808         buf = os_strdup(value);
809         if (buf == NULL)
810                 return -1;
811         start = buf;
812
813         while (*start != '\0') {
814                 while (*start == ' ' || *start == '\t')
815                         start++;
816                 if (*start == '\0')
817                         break;
818                 end = start;
819                 while (*end != ' ' && *end != '\t' && *end != '\0')
820                         end++;
821                 last = *end == '\0';
822                 *end = '\0';
823                 if (os_strcmp(start, "OPEN") == 0)
824                         val |= WPA_AUTH_ALG_OPEN;
825                 else if (os_strcmp(start, "SHARED") == 0)
826                         val |= WPA_AUTH_ALG_SHARED;
827                 else if (os_strcmp(start, "LEAP") == 0)
828                         val |= WPA_AUTH_ALG_LEAP;
829                 else {
830                         wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
831                                    line, start);
832                         errors++;
833                 }
834
835                 if (last)
836                         break;
837                 start = end + 1;
838         }
839         os_free(buf);
840
841         if (val == 0) {
842                 wpa_printf(MSG_ERROR,
843                            "Line %d: no auth_alg values configured.", line);
844                 errors++;
845         }
846
847         wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
848         ssid->auth_alg = val;
849         return errors ? -1 : 0;
850 }
851
852
853 #ifndef NO_CONFIG_WRITE
854 static char * wpa_config_write_auth_alg(const struct parse_data *data,
855                                         struct wpa_ssid *ssid)
856 {
857         char *buf, *pos, *end;
858         int ret;
859
860         pos = buf = os_zalloc(30);
861         if (buf == NULL)
862                 return NULL;
863         end = buf + 30;
864
865         if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
866                 ret = os_snprintf(pos, end - pos, "%sOPEN",
867                                   pos == buf ? "" : " ");
868                 if (ret < 0 || ret >= end - pos) {
869                         end[-1] = '\0';
870                         return buf;
871                 }
872                 pos += ret;
873         }
874
875         if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
876                 ret = os_snprintf(pos, end - pos, "%sSHARED",
877                                   pos == buf ? "" : " ");
878                 if (ret < 0 || ret >= end - pos) {
879                         end[-1] = '\0';
880                         return buf;
881                 }
882                 pos += ret;
883         }
884
885         if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
886                 ret = os_snprintf(pos, end - pos, "%sLEAP",
887                                   pos == buf ? "" : " ");
888                 if (ret < 0 || ret >= end - pos) {
889                         end[-1] = '\0';
890                         return buf;
891                 }
892                 pos += ret;
893         }
894
895         return buf;
896 }
897 #endif /* NO_CONFIG_WRITE */
898
899
900 static int * wpa_config_parse_freqs(const struct parse_data *data,
901                                     struct wpa_ssid *ssid, int line,
902                                     const char *value)
903 {
904         int *freqs;
905         size_t used, len;
906         const char *pos;
907
908         used = 0;
909         len = 10;
910         freqs = os_calloc(len + 1, sizeof(int));
911         if (freqs == NULL)
912                 return NULL;
913
914         pos = value;
915         while (pos) {
916                 while (*pos == ' ')
917                         pos++;
918                 if (used == len) {
919                         int *n;
920                         size_t i;
921                         n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
922                         if (n == NULL) {
923                                 os_free(freqs);
924                                 return NULL;
925                         }
926                         for (i = len; i <= len * 2; i++)
927                                 n[i] = 0;
928                         freqs = n;
929                         len *= 2;
930                 }
931
932                 freqs[used] = atoi(pos);
933                 if (freqs[used] == 0)
934                         break;
935                 used++;
936                 pos = os_strchr(pos + 1, ' ');
937         }
938
939         return freqs;
940 }
941
942
943 static int wpa_config_parse_scan_freq(const struct parse_data *data,
944                                       struct wpa_ssid *ssid, int line,
945                                       const char *value)
946 {
947         int *freqs;
948
949         freqs = wpa_config_parse_freqs(data, ssid, line, value);
950         if (freqs == NULL)
951                 return -1;
952         os_free(ssid->scan_freq);
953         ssid->scan_freq = freqs;
954
955         return 0;
956 }
957
958
959 static int wpa_config_parse_freq_list(const struct parse_data *data,
960                                       struct wpa_ssid *ssid, int line,
961                                       const char *value)
962 {
963         int *freqs;
964
965         freqs = wpa_config_parse_freqs(data, ssid, line, value);
966         if (freqs == NULL)
967                 return -1;
968         os_free(ssid->freq_list);
969         ssid->freq_list = freqs;
970
971         return 0;
972 }
973
974
975 #ifndef NO_CONFIG_WRITE
976 static char * wpa_config_write_freqs(const struct parse_data *data,
977                                      const int *freqs)
978 {
979         char *buf, *pos, *end;
980         int i, ret;
981         size_t count;
982
983         if (freqs == NULL)
984                 return NULL;
985
986         count = 0;
987         for (i = 0; freqs[i]; i++)
988                 count++;
989
990         pos = buf = os_zalloc(10 * count + 1);
991         if (buf == NULL)
992                 return NULL;
993         end = buf + 10 * count + 1;
994
995         for (i = 0; freqs[i]; i++) {
996                 ret = os_snprintf(pos, end - pos, "%s%u",
997                                   i == 0 ? "" : " ", freqs[i]);
998                 if (ret < 0 || ret >= end - pos) {
999                         end[-1] = '\0';
1000                         return buf;
1001                 }
1002                 pos += ret;
1003         }
1004
1005         return buf;
1006 }
1007
1008
1009 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1010                                          struct wpa_ssid *ssid)
1011 {
1012         return wpa_config_write_freqs(data, ssid->scan_freq);
1013 }
1014
1015
1016 static char * wpa_config_write_freq_list(const struct parse_data *data,
1017                                          struct wpa_ssid *ssid)
1018 {
1019         return wpa_config_write_freqs(data, ssid->freq_list);
1020 }
1021 #endif /* NO_CONFIG_WRITE */
1022
1023
1024 #ifdef IEEE8021X_EAPOL
1025 static int wpa_config_parse_eap(const struct parse_data *data,
1026                                 struct wpa_ssid *ssid, int line,
1027                                 const char *value)
1028 {
1029         int last, errors = 0;
1030         char *start, *end, *buf;
1031         struct eap_method_type *methods = NULL, *tmp;
1032         size_t num_methods = 0;
1033
1034         buf = os_strdup(value);
1035         if (buf == NULL)
1036                 return -1;
1037         start = buf;
1038
1039         while (*start != '\0') {
1040                 while (*start == ' ' || *start == '\t')
1041                         start++;
1042                 if (*start == '\0')
1043                         break;
1044                 end = start;
1045                 while (*end != ' ' && *end != '\t' && *end != '\0')
1046                         end++;
1047                 last = *end == '\0';
1048                 *end = '\0';
1049                 tmp = methods;
1050                 methods = os_realloc_array(methods, num_methods + 1,
1051                                            sizeof(*methods));
1052                 if (methods == NULL) {
1053                         os_free(tmp);
1054                         os_free(buf);
1055                         return -1;
1056                 }
1057                 methods[num_methods].method = eap_peer_get_type(
1058                         start, &methods[num_methods].vendor);
1059                 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1060                     methods[num_methods].method == EAP_TYPE_NONE) {
1061                         wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1062                                    "'%s'", line, start);
1063                         wpa_printf(MSG_ERROR, "You may need to add support for"
1064                                    " this EAP method during wpa_supplicant\n"
1065                                    "build time configuration.\n"
1066                                    "See README for more information.");
1067                         errors++;
1068                 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1069                            methods[num_methods].method == EAP_TYPE_LEAP)
1070                         ssid->leap++;
1071                 else
1072                         ssid->non_leap++;
1073                 num_methods++;
1074                 if (last)
1075                         break;
1076                 start = end + 1;
1077         }
1078         os_free(buf);
1079
1080         tmp = methods;
1081         methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1082         if (methods == NULL) {
1083                 os_free(tmp);
1084                 return -1;
1085         }
1086         methods[num_methods].vendor = EAP_VENDOR_IETF;
1087         methods[num_methods].method = EAP_TYPE_NONE;
1088         num_methods++;
1089
1090         wpa_hexdump(MSG_MSGDUMP, "eap methods",
1091                     (u8 *) methods, num_methods * sizeof(*methods));
1092         os_free(ssid->eap.eap_methods);
1093         ssid->eap.eap_methods = methods;
1094         return errors ? -1 : 0;
1095 }
1096
1097
1098 static char * wpa_config_write_eap(const struct parse_data *data,
1099                                    struct wpa_ssid *ssid)
1100 {
1101         int i, ret;
1102         char *buf, *pos, *end;
1103         const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1104         const char *name;
1105
1106         if (eap_methods == NULL)
1107                 return NULL;
1108
1109         pos = buf = os_zalloc(100);
1110         if (buf == NULL)
1111                 return NULL;
1112         end = buf + 100;
1113
1114         for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1115                      eap_methods[i].method != EAP_TYPE_NONE; i++) {
1116                 name = eap_get_name(eap_methods[i].vendor,
1117                                     eap_methods[i].method);
1118                 if (name) {
1119                         ret = os_snprintf(pos, end - pos, "%s%s",
1120                                           pos == buf ? "" : " ", name);
1121                         if (ret < 0 || ret >= end - pos)
1122                                 break;
1123                         pos += ret;
1124                 }
1125         }
1126
1127         end[-1] = '\0';
1128
1129         return buf;
1130 }
1131
1132
1133 static int wpa_config_parse_password(const struct parse_data *data,
1134                                      struct wpa_ssid *ssid, int line,
1135                                      const char *value)
1136 {
1137         u8 *hash;
1138
1139         if (os_strcmp(value, "NULL") == 0) {
1140                 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1141                 os_free(ssid->eap.password);
1142                 ssid->eap.password = NULL;
1143                 ssid->eap.password_len = 0;
1144                 return 0;
1145         }
1146
1147 #ifdef CONFIG_EXT_PASSWORD
1148         if (os_strncmp(value, "ext:", 4) == 0) {
1149                 char *name = os_strdup(value + 4);
1150                 if (name == NULL)
1151                         return -1;
1152                 os_free(ssid->eap.password);
1153                 ssid->eap.password = (u8 *) name;
1154                 ssid->eap.password_len = os_strlen(name);
1155                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1156                 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1157                 return 0;
1158         }
1159 #endif /* CONFIG_EXT_PASSWORD */
1160
1161         if (os_strncmp(value, "hash:", 5) != 0) {
1162                 char *tmp;
1163                 size_t res_len;
1164
1165                 tmp = wpa_config_parse_string(value, &res_len);
1166                 if (tmp == NULL) {
1167                         wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1168                                    "password.", line);
1169                         return -1;
1170                 }
1171                 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1172                                       (u8 *) tmp, res_len);
1173
1174                 os_free(ssid->eap.password);
1175                 ssid->eap.password = (u8 *) tmp;
1176                 ssid->eap.password_len = res_len;
1177                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1178                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1179
1180                 return 0;
1181         }
1182
1183
1184         /* NtPasswordHash: hash:<32 hex digits> */
1185         if (os_strlen(value + 5) != 2 * 16) {
1186                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1187                            "(expected 32 hex digits)", line);
1188                 return -1;
1189         }
1190
1191         hash = os_malloc(16);
1192         if (hash == NULL)
1193                 return -1;
1194
1195         if (hexstr2bin(value + 5, hash, 16)) {
1196                 os_free(hash);
1197                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1198                 return -1;
1199         }
1200
1201         wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1202
1203         os_free(ssid->eap.password);
1204         ssid->eap.password = hash;
1205         ssid->eap.password_len = 16;
1206         ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1207         ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1208
1209         return 0;
1210 }
1211
1212
1213 static char * wpa_config_write_password(const struct parse_data *data,
1214                                         struct wpa_ssid *ssid)
1215 {
1216         char *buf;
1217
1218         if (ssid->eap.password == NULL)
1219                 return NULL;
1220
1221 #ifdef CONFIG_EXT_PASSWORD
1222         if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1223                 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1224                 if (buf == NULL)
1225                         return NULL;
1226                 os_memcpy(buf, "ext:", 4);
1227                 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1228                 return buf;
1229         }
1230 #endif /* CONFIG_EXT_PASSWORD */
1231
1232         if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1233                 return wpa_config_write_string(
1234                         ssid->eap.password, ssid->eap.password_len);
1235         }
1236
1237         buf = os_malloc(5 + 32 + 1);
1238         if (buf == NULL)
1239                 return NULL;
1240
1241         os_memcpy(buf, "hash:", 5);
1242         wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1243
1244         return buf;
1245 }
1246 #endif /* IEEE8021X_EAPOL */
1247
1248
1249 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1250                                     const char *value, int idx)
1251 {
1252         char *buf, title[20];
1253         int res;
1254
1255         buf = wpa_config_parse_string(value, len);
1256         if (buf == NULL) {
1257                 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1258                            line, idx, value);
1259                 return -1;
1260         }
1261         if (*len > MAX_WEP_KEY_LEN) {
1262                 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1263                            line, idx, value);
1264                 os_free(buf);
1265                 return -1;
1266         }
1267         if (*len && *len != 5 && *len != 13 && *len != 16) {
1268                 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1269                            "this network block will be ignored",
1270                            line, (unsigned int) *len);
1271         }
1272         os_memcpy(key, buf, *len);
1273         os_free(buf);
1274         res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1275         if (res >= 0 && (size_t) res < sizeof(title))
1276                 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1277         return 0;
1278 }
1279
1280
1281 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1282                                      struct wpa_ssid *ssid, int line,
1283                                      const char *value)
1284 {
1285         return wpa_config_parse_wep_key(ssid->wep_key[0],
1286                                         &ssid->wep_key_len[0], line,
1287                                         value, 0);
1288 }
1289
1290
1291 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1292                                      struct wpa_ssid *ssid, int line,
1293                                      const char *value)
1294 {
1295         return wpa_config_parse_wep_key(ssid->wep_key[1],
1296                                         &ssid->wep_key_len[1], line,
1297                                         value, 1);
1298 }
1299
1300
1301 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1302                                      struct wpa_ssid *ssid, int line,
1303                                      const char *value)
1304 {
1305         return wpa_config_parse_wep_key(ssid->wep_key[2],
1306                                         &ssid->wep_key_len[2], line,
1307                                         value, 2);
1308 }
1309
1310
1311 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1312                                      struct wpa_ssid *ssid, int line,
1313                                      const char *value)
1314 {
1315         return wpa_config_parse_wep_key(ssid->wep_key[3],
1316                                         &ssid->wep_key_len[3], line,
1317                                         value, 3);
1318 }
1319
1320
1321 #ifndef NO_CONFIG_WRITE
1322 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1323 {
1324         if (ssid->wep_key_len[idx] == 0)
1325                 return NULL;
1326         return wpa_config_write_string(ssid->wep_key[idx],
1327                                        ssid->wep_key_len[idx]);
1328 }
1329
1330
1331 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1332                                         struct wpa_ssid *ssid)
1333 {
1334         return wpa_config_write_wep_key(ssid, 0);
1335 }
1336
1337
1338 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1339                                         struct wpa_ssid *ssid)
1340 {
1341         return wpa_config_write_wep_key(ssid, 1);
1342 }
1343
1344
1345 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1346                                         struct wpa_ssid *ssid)
1347 {
1348         return wpa_config_write_wep_key(ssid, 2);
1349 }
1350
1351
1352 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1353                                         struct wpa_ssid *ssid)
1354 {
1355         return wpa_config_write_wep_key(ssid, 3);
1356 }
1357 #endif /* NO_CONFIG_WRITE */
1358
1359
1360 #ifdef CONFIG_P2P
1361
1362 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1363                                             struct wpa_ssid *ssid, int line,
1364                                             const char *value)
1365 {
1366         const char *pos;
1367         u8 *buf, *n, addr[ETH_ALEN];
1368         size_t count;
1369
1370         buf = NULL;
1371         count = 0;
1372
1373         pos = value;
1374         while (pos && *pos) {
1375                 while (*pos == ' ')
1376                         pos++;
1377
1378                 if (hwaddr_aton(pos, addr)) {
1379                         if (count == 0) {
1380                                 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1381                                            "p2p_client_list address '%s'.",
1382                                            line, value);
1383                                 os_free(buf);
1384                                 return -1;
1385                         }
1386                         /* continue anyway since this could have been from a
1387                          * truncated configuration file line */
1388                         wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1389                                    "truncated p2p_client_list address '%s'",
1390                                    line, pos);
1391                 } else {
1392                         n = os_realloc_array(buf, count + 1, ETH_ALEN);
1393                         if (n == NULL) {
1394                                 os_free(buf);
1395                                 return -1;
1396                         }
1397                         buf = n;
1398                         os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1399                         os_memcpy(buf, addr, ETH_ALEN);
1400                         count++;
1401                         wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1402                                     addr, ETH_ALEN);
1403                 }
1404
1405                 pos = os_strchr(pos, ' ');
1406         }
1407
1408         os_free(ssid->p2p_client_list);
1409         ssid->p2p_client_list = buf;
1410         ssid->num_p2p_clients = count;
1411
1412         return 0;
1413 }
1414
1415
1416 #ifndef NO_CONFIG_WRITE
1417 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1418                                                struct wpa_ssid *ssid)
1419 {
1420         char *value, *end, *pos;
1421         int res;
1422         size_t i;
1423
1424         if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1425                 return NULL;
1426
1427         value = os_malloc(20 * ssid->num_p2p_clients);
1428         if (value == NULL)
1429                 return NULL;
1430         pos = value;
1431         end = value + 20 * ssid->num_p2p_clients;
1432
1433         for (i = ssid->num_p2p_clients; i > 0; i--) {
1434                 res = os_snprintf(pos, end - pos, MACSTR " ",
1435                                   MAC2STR(ssid->p2p_client_list +
1436                                           (i - 1) * ETH_ALEN));
1437                 if (res < 0 || res >= end - pos) {
1438                         os_free(value);
1439                         return NULL;
1440                 }
1441                 pos += res;
1442         }
1443
1444         if (pos > value)
1445                 pos[-1] = '\0';
1446
1447         return value;
1448 }
1449 #endif /* NO_CONFIG_WRITE */
1450
1451 #endif /* CONFIG_P2P */
1452
1453 /* Helper macros for network block parser */
1454
1455 #ifdef OFFSET
1456 #undef OFFSET
1457 #endif /* OFFSET */
1458 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1459 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1460
1461 /* STR: Define a string variable for an ASCII string; f = field name */
1462 #ifdef NO_CONFIG_WRITE
1463 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1464 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1465 #else /* NO_CONFIG_WRITE */
1466 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1467 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1468 #endif /* NO_CONFIG_WRITE */
1469 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1470 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1471 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1472 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1473
1474 /* STR_LEN: Define a string variable with a separate variable for storing the
1475  * data length. Unlike STR(), this can be used to store arbitrary binary data
1476  * (i.e., even nul termination character). */
1477 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1478 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1479 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1480 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1481 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1482
1483 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1484  * explicitly specified. */
1485 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1486 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1487 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1488
1489 #ifdef NO_CONFIG_WRITE
1490 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1491 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1492 #else /* NO_CONFIG_WRITE */
1493 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1494         OFFSET(f), (void *) 0
1495 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1496         OFFSET(eap.f), (void *) 0
1497 #endif /* NO_CONFIG_WRITE */
1498
1499 /* INT: Define an integer variable */
1500 #define INT(f) _INT(f), NULL, NULL, 0
1501 #define INTe(f) _INTe(f), NULL, NULL, 0
1502
1503 /* INT_RANGE: Define an integer variable with allowed value range */
1504 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1505
1506 /* FUNC: Define a configuration variable that uses a custom function for
1507  * parsing and writing the value. */
1508 #ifdef NO_CONFIG_WRITE
1509 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1510 #else /* NO_CONFIG_WRITE */
1511 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1512         NULL, NULL, NULL, NULL
1513 #endif /* NO_CONFIG_WRITE */
1514 #define FUNC(f) _FUNC(f), 0
1515 #define FUNC_KEY(f) _FUNC(f), 1
1516
1517 /*
1518  * Table of network configuration variables. This table is used to parse each
1519  * network configuration variable, e.g., each line in wpa_supplicant.conf file
1520  * that is inside a network block.
1521  *
1522  * This table is generated using the helper macros defined above and with
1523  * generous help from the C pre-processor. The field name is stored as a string
1524  * into .name and for STR and INT types, the offset of the target buffer within
1525  * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1526  * offset to the field containing the length of the configuration variable.
1527  * .param3 and .param4 can be used to mark the allowed range (length for STR
1528  * and value for INT).
1529  *
1530  * For each configuration line in wpa_supplicant.conf, the parser goes through
1531  * this table and select the entry that matches with the field name. The parser
1532  * function (.parser) is then called to parse the actual value of the field.
1533  *
1534  * This kind of mechanism makes it easy to add new configuration parameters,
1535  * since only one line needs to be added into this table and into the
1536  * struct wpa_ssid definition if the new variable is either a string or
1537  * integer. More complex types will need to use their own parser and writer
1538  * functions.
1539  */
1540 static const struct parse_data ssid_fields[] = {
1541         { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1542         { INT_RANGE(scan_ssid, 0, 1) },
1543         { FUNC(bssid) },
1544         { FUNC_KEY(psk) },
1545         { FUNC(proto) },
1546         { FUNC(key_mgmt) },
1547         { INT(bg_scan_period) },
1548         { FUNC(pairwise) },
1549         { FUNC(group) },
1550         { FUNC(auth_alg) },
1551         { FUNC(scan_freq) },
1552         { FUNC(freq_list) },
1553 #ifdef IEEE8021X_EAPOL
1554         { FUNC(eap) },
1555         { STR_LENe(identity) },
1556         { STR_LENe(anonymous_identity) },
1557         { FUNC_KEY(password) },
1558         { STRe(ca_cert) },
1559         { STRe(ca_path) },
1560         { STRe(client_cert) },
1561         { STRe(private_key) },
1562         { STR_KEYe(private_key_passwd) },
1563         { STRe(dh_file) },
1564         { STRe(subject_match) },
1565         { STRe(altsubject_match) },
1566         { STRe(ca_cert2) },
1567         { STRe(ca_path2) },
1568         { STRe(client_cert2) },
1569         { STRe(private_key2) },
1570         { STR_KEYe(private_key2_passwd) },
1571         { STRe(dh_file2) },
1572         { STRe(subject_match2) },
1573         { STRe(altsubject_match2) },
1574         { STRe(phase1) },
1575         { STRe(phase2) },
1576         { STRe(pcsc) },
1577         { STR_KEYe(pin) },
1578         { STRe(engine_id) },
1579         { STRe(key_id) },
1580         { STRe(cert_id) },
1581         { STRe(ca_cert_id) },
1582         { STR_KEYe(pin2) },
1583         { STRe(engine2_id) },
1584         { STRe(key2_id) },
1585         { STRe(cert2_id) },
1586         { STRe(ca_cert2_id) },
1587         { INTe(engine) },
1588         { INTe(engine2) },
1589         { INT(eapol_flags) },
1590 #endif /* IEEE8021X_EAPOL */
1591         { FUNC_KEY(wep_key0) },
1592         { FUNC_KEY(wep_key1) },
1593         { FUNC_KEY(wep_key2) },
1594         { FUNC_KEY(wep_key3) },
1595         { INT(wep_tx_keyidx) },
1596         { INT(priority) },
1597 #ifdef IEEE8021X_EAPOL
1598         { INT(eap_workaround) },
1599         { STRe(pac_file) },
1600         { INTe(fragment_size) },
1601 #endif /* IEEE8021X_EAPOL */
1602         { INT_RANGE(mode, 0, 4) },
1603         { INT_RANGE(proactive_key_caching, 0, 1) },
1604         { INT_RANGE(disabled, 0, 2) },
1605         { STR(id_str) },
1606 #ifdef CONFIG_IEEE80211W
1607         { INT_RANGE(ieee80211w, 0, 2) },
1608 #endif /* CONFIG_IEEE80211W */
1609         { INT_RANGE(peerkey, 0, 1) },
1610         { INT_RANGE(mixed_cell, 0, 1) },
1611         { INT_RANGE(frequency, 0, 10000) },
1612         { INT(wpa_ptk_rekey) },
1613         { STR(bgscan) },
1614         { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1615 #ifdef CONFIG_P2P
1616         { FUNC(p2p_client_list) },
1617 #endif /* CONFIG_P2P */
1618 #ifdef CONFIG_HT_OVERRIDES
1619         { INT_RANGE(disable_ht, 0, 1) },
1620         { INT_RANGE(disable_ht40, -1, 1) },
1621         { INT_RANGE(disable_max_amsdu, -1, 1) },
1622         { INT_RANGE(ampdu_factor, -1, 3) },
1623         { INT_RANGE(ampdu_density, -1, 7) },
1624         { STR(ht_mcs) },
1625 #endif /* CONFIG_HT_OVERRIDES */
1626         { INT(ap_max_inactivity) },
1627         { INT(dtim_period) },
1628 };
1629
1630 #undef OFFSET
1631 #undef _STR
1632 #undef STR
1633 #undef STR_KEY
1634 #undef _STR_LEN
1635 #undef STR_LEN
1636 #undef STR_LEN_KEY
1637 #undef _STR_RANGE
1638 #undef STR_RANGE
1639 #undef STR_RANGE_KEY
1640 #undef _INT
1641 #undef INT
1642 #undef INT_RANGE
1643 #undef _FUNC
1644 #undef FUNC
1645 #undef FUNC_KEY
1646 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1647
1648
1649 /**
1650  * wpa_config_add_prio_network - Add a network to priority lists
1651  * @config: Configuration data from wpa_config_read()
1652  * @ssid: Pointer to the network configuration to be added to the list
1653  * Returns: 0 on success, -1 on failure
1654  *
1655  * This function is used to add a network block to the priority list of
1656  * networks. This must be called for each network when reading in the full
1657  * configuration. In addition, this can be used indirectly when updating
1658  * priorities by calling wpa_config_update_prio_list().
1659  */
1660 int wpa_config_add_prio_network(struct wpa_config *config,
1661                                 struct wpa_ssid *ssid)
1662 {
1663         int prio;
1664         struct wpa_ssid *prev, **nlist;
1665
1666         /*
1667          * Add to an existing priority list if one is available for the
1668          * configured priority level for this network.
1669          */
1670         for (prio = 0; prio < config->num_prio; prio++) {
1671                 prev = config->pssid[prio];
1672                 if (prev->priority == ssid->priority) {
1673                         while (prev->pnext)
1674                                 prev = prev->pnext;
1675                         prev->pnext = ssid;
1676                         return 0;
1677                 }
1678         }
1679
1680         /* First network for this priority - add a new priority list */
1681         nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1682                                  sizeof(struct wpa_ssid *));
1683         if (nlist == NULL)
1684                 return -1;
1685
1686         for (prio = 0; prio < config->num_prio; prio++) {
1687                 if (nlist[prio]->priority < ssid->priority) {
1688                         os_memmove(&nlist[prio + 1], &nlist[prio],
1689                                    (config->num_prio - prio) *
1690                                    sizeof(struct wpa_ssid *));
1691                         break;
1692                 }
1693         }
1694
1695         nlist[prio] = ssid;
1696         config->num_prio++;
1697         config->pssid = nlist;
1698
1699         return 0;
1700 }
1701
1702
1703 /**
1704  * wpa_config_update_prio_list - Update network priority list
1705  * @config: Configuration data from wpa_config_read()
1706  * Returns: 0 on success, -1 on failure
1707  *
1708  * This function is called to update the priority list of networks in the
1709  * configuration when a network is being added or removed. This is also called
1710  * if a priority for a network is changed.
1711  */
1712 int wpa_config_update_prio_list(struct wpa_config *config)
1713 {
1714         struct wpa_ssid *ssid;
1715         int ret = 0;
1716
1717         os_free(config->pssid);
1718         config->pssid = NULL;
1719         config->num_prio = 0;
1720
1721         ssid = config->ssid;
1722         while (ssid) {
1723                 ssid->pnext = NULL;
1724                 if (wpa_config_add_prio_network(config, ssid) < 0)
1725                         ret = -1;
1726                 ssid = ssid->next;
1727         }
1728
1729         return ret;
1730 }
1731
1732
1733 #ifdef IEEE8021X_EAPOL
1734 static void eap_peer_config_free(struct eap_peer_config *eap)
1735 {
1736         os_free(eap->eap_methods);
1737         os_free(eap->identity);
1738         os_free(eap->anonymous_identity);
1739         os_free(eap->password);
1740         os_free(eap->ca_cert);
1741         os_free(eap->ca_path);
1742         os_free(eap->client_cert);
1743         os_free(eap->private_key);
1744         os_free(eap->private_key_passwd);
1745         os_free(eap->dh_file);
1746         os_free(eap->subject_match);
1747         os_free(eap->altsubject_match);
1748         os_free(eap->ca_cert2);
1749         os_free(eap->ca_path2);
1750         os_free(eap->client_cert2);
1751         os_free(eap->private_key2);
1752         os_free(eap->private_key2_passwd);
1753         os_free(eap->dh_file2);
1754         os_free(eap->subject_match2);
1755         os_free(eap->altsubject_match2);
1756         os_free(eap->phase1);
1757         os_free(eap->phase2);
1758         os_free(eap->pcsc);
1759         os_free(eap->pin);
1760         os_free(eap->engine_id);
1761         os_free(eap->key_id);
1762         os_free(eap->cert_id);
1763         os_free(eap->ca_cert_id);
1764         os_free(eap->key2_id);
1765         os_free(eap->cert2_id);
1766         os_free(eap->ca_cert2_id);
1767         os_free(eap->pin2);
1768         os_free(eap->engine2_id);
1769         os_free(eap->otp);
1770         os_free(eap->pending_req_otp);
1771         os_free(eap->pac_file);
1772         os_free(eap->new_password);
1773 }
1774 #endif /* IEEE8021X_EAPOL */
1775
1776
1777 /**
1778  * wpa_config_free_ssid - Free network/ssid configuration data
1779  * @ssid: Configuration data for the network
1780  *
1781  * This function frees all resources allocated for the network configuration
1782  * data.
1783  */
1784 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1785 {
1786         os_free(ssid->ssid);
1787         os_free(ssid->passphrase);
1788         os_free(ssid->ext_psk);
1789 #ifdef IEEE8021X_EAPOL
1790         eap_peer_config_free(&ssid->eap);
1791 #endif /* IEEE8021X_EAPOL */
1792         os_free(ssid->id_str);
1793         os_free(ssid->scan_freq);
1794         os_free(ssid->freq_list);
1795         os_free(ssid->bgscan);
1796         os_free(ssid->p2p_client_list);
1797 #ifdef CONFIG_HT_OVERRIDES
1798         os_free(ssid->ht_mcs);
1799 #endif /* CONFIG_HT_OVERRIDES */
1800         os_free(ssid);
1801 }
1802
1803
1804 void wpa_config_free_cred(struct wpa_cred *cred)
1805 {
1806         os_free(cred->realm);
1807         os_free(cred->username);
1808         os_free(cred->password);
1809         os_free(cred->ca_cert);
1810         os_free(cred->client_cert);
1811         os_free(cred->private_key);
1812         os_free(cred->private_key_passwd);
1813         os_free(cred->imsi);
1814         os_free(cred->milenage);
1815         os_free(cred->domain);
1816         os_free(cred->eap_method);
1817         os_free(cred->phase1);
1818         os_free(cred->phase2);
1819         os_free(cred);
1820 }
1821
1822
1823 /**
1824  * wpa_config_free - Free configuration data
1825  * @config: Configuration data from wpa_config_read()
1826  *
1827  * This function frees all resources allocated for the configuration data by
1828  * wpa_config_read().
1829  */
1830 void wpa_config_free(struct wpa_config *config)
1831 {
1832 #ifndef CONFIG_NO_CONFIG_BLOBS
1833         struct wpa_config_blob *blob, *prevblob;
1834 #endif /* CONFIG_NO_CONFIG_BLOBS */
1835         struct wpa_ssid *ssid, *prev = NULL;
1836         struct wpa_cred *cred, *cprev;
1837
1838         ssid = config->ssid;
1839         while (ssid) {
1840                 prev = ssid;
1841                 ssid = ssid->next;
1842                 wpa_config_free_ssid(prev);
1843         }
1844
1845         cred = config->cred;
1846         while (cred) {
1847                 cprev = cred;
1848                 cred = cred->next;
1849                 wpa_config_free_cred(cprev);
1850         }
1851
1852 #ifndef CONFIG_NO_CONFIG_BLOBS
1853         blob = config->blobs;
1854         prevblob = NULL;
1855         while (blob) {
1856                 prevblob = blob;
1857                 blob = blob->next;
1858                 wpa_config_free_blob(prevblob);
1859         }
1860 #endif /* CONFIG_NO_CONFIG_BLOBS */
1861
1862         wpabuf_free(config->wps_vendor_ext_m1);
1863         os_free(config->ctrl_interface);
1864         os_free(config->ctrl_interface_group);
1865         os_free(config->opensc_engine_path);
1866         os_free(config->pkcs11_engine_path);
1867         os_free(config->pkcs11_module_path);
1868         os_free(config->pcsc_reader);
1869         os_free(config->pcsc_pin);
1870         os_free(config->driver_param);
1871         os_free(config->device_name);
1872         os_free(config->manufacturer);
1873         os_free(config->model_name);
1874         os_free(config->model_number);
1875         os_free(config->serial_number);
1876         os_free(config->config_methods);
1877         os_free(config->p2p_ssid_postfix);
1878         os_free(config->pssid);
1879         os_free(config->p2p_pref_chan);
1880         os_free(config->autoscan);
1881         wpabuf_free(config->wps_nfc_dh_pubkey);
1882         wpabuf_free(config->wps_nfc_dh_privkey);
1883         wpabuf_free(config->wps_nfc_dev_pw);
1884         os_free(config->ext_password_backend);
1885         os_free(config);
1886 }
1887
1888
1889 /**
1890  * wpa_config_foreach_network - Iterate over each configured network
1891  * @config: Configuration data from wpa_config_read()
1892  * @func: Callback function to process each network
1893  * @arg: Opaque argument to pass to callback function
1894  *
1895  * Iterate over the set of configured networks calling the specified
1896  * function for each item. We guard against callbacks removing the
1897  * supplied network.
1898  */
1899 void wpa_config_foreach_network(struct wpa_config *config,
1900                                 void (*func)(void *, struct wpa_ssid *),
1901                                 void *arg)
1902 {
1903         struct wpa_ssid *ssid, *next;
1904
1905         ssid = config->ssid;
1906         while (ssid) {
1907                 next = ssid->next;
1908                 func(arg, ssid);
1909                 ssid = next;
1910         }
1911 }
1912
1913
1914 /**
1915  * wpa_config_get_network - Get configured network based on id
1916  * @config: Configuration data from wpa_config_read()
1917  * @id: Unique network id to search for
1918  * Returns: Network configuration or %NULL if not found
1919  */
1920 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1921 {
1922         struct wpa_ssid *ssid;
1923
1924         ssid = config->ssid;
1925         while (ssid) {
1926                 if (id == ssid->id)
1927                         break;
1928                 ssid = ssid->next;
1929         }
1930
1931         return ssid;
1932 }
1933
1934
1935 /**
1936  * wpa_config_add_network - Add a new network with empty configuration
1937  * @config: Configuration data from wpa_config_read()
1938  * Returns: The new network configuration or %NULL if operation failed
1939  */
1940 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1941 {
1942         int id;
1943         struct wpa_ssid *ssid, *last = NULL;
1944
1945         id = -1;
1946         ssid = config->ssid;
1947         while (ssid) {
1948                 if (ssid->id > id)
1949                         id = ssid->id;
1950                 last = ssid;
1951                 ssid = ssid->next;
1952         }
1953         id++;
1954
1955         ssid = os_zalloc(sizeof(*ssid));
1956         if (ssid == NULL)
1957                 return NULL;
1958         ssid->id = id;
1959         if (last)
1960                 last->next = ssid;
1961         else
1962                 config->ssid = ssid;
1963
1964         wpa_config_update_prio_list(config);
1965
1966         return ssid;
1967 }
1968
1969
1970 /**
1971  * wpa_config_remove_network - Remove a configured network based on id
1972  * @config: Configuration data from wpa_config_read()
1973  * @id: Unique network id to search for
1974  * Returns: 0 on success, or -1 if the network was not found
1975  */
1976 int wpa_config_remove_network(struct wpa_config *config, int id)
1977 {
1978         struct wpa_ssid *ssid, *prev = NULL;
1979
1980         ssid = config->ssid;
1981         while (ssid) {
1982                 if (id == ssid->id)
1983                         break;
1984                 prev = ssid;
1985                 ssid = ssid->next;
1986         }
1987
1988         if (ssid == NULL)
1989                 return -1;
1990
1991         if (prev)
1992                 prev->next = ssid->next;
1993         else
1994                 config->ssid = ssid->next;
1995
1996         wpa_config_update_prio_list(config);
1997         wpa_config_free_ssid(ssid);
1998         return 0;
1999 }
2000
2001
2002 /**
2003  * wpa_config_set_network_defaults - Set network default values
2004  * @ssid: Pointer to network configuration data
2005  */
2006 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2007 {
2008         ssid->proto = DEFAULT_PROTO;
2009         ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2010         ssid->group_cipher = DEFAULT_GROUP;
2011         ssid->key_mgmt = DEFAULT_KEY_MGMT;
2012         ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
2013 #ifdef IEEE8021X_EAPOL
2014         ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2015         ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2016         ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2017 #endif /* IEEE8021X_EAPOL */
2018 #ifdef CONFIG_HT_OVERRIDES
2019         ssid->disable_ht = DEFAULT_DISABLE_HT;
2020         ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2021         ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2022         ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2023         ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2024 #endif /* CONFIG_HT_OVERRIDES */
2025 }
2026
2027
2028 /**
2029  * wpa_config_set - Set a variable in network configuration
2030  * @ssid: Pointer to network configuration data
2031  * @var: Variable name, e.g., "ssid"
2032  * @value: Variable value
2033  * @line: Line number in configuration file or 0 if not used
2034  * Returns: 0 on success, -1 on failure
2035  *
2036  * This function can be used to set network configuration variables based on
2037  * both the configuration file and management interface input. The value
2038  * parameter must be in the same format as the text-based configuration file is
2039  * using. For example, strings are using double quotation marks.
2040  */
2041 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2042                    int line)
2043 {
2044         size_t i;
2045         int ret = 0;
2046
2047         if (ssid == NULL || var == NULL || value == NULL)
2048                 return -1;
2049
2050         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2051                 const struct parse_data *field = &ssid_fields[i];
2052                 if (os_strcmp(var, field->name) != 0)
2053                         continue;
2054
2055                 if (field->parser(field, ssid, line, value)) {
2056                         if (line) {
2057                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
2058                                            "parse %s '%s'.", line, var, value);
2059                         }
2060                         ret = -1;
2061                 }
2062                 break;
2063         }
2064         if (i == NUM_SSID_FIELDS) {
2065                 if (line) {
2066                         wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2067                                    "'%s'.", line, var);
2068                 }
2069                 ret = -1;
2070         }
2071
2072         return ret;
2073 }
2074
2075
2076 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2077                           const char *value)
2078 {
2079         size_t len;
2080         char *buf;
2081         int ret;
2082
2083         len = os_strlen(value);
2084         buf = os_malloc(len + 3);
2085         if (buf == NULL)
2086                 return -1;
2087         buf[0] = '"';
2088         os_memcpy(buf + 1, value, len);
2089         buf[len + 1] = '"';
2090         buf[len + 2] = '\0';
2091         ret = wpa_config_set(ssid, var, buf, 0);
2092         os_free(buf);
2093         return ret;
2094 }
2095
2096
2097 /**
2098  * wpa_config_get_all - Get all options from network configuration
2099  * @ssid: Pointer to network configuration data
2100  * @get_keys: Determines if keys/passwords will be included in returned list
2101  *      (if they may be exported)
2102  * Returns: %NULL terminated list of all set keys and their values in the form
2103  * of [key1, val1, key2, val2, ... , NULL]
2104  *
2105  * This function can be used to get list of all configured network properties.
2106  * The caller is responsible for freeing the returned list and all its
2107  * elements.
2108  */
2109 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2110 {
2111         const struct parse_data *field;
2112         char *key, *value;
2113         size_t i;
2114         char **props;
2115         int fields_num;
2116
2117         get_keys = get_keys && ssid->export_keys;
2118
2119         props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2120         if (!props)
2121                 return NULL;
2122
2123         fields_num = 0;
2124         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2125                 field = &ssid_fields[i];
2126                 if (field->key_data && !get_keys)
2127                         continue;
2128                 value = field->writer(field, ssid);
2129                 if (value == NULL)
2130                         continue;
2131                 if (os_strlen(value) == 0) {
2132                         os_free(value);
2133                         continue;
2134                 }
2135
2136                 key = os_strdup(field->name);
2137                 if (key == NULL) {
2138                         os_free(value);
2139                         goto err;
2140                 }
2141
2142                 props[fields_num * 2] = key;
2143                 props[fields_num * 2 + 1] = value;
2144
2145                 fields_num++;
2146         }
2147
2148         return props;
2149
2150 err:
2151         value = *props;
2152         while (value)
2153                 os_free(value++);
2154         os_free(props);
2155         return NULL;
2156 }
2157
2158
2159 #ifndef NO_CONFIG_WRITE
2160 /**
2161  * wpa_config_get - Get a variable in network configuration
2162  * @ssid: Pointer to network configuration data
2163  * @var: Variable name, e.g., "ssid"
2164  * Returns: Value of the variable or %NULL on failure
2165  *
2166  * This function can be used to get network configuration variables. The
2167  * returned value is a copy of the configuration variable in text format, i.e,.
2168  * the same format that the text-based configuration file and wpa_config_set()
2169  * are using for the value. The caller is responsible for freeing the returned
2170  * value.
2171  */
2172 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2173 {
2174         size_t i;
2175
2176         if (ssid == NULL || var == NULL)
2177                 return NULL;
2178
2179         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2180                 const struct parse_data *field = &ssid_fields[i];
2181                 if (os_strcmp(var, field->name) == 0)
2182                         return field->writer(field, ssid);
2183         }
2184
2185         return NULL;
2186 }
2187
2188
2189 /**
2190  * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2191  * @ssid: Pointer to network configuration data
2192  * @var: Variable name, e.g., "ssid"
2193  * Returns: Value of the variable or %NULL on failure
2194  *
2195  * This function can be used to get network configuration variable like
2196  * wpa_config_get(). The only difference is that this functions does not expose
2197  * key/password material from the configuration. In case a key/password field
2198  * is requested, the returned value is an empty string or %NULL if the variable
2199  * is not set or "*" if the variable is set (regardless of its value). The
2200  * returned value is a copy of the configuration variable in text format, i.e,.
2201  * the same format that the text-based configuration file and wpa_config_set()
2202  * are using for the value. The caller is responsible for freeing the returned
2203  * value.
2204  */
2205 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2206 {
2207         size_t i;
2208
2209         if (ssid == NULL || var == NULL)
2210                 return NULL;
2211
2212         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2213                 const struct parse_data *field = &ssid_fields[i];
2214                 if (os_strcmp(var, field->name) == 0) {
2215                         char *res = field->writer(field, ssid);
2216                         if (field->key_data) {
2217                                 if (res && res[0]) {
2218                                         wpa_printf(MSG_DEBUG, "Do not allow "
2219                                                    "key_data field to be "
2220                                                    "exposed");
2221                                         os_free(res);
2222                                         return os_strdup("*");
2223                                 }
2224
2225                                 os_free(res);
2226                                 return NULL;
2227                         }
2228                         return res;
2229                 }
2230         }
2231
2232         return NULL;
2233 }
2234 #endif /* NO_CONFIG_WRITE */
2235
2236
2237 /**
2238  * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2239  * @ssid: Pointer to network configuration data
2240  *
2241  * This function must be called to update WPA PSK when either SSID or the
2242  * passphrase has changed for the network configuration.
2243  */
2244 void wpa_config_update_psk(struct wpa_ssid *ssid)
2245 {
2246 #ifndef CONFIG_NO_PBKDF2
2247         pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2248                     ssid->psk, PMK_LEN);
2249         wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2250                         ssid->psk, PMK_LEN);
2251         ssid->psk_set = 1;
2252 #endif /* CONFIG_NO_PBKDF2 */
2253 }
2254
2255
2256 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2257                         const char *value, int line)
2258 {
2259         char *val;
2260         size_t len;
2261
2262         if (os_strcmp(var, "priority") == 0) {
2263                 cred->priority = atoi(value);
2264                 return 0;
2265         }
2266
2267         if (os_strcmp(var, "pcsc") == 0) {
2268                 cred->pcsc = atoi(value);
2269                 return 0;
2270         }
2271
2272         if (os_strcmp(var, "eap") == 0) {
2273                 struct eap_method_type method;
2274                 method.method = eap_peer_get_type(value, &method.vendor);
2275                 if (method.vendor == EAP_VENDOR_IETF &&
2276                     method.method == EAP_TYPE_NONE) {
2277                         wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2278                                    "for a credential", line, value);
2279                         return -1;
2280                 }
2281                 os_free(cred->eap_method);
2282                 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2283                 if (cred->eap_method == NULL)
2284                         return -1;
2285                 os_memcpy(cred->eap_method, &method, sizeof(method));
2286                 return 0;
2287         }
2288
2289         if (os_strcmp(var, "password") == 0 &&
2290             os_strncmp(value, "ext:", 4) == 0) {
2291                 os_free(cred->password);
2292                 cred->password = os_strdup(value);
2293                 cred->ext_password = 1;
2294                 return 0;
2295         }
2296
2297         val = wpa_config_parse_string(value, &len);
2298         if (val == NULL) {
2299                 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2300                            "value '%s'.", line, var, value);
2301                 return -1;
2302         }
2303
2304         if (os_strcmp(var, "realm") == 0) {
2305                 os_free(cred->realm);
2306                 cred->realm = val;
2307                 return 0;
2308         }
2309
2310         if (os_strcmp(var, "username") == 0) {
2311                 os_free(cred->username);
2312                 cred->username = val;
2313                 return 0;
2314         }
2315
2316         if (os_strcmp(var, "password") == 0) {
2317                 os_free(cred->password);
2318                 cred->password = val;
2319                 cred->ext_password = 0;
2320                 return 0;
2321         }
2322
2323         if (os_strcmp(var, "ca_cert") == 0) {
2324                 os_free(cred->ca_cert);
2325                 cred->ca_cert = val;
2326                 return 0;
2327         }
2328
2329         if (os_strcmp(var, "client_cert") == 0) {
2330                 os_free(cred->client_cert);
2331                 cred->client_cert = val;
2332                 return 0;
2333         }
2334
2335         if (os_strcmp(var, "private_key") == 0) {
2336                 os_free(cred->private_key);
2337                 cred->private_key = val;
2338                 return 0;
2339         }
2340
2341         if (os_strcmp(var, "private_key_passwd") == 0) {
2342                 os_free(cred->private_key_passwd);
2343                 cred->private_key_passwd = val;
2344                 return 0;
2345         }
2346
2347         if (os_strcmp(var, "imsi") == 0) {
2348                 os_free(cred->imsi);
2349                 cred->imsi = val;
2350                 return 0;
2351         }
2352
2353         if (os_strcmp(var, "milenage") == 0) {
2354                 os_free(cred->milenage);
2355                 cred->milenage = val;
2356                 return 0;
2357         }
2358
2359         if (os_strcmp(var, "domain") == 0) {
2360                 os_free(cred->domain);
2361                 cred->domain = val;
2362                 return 0;
2363         }
2364
2365         if (os_strcmp(var, "phase1") == 0) {
2366                 os_free(cred->phase1);
2367                 cred->phase1 = val;
2368                 return 0;
2369         }
2370
2371         if (os_strcmp(var, "phase2") == 0) {
2372                 os_free(cred->phase2);
2373                 cred->phase2 = val;
2374                 return 0;
2375         }
2376
2377         if (os_strcmp(var, "roaming_consortium") == 0) {
2378                 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2379                         wpa_printf(MSG_ERROR, "Line %d: invalid "
2380                                    "roaming_consortium length %d (3..15 "
2381                                    "expected)", line, (int) len);
2382                         os_free(val);
2383                         return -1;
2384                 }
2385                 os_memcpy(cred->roaming_consortium, val, len);
2386                 cred->roaming_consortium_len = len;
2387                 os_free(val);
2388                 return 0;
2389         }
2390
2391         if (line) {
2392                 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2393                            line, var);
2394         }
2395
2396         os_free(val);
2397
2398         return -1;
2399 }
2400
2401
2402 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2403 {
2404         struct wpa_cred *cred;
2405
2406         cred = config->cred;
2407         while (cred) {
2408                 if (id == cred->id)
2409                         break;
2410                 cred = cred->next;
2411         }
2412
2413         return cred;
2414 }
2415
2416
2417 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2418 {
2419         int id;
2420         struct wpa_cred *cred, *last = NULL;
2421
2422         id = -1;
2423         cred = config->cred;
2424         while (cred) {
2425                 if (cred->id > id)
2426                         id = cred->id;
2427                 last = cred;
2428                 cred = cred->next;
2429         }
2430         id++;
2431
2432         cred = os_zalloc(sizeof(*cred));
2433         if (cred == NULL)
2434                 return NULL;
2435         cred->id = id;
2436         if (last)
2437                 last->next = cred;
2438         else
2439                 config->cred = cred;
2440
2441         return cred;
2442 }
2443
2444
2445 int wpa_config_remove_cred(struct wpa_config *config, int id)
2446 {
2447         struct wpa_cred *cred, *prev = NULL;
2448
2449         cred = config->cred;
2450         while (cred) {
2451                 if (id == cred->id)
2452                         break;
2453                 prev = cred;
2454                 cred = cred->next;
2455         }
2456
2457         if (cred == NULL)
2458                 return -1;
2459
2460         if (prev)
2461                 prev->next = cred->next;
2462         else
2463                 config->cred = cred->next;
2464
2465         wpa_config_free_cred(cred);
2466         return 0;
2467 }
2468
2469
2470 #ifndef CONFIG_NO_CONFIG_BLOBS
2471 /**
2472  * wpa_config_get_blob - Get a named configuration blob
2473  * @config: Configuration data from wpa_config_read()
2474  * @name: Name of the blob
2475  * Returns: Pointer to blob data or %NULL if not found
2476  */
2477 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2478                                                    const char *name)
2479 {
2480         struct wpa_config_blob *blob = config->blobs;
2481
2482         while (blob) {
2483                 if (os_strcmp(blob->name, name) == 0)
2484                         return blob;
2485                 blob = blob->next;
2486         }
2487         return NULL;
2488 }
2489
2490
2491 /**
2492  * wpa_config_set_blob - Set or add a named configuration blob
2493  * @config: Configuration data from wpa_config_read()
2494  * @blob: New value for the blob
2495  *
2496  * Adds a new configuration blob or replaces the current value of an existing
2497  * blob.
2498  */
2499 void wpa_config_set_blob(struct wpa_config *config,
2500                          struct wpa_config_blob *blob)
2501 {
2502         wpa_config_remove_blob(config, blob->name);
2503         blob->next = config->blobs;
2504         config->blobs = blob;
2505 }
2506
2507
2508 /**
2509  * wpa_config_free_blob - Free blob data
2510  * @blob: Pointer to blob to be freed
2511  */
2512 void wpa_config_free_blob(struct wpa_config_blob *blob)
2513 {
2514         if (blob) {
2515                 os_free(blob->name);
2516                 os_free(blob->data);
2517                 os_free(blob);
2518         }
2519 }
2520
2521
2522 /**
2523  * wpa_config_remove_blob - Remove a named configuration blob
2524  * @config: Configuration data from wpa_config_read()
2525  * @name: Name of the blob to remove
2526  * Returns: 0 if blob was removed or -1 if blob was not found
2527  */
2528 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2529 {
2530         struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2531
2532         while (pos) {
2533                 if (os_strcmp(pos->name, name) == 0) {
2534                         if (prev)
2535                                 prev->next = pos->next;
2536                         else
2537                                 config->blobs = pos->next;
2538                         wpa_config_free_blob(pos);
2539                         return 0;
2540                 }
2541                 prev = pos;
2542                 pos = pos->next;
2543         }
2544
2545         return -1;
2546 }
2547 #endif /* CONFIG_NO_CONFIG_BLOBS */
2548
2549
2550 /**
2551  * wpa_config_alloc_empty - Allocate an empty configuration
2552  * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2553  * socket
2554  * @driver_param: Driver parameters
2555  * Returns: Pointer to allocated configuration data or %NULL on failure
2556  */
2557 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2558                                            const char *driver_param)
2559 {
2560         struct wpa_config *config;
2561         const int aCWmin = 4, aCWmax = 10;
2562         const struct hostapd_wmm_ac_params ac_bk =
2563                 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2564         const struct hostapd_wmm_ac_params ac_be =
2565                 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2566         const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2567                 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2568         const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2569                 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
2570
2571         config = os_zalloc(sizeof(*config));
2572         if (config == NULL)
2573                 return NULL;
2574         config->eapol_version = DEFAULT_EAPOL_VERSION;
2575         config->ap_scan = DEFAULT_AP_SCAN;
2576         config->fast_reauth = DEFAULT_FAST_REAUTH;
2577         config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2578         config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2579         config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
2580         config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2581         config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2582         config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2583         config->max_num_sta = DEFAULT_MAX_NUM_STA;
2584         config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2585         config->wmm_ac_params[0] = ac_be;
2586         config->wmm_ac_params[1] = ac_bk;
2587         config->wmm_ac_params[2] = ac_vi;
2588         config->wmm_ac_params[3] = ac_vo;
2589
2590         if (ctrl_interface)
2591                 config->ctrl_interface = os_strdup(ctrl_interface);
2592         if (driver_param)
2593                 config->driver_param = os_strdup(driver_param);
2594
2595         return config;
2596 }
2597
2598
2599 #ifndef CONFIG_NO_STDOUT_DEBUG
2600 /**
2601  * wpa_config_debug_dump_networks - Debug dump of configured networks
2602  * @config: Configuration data from wpa_config_read()
2603  */
2604 void wpa_config_debug_dump_networks(struct wpa_config *config)
2605 {
2606         int prio;
2607         struct wpa_ssid *ssid;
2608
2609         for (prio = 0; prio < config->num_prio; prio++) {
2610                 ssid = config->pssid[prio];
2611                 wpa_printf(MSG_DEBUG, "Priority group %d",
2612                            ssid->priority);
2613                 while (ssid) {
2614                         wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
2615                                    ssid->id,
2616                                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2617                         ssid = ssid->pnext;
2618                 }
2619         }
2620 }
2621 #endif /* CONFIG_NO_STDOUT_DEBUG */
2622
2623
2624 struct global_parse_data {
2625         char *name;
2626         int (*parser)(const struct global_parse_data *data,
2627                       struct wpa_config *config, int line, const char *value);
2628         void *param1, *param2, *param3;
2629         unsigned int changed_flag;
2630 };
2631
2632
2633 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2634                                        struct wpa_config *config, int line,
2635                                        const char *pos)
2636 {
2637         int *dst;
2638         dst = (int *) (((u8 *) config) + (long) data->param1);
2639         *dst = atoi(pos);
2640         wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2641
2642         if (data->param2 && *dst < (long) data->param2) {
2643                 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2644                            "min_value=%ld)", line, data->name, *dst,
2645                            (long) data->param2);
2646                 *dst = (long) data->param2;
2647                 return -1;
2648         }
2649
2650         if (data->param3 && *dst > (long) data->param3) {
2651                 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2652                            "max_value=%ld)", line, data->name, *dst,
2653                            (long) data->param3);
2654                 *dst = (long) data->param3;
2655                 return -1;
2656         }
2657
2658         return 0;
2659 }
2660
2661
2662 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2663                                        struct wpa_config *config, int line,
2664                                        const char *pos)
2665 {
2666         size_t len;
2667         char **dst, *tmp;
2668
2669         len = os_strlen(pos);
2670         if (data->param2 && len < (size_t) data->param2) {
2671                 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2672                            "min_len=%ld)", line, data->name,
2673                            (unsigned long) len, (long) data->param2);
2674                 return -1;
2675         }
2676
2677         if (data->param3 && len > (size_t) data->param3) {
2678                 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2679                            "max_len=%ld)", line, data->name,
2680                            (unsigned long) len, (long) data->param3);
2681                 return -1;
2682         }
2683
2684         tmp = os_strdup(pos);
2685         if (tmp == NULL)
2686                 return -1;
2687
2688         dst = (char **) (((u8 *) config) + (long) data->param1);
2689         os_free(*dst);
2690         *dst = tmp;
2691         wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2692
2693         return 0;
2694 }
2695
2696
2697 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2698                                        struct wpa_config *config, int line,
2699                                        const char *pos)
2700 {
2701         size_t len;
2702         struct wpabuf **dst, *tmp;
2703
2704         len = os_strlen(pos);
2705         if (len & 0x01)
2706                 return -1;
2707
2708         tmp = wpabuf_alloc(len / 2);
2709         if (tmp == NULL)
2710                 return -1;
2711
2712         if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2713                 wpabuf_free(tmp);
2714                 return -1;
2715         }
2716
2717         dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2718         wpabuf_free(*dst);
2719         *dst = tmp;
2720         wpa_printf(MSG_DEBUG, "%s", data->name);
2721
2722         return 0;
2723 }
2724
2725
2726 static int wpa_config_process_country(const struct global_parse_data *data,
2727                                       struct wpa_config *config, int line,
2728                                       const char *pos)
2729 {
2730         if (!pos[0] || !pos[1]) {
2731                 wpa_printf(MSG_DEBUG, "Invalid country set");
2732                 return -1;
2733         }
2734         config->country[0] = pos[0];
2735         config->country[1] = pos[1];
2736         wpa_printf(MSG_DEBUG, "country='%c%c'",
2737                    config->country[0], config->country[1]);
2738         return 0;
2739 }
2740
2741
2742 static int wpa_config_process_load_dynamic_eap(
2743         const struct global_parse_data *data, struct wpa_config *config,
2744         int line, const char *so)
2745 {
2746         int ret;
2747         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2748         ret = eap_peer_method_load(so);
2749         if (ret == -2) {
2750                 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2751                            "reloading.");
2752         } else if (ret) {
2753                 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2754                            "method '%s'.", line, so);
2755                 return -1;
2756         }
2757
2758         return 0;
2759 }
2760
2761
2762 #ifdef CONFIG_WPS
2763
2764 static int wpa_config_process_uuid(const struct global_parse_data *data,
2765                                    struct wpa_config *config, int line,
2766                                    const char *pos)
2767 {
2768         char buf[40];
2769         if (uuid_str2bin(pos, config->uuid)) {
2770                 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2771                 return -1;
2772         }
2773         uuid_bin2str(config->uuid, buf, sizeof(buf));
2774         wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2775         return 0;
2776 }
2777
2778
2779 static int wpa_config_process_device_type(
2780         const struct global_parse_data *data,
2781         struct wpa_config *config, int line, const char *pos)
2782 {
2783         return wps_dev_type_str2bin(pos, config->device_type);
2784 }
2785
2786
2787 static int wpa_config_process_os_version(const struct global_parse_data *data,
2788                                          struct wpa_config *config, int line,
2789                                          const char *pos)
2790 {
2791         if (hexstr2bin(pos, config->os_version, 4)) {
2792                 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2793                 return -1;
2794         }
2795         wpa_printf(MSG_DEBUG, "os_version=%08x",
2796                    WPA_GET_BE32(config->os_version));
2797         return 0;
2798 }
2799
2800
2801 static int wpa_config_process_wps_vendor_ext_m1(
2802         const struct global_parse_data *data,
2803         struct wpa_config *config, int line, const char *pos)
2804 {
2805         struct wpabuf *tmp;
2806         int len = os_strlen(pos) / 2;
2807         u8 *p;
2808
2809         if (!len) {
2810                 wpa_printf(MSG_ERROR, "Line %d: "
2811                            "invalid wps_vendor_ext_m1", line);
2812                 return -1;
2813         }
2814
2815         tmp = wpabuf_alloc(len);
2816         if (tmp) {
2817                 p = wpabuf_put(tmp, len);
2818
2819                 if (hexstr2bin(pos, p, len)) {
2820                         wpa_printf(MSG_ERROR, "Line %d: "
2821                                    "invalid wps_vendor_ext_m1", line);
2822                         wpabuf_free(tmp);
2823                         return -1;
2824                 }
2825
2826                 wpabuf_free(config->wps_vendor_ext_m1);
2827                 config->wps_vendor_ext_m1 = tmp;
2828         } else {
2829                 wpa_printf(MSG_ERROR, "Can not allocate "
2830                            "memory for wps_vendor_ext_m1");
2831                 return -1;
2832         }
2833
2834         return 0;
2835 }
2836
2837 #endif /* CONFIG_WPS */
2838
2839 #ifdef CONFIG_P2P
2840 static int wpa_config_process_sec_device_type(
2841         const struct global_parse_data *data,
2842         struct wpa_config *config, int line, const char *pos)
2843 {
2844         int idx;
2845
2846         if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2847                 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2848                            "items", line);
2849                 return -1;
2850         }
2851
2852         idx = config->num_sec_device_types;
2853
2854         if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2855                 return -1;
2856
2857         config->num_sec_device_types++;
2858         return 0;
2859 }
2860
2861
2862 static int wpa_config_process_p2p_pref_chan(
2863         const struct global_parse_data *data,
2864         struct wpa_config *config, int line, const char *pos)
2865 {
2866         struct p2p_channel *pref = NULL, *n;
2867         unsigned int num = 0;
2868         const char *pos2;
2869         u8 op_class, chan;
2870
2871         /* format: class:chan,class:chan,... */
2872
2873         while (*pos) {
2874                 op_class = atoi(pos);
2875                 pos2 = os_strchr(pos, ':');
2876                 if (pos2 == NULL)
2877                         goto fail;
2878                 pos2++;
2879                 chan = atoi(pos2);
2880
2881                 n = os_realloc_array(pref, num + 1,
2882                                      sizeof(struct p2p_channel));
2883                 if (n == NULL)
2884                         goto fail;
2885                 pref = n;
2886                 pref[num].op_class = op_class;
2887                 pref[num].chan = chan;
2888                 num++;
2889
2890                 pos = os_strchr(pos2, ',');
2891                 if (pos == NULL)
2892                         break;
2893                 pos++;
2894         }
2895
2896         os_free(config->p2p_pref_chan);
2897         config->p2p_pref_chan = pref;
2898         config->num_p2p_pref_chan = num;
2899         wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2900                     (u8 *) config->p2p_pref_chan,
2901                     config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2902
2903         return 0;
2904
2905 fail:
2906         os_free(pref);
2907         wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2908         return -1;
2909 }
2910 #endif /* CONFIG_P2P */
2911
2912
2913 static int wpa_config_process_hessid(
2914         const struct global_parse_data *data,
2915         struct wpa_config *config, int line, const char *pos)
2916 {
2917         if (hwaddr_aton2(pos, config->hessid) < 0) {
2918                 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2919                            line, pos);
2920                 return -1;
2921         }
2922
2923         return 0;
2924 }
2925
2926
2927 #ifdef OFFSET
2928 #undef OFFSET
2929 #endif /* OFFSET */
2930 /* OFFSET: Get offset of a variable within the wpa_config structure */
2931 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2932
2933 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2934 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2935 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2936 #define INT(f) _INT(f), NULL, NULL
2937 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2938 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2939 #define STR(f) _STR(f), NULL, NULL
2940 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2941 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
2942
2943 static const struct global_parse_data global_fields[] = {
2944 #ifdef CONFIG_CTRL_IFACE
2945         { STR(ctrl_interface), 0 },
2946         { STR(ctrl_interface_group), 0 } /* deprecated */,
2947 #endif /* CONFIG_CTRL_IFACE */
2948         { INT_RANGE(eapol_version, 1, 2), 0 },
2949         { INT(ap_scan), 0 },
2950         { INT(disable_scan_offload), 0 },
2951         { INT(fast_reauth), 0 },
2952         { STR(opensc_engine_path), 0 },
2953         { STR(pkcs11_engine_path), 0 },
2954         { STR(pkcs11_module_path), 0 },
2955         { STR(pcsc_reader), 0 },
2956         { STR(pcsc_pin), 0 },
2957         { STR(driver_param), 0 },
2958         { INT(dot11RSNAConfigPMKLifetime), 0 },
2959         { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2960         { INT(dot11RSNAConfigSATimeout), 0 },
2961 #ifndef CONFIG_NO_CONFIG_WRITE
2962         { INT(update_config), 0 },
2963 #endif /* CONFIG_NO_CONFIG_WRITE */
2964         { FUNC_NO_VAR(load_dynamic_eap), 0 },
2965 #ifdef CONFIG_WPS
2966         { FUNC(uuid), CFG_CHANGED_UUID },
2967         { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2968         { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2969         { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2970         { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2971         { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2972         { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2973         { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2974         { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2975         { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2976         { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
2977 #endif /* CONFIG_WPS */
2978 #ifdef CONFIG_P2P
2979         { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2980         { INT(p2p_listen_reg_class), 0 },
2981         { INT(p2p_listen_channel), 0 },
2982         { INT(p2p_oper_reg_class), 0 },
2983         { INT(p2p_oper_channel), 0 },
2984         { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2985         { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2986         { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2987         { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2988         { INT(p2p_group_idle), 0 },
2989         { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
2990 #endif /* CONFIG_P2P */
2991         { FUNC(country), CFG_CHANGED_COUNTRY },
2992         { INT(bss_max_count), 0 },
2993         { INT(bss_expiration_age), 0 },
2994         { INT(bss_expiration_scan_count), 0 },
2995         { INT_RANGE(filter_ssids, 0, 1), 0 },
2996         { INT_RANGE(filter_rssi, -100, 0), 0 },
2997         { INT(max_num_sta), 0 },
2998         { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2999 #ifdef CONFIG_HS20
3000         { INT_RANGE(hs20, 0, 1), 0 },
3001 #endif /* CONFIG_HS20 */
3002         { INT_RANGE(interworking, 0, 1), 0 },
3003         { FUNC(hessid), 0 },
3004         { INT_RANGE(access_network_type, 0, 15), 0 },
3005         { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3006         { STR(autoscan), 0 },
3007         { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff), 0 },
3008         { BIN(wps_nfc_dh_pubkey), 0 },
3009         { BIN(wps_nfc_dh_privkey), 0 },
3010         { BIN(wps_nfc_dev_pw), 0 },
3011         { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3012         { INT(p2p_go_max_inactivity), 0 },
3013 };
3014
3015 #undef FUNC
3016 #undef _INT
3017 #undef INT
3018 #undef INT_RANGE
3019 #undef _STR
3020 #undef STR
3021 #undef STR_RANGE
3022 #undef BIN
3023 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
3024
3025
3026 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3027 {
3028         size_t i;
3029         int ret = 0;
3030
3031         for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3032                 const struct global_parse_data *field = &global_fields[i];
3033                 size_t flen = os_strlen(field->name);
3034                 if (os_strncmp(pos, field->name, flen) != 0 ||
3035                     pos[flen] != '=')
3036                         continue;
3037
3038                 if (field->parser(field, config, line, pos + flen + 1)) {
3039                         wpa_printf(MSG_ERROR, "Line %d: failed to "
3040                                    "parse '%s'.", line, pos);
3041                         ret = -1;
3042                 }
3043                 config->changed_parameters |= field->changed_flag;
3044                 break;
3045         }
3046         if (i == NUM_GLOBAL_FIELDS) {
3047 #ifdef CONFIG_AP
3048                 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3049                         char *tmp = os_strchr(pos, '=');
3050                         if (tmp == NULL) {
3051                                 if (line < 0)
3052                                         return -1;
3053                                 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3054                                            "'%s'", line, pos);
3055                                 return -1;
3056                         }
3057                         *tmp++ = '\0';
3058                         if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3059                                                   tmp)) {
3060                                 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3061                                            "AC item", line);
3062                                 return -1;
3063                         }
3064                 }
3065 #endif /* CONFIG_AP */
3066                 if (line < 0)
3067                         return -1;
3068                 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3069                            line, pos);
3070                 ret = -1;
3071         }
3072
3073         return ret;
3074 }