OSDN Git Service

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