Verify that driver supports configured HT capabilities
[wpasupplicant] / hostapd / config.c
1 /*
2  * hostapd / Configuration file
3  * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007-2008, Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
18 #include <grp.h>
19 #endif /* CONFIG_NATIVE_WINDOWS */
20
21 #include "hostapd.h"
22 #include "driver.h"
23 #include "sha1.h"
24 #include "eap_server/eap.h"
25 #include "radius/radius_client.h"
26 #include "wpa_common.h"
27 #include "wpa.h"
28 #include "uuid.h"
29 #include "eap_common/eap_wsc_common.h"
30
31
32 #define MAX_STA_COUNT 2007
33
34 extern struct wpa_driver_ops *hostapd_drivers[];
35
36
37 #ifndef CONFIG_NO_VLAN
38 static int hostapd_config_read_vlan_file(struct hostapd_bss_config *bss,
39                                          const char *fname)
40 {
41         FILE *f;
42         char buf[128], *pos, *pos2;
43         int line = 0, vlan_id;
44         struct hostapd_vlan *vlan;
45
46         f = fopen(fname, "r");
47         if (!f) {
48                 wpa_printf(MSG_ERROR, "VLAN file '%s' not readable.", fname);
49                 return -1;
50         }
51
52         while (fgets(buf, sizeof(buf), f)) {
53                 line++;
54
55                 if (buf[0] == '#')
56                         continue;
57                 pos = buf;
58                 while (*pos != '\0') {
59                         if (*pos == '\n') {
60                                 *pos = '\0';
61                                 break;
62                         }
63                         pos++;
64                 }
65                 if (buf[0] == '\0')
66                         continue;
67
68                 if (buf[0] == '*') {
69                         vlan_id = VLAN_ID_WILDCARD;
70                         pos = buf + 1;
71                 } else {
72                         vlan_id = strtol(buf, &pos, 10);
73                         if (buf == pos || vlan_id < 1 ||
74                             vlan_id > MAX_VLAN_ID) {
75                                 wpa_printf(MSG_ERROR, "Invalid VLAN ID at "
76                                            "line %d in '%s'", line, fname);
77                                 fclose(f);
78                                 return -1;
79                         }
80                 }
81
82                 while (*pos == ' ' || *pos == '\t')
83                         pos++;
84                 pos2 = pos;
85                 while (*pos2 != ' ' && *pos2 != '\t' && *pos2 != '\0')
86                         pos2++;
87                 *pos2 = '\0';
88                 if (*pos == '\0' || os_strlen(pos) > IFNAMSIZ) {
89                         wpa_printf(MSG_ERROR, "Invalid VLAN ifname at line %d "
90                                    "in '%s'", line, fname);
91                         fclose(f);
92                         return -1;
93                 }
94
95                 vlan = os_malloc(sizeof(*vlan));
96                 if (vlan == NULL) {
97                         wpa_printf(MSG_ERROR, "Out of memory while reading "
98                                    "VLAN interfaces from '%s'", fname);
99                         fclose(f);
100                         return -1;
101                 }
102
103                 os_memset(vlan, 0, sizeof(*vlan));
104                 vlan->vlan_id = vlan_id;
105                 os_strlcpy(vlan->ifname, pos, sizeof(vlan->ifname));
106                 if (bss->vlan_tail)
107                         bss->vlan_tail->next = vlan;
108                 else
109                         bss->vlan = vlan;
110                 bss->vlan_tail = vlan;
111         }
112
113         fclose(f);
114
115         return 0;
116 }
117 #endif /* CONFIG_NO_VLAN */
118
119
120 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
121 {
122         struct hostapd_vlan *vlan, *prev;
123
124         vlan = bss->vlan;
125         prev = NULL;
126         while (vlan) {
127                 prev = vlan;
128                 vlan = vlan->next;
129                 os_free(prev);
130         }
131
132         bss->vlan = NULL;
133 }
134
135
136 /* convert floats with one decimal place to value*10 int, i.e.,
137  * "1.5" will return 15 */
138 static int hostapd_config_read_int10(const char *value)
139 {
140         int i, d;
141         char *pos;
142
143         i = atoi(value);
144         pos = os_strchr(value, '.');
145         d = 0;
146         if (pos) {
147                 pos++;
148                 if (*pos >= '0' && *pos <= '9')
149                         d = *pos - '0';
150         }
151
152         return i * 10 + d;
153 }
154
155
156 static void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
157 {
158         bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
159         bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
160         bss->logger_syslog = (unsigned int) -1;
161         bss->logger_stdout = (unsigned int) -1;
162
163         bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
164
165         bss->wep_rekeying_period = 300;
166         /* use key0 in individual key and key1 in broadcast key */
167         bss->broadcast_key_idx_min = 1;
168         bss->broadcast_key_idx_max = 2;
169         bss->eap_reauth_period = 3600;
170
171         bss->wpa_group_rekey = 600;
172         bss->wpa_gmk_rekey = 86400;
173         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
174         bss->wpa_pairwise = WPA_CIPHER_TKIP;
175         bss->wpa_group = WPA_CIPHER_TKIP;
176         bss->rsn_pairwise = 0;
177
178         bss->max_num_sta = MAX_STA_COUNT;
179
180         bss->dtim_period = 2;
181
182         bss->radius_server_auth_port = 1812;
183         bss->ap_max_inactivity = AP_MAX_INACTIVITY;
184         bss->eapol_version = EAPOL_VERSION;
185
186         bss->max_listen_interval = 65535;
187
188 #ifdef CONFIG_IEEE80211W
189         bss->assoc_sa_query_max_timeout = 1000;
190         bss->assoc_sa_query_retry_timeout = 201;
191 #endif /* CONFIG_IEEE80211W */
192 #ifdef EAP_FAST
193          /* both anonymous and authenticated provisioning */
194         bss->eap_fast_prov = 3;
195         bss->pac_key_lifetime = 7 * 24 * 60 * 60;
196         bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
197 #endif /* EAP_FAST */
198 }
199
200
201 static struct hostapd_config * hostapd_config_defaults(void)
202 {
203         struct hostapd_config *conf;
204         struct hostapd_bss_config *bss;
205         int i;
206         const int aCWmin = 15, aCWmax = 1024;
207         const struct hostapd_wme_ac_params ac_bk =
208                 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
209         const struct hostapd_wme_ac_params ac_be =
210                 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
211         const struct hostapd_wme_ac_params ac_vi = /* video traffic */
212                 { aCWmin >> 1, aCWmin, 2, 3000 / 32, 1 };
213         const struct hostapd_wme_ac_params ac_vo = /* voice traffic */
214                 { aCWmin >> 2, aCWmin >> 1, 2, 1500 / 32, 1 };
215
216         conf = os_zalloc(sizeof(*conf));
217         bss = os_zalloc(sizeof(*bss));
218         if (conf == NULL || bss == NULL) {
219                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
220                            "configuration data.");
221                 os_free(conf);
222                 os_free(bss);
223                 return NULL;
224         }
225
226         /* set default driver based on configuration */
227         conf->driver = hostapd_drivers[0];
228         if (conf->driver == NULL) {
229                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
230                 os_free(conf);
231                 os_free(bss);
232                 return NULL;
233         }
234
235         bss->radius = os_zalloc(sizeof(*bss->radius));
236         if (bss->radius == NULL) {
237                 os_free(conf);
238                 os_free(bss);
239                 return NULL;
240         }
241
242         hostapd_config_defaults_bss(bss);
243
244         conf->num_bss = 1;
245         conf->bss = bss;
246
247         conf->beacon_int = 100;
248         conf->rts_threshold = -1; /* use driver default: 2347 */
249         conf->fragm_threshold = -1; /* user driver default: 2346 */
250         conf->send_probe_response = 1;
251         conf->bridge_packets = INTERNAL_BRIDGE_DO_NOT_CONTROL;
252
253         for (i = 0; i < NUM_TX_QUEUES; i++)
254                 conf->tx_queue[i].aifs = -1; /* use hw default */
255
256         conf->wme_ac_params[0] = ac_be;
257         conf->wme_ac_params[1] = ac_bk;
258         conf->wme_ac_params[2] = ac_vi;
259         conf->wme_ac_params[3] = ac_vo;
260
261 #ifdef CONFIG_IEEE80211N
262         conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
263 #endif /* CONFIG_IEEE80211N */
264
265         return conf;
266 }
267
268
269 int hostapd_mac_comp(const void *a, const void *b)
270 {
271         return os_memcmp(a, b, sizeof(macaddr));
272 }
273
274
275 int hostapd_mac_comp_empty(const void *a)
276 {
277         macaddr empty = { 0 };
278         return os_memcmp(a, empty, sizeof(macaddr));
279 }
280
281
282 static int hostapd_acl_comp(const void *a, const void *b)
283 {
284         const struct mac_acl_entry *aa = a;
285         const struct mac_acl_entry *bb = b;
286         return os_memcmp(aa->addr, bb->addr, sizeof(macaddr));
287 }
288
289
290 static int hostapd_config_read_maclist(const char *fname,
291                                        struct mac_acl_entry **acl, int *num)
292 {
293         FILE *f;
294         char buf[128], *pos;
295         int line = 0;
296         u8 addr[ETH_ALEN];
297         struct mac_acl_entry *newacl;
298         int vlan_id;
299
300         if (!fname)
301                 return 0;
302
303         f = fopen(fname, "r");
304         if (!f) {
305                 wpa_printf(MSG_ERROR, "MAC list file '%s' not found.", fname);
306                 return -1;
307         }
308
309         while (fgets(buf, sizeof(buf), f)) {
310                 line++;
311
312                 if (buf[0] == '#')
313                         continue;
314                 pos = buf;
315                 while (*pos != '\0') {
316                         if (*pos == '\n') {
317                                 *pos = '\0';
318                                 break;
319                         }
320                         pos++;
321                 }
322                 if (buf[0] == '\0')
323                         continue;
324
325                 if (hwaddr_aton(buf, addr)) {
326                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' at "
327                                    "line %d in '%s'", buf, line, fname);
328                         fclose(f);
329                         return -1;
330                 }
331
332                 vlan_id = 0;
333                 pos = buf;
334                 while (*pos != '\0' && *pos != ' ' && *pos != '\t')
335                         pos++;
336                 while (*pos == ' ' || *pos == '\t')
337                         pos++;
338                 if (*pos != '\0')
339                         vlan_id = atoi(pos);
340
341                 newacl = os_realloc(*acl, (*num + 1) * sizeof(**acl));
342                 if (newacl == NULL) {
343                         wpa_printf(MSG_ERROR, "MAC list reallocation failed");
344                         fclose(f);
345                         return -1;
346                 }
347
348                 *acl = newacl;
349                 os_memcpy((*acl)[*num].addr, addr, ETH_ALEN);
350                 (*acl)[*num].vlan_id = vlan_id;
351                 (*num)++;
352         }
353
354         fclose(f);
355
356         qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
357
358         return 0;
359 }
360
361
362 static int hostapd_config_read_wpa_psk(const char *fname,
363                                        struct hostapd_ssid *ssid)
364 {
365         FILE *f;
366         char buf[128], *pos;
367         int line = 0, ret = 0, len, ok;
368         u8 addr[ETH_ALEN];
369         struct hostapd_wpa_psk *psk;
370
371         if (!fname)
372                 return 0;
373
374         f = fopen(fname, "r");
375         if (!f) {
376                 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
377                 return -1;
378         }
379
380         while (fgets(buf, sizeof(buf), f)) {
381                 line++;
382
383                 if (buf[0] == '#')
384                         continue;
385                 pos = buf;
386                 while (*pos != '\0') {
387                         if (*pos == '\n') {
388                                 *pos = '\0';
389                                 break;
390                         }
391                         pos++;
392                 }
393                 if (buf[0] == '\0')
394                         continue;
395
396                 if (hwaddr_aton(buf, addr)) {
397                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
398                                    "line %d in '%s'", buf, line, fname);
399                         ret = -1;
400                         break;
401                 }
402
403                 psk = os_zalloc(sizeof(*psk));
404                 if (psk == NULL) {
405                         wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
406                         ret = -1;
407                         break;
408                 }
409                 if (is_zero_ether_addr(addr))
410                         psk->group = 1;
411                 else
412                         os_memcpy(psk->addr, addr, ETH_ALEN);
413
414                 pos = buf + 17;
415                 if (*pos == '\0') {
416                         wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
417                                    line, fname);
418                         os_free(psk);
419                         ret = -1;
420                         break;
421                 }
422                 pos++;
423
424                 ok = 0;
425                 len = os_strlen(pos);
426                 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
427                         ok = 1;
428                 else if (len >= 8 && len < 64) {
429                         pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
430                                     4096, psk->psk, PMK_LEN);
431                         ok = 1;
432                 }
433                 if (!ok) {
434                         wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
435                                    "'%s'", pos, line, fname);
436                         os_free(psk);
437                         ret = -1;
438                         break;
439                 }
440
441                 psk->next = ssid->wpa_psk;
442                 ssid->wpa_psk = psk;
443         }
444
445         fclose(f);
446
447         return ret;
448 }
449
450
451 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
452 {
453         struct hostapd_ssid *ssid = &conf->ssid;
454
455         if (ssid->wpa_passphrase != NULL) {
456                 if (ssid->wpa_psk != NULL) {
457                         wpa_printf(MSG_ERROR, "Warning: both WPA PSK and "
458                                    "passphrase set. Using passphrase.");
459                         os_free(ssid->wpa_psk);
460                 }
461                 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
462                 if (ssid->wpa_psk == NULL) {
463                         wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
464                         return -1;
465                 }
466                 wpa_hexdump_ascii(MSG_DEBUG, "SSID",
467                                   (u8 *) ssid->ssid, ssid->ssid_len);
468                 wpa_hexdump_ascii(MSG_DEBUG, "PSK (ASCII passphrase)",
469                                   (u8 *) ssid->wpa_passphrase,
470                                   os_strlen(ssid->wpa_passphrase));
471                 pbkdf2_sha1(ssid->wpa_passphrase,
472                             ssid->ssid, ssid->ssid_len,
473                             4096, ssid->wpa_psk->psk, PMK_LEN);
474                 wpa_hexdump(MSG_DEBUG, "PSK (from passphrase)",
475                             ssid->wpa_psk->psk, PMK_LEN);
476                 ssid->wpa_psk->group = 1;
477         }
478
479         if (ssid->wpa_psk_file) {
480                 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
481                                                 &conf->ssid))
482                         return -1;
483         }
484
485         return 0;
486 }
487
488
489 #ifdef EAP_SERVER
490 static int hostapd_config_read_eap_user(const char *fname,
491                                         struct hostapd_bss_config *conf)
492 {
493         FILE *f;
494         char buf[512], *pos, *start, *pos2;
495         int line = 0, ret = 0, num_methods;
496         struct hostapd_eap_user *user, *tail = NULL;
497
498         if (!fname)
499                 return 0;
500
501         f = fopen(fname, "r");
502         if (!f) {
503                 wpa_printf(MSG_ERROR, "EAP user file '%s' not found.", fname);
504                 return -1;
505         }
506
507         /* Lines: "user" METHOD,METHOD2 "password" (password optional) */
508         while (fgets(buf, sizeof(buf), f)) {
509                 line++;
510
511                 if (buf[0] == '#')
512                         continue;
513                 pos = buf;
514                 while (*pos != '\0') {
515                         if (*pos == '\n') {
516                                 *pos = '\0';
517                                 break;
518                         }
519                         pos++;
520                 }
521                 if (buf[0] == '\0')
522                         continue;
523
524                 user = NULL;
525
526                 if (buf[0] != '"' && buf[0] != '*') {
527                         wpa_printf(MSG_ERROR, "Invalid EAP identity (no \" in "
528                                    "start) on line %d in '%s'", line, fname);
529                         goto failed;
530                 }
531
532                 user = os_zalloc(sizeof(*user));
533                 if (user == NULL) {
534                         wpa_printf(MSG_ERROR, "EAP user allocation failed");
535                         goto failed;
536                 }
537                 user->force_version = -1;
538
539                 if (buf[0] == '*') {
540                         pos = buf;
541                 } else {
542                         pos = buf + 1;
543                         start = pos;
544                         while (*pos != '"' && *pos != '\0')
545                                 pos++;
546                         if (*pos == '\0') {
547                                 wpa_printf(MSG_ERROR, "Invalid EAP identity "
548                                            "(no \" in end) on line %d in '%s'",
549                                            line, fname);
550                                 goto failed;
551                         }
552
553                         user->identity = os_malloc(pos - start);
554                         if (user->identity == NULL) {
555                                 wpa_printf(MSG_ERROR, "Failed to allocate "
556                                            "memory for EAP identity");
557                                 goto failed;
558                         }
559                         os_memcpy(user->identity, start, pos - start);
560                         user->identity_len = pos - start;
561
562                         if (pos[0] == '"' && pos[1] == '*') {
563                                 user->wildcard_prefix = 1;
564                                 pos++;
565                         }
566                 }
567                 pos++;
568                 while (*pos == ' ' || *pos == '\t')
569                         pos++;
570
571                 if (*pos == '\0') {
572                         wpa_printf(MSG_ERROR, "No EAP method on line %d in "
573                                    "'%s'", line, fname);
574                         goto failed;
575                 }
576
577                 start = pos;
578                 while (*pos != ' ' && *pos != '\t' && *pos != '\0')
579                         pos++;
580                 if (*pos == '\0') {
581                         pos = NULL;
582                 } else {
583                         *pos = '\0';
584                         pos++;
585                 }
586                 num_methods = 0;
587                 while (*start) {
588                         char *pos3 = os_strchr(start, ',');
589                         if (pos3) {
590                                 *pos3++ = '\0';
591                         }
592                         user->methods[num_methods].method =
593                                 eap_server_get_type(
594                                         start,
595                                         &user->methods[num_methods].vendor);
596                         if (user->methods[num_methods].vendor ==
597                             EAP_VENDOR_IETF &&
598                             user->methods[num_methods].method == EAP_TYPE_NONE)
599                         {
600                                 if (os_strcmp(start, "TTLS-PAP") == 0) {
601                                         user->ttls_auth |= EAP_TTLS_AUTH_PAP;
602                                         goto skip_eap;
603                                 }
604                                 if (os_strcmp(start, "TTLS-CHAP") == 0) {
605                                         user->ttls_auth |= EAP_TTLS_AUTH_CHAP;
606                                         goto skip_eap;
607                                 }
608                                 if (os_strcmp(start, "TTLS-MSCHAP") == 0) {
609                                         user->ttls_auth |=
610                                                 EAP_TTLS_AUTH_MSCHAP;
611                                         goto skip_eap;
612                                 }
613                                 if (os_strcmp(start, "TTLS-MSCHAPV2") == 0) {
614                                         user->ttls_auth |=
615                                                 EAP_TTLS_AUTH_MSCHAPV2;
616                                         goto skip_eap;
617                                 }
618                                 wpa_printf(MSG_ERROR, "Unsupported EAP type "
619                                            "'%s' on line %d in '%s'",
620                                            start, line, fname);
621                                 goto failed;
622                         }
623
624                         num_methods++;
625                         if (num_methods >= EAP_USER_MAX_METHODS)
626                                 break;
627                 skip_eap:
628                         if (pos3 == NULL)
629                                 break;
630                         start = pos3;
631                 }
632                 if (num_methods == 0 && user->ttls_auth == 0) {
633                         wpa_printf(MSG_ERROR, "No EAP types configured on "
634                                    "line %d in '%s'", line, fname);
635                         goto failed;
636                 }
637
638                 if (pos == NULL)
639                         goto done;
640
641                 while (*pos == ' ' || *pos == '\t')
642                         pos++;
643                 if (*pos == '\0')
644                         goto done;
645
646                 if (os_strncmp(pos, "[ver=0]", 7) == 0) {
647                         user->force_version = 0;
648                         goto done;
649                 }
650
651                 if (os_strncmp(pos, "[ver=1]", 7) == 0) {
652                         user->force_version = 1;
653                         goto done;
654                 }
655
656                 if (os_strncmp(pos, "[2]", 3) == 0) {
657                         user->phase2 = 1;
658                         goto done;
659                 }
660
661                 if (*pos == '"') {
662                         pos++;
663                         start = pos;
664                         while (*pos != '"' && *pos != '\0')
665                                 pos++;
666                         if (*pos == '\0') {
667                                 wpa_printf(MSG_ERROR, "Invalid EAP password "
668                                            "(no \" in end) on line %d in '%s'",
669                                            line, fname);
670                                 goto failed;
671                         }
672
673                         user->password = os_malloc(pos - start);
674                         if (user->password == NULL) {
675                                 wpa_printf(MSG_ERROR, "Failed to allocate "
676                                            "memory for EAP password");
677                                 goto failed;
678                         }
679                         os_memcpy(user->password, start, pos - start);
680                         user->password_len = pos - start;
681
682                         pos++;
683                 } else if (os_strncmp(pos, "hash:", 5) == 0) {
684                         pos += 5;
685                         pos2 = pos;
686                         while (*pos2 != '\0' && *pos2 != ' ' &&
687                                *pos2 != '\t' && *pos2 != '#')
688                                 pos2++;
689                         if (pos2 - pos != 32) {
690                                 wpa_printf(MSG_ERROR, "Invalid password hash "
691                                            "on line %d in '%s'", line, fname);
692                                 goto failed;
693                         }
694                         user->password = os_malloc(16);
695                         if (user->password == NULL) {
696                                 wpa_printf(MSG_ERROR, "Failed to allocate "
697                                            "memory for EAP password hash");
698                                 goto failed;
699                         }
700                         if (hexstr2bin(pos, user->password, 16) < 0) {
701                                 wpa_printf(MSG_ERROR, "Invalid hash password "
702                                            "on line %d in '%s'", line, fname);
703                                 goto failed;
704                         }
705                         user->password_len = 16;
706                         user->password_hash = 1;
707                         pos = pos2;
708                 } else {
709                         pos2 = pos;
710                         while (*pos2 != '\0' && *pos2 != ' ' &&
711                                *pos2 != '\t' && *pos2 != '#')
712                                 pos2++;
713                         if ((pos2 - pos) & 1) {
714                                 wpa_printf(MSG_ERROR, "Invalid hex password "
715                                            "on line %d in '%s'", line, fname);
716                                 goto failed;
717                         }
718                         user->password = os_malloc((pos2 - pos) / 2);
719                         if (user->password == NULL) {
720                                 wpa_printf(MSG_ERROR, "Failed to allocate "
721                                            "memory for EAP password");
722                                 goto failed;
723                         }
724                         if (hexstr2bin(pos, user->password,
725                                        (pos2 - pos) / 2) < 0) {
726                                 wpa_printf(MSG_ERROR, "Invalid hex password "
727                                            "on line %d in '%s'", line, fname);
728                                 goto failed;
729                         }
730                         user->password_len = (pos2 - pos) / 2;
731                         pos = pos2;
732                 }
733
734                 while (*pos == ' ' || *pos == '\t')
735                         pos++;
736                 if (os_strncmp(pos, "[2]", 3) == 0) {
737                         user->phase2 = 1;
738                 }
739
740         done:
741                 if (tail == NULL) {
742                         tail = conf->eap_user = user;
743                 } else {
744                         tail->next = user;
745                         tail = user;
746                 }
747                 continue;
748
749         failed:
750                 if (user) {
751                         os_free(user->password);
752                         os_free(user->identity);
753                         os_free(user);
754                 }
755                 ret = -1;
756                 break;
757         }
758
759         fclose(f);
760
761         return ret;
762 }
763 #endif /* EAP_SERVER */
764
765
766 #ifndef CONFIG_NO_RADIUS
767 static int
768 hostapd_config_read_radius_addr(struct hostapd_radius_server **server,
769                                 int *num_server, const char *val, int def_port,
770                                 struct hostapd_radius_server **curr_serv)
771 {
772         struct hostapd_radius_server *nserv;
773         int ret;
774         static int server_index = 1;
775
776         nserv = os_realloc(*server, (*num_server + 1) * sizeof(*nserv));
777         if (nserv == NULL)
778                 return -1;
779
780         *server = nserv;
781         nserv = &nserv[*num_server];
782         (*num_server)++;
783         (*curr_serv) = nserv;
784
785         os_memset(nserv, 0, sizeof(*nserv));
786         nserv->port = def_port;
787         ret = hostapd_parse_ip_addr(val, &nserv->addr);
788         nserv->index = server_index++;
789
790         return ret;
791 }
792 #endif /* CONFIG_NO_RADIUS */
793
794
795 static int hostapd_config_parse_key_mgmt(int line, const char *value)
796 {
797         int val = 0, last;
798         char *start, *end, *buf;
799
800         buf = os_strdup(value);
801         if (buf == NULL)
802                 return -1;
803         start = buf;
804
805         while (*start != '\0') {
806                 while (*start == ' ' || *start == '\t')
807                         start++;
808                 if (*start == '\0')
809                         break;
810                 end = start;
811                 while (*end != ' ' && *end != '\t' && *end != '\0')
812                         end++;
813                 last = *end == '\0';
814                 *end = '\0';
815                 if (os_strcmp(start, "WPA-PSK") == 0)
816                         val |= WPA_KEY_MGMT_PSK;
817                 else if (os_strcmp(start, "WPA-EAP") == 0)
818                         val |= WPA_KEY_MGMT_IEEE8021X;
819 #ifdef CONFIG_IEEE80211R
820                 else if (os_strcmp(start, "FT-PSK") == 0)
821                         val |= WPA_KEY_MGMT_FT_PSK;
822                 else if (os_strcmp(start, "FT-EAP") == 0)
823                         val |= WPA_KEY_MGMT_FT_IEEE8021X;
824 #endif /* CONFIG_IEEE80211R */
825 #ifdef CONFIG_IEEE80211W
826                 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
827                         val |= WPA_KEY_MGMT_PSK_SHA256;
828                 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
829                         val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
830 #endif /* CONFIG_IEEE80211W */
831                 else {
832                         wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
833                                    line, start);
834                         os_free(buf);
835                         return -1;
836                 }
837
838                 if (last)
839                         break;
840                 start = end + 1;
841         }
842
843         os_free(buf);
844         if (val == 0) {
845                 wpa_printf(MSG_ERROR, "Line %d: no key_mgmt values "
846                            "configured.", line);
847                 return -1;
848         }
849
850         return val;
851 }
852
853
854 static int hostapd_config_parse_cipher(int line, const char *value)
855 {
856         int val = 0, last;
857         char *start, *end, *buf;
858
859         buf = os_strdup(value);
860         if (buf == NULL)
861                 return -1;
862         start = buf;
863
864         while (*start != '\0') {
865                 while (*start == ' ' || *start == '\t')
866                         start++;
867                 if (*start == '\0')
868                         break;
869                 end = start;
870                 while (*end != ' ' && *end != '\t' && *end != '\0')
871                         end++;
872                 last = *end == '\0';
873                 *end = '\0';
874                 if (os_strcmp(start, "CCMP") == 0)
875                         val |= WPA_CIPHER_CCMP;
876                 else if (os_strcmp(start, "TKIP") == 0)
877                         val |= WPA_CIPHER_TKIP;
878                 else if (os_strcmp(start, "WEP104") == 0)
879                         val |= WPA_CIPHER_WEP104;
880                 else if (os_strcmp(start, "WEP40") == 0)
881                         val |= WPA_CIPHER_WEP40;
882                 else if (os_strcmp(start, "NONE") == 0)
883                         val |= WPA_CIPHER_NONE;
884                 else {
885                         wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
886                                    line, start);
887                         os_free(buf);
888                         return -1;
889                 }
890
891                 if (last)
892                         break;
893                 start = end + 1;
894         }
895         os_free(buf);
896
897         if (val == 0) {
898                 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
899                            line);
900                 return -1;
901         }
902         return val;
903 }
904
905
906 static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
907                                     struct hostapd_config *conf)
908 {
909         if (bss->ieee802_1x && !bss->eap_server &&
910             !bss->radius->auth_servers) {
911                 wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
912                            "EAP authenticator configured).");
913                 return -1;
914         }
915
916         if (bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
917             bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
918             bss->ssid.wpa_psk_file == NULL) {
919                 wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
920                            "is not configured.");
921                 return -1;
922         }
923
924         if (hostapd_mac_comp_empty(bss->bssid) != 0) {
925                 size_t i;
926
927                 for (i = 0; i < conf->num_bss; i++) {
928                         if ((&conf->bss[i] != bss) &&
929                             (hostapd_mac_comp(conf->bss[i].bssid,
930                                               bss->bssid) == 0)) {
931                                 wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
932                                            " on interface '%s' and '%s'.",
933                                            MAC2STR(bss->bssid),
934                                            conf->bss[i].iface, bss->iface);
935                                 return -1;
936                         }
937                 }
938         }
939
940 #ifdef CONFIG_IEEE80211R
941         if ((bss->wpa_key_mgmt &
942              (WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X)) &&
943             (bss->nas_identifier == NULL ||
944              os_strlen(bss->nas_identifier) < 1 ||
945              os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
946                 wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
947                            "nas_identifier to be configured as a 1..48 octet "
948                            "string");
949                 return -1;
950         }
951 #endif /* CONFIG_IEEE80211R */
952
953 #ifdef CONFIG_IEEE80211N
954         if (conf->ieee80211n && bss->wpa &&
955             !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
956             !(bss->rsn_pairwise & WPA_CIPHER_CCMP)) {
957                 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
958                            "requires CCMP to be enabled");
959                 return -1;
960         }
961 #endif /* CONFIG_IEEE80211N */
962
963         return 0;
964 }
965
966
967 static int hostapd_config_check(struct hostapd_config *conf)
968 {
969         size_t i;
970
971         if (conf->ieee80211d && (!conf->country[0] || !conf->country[1])) {
972                 wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
973                            "setting the country_code");
974                 return -1;
975         }
976
977         for (i = 0; i < conf->num_bss; i++) {
978                 if (hostapd_config_check_bss(&conf->bss[i], conf))
979                         return -1;
980         }
981
982         return 0;
983 }
984
985
986 static int hostapd_config_read_wep(struct hostapd_wep_keys *wep, int keyidx,
987                                    char *val)
988 {
989         size_t len = os_strlen(val);
990
991         if (keyidx < 0 || keyidx > 3 || wep->key[keyidx] != NULL)
992                 return -1;
993
994         if (val[0] == '"') {
995                 if (len < 2 || val[len - 1] != '"')
996                         return -1;
997                 len -= 2;
998                 wep->key[keyidx] = os_malloc(len);
999                 if (wep->key[keyidx] == NULL)
1000                         return -1;
1001                 os_memcpy(wep->key[keyidx], val + 1, len);
1002                 wep->len[keyidx] = len;
1003         } else {
1004                 if (len & 1)
1005                         return -1;
1006                 len /= 2;
1007                 wep->key[keyidx] = os_malloc(len);
1008                 if (wep->key[keyidx] == NULL)
1009                         return -1;
1010                 wep->len[keyidx] = len;
1011                 if (hexstr2bin(val, wep->key[keyidx], len) < 0)
1012                         return -1;
1013         }
1014
1015         wep->keys_set++;
1016
1017         return 0;
1018 }
1019
1020
1021 static int hostapd_parse_rates(int **rate_list, char *val)
1022 {
1023         int *list;
1024         int count;
1025         char *pos, *end;
1026
1027         os_free(*rate_list);
1028         *rate_list = NULL;
1029
1030         pos = val;
1031         count = 0;
1032         while (*pos != '\0') {
1033                 if (*pos == ' ')
1034                         count++;
1035                 pos++;
1036         }
1037
1038         list = os_malloc(sizeof(int) * (count + 2));
1039         if (list == NULL)
1040                 return -1;
1041         pos = val;
1042         count = 0;
1043         while (*pos != '\0') {
1044                 end = os_strchr(pos, ' ');
1045                 if (end)
1046                         *end = '\0';
1047
1048                 list[count++] = atoi(pos);
1049                 if (!end)
1050                         break;
1051                 pos = end + 1;
1052         }
1053         list[count] = -1;
1054
1055         *rate_list = list;
1056         return 0;
1057 }
1058
1059
1060 static int hostapd_config_bss(struct hostapd_config *conf, const char *ifname)
1061 {
1062         struct hostapd_bss_config *bss;
1063
1064         if (*ifname == '\0')
1065                 return -1;
1066
1067         bss = os_realloc(conf->bss, (conf->num_bss + 1) *
1068                          sizeof(struct hostapd_bss_config));
1069         if (bss == NULL) {
1070                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
1071                            "multi-BSS entry");
1072                 return -1;
1073         }
1074         conf->bss = bss;
1075
1076         bss = &(conf->bss[conf->num_bss]);
1077         os_memset(bss, 0, sizeof(*bss));
1078         bss->radius = os_zalloc(sizeof(*bss->radius));
1079         if (bss->radius == NULL) {
1080                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
1081                            "multi-BSS RADIUS data");
1082                 return -1;
1083         }
1084
1085         conf->num_bss++;
1086         conf->last_bss = bss;
1087
1088         hostapd_config_defaults_bss(bss);
1089         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1090         os_memcpy(bss->ssid.vlan, bss->iface, IFNAMSIZ + 1);
1091
1092         return 0;
1093 }
1094
1095
1096 static int valid_cw(int cw)
1097 {
1098         return (cw == 1 || cw == 3 || cw == 7 || cw == 15 || cw == 31 ||
1099                 cw == 63 || cw == 127 || cw == 255 || cw == 511 || cw == 1023);
1100 }
1101
1102
1103 enum {
1104         IEEE80211_TX_QUEUE_DATA0 = 0, /* used for EDCA AC_VO data */
1105         IEEE80211_TX_QUEUE_DATA1 = 1, /* used for EDCA AC_VI data */
1106         IEEE80211_TX_QUEUE_DATA2 = 2, /* used for EDCA AC_BE data */
1107         IEEE80211_TX_QUEUE_DATA3 = 3, /* used for EDCA AC_BK data */
1108         IEEE80211_TX_QUEUE_DATA4 = 4,
1109         IEEE80211_TX_QUEUE_AFTER_BEACON = 6,
1110         IEEE80211_TX_QUEUE_BEACON = 7
1111 };
1112
1113 static int hostapd_config_tx_queue(struct hostapd_config *conf, char *name,
1114                                    char *val)
1115 {
1116         int num;
1117         char *pos;
1118         struct hostapd_tx_queue_params *queue;
1119
1120         /* skip 'tx_queue_' prefix */
1121         pos = name + 9;
1122         if (os_strncmp(pos, "data", 4) == 0 &&
1123             pos[4] >= '0' && pos[4] <= '9' && pos[5] == '_') {
1124                 num = pos[4] - '0';
1125                 pos += 6;
1126         } else if (os_strncmp(pos, "after_beacon_", 13) == 0) {
1127                 num = IEEE80211_TX_QUEUE_AFTER_BEACON;
1128                 pos += 13;
1129         } else if (os_strncmp(pos, "beacon_", 7) == 0) {
1130                 num = IEEE80211_TX_QUEUE_BEACON;
1131                 pos += 7;
1132         } else {
1133                 wpa_printf(MSG_ERROR, "Unknown tx_queue name '%s'", pos);
1134                 return -1;
1135         }
1136
1137         queue = &conf->tx_queue[num];
1138
1139         if (os_strcmp(pos, "aifs") == 0) {
1140                 queue->aifs = atoi(val);
1141                 if (queue->aifs < 0 || queue->aifs > 255) {
1142                         wpa_printf(MSG_ERROR, "Invalid AIFS value %d",
1143                                    queue->aifs);
1144                         return -1;
1145                 }
1146         } else if (os_strcmp(pos, "cwmin") == 0) {
1147                 queue->cwmin = atoi(val);
1148                 if (!valid_cw(queue->cwmin)) {
1149                         wpa_printf(MSG_ERROR, "Invalid cwMin value %d",
1150                                    queue->cwmin);
1151                         return -1;
1152                 }
1153         } else if (os_strcmp(pos, "cwmax") == 0) {
1154                 queue->cwmax = atoi(val);
1155                 if (!valid_cw(queue->cwmax)) {
1156                         wpa_printf(MSG_ERROR, "Invalid cwMax value %d",
1157                                    queue->cwmax);
1158                         return -1;
1159                 }
1160         } else if (os_strcmp(pos, "burst") == 0) {
1161                 queue->burst = hostapd_config_read_int10(val);
1162         } else {
1163                 wpa_printf(MSG_ERROR, "Unknown tx_queue field '%s'", pos);
1164                 return -1;
1165         }
1166
1167         queue->configured = 1;
1168
1169         return 0;
1170 }
1171
1172
1173 static int hostapd_config_wme_ac(struct hostapd_config *conf, char *name,
1174                                    char *val)
1175 {
1176         int num, v;
1177         char *pos;
1178         struct hostapd_wme_ac_params *ac;
1179
1180         /* skip 'wme_ac_' prefix */
1181         pos = name + 7;
1182         if (os_strncmp(pos, "be_", 3) == 0) {
1183                 num = 0;
1184                 pos += 3;
1185         } else if (os_strncmp(pos, "bk_", 3) == 0) {
1186                 num = 1;
1187                 pos += 3;
1188         } else if (os_strncmp(pos, "vi_", 3) == 0) {
1189                 num = 2;
1190                 pos += 3;
1191         } else if (os_strncmp(pos, "vo_", 3) == 0) {
1192                 num = 3;
1193                 pos += 3;
1194         } else {
1195                 wpa_printf(MSG_ERROR, "Unknown wme name '%s'", pos);
1196                 return -1;
1197         }
1198
1199         ac = &conf->wme_ac_params[num];
1200
1201         if (os_strcmp(pos, "aifs") == 0) {
1202                 v = atoi(val);
1203                 if (v < 1 || v > 255) {
1204                         wpa_printf(MSG_ERROR, "Invalid AIFS value %d", v);
1205                         return -1;
1206                 }
1207                 ac->aifs = v;
1208         } else if (os_strcmp(pos, "cwmin") == 0) {
1209                 v = atoi(val);
1210                 if (v < 0 || v > 12) {
1211                         wpa_printf(MSG_ERROR, "Invalid cwMin value %d", v);
1212                         return -1;
1213                 }
1214                 ac->cwmin = v;
1215         } else if (os_strcmp(pos, "cwmax") == 0) {
1216                 v = atoi(val);
1217                 if (v < 0 || v > 12) {
1218                         wpa_printf(MSG_ERROR, "Invalid cwMax value %d", v);
1219                         return -1;
1220                 }
1221                 ac->cwmax = v;
1222         } else if (os_strcmp(pos, "txop_limit") == 0) {
1223                 v = atoi(val);
1224                 if (v < 0 || v > 0xffff) {
1225                         wpa_printf(MSG_ERROR, "Invalid txop value %d", v);
1226                         return -1;
1227                 }
1228                 ac->txopLimit = v;
1229         } else if (os_strcmp(pos, "acm") == 0) {
1230                 v = atoi(val);
1231                 if (v < 0 || v > 1) {
1232                         wpa_printf(MSG_ERROR, "Invalid acm value %d", v);
1233                         return -1;
1234                 }
1235                 ac->admission_control_mandatory = v;
1236         } else {
1237                 wpa_printf(MSG_ERROR, "Unknown wme_ac_ field '%s'", pos);
1238                 return -1;
1239         }
1240
1241         return 0;
1242 }
1243
1244
1245 #ifdef CONFIG_IEEE80211R
1246 static int add_r0kh(struct hostapd_bss_config *bss, char *value)
1247 {
1248         struct ft_remote_r0kh *r0kh;
1249         char *pos, *next;
1250
1251         r0kh = os_zalloc(sizeof(*r0kh));
1252         if (r0kh == NULL)
1253                 return -1;
1254
1255         /* 02:01:02:03:04:05 a.example.com 000102030405060708090a0b0c0d0e0f */
1256         pos = value;
1257         next = os_strchr(pos, ' ');
1258         if (next)
1259                 *next++ = '\0';
1260         if (next == NULL || hwaddr_aton(pos, r0kh->addr)) {
1261                 wpa_printf(MSG_ERROR, "Invalid R0KH MAC address: '%s'", pos);
1262                 os_free(r0kh);
1263                 return -1;
1264         }
1265
1266         pos = next;
1267         next = os_strchr(pos, ' ');
1268         if (next)
1269                 *next++ = '\0';
1270         if (next == NULL || next - pos > FT_R0KH_ID_MAX_LEN) {
1271                 wpa_printf(MSG_ERROR, "Invalid R0KH-ID: '%s'", pos);
1272                 os_free(r0kh);
1273                 return -1;
1274         }
1275         r0kh->id_len = next - pos - 1;
1276         os_memcpy(r0kh->id, pos, r0kh->id_len);
1277
1278         pos = next;
1279         if (hexstr2bin(pos, r0kh->key, sizeof(r0kh->key))) {
1280                 wpa_printf(MSG_ERROR, "Invalid R0KH key: '%s'", pos);
1281                 os_free(r0kh);
1282                 return -1;
1283         }
1284
1285         r0kh->next = bss->r0kh_list;
1286         bss->r0kh_list = r0kh;
1287
1288         return 0;
1289 }
1290
1291
1292 static int add_r1kh(struct hostapd_bss_config *bss, char *value)
1293 {
1294         struct ft_remote_r1kh *r1kh;
1295         char *pos, *next;
1296
1297         r1kh = os_zalloc(sizeof(*r1kh));
1298         if (r1kh == NULL)
1299                 return -1;
1300
1301         /* 02:01:02:03:04:05 02:01:02:03:04:05
1302          * 000102030405060708090a0b0c0d0e0f */
1303         pos = value;
1304         next = os_strchr(pos, ' ');
1305         if (next)
1306                 *next++ = '\0';
1307         if (next == NULL || hwaddr_aton(pos, r1kh->addr)) {
1308                 wpa_printf(MSG_ERROR, "Invalid R1KH MAC address: '%s'", pos);
1309                 os_free(r1kh);
1310                 return -1;
1311         }
1312
1313         pos = next;
1314         next = os_strchr(pos, ' ');
1315         if (next)
1316                 *next++ = '\0';
1317         if (next == NULL || hwaddr_aton(pos, r1kh->id)) {
1318                 wpa_printf(MSG_ERROR, "Invalid R1KH-ID: '%s'", pos);
1319                 os_free(r1kh);
1320                 return -1;
1321         }
1322
1323         pos = next;
1324         if (hexstr2bin(pos, r1kh->key, sizeof(r1kh->key))) {
1325                 wpa_printf(MSG_ERROR, "Invalid R1KH key: '%s'", pos);
1326                 os_free(r1kh);
1327                 return -1;
1328         }
1329
1330         r1kh->next = bss->r1kh_list;
1331         bss->r1kh_list = r1kh;
1332
1333         return 0;
1334 }
1335 #endif /* CONFIG_IEEE80211R */
1336
1337
1338 #ifdef CONFIG_IEEE80211N
1339 static int hostapd_config_ht_capab(struct hostapd_config *conf,
1340                                    const char *capab)
1341 {
1342         if (os_strstr(capab, "[LDPC]"))
1343                 conf->ht_capab |= HT_CAP_INFO_LDPC_CODING_CAP;
1344         if (os_strstr(capab, "[HT40-]")) {
1345                 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1346                 conf->secondary_channel = -1;
1347         }
1348         if (os_strstr(capab, "[HT40+]")) {
1349                 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1350                 conf->secondary_channel = 1;
1351         }
1352         if (os_strstr(capab, "[SMPS-STATIC]")) {
1353                 conf->ht_capab &= ~HT_CAP_INFO_SMPS_MASK;
1354                 conf->ht_capab |= HT_CAP_INFO_SMPS_STATIC;
1355         }
1356         if (os_strstr(capab, "[SMPS-DYNAMIC]")) {
1357                 conf->ht_capab &= ~HT_CAP_INFO_SMPS_MASK;
1358                 conf->ht_capab |= HT_CAP_INFO_SMPS_DYNAMIC;
1359         }
1360         if (os_strstr(capab, "[GF]"))
1361                 conf->ht_capab |= HT_CAP_INFO_GREEN_FIELD;
1362         if (os_strstr(capab, "[SHORT-GI-20]"))
1363                 conf->ht_capab |= HT_CAP_INFO_SHORT_GI20MHZ;
1364         if (os_strstr(capab, "[SHORT-GI-40]"))
1365                 conf->ht_capab |= HT_CAP_INFO_SHORT_GI40MHZ;
1366         if (os_strstr(capab, "[TX-STBC]"))
1367                 conf->ht_capab |= HT_CAP_INFO_TX_STBC;
1368         if (os_strstr(capab, "[RX-STBC1]")) {
1369                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1370                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_1;
1371         }
1372         if (os_strstr(capab, "[RX-STBC12]")) {
1373                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1374                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_12;
1375         }
1376         if (os_strstr(capab, "[RX-STBC123]")) {
1377                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1378                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_123;
1379         }
1380         if (os_strstr(capab, "[DELAYED-BA]"))
1381                 conf->ht_capab |= HT_CAP_INFO_DELAYED_BA;
1382         if (os_strstr(capab, "[MAX-AMSDU-7935]"))
1383                 conf->ht_capab |= HT_CAP_INFO_MAX_AMSDU_SIZE;
1384         if (os_strstr(capab, "[DSSS_CCK-40]"))
1385                 conf->ht_capab |= HT_CAP_INFO_DSSS_CCK40MHZ;
1386         if (os_strstr(capab, "[PSMP]"))
1387                 conf->ht_capab |= HT_CAP_INFO_PSMP_SUPP;
1388         if (os_strstr(capab, "[LSIG-TXOP-PROT]"))
1389                 conf->ht_capab |= HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT;
1390
1391         return 0;
1392 }
1393 #endif /* CONFIG_IEEE80211N */
1394
1395
1396 /**
1397  * hostapd_config_read - Read and parse a configuration file
1398  * @fname: Configuration file name (including path, if needed)
1399  * Returns: Allocated configuration data structure
1400  */
1401 struct hostapd_config * hostapd_config_read(const char *fname)
1402 {
1403         struct hostapd_config *conf;
1404         struct hostapd_bss_config *bss;
1405         FILE *f;
1406         char buf[256], *pos;
1407         int line = 0;
1408         int errors = 0;
1409         int pairwise;
1410         size_t i;
1411
1412         f = fopen(fname, "r");
1413         if (f == NULL) {
1414                 wpa_printf(MSG_ERROR, "Could not open configuration file '%s' "
1415                            "for reading.", fname);
1416                 return NULL;
1417         }
1418
1419         conf = hostapd_config_defaults();
1420         if (conf == NULL) {
1421                 fclose(f);
1422                 return NULL;
1423         }
1424         bss = conf->last_bss = conf->bss;
1425
1426         while (fgets(buf, sizeof(buf), f)) {
1427                 bss = conf->last_bss;
1428                 line++;
1429
1430                 if (buf[0] == '#')
1431                         continue;
1432                 pos = buf;
1433                 while (*pos != '\0') {
1434                         if (*pos == '\n') {
1435                                 *pos = '\0';
1436                                 break;
1437                         }
1438                         pos++;
1439                 }
1440                 if (buf[0] == '\0')
1441                         continue;
1442
1443                 pos = os_strchr(buf, '=');
1444                 if (pos == NULL) {
1445                         wpa_printf(MSG_ERROR, "Line %d: invalid line '%s'",
1446                                    line, buf);
1447                         errors++;
1448                         continue;
1449                 }
1450                 *pos = '\0';
1451                 pos++;
1452
1453                 if (os_strcmp(buf, "interface") == 0) {
1454                         os_strlcpy(conf->bss[0].iface, pos,
1455                                    sizeof(conf->bss[0].iface));
1456                 } else if (os_strcmp(buf, "bridge") == 0) {
1457                         os_strlcpy(bss->bridge, pos, sizeof(bss->bridge));
1458                 } else if (os_strcmp(buf, "driver") == 0) {
1459                         int i;
1460                         /* clear to get error below if setting is invalid */
1461                         conf->driver = NULL;
1462                         for (i = 0; hostapd_drivers[i]; i++) {
1463                                 if (os_strcmp(pos, hostapd_drivers[i]->name) ==
1464                                     0) {
1465                                         conf->driver = hostapd_drivers[i];
1466                                         break;
1467                                 }
1468                         }
1469                         if (conf->driver == NULL) {
1470                                 wpa_printf(MSG_ERROR, "Line %d: invalid/"
1471                                            "unknown driver '%s'", line, pos);
1472                                 errors++;
1473                         }
1474                 } else if (os_strcmp(buf, "debug") == 0) {
1475                         wpa_printf(MSG_DEBUG, "Line %d: DEPRECATED: 'debug' "
1476                                    "configuration variable is not used "
1477                                    "anymore", line);
1478                 } else if (os_strcmp(buf, "logger_syslog_level") == 0) {
1479                         bss->logger_syslog_level = atoi(pos);
1480                 } else if (os_strcmp(buf, "logger_stdout_level") == 0) {
1481                         bss->logger_stdout_level = atoi(pos);
1482                 } else if (os_strcmp(buf, "logger_syslog") == 0) {
1483                         bss->logger_syslog = atoi(pos);
1484                 } else if (os_strcmp(buf, "logger_stdout") == 0) {
1485                         bss->logger_stdout = atoi(pos);
1486                 } else if (os_strcmp(buf, "dump_file") == 0) {
1487                         bss->dump_log_name = os_strdup(pos);
1488                 } else if (os_strcmp(buf, "ssid") == 0) {
1489                         bss->ssid.ssid_len = os_strlen(pos);
1490                         if (bss->ssid.ssid_len > HOSTAPD_MAX_SSID_LEN ||
1491                             bss->ssid.ssid_len < 1) {
1492                                 wpa_printf(MSG_ERROR, "Line %d: invalid SSID "
1493                                            "'%s'", line, pos);
1494                                 errors++;
1495                         } else {
1496                                 os_memcpy(bss->ssid.ssid, pos,
1497                                           bss->ssid.ssid_len);
1498                                 bss->ssid.ssid[bss->ssid.ssid_len] = '\0';
1499                                 bss->ssid.ssid_set = 1;
1500                         }
1501                 } else if (os_strcmp(buf, "macaddr_acl") == 0) {
1502                         bss->macaddr_acl = atoi(pos);
1503                         if (bss->macaddr_acl != ACCEPT_UNLESS_DENIED &&
1504                             bss->macaddr_acl != DENY_UNLESS_ACCEPTED &&
1505                             bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
1506                                 wpa_printf(MSG_ERROR, "Line %d: unknown "
1507                                            "macaddr_acl %d",
1508                                            line, bss->macaddr_acl);
1509                         }
1510                 } else if (os_strcmp(buf, "accept_mac_file") == 0) {
1511                         if (hostapd_config_read_maclist(pos, &bss->accept_mac,
1512                                                         &bss->num_accept_mac))
1513                         {
1514                                 wpa_printf(MSG_ERROR, "Line %d: Failed to "
1515                                            "read accept_mac_file '%s'",
1516                                            line, pos);
1517                                 errors++;
1518                         }
1519                 } else if (os_strcmp(buf, "deny_mac_file") == 0) {
1520                         if (hostapd_config_read_maclist(pos, &bss->deny_mac,
1521                                                         &bss->num_deny_mac)) {
1522                                 wpa_printf(MSG_ERROR, "Line %d: Failed to "
1523                                            "read deny_mac_file '%s'",
1524                                            line, pos);
1525                                 errors++;
1526                         }
1527                 } else if (os_strcmp(buf, "ap_max_inactivity") == 0) {
1528                         bss->ap_max_inactivity = atoi(pos);
1529                 } else if (os_strcmp(buf, "country_code") == 0) {
1530                         os_memcpy(conf->country, pos, 2);
1531                         /* FIX: make this configurable */
1532                         conf->country[2] = ' ';
1533                 } else if (os_strcmp(buf, "ieee80211d") == 0) {
1534                         conf->ieee80211d = atoi(pos);
1535                 } else if (os_strcmp(buf, "ieee8021x") == 0) {
1536                         bss->ieee802_1x = atoi(pos);
1537                 } else if (os_strcmp(buf, "eapol_version") == 0) {
1538                         bss->eapol_version = atoi(pos);
1539                         if (bss->eapol_version < 1 ||
1540                             bss->eapol_version > 2) {
1541                                 wpa_printf(MSG_ERROR, "Line %d: invalid EAPOL "
1542                                            "version (%d): '%s'.",
1543                                            line, bss->eapol_version, pos);
1544                                 errors++;
1545                         } else
1546                                 wpa_printf(MSG_DEBUG, "eapol_version=%d",
1547                                            bss->eapol_version);
1548 #ifdef EAP_SERVER
1549                 } else if (os_strcmp(buf, "eap_authenticator") == 0) {
1550                         bss->eap_server = atoi(pos);
1551                         wpa_printf(MSG_ERROR, "Line %d: obsolete "
1552                                    "eap_authenticator used; this has been "
1553                                    "renamed to eap_server", line);
1554                 } else if (os_strcmp(buf, "eap_server") == 0) {
1555                         bss->eap_server = atoi(pos);
1556                 } else if (os_strcmp(buf, "eap_user_file") == 0) {
1557                         if (hostapd_config_read_eap_user(pos, bss))
1558                                 errors++;
1559                 } else if (os_strcmp(buf, "ca_cert") == 0) {
1560                         os_free(bss->ca_cert);
1561                         bss->ca_cert = os_strdup(pos);
1562                 } else if (os_strcmp(buf, "server_cert") == 0) {
1563                         os_free(bss->server_cert);
1564                         bss->server_cert = os_strdup(pos);
1565                 } else if (os_strcmp(buf, "private_key") == 0) {
1566                         os_free(bss->private_key);
1567                         bss->private_key = os_strdup(pos);
1568                 } else if (os_strcmp(buf, "private_key_passwd") == 0) {
1569                         os_free(bss->private_key_passwd);
1570                         bss->private_key_passwd = os_strdup(pos);
1571                 } else if (os_strcmp(buf, "check_crl") == 0) {
1572                         bss->check_crl = atoi(pos);
1573                 } else if (os_strcmp(buf, "dh_file") == 0) {
1574                         os_free(bss->dh_file);
1575                         bss->dh_file = os_strdup(pos);
1576 #ifdef EAP_FAST
1577                 } else if (os_strcmp(buf, "pac_opaque_encr_key") == 0) {
1578                         os_free(bss->pac_opaque_encr_key);
1579                         bss->pac_opaque_encr_key = os_malloc(16);
1580                         if (bss->pac_opaque_encr_key == NULL) {
1581                                 wpa_printf(MSG_ERROR, "Line %d: No memory for "
1582                                            "pac_opaque_encr_key", line);
1583                                 errors++;
1584                         } else if (hexstr2bin(pos, bss->pac_opaque_encr_key,
1585                                               16)) {
1586                                 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1587                                            "pac_opaque_encr_key", line);
1588                                 errors++;
1589                         }
1590                 } else if (os_strcmp(buf, "eap_fast_a_id") == 0) {
1591                         size_t idlen = os_strlen(pos);
1592                         if (idlen & 1) {
1593                                 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1594                                            "eap_fast_a_id", line);
1595                                 errors++;
1596                         } else {
1597                                 os_free(bss->eap_fast_a_id);
1598                                 bss->eap_fast_a_id = os_malloc(idlen / 2);
1599                                 if (bss->eap_fast_a_id == NULL ||
1600                                     hexstr2bin(pos, bss->eap_fast_a_id,
1601                                                idlen / 2)) {
1602                                         wpa_printf(MSG_ERROR, "Line %d: "
1603                                                    "Failed to parse "
1604                                                    "eap_fast_a_id", line);
1605                                         errors++;
1606                                 } else
1607                                         bss->eap_fast_a_id_len = idlen / 2;
1608                         }
1609                 } else if (os_strcmp(buf, "eap_fast_a_id_info") == 0) {
1610                         os_free(bss->eap_fast_a_id_info);
1611                         bss->eap_fast_a_id_info = os_strdup(pos);
1612                 } else if (os_strcmp(buf, "eap_fast_prov") == 0) {
1613                         bss->eap_fast_prov = atoi(pos);
1614                 } else if (os_strcmp(buf, "pac_key_lifetime") == 0) {
1615                         bss->pac_key_lifetime = atoi(pos);
1616                 } else if (os_strcmp(buf, "pac_key_refresh_time") == 0) {
1617                         bss->pac_key_refresh_time = atoi(pos);
1618 #endif /* EAP_FAST */
1619 #ifdef EAP_SIM
1620                 } else if (os_strcmp(buf, "eap_sim_db") == 0) {
1621                         os_free(bss->eap_sim_db);
1622                         bss->eap_sim_db = os_strdup(pos);
1623                 } else if (os_strcmp(buf, "eap_sim_aka_result_ind") == 0) {
1624                         bss->eap_sim_aka_result_ind = atoi(pos);
1625 #endif /* EAP_SIM */
1626 #ifdef EAP_TNC
1627                 } else if (os_strcmp(buf, "tnc") == 0) {
1628                         bss->tnc = atoi(pos);
1629 #endif /* EAP_TNC */
1630 #endif /* EAP_SERVER */
1631                 } else if (os_strcmp(buf, "eap_message") == 0) {
1632                         char *term;
1633                         bss->eap_req_id_text = os_strdup(pos);
1634                         if (bss->eap_req_id_text == NULL) {
1635                                 wpa_printf(MSG_ERROR, "Line %d: Failed to "
1636                                            "allocate memory for "
1637                                            "eap_req_id_text", line);
1638                                 errors++;
1639                                 continue;
1640                         }
1641                         bss->eap_req_id_text_len =
1642                                 os_strlen(bss->eap_req_id_text);
1643                         term = os_strstr(bss->eap_req_id_text, "\\0");
1644                         if (term) {
1645                                 *term++ = '\0';
1646                                 os_memmove(term, term + 1,
1647                                            bss->eap_req_id_text_len -
1648                                            (term - bss->eap_req_id_text) - 1);
1649                                 bss->eap_req_id_text_len--;
1650                         }
1651                 } else if (os_strcmp(buf, "wep_key_len_broadcast") == 0) {
1652                         bss->default_wep_key_len = atoi(pos);
1653                         if (bss->default_wep_key_len > 13) {
1654                                 wpa_printf(MSG_ERROR, "Line %d: invalid WEP "
1655                                            "key len %lu (= %lu bits)", line,
1656                                            (unsigned long)
1657                                            bss->default_wep_key_len,
1658                                            (unsigned long)
1659                                            bss->default_wep_key_len * 8);
1660                                 errors++;
1661                         }
1662                 } else if (os_strcmp(buf, "wep_key_len_unicast") == 0) {
1663                         bss->individual_wep_key_len = atoi(pos);
1664                         if (bss->individual_wep_key_len < 0 ||
1665                             bss->individual_wep_key_len > 13) {
1666                                 wpa_printf(MSG_ERROR, "Line %d: invalid WEP "
1667                                            "key len %d (= %d bits)", line,
1668                                            bss->individual_wep_key_len,
1669                                            bss->individual_wep_key_len * 8);
1670                                 errors++;
1671                         }
1672                 } else if (os_strcmp(buf, "wep_rekey_period") == 0) {
1673                         bss->wep_rekeying_period = atoi(pos);
1674                         if (bss->wep_rekeying_period < 0) {
1675                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
1676                                            "period %d",
1677                                            line, bss->wep_rekeying_period);
1678                                 errors++;
1679                         }
1680                 } else if (os_strcmp(buf, "eap_reauth_period") == 0) {
1681                         bss->eap_reauth_period = atoi(pos);
1682                         if (bss->eap_reauth_period < 0) {
1683                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
1684                                            "period %d",
1685                                            line, bss->eap_reauth_period);
1686                                 errors++;
1687                         }
1688                 } else if (os_strcmp(buf, "eapol_key_index_workaround") == 0) {
1689                         bss->eapol_key_index_workaround = atoi(pos);
1690 #ifdef CONFIG_IAPP
1691                 } else if (os_strcmp(buf, "iapp_interface") == 0) {
1692                         bss->ieee802_11f = 1;
1693                         os_strlcpy(bss->iapp_iface, pos,
1694                                    sizeof(bss->iapp_iface));
1695 #endif /* CONFIG_IAPP */
1696                 } else if (os_strcmp(buf, "own_ip_addr") == 0) {
1697                         if (hostapd_parse_ip_addr(pos, &bss->own_ip_addr)) {
1698                                 wpa_printf(MSG_ERROR, "Line %d: invalid IP "
1699                                            "address '%s'", line, pos);
1700                                 errors++;
1701                         }
1702                 } else if (os_strcmp(buf, "nas_identifier") == 0) {
1703                         bss->nas_identifier = os_strdup(pos);
1704 #ifndef CONFIG_NO_RADIUS
1705                 } else if (os_strcmp(buf, "auth_server_addr") == 0) {
1706                         if (hostapd_config_read_radius_addr(
1707                                     &bss->radius->auth_servers,
1708                                     &bss->radius->num_auth_servers, pos, 1812,
1709                                     &bss->radius->auth_server)) {
1710                                 wpa_printf(MSG_ERROR, "Line %d: invalid IP "
1711                                            "address '%s'", line, pos);
1712                                 errors++;
1713                         }
1714                 } else if (bss->radius->auth_server &&
1715                            os_strcmp(buf, "auth_server_port") == 0) {
1716                         bss->radius->auth_server->port = atoi(pos);
1717                 } else if (bss->radius->auth_server &&
1718                            os_strcmp(buf, "auth_server_shared_secret") == 0) {
1719                         int len = os_strlen(pos);
1720                         if (len == 0) {
1721                                 /* RFC 2865, Ch. 3 */
1722                                 wpa_printf(MSG_ERROR, "Line %d: empty shared "
1723                                            "secret is not allowed.", line);
1724                                 errors++;
1725                         }
1726                         bss->radius->auth_server->shared_secret =
1727                                 (u8 *) os_strdup(pos);
1728                         bss->radius->auth_server->shared_secret_len = len;
1729                 } else if (os_strcmp(buf, "acct_server_addr") == 0) {
1730                         if (hostapd_config_read_radius_addr(
1731                                     &bss->radius->acct_servers,
1732                                     &bss->radius->num_acct_servers, pos, 1813,
1733                                     &bss->radius->acct_server)) {
1734                                 wpa_printf(MSG_ERROR, "Line %d: invalid IP "
1735                                            "address '%s'", line, pos);
1736                                 errors++;
1737                         }
1738                 } else if (bss->radius->acct_server &&
1739                            os_strcmp(buf, "acct_server_port") == 0) {
1740                         bss->radius->acct_server->port = atoi(pos);
1741                 } else if (bss->radius->acct_server &&
1742                            os_strcmp(buf, "acct_server_shared_secret") == 0) {
1743                         int len = os_strlen(pos);
1744                         if (len == 0) {
1745                                 /* RFC 2865, Ch. 3 */
1746                                 wpa_printf(MSG_ERROR, "Line %d: empty shared "
1747                                            "secret is not allowed.", line);
1748                                 errors++;
1749                         }
1750                         bss->radius->acct_server->shared_secret =
1751                                 (u8 *) os_strdup(pos);
1752                         bss->radius->acct_server->shared_secret_len = len;
1753                 } else if (os_strcmp(buf, "radius_retry_primary_interval") ==
1754                            0) {
1755                         bss->radius->retry_primary_interval = atoi(pos);
1756                 } else if (os_strcmp(buf, "radius_acct_interim_interval") == 0)
1757                 {
1758                         bss->radius->acct_interim_interval = atoi(pos);
1759 #endif /* CONFIG_NO_RADIUS */
1760                 } else if (os_strcmp(buf, "auth_algs") == 0) {
1761                         bss->auth_algs = atoi(pos);
1762                         if (bss->auth_algs == 0) {
1763                                 wpa_printf(MSG_ERROR, "Line %d: no "
1764                                            "authentication algorithms allowed",
1765                                            line);
1766                                 errors++;
1767                         }
1768                 } else if (os_strcmp(buf, "max_num_sta") == 0) {
1769                         bss->max_num_sta = atoi(pos);
1770                         if (bss->max_num_sta < 0 ||
1771                             bss->max_num_sta > MAX_STA_COUNT) {
1772                                 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1773                                            "max_num_sta=%d; allowed range "
1774                                            "0..%d", line, bss->max_num_sta,
1775                                            MAX_STA_COUNT);
1776                                 errors++;
1777                         }
1778                 } else if (os_strcmp(buf, "wpa") == 0) {
1779                         bss->wpa = atoi(pos);
1780                 } else if (os_strcmp(buf, "wpa_group_rekey") == 0) {
1781                         bss->wpa_group_rekey = atoi(pos);
1782                 } else if (os_strcmp(buf, "wpa_strict_rekey") == 0) {
1783                         bss->wpa_strict_rekey = atoi(pos);
1784                 } else if (os_strcmp(buf, "wpa_gmk_rekey") == 0) {
1785                         bss->wpa_gmk_rekey = atoi(pos);
1786                 } else if (os_strcmp(buf, "wpa_ptk_rekey") == 0) {
1787                         bss->wpa_ptk_rekey = atoi(pos);
1788                 } else if (os_strcmp(buf, "wpa_passphrase") == 0) {
1789                         int len = os_strlen(pos);
1790                         if (len < 8 || len > 63) {
1791                                 wpa_printf(MSG_ERROR, "Line %d: invalid WPA "
1792                                            "passphrase length %d (expected "
1793                                            "8..63)", line, len);
1794                                 errors++;
1795                         } else {
1796                                 os_free(bss->ssid.wpa_passphrase);
1797                                 bss->ssid.wpa_passphrase = os_strdup(pos);
1798                         }
1799                 } else if (os_strcmp(buf, "wpa_psk") == 0) {
1800                         os_free(bss->ssid.wpa_psk);
1801                         bss->ssid.wpa_psk =
1802                                 os_zalloc(sizeof(struct hostapd_wpa_psk));
1803                         if (bss->ssid.wpa_psk == NULL)
1804                                 errors++;
1805                         else if (hexstr2bin(pos, bss->ssid.wpa_psk->psk,
1806                                             PMK_LEN) ||
1807                                  pos[PMK_LEN * 2] != '\0') {
1808                                 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK "
1809                                            "'%s'.", line, pos);
1810                                 errors++;
1811                         } else {
1812                                 bss->ssid.wpa_psk->group = 1;
1813                         }
1814                 } else if (os_strcmp(buf, "wpa_psk_file") == 0) {
1815                         os_free(bss->ssid.wpa_psk_file);
1816                         bss->ssid.wpa_psk_file = os_strdup(pos);
1817                         if (!bss->ssid.wpa_psk_file) {
1818                                 wpa_printf(MSG_ERROR, "Line %d: allocation "
1819                                            "failed", line);
1820                                 errors++;
1821                         }
1822                 } else if (os_strcmp(buf, "wpa_key_mgmt") == 0) {
1823                         bss->wpa_key_mgmt =
1824                                 hostapd_config_parse_key_mgmt(line, pos);
1825                         if (bss->wpa_key_mgmt == -1)
1826                                 errors++;
1827                 } else if (os_strcmp(buf, "wpa_pairwise") == 0) {
1828                         bss->wpa_pairwise =
1829                                 hostapd_config_parse_cipher(line, pos);
1830                         if (bss->wpa_pairwise == -1 ||
1831                             bss->wpa_pairwise == 0)
1832                                 errors++;
1833                         else if (bss->wpa_pairwise &
1834                                  (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
1835                                   WPA_CIPHER_WEP104)) {
1836                                 wpa_printf(MSG_ERROR, "Line %d: unsupported "
1837                                            "pairwise cipher suite '%s'",
1838                                            bss->wpa_pairwise, pos);
1839                                 errors++;
1840                         }
1841                 } else if (os_strcmp(buf, "rsn_pairwise") == 0) {
1842                         bss->rsn_pairwise =
1843                                 hostapd_config_parse_cipher(line, pos);
1844                         if (bss->rsn_pairwise == -1 ||
1845                             bss->rsn_pairwise == 0)
1846                                 errors++;
1847                         else if (bss->rsn_pairwise &
1848                                  (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
1849                                   WPA_CIPHER_WEP104)) {
1850                                 wpa_printf(MSG_ERROR, "Line %d: unsupported "
1851                                            "pairwise cipher suite '%s'",
1852                                            bss->rsn_pairwise, pos);
1853                                 errors++;
1854                         }
1855 #ifdef CONFIG_RSN_PREAUTH
1856                 } else if (os_strcmp(buf, "rsn_preauth") == 0) {
1857                         bss->rsn_preauth = atoi(pos);
1858                 } else if (os_strcmp(buf, "rsn_preauth_interfaces") == 0) {
1859                         bss->rsn_preauth_interfaces = os_strdup(pos);
1860 #endif /* CONFIG_RSN_PREAUTH */
1861 #ifdef CONFIG_PEERKEY
1862                 } else if (os_strcmp(buf, "peerkey") == 0) {
1863                         bss->peerkey = atoi(pos);
1864 #endif /* CONFIG_PEERKEY */
1865 #ifdef CONFIG_IEEE80211R
1866                 } else if (os_strcmp(buf, "mobility_domain") == 0) {
1867                         if (os_strlen(pos) != 2 * MOBILITY_DOMAIN_ID_LEN ||
1868                             hexstr2bin(pos, bss->mobility_domain,
1869                                        MOBILITY_DOMAIN_ID_LEN) != 0) {
1870                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid "
1871                                            "mobility_domain '%s'", line, pos);
1872                                 errors++;
1873                                 continue;
1874                         }
1875                 } else if (os_strcmp(buf, "r1_key_holder") == 0) {
1876                         if (os_strlen(pos) != 2 * FT_R1KH_ID_LEN ||
1877                             hexstr2bin(pos, bss->r1_key_holder,
1878                                        FT_R1KH_ID_LEN) != 0) {
1879                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid "
1880                                            "r1_key_holder '%s'", line, pos);
1881                                 errors++;
1882                                 continue;
1883                         }
1884                 } else if (os_strcmp(buf, "r0_key_lifetime") == 0) {
1885                         bss->r0_key_lifetime = atoi(pos);
1886                 } else if (os_strcmp(buf, "reassociation_deadline") == 0) {
1887                         bss->reassociation_deadline = atoi(pos);
1888                 } else if (os_strcmp(buf, "r0kh") == 0) {
1889                         if (add_r0kh(bss, pos) < 0) {
1890                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid "
1891                                            "r0kh '%s'", line, pos);
1892                                 errors++;
1893                                 continue;
1894                         }
1895                 } else if (os_strcmp(buf, "r1kh") == 0) {
1896                         if (add_r1kh(bss, pos) < 0) {
1897                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid "
1898                                            "r1kh '%s'", line, pos);
1899                                 errors++;
1900                                 continue;
1901                         }
1902                 } else if (os_strcmp(buf, "pmk_r1_push") == 0) {
1903                         bss->pmk_r1_push = atoi(pos);
1904 #endif /* CONFIG_IEEE80211R */
1905 #ifndef CONFIG_NO_CTRL_IFACE
1906                 } else if (os_strcmp(buf, "ctrl_interface") == 0) {
1907                         os_free(bss->ctrl_interface);
1908                         bss->ctrl_interface = os_strdup(pos);
1909                 } else if (os_strcmp(buf, "ctrl_interface_group") == 0) {
1910 #ifndef CONFIG_NATIVE_WINDOWS
1911                         struct group *grp;
1912                         char *endp;
1913                         const char *group = pos;
1914
1915                         grp = getgrnam(group);
1916                         if (grp) {
1917                                 bss->ctrl_interface_gid = grp->gr_gid;
1918                                 bss->ctrl_interface_gid_set = 1;
1919                                 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1920                                            " (from group name '%s')",
1921                                            bss->ctrl_interface_gid, group);
1922                                 continue;
1923                         }
1924
1925                         /* Group name not found - try to parse this as gid */
1926                         bss->ctrl_interface_gid = strtol(group, &endp, 10);
1927                         if (*group == '\0' || *endp != '\0') {
1928                                 wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
1929                                            "'%s'", line, group);
1930                                 errors++;
1931                                 continue;
1932                         }
1933                         bss->ctrl_interface_gid_set = 1;
1934                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1935                                    bss->ctrl_interface_gid);
1936 #endif /* CONFIG_NATIVE_WINDOWS */
1937 #endif /* CONFIG_NO_CTRL_IFACE */
1938 #ifdef RADIUS_SERVER
1939                 } else if (os_strcmp(buf, "radius_server_clients") == 0) {
1940                         os_free(bss->radius_server_clients);
1941                         bss->radius_server_clients = os_strdup(pos);
1942                 } else if (os_strcmp(buf, "radius_server_auth_port") == 0) {
1943                         bss->radius_server_auth_port = atoi(pos);
1944                 } else if (os_strcmp(buf, "radius_server_ipv6") == 0) {
1945                         bss->radius_server_ipv6 = atoi(pos);
1946 #endif /* RADIUS_SERVER */
1947                 } else if (os_strcmp(buf, "test_socket") == 0) {
1948                         os_free(bss->test_socket);
1949                         bss->test_socket = os_strdup(pos);
1950                 } else if (os_strcmp(buf, "use_pae_group_addr") == 0) {
1951                         bss->use_pae_group_addr = atoi(pos);
1952                 } else if (os_strcmp(buf, "hw_mode") == 0) {
1953                         if (os_strcmp(pos, "a") == 0)
1954                                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
1955                         else if (os_strcmp(pos, "b") == 0)
1956                                 conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
1957                         else if (os_strcmp(pos, "g") == 0)
1958                                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
1959                         else {
1960                                 wpa_printf(MSG_ERROR, "Line %d: unknown "
1961                                            "hw_mode '%s'", line, pos);
1962                                 errors++;
1963                         }
1964                 } else if (os_strcmp(buf, "channel") == 0) {
1965                         conf->channel = atoi(pos);
1966                 } else if (os_strcmp(buf, "beacon_int") == 0) {
1967                         int val = atoi(pos);
1968                         /* MIB defines range as 1..65535, but very small values
1969                          * cause problems with the current implementation.
1970                          * Since it is unlikely that this small numbers are
1971                          * useful in real life scenarios, do not allow beacon
1972                          * period to be set below 15 TU. */
1973                         if (val < 15 || val > 65535) {
1974                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
1975                                            "beacon_int %d (expected "
1976                                            "15..65535)", line, val);
1977                                 errors++;
1978                         } else
1979                                 conf->beacon_int = val;
1980                 } else if (os_strcmp(buf, "dtim_period") == 0) {
1981                         bss->dtim_period = atoi(pos);
1982                         if (bss->dtim_period < 1 || bss->dtim_period > 255) {
1983                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
1984                                            "dtim_period %d",
1985                                            line, bss->dtim_period);
1986                                 errors++;
1987                         }
1988                 } else if (os_strcmp(buf, "rts_threshold") == 0) {
1989                         conf->rts_threshold = atoi(pos);
1990                         if (conf->rts_threshold < 0 ||
1991                             conf->rts_threshold > 2347) {
1992                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
1993                                            "rts_threshold %d",
1994                                            line, conf->rts_threshold);
1995                                 errors++;
1996                         }
1997                 } else if (os_strcmp(buf, "fragm_threshold") == 0) {
1998                         conf->fragm_threshold = atoi(pos);
1999                         if (conf->fragm_threshold < 256 ||
2000                             conf->fragm_threshold > 2346) {
2001                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2002                                            "fragm_threshold %d",
2003                                            line, conf->fragm_threshold);
2004                                 errors++;
2005                         }
2006                 } else if (os_strcmp(buf, "send_probe_response") == 0) {
2007                         int val = atoi(pos);
2008                         if (val != 0 && val != 1) {
2009                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2010                                            "send_probe_response %d (expected "
2011                                            "0 or 1)", line, val);
2012                         } else
2013                                 conf->send_probe_response = val;
2014                 } else if (os_strcmp(buf, "supported_rates") == 0) {
2015                         if (hostapd_parse_rates(&conf->supported_rates, pos)) {
2016                                 wpa_printf(MSG_ERROR, "Line %d: invalid rate "
2017                                            "list", line);
2018                                 errors++;
2019                         }
2020                 } else if (os_strcmp(buf, "basic_rates") == 0) {
2021                         if (hostapd_parse_rates(&conf->basic_rates, pos)) {
2022                                 wpa_printf(MSG_ERROR, "Line %d: invalid rate "
2023                                            "list", line);
2024                                 errors++;
2025                         }
2026                 } else if (os_strcmp(buf, "preamble") == 0) {
2027                         if (atoi(pos))
2028                                 conf->preamble = SHORT_PREAMBLE;
2029                         else
2030                                 conf->preamble = LONG_PREAMBLE;
2031                 } else if (os_strcmp(buf, "ignore_broadcast_ssid") == 0) {
2032                         bss->ignore_broadcast_ssid = atoi(pos);
2033                 } else if (os_strcmp(buf, "bridge_packets") == 0) {
2034                         conf->bridge_packets = atoi(pos);
2035                 } else if (os_strcmp(buf, "wep_default_key") == 0) {
2036                         bss->ssid.wep.idx = atoi(pos);
2037                         if (bss->ssid.wep.idx > 3) {
2038                                 wpa_printf(MSG_ERROR, "Invalid "
2039                                            "wep_default_key index %d",
2040                                            bss->ssid.wep.idx);
2041                                 errors++;
2042                         }
2043                 } else if (os_strcmp(buf, "wep_key0") == 0 ||
2044                            os_strcmp(buf, "wep_key1") == 0 ||
2045                            os_strcmp(buf, "wep_key2") == 0 ||
2046                            os_strcmp(buf, "wep_key3") == 0) {
2047                         if (hostapd_config_read_wep(&bss->ssid.wep,
2048                                                     buf[7] - '0', pos)) {
2049                                 wpa_printf(MSG_ERROR, "Line %d: invalid WEP "
2050                                            "key '%s'", line, buf);
2051                                 errors++;
2052                         }
2053 #ifndef CONFIG_NO_VLAN
2054                 } else if (os_strcmp(buf, "dynamic_vlan") == 0) {
2055                         bss->ssid.dynamic_vlan = atoi(pos);
2056                 } else if (os_strcmp(buf, "vlan_file") == 0) {
2057                         if (hostapd_config_read_vlan_file(bss, pos)) {
2058                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
2059                                            "read VLAN file '%s'", line, pos);
2060                                 errors++;
2061                         }
2062 #ifdef CONFIG_FULL_DYNAMIC_VLAN
2063                 } else if (os_strcmp(buf, "vlan_tagged_interface") == 0) {
2064                         bss->ssid.vlan_tagged_interface = os_strdup(pos);
2065 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
2066 #endif /* CONFIG_NO_VLAN */
2067                 } else if (os_strcmp(buf, "passive_scan_interval") == 0) {
2068                         conf->passive_scan_interval = atoi(pos);
2069                 } else if (os_strcmp(buf, "passive_scan_listen") == 0) {
2070                         conf->passive_scan_listen = atoi(pos);
2071                 } else if (os_strcmp(buf, "passive_scan_mode") == 0) {
2072                         conf->passive_scan_mode = atoi(pos);
2073                 } else if (os_strcmp(buf, "ap_table_max_size") == 0) {
2074                         conf->ap_table_max_size = atoi(pos);
2075                 } else if (os_strcmp(buf, "ap_table_expiration_time") == 0) {
2076                         conf->ap_table_expiration_time = atoi(pos);
2077                 } else if (os_strncmp(buf, "tx_queue_", 9) == 0) {
2078                         if (hostapd_config_tx_queue(conf, buf, pos)) {
2079                                 wpa_printf(MSG_ERROR, "Line %d: invalid TX "
2080                                            "queue item", line);
2081                                 errors++;
2082                         }
2083                 } else if (os_strcmp(buf, "wme_enabled") == 0) {
2084                         bss->wme_enabled = atoi(pos);
2085                 } else if (os_strncmp(buf, "wme_ac_", 7) == 0) {
2086                         if (hostapd_config_wme_ac(conf, buf, pos)) {
2087                                 wpa_printf(MSG_ERROR, "Line %d: invalid wme "
2088                                            "ac item", line);
2089                                 errors++;
2090                         }
2091                 } else if (os_strcmp(buf, "bss") == 0) {
2092                         if (hostapd_config_bss(conf, pos)) {
2093                                 wpa_printf(MSG_ERROR, "Line %d: invalid bss "
2094                                            "item", line);
2095                                 errors++;
2096                         }
2097                 } else if (os_strcmp(buf, "bssid") == 0) {
2098                         if (bss == conf->bss &&
2099                             (!conf->driver || !conf->driver->init_bssid)) {
2100                                 wpa_printf(MSG_ERROR, "Line %d: bssid item "
2101                                            "not allowed for the default "
2102                                            "interface and this driver", line);
2103                                 errors++;
2104                         } else if (hwaddr_aton(pos, bss->bssid)) {
2105                                 wpa_printf(MSG_ERROR, "Line %d: invalid bssid "
2106                                            "item", line);
2107                                 errors++;
2108                         }
2109 #ifdef CONFIG_IEEE80211W
2110                 } else if (os_strcmp(buf, "ieee80211w") == 0) {
2111                         bss->ieee80211w = atoi(pos);
2112                 } else if (os_strcmp(buf, "assoc_sa_query_max_timeout") == 0) {
2113                         bss->assoc_sa_query_max_timeout = atoi(pos);
2114                         if (bss->assoc_sa_query_max_timeout == 0) {
2115                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2116                                            "assoc_sa_query_max_timeout", line);
2117                                 errors++;
2118                         }
2119                 } else if (os_strcmp(buf, "assoc_sa_query_retry_timeout") == 0)
2120                 {
2121                         bss->assoc_sa_query_retry_timeout = atoi(pos);
2122                         if (bss->assoc_sa_query_retry_timeout == 0) {
2123                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2124                                            "assoc_sa_query_retry_timeout",
2125                                            line);
2126                                 errors++;
2127                         }
2128 #endif /* CONFIG_IEEE80211W */
2129 #ifdef CONFIG_IEEE80211N
2130                 } else if (os_strcmp(buf, "ieee80211n") == 0) {
2131                         conf->ieee80211n = atoi(pos);
2132                 } else if (os_strcmp(buf, "ht_capab") == 0) {
2133                         if (hostapd_config_ht_capab(conf, pos) < 0) {
2134                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2135                                            "ht_capab", line);
2136                                 errors++;
2137                         }
2138 #endif /* CONFIG_IEEE80211N */
2139                 } else if (os_strcmp(buf, "max_listen_interval") == 0) {
2140                         bss->max_listen_interval = atoi(pos);
2141                 } else if (os_strcmp(buf, "okc") == 0) {
2142                         bss->okc = atoi(pos);
2143 #ifdef CONFIG_WPS
2144                 } else if (os_strcmp(buf, "wps_state") == 0) {
2145                         bss->wps_state = atoi(pos);
2146                         if (bss->wps_state < 0 || bss->wps_state > 2) {
2147                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2148                                            "wps_state", line);
2149                                 errors++;
2150                         }
2151                 } else if (os_strcmp(buf, "ap_setup_locked") == 0) {
2152                         bss->ap_setup_locked = atoi(pos);
2153                 } else if (os_strcmp(buf, "uuid") == 0) {
2154                         if (uuid_str2bin(pos, bss->uuid)) {
2155                                 wpa_printf(MSG_ERROR, "Line %d: invalid UUID",
2156                                            line);
2157                                 errors++;
2158                         }
2159                 } else if (os_strcmp(buf, "wps_pin_requests") == 0) {
2160                         os_free(bss->wps_pin_requests);
2161                         bss->wps_pin_requests = os_strdup(pos);
2162                 } else if (os_strcmp(buf, "device_name") == 0) {
2163                         if (os_strlen(pos) > 32) {
2164                                 wpa_printf(MSG_ERROR, "Line %d: Too long "
2165                                            "device_name", line);
2166                                 errors++;
2167                         }
2168                         os_free(bss->device_name);
2169                         bss->device_name = os_strdup(pos);
2170                 } else if (os_strcmp(buf, "manufacturer") == 0) {
2171                         if (os_strlen(pos) > 64) {
2172                                 wpa_printf(MSG_ERROR, "Line %d: Too long "
2173                                            "manufacturer", line);
2174                                 errors++;
2175                         }
2176                         os_free(bss->manufacturer);
2177                         bss->manufacturer = os_strdup(pos);
2178                 } else if (os_strcmp(buf, "model_name") == 0) {
2179                         if (os_strlen(pos) > 32) {
2180                                 wpa_printf(MSG_ERROR, "Line %d: Too long "
2181                                            "model_name", line);
2182                                 errors++;
2183                         }
2184                         os_free(bss->model_name);
2185                         bss->model_name = os_strdup(pos);
2186                 } else if (os_strcmp(buf, "model_number") == 0) {
2187                         if (os_strlen(pos) > 32) {
2188                                 wpa_printf(MSG_ERROR, "Line %d: Too long "
2189                                            "model_number", line);
2190                                 errors++;
2191                         }
2192                         os_free(bss->model_number);
2193                         bss->model_number = os_strdup(pos);
2194                 } else if (os_strcmp(buf, "serial_number") == 0) {
2195                         if (os_strlen(pos) > 32) {
2196                                 wpa_printf(MSG_ERROR, "Line %d: Too long "
2197                                            "serial_number", line);
2198                                 errors++;
2199                         }
2200                         os_free(bss->serial_number);
2201                         bss->serial_number = os_strdup(pos);
2202                 } else if (os_strcmp(buf, "device_type") == 0) {
2203                         os_free(bss->device_type);
2204                         bss->device_type = os_strdup(pos);
2205                 } else if (os_strcmp(buf, "config_methods") == 0) {
2206                         os_free(bss->config_methods);
2207                         bss->config_methods = os_strdup(pos);
2208                 } else if (os_strcmp(buf, "os_version") == 0) {
2209                         if (hexstr2bin(pos, bss->os_version, 4)) {
2210                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
2211                                            "os_version", line);
2212                                 errors++;
2213                         }
2214                 } else if (os_strcmp(buf, "ap_pin") == 0) {
2215                         os_free(bss->ap_pin);
2216                         bss->ap_pin = os_strdup(pos);
2217                 } else if (os_strcmp(buf, "skip_cred_build") == 0) {
2218                         bss->skip_cred_build = atoi(pos);
2219                 } else if (os_strcmp(buf, "extra_cred") == 0) {
2220                         os_free(bss->extra_cred);
2221                         bss->extra_cred =
2222                                 (u8 *) os_readfile(pos, &bss->extra_cred_len);
2223                         if (bss->extra_cred == NULL) {
2224                                 wpa_printf(MSG_ERROR, "Line %d: could not "
2225                                            "read Credentials from '%s'",
2226                                            line, pos);
2227                                 errors++;
2228                         }
2229                 } else if (os_strcmp(buf, "wps_cred_processing") == 0) {
2230                         bss->wps_cred_processing = atoi(pos);
2231                 } else if (os_strcmp(buf, "ap_settings") == 0) {
2232                         os_free(bss->ap_settings);
2233                         bss->ap_settings =
2234                                 (u8 *) os_readfile(pos, &bss->ap_settings_len);
2235                         if (bss->ap_settings == NULL) {
2236                                 wpa_printf(MSG_ERROR, "Line %d: could not "
2237                                            "read AP Settings from '%s'",
2238                                            line, pos);
2239                                 errors++;
2240                         }
2241                 } else if (os_strcmp(buf, "upnp_iface") == 0) {
2242                         bss->upnp_iface = os_strdup(pos);
2243                 } else if (os_strcmp(buf, "friendly_name") == 0) {
2244                         os_free(bss->friendly_name);
2245                         bss->friendly_name = os_strdup(pos);
2246                 } else if (os_strcmp(buf, "manufacturer_url") == 0) {
2247                         os_free(bss->manufacturer_url);
2248                         bss->manufacturer_url = os_strdup(pos);
2249                 } else if (os_strcmp(buf, "model_description") == 0) {
2250                         os_free(bss->model_description);
2251                         bss->model_description = os_strdup(pos);
2252                 } else if (os_strcmp(buf, "model_url") == 0) {
2253                         os_free(bss->model_url);
2254                         bss->model_url = os_strdup(pos);
2255                 } else if (os_strcmp(buf, "upc") == 0) {
2256                         os_free(bss->upc);
2257                         bss->upc = os_strdup(pos);
2258 #endif /* CONFIG_WPS */
2259                 } else {
2260                         wpa_printf(MSG_ERROR, "Line %d: unknown configuration "
2261                                    "item '%s'", line, buf);
2262                         errors++;
2263                 }
2264         }
2265
2266         fclose(f);
2267
2268         if (bss->individual_wep_key_len == 0) {
2269                 /* individual keys are not use; can use key idx0 for broadcast
2270                  * keys */
2271                 bss->broadcast_key_idx_min = 0;
2272         }
2273
2274         /* Select group cipher based on the enabled pairwise cipher suites */
2275         pairwise = 0;
2276         if (bss->wpa & 1)
2277                 pairwise |= bss->wpa_pairwise;
2278         if (bss->wpa & 2) {
2279                 if (bss->rsn_pairwise == 0)
2280                         bss->rsn_pairwise = bss->wpa_pairwise;
2281                 pairwise |= bss->rsn_pairwise;
2282         }
2283         if (pairwise & WPA_CIPHER_TKIP)
2284                 bss->wpa_group = WPA_CIPHER_TKIP;
2285         else
2286                 bss->wpa_group = WPA_CIPHER_CCMP;
2287
2288         for (i = 0; i < conf->num_bss; i++) {
2289                 bss = &conf->bss[i];
2290
2291                 bss->radius->auth_server = bss->radius->auth_servers;
2292                 bss->radius->acct_server = bss->radius->acct_servers;
2293
2294                 if (bss->wpa && bss->ieee802_1x) {
2295                         bss->ssid.security_policy = SECURITY_WPA;
2296                 } else if (bss->wpa) {
2297                         bss->ssid.security_policy = SECURITY_WPA_PSK;
2298                 } else if (bss->ieee802_1x) {
2299                         bss->ssid.security_policy = SECURITY_IEEE_802_1X;
2300                         bss->ssid.wep.default_len = bss->default_wep_key_len;
2301                 } else if (bss->ssid.wep.keys_set)
2302                         bss->ssid.security_policy = SECURITY_STATIC_WEP;
2303                 else
2304                         bss->ssid.security_policy = SECURITY_PLAINTEXT;
2305         }
2306
2307         if (hostapd_config_check(conf))
2308                 errors++;
2309
2310         if (errors) {
2311                 wpa_printf(MSG_ERROR, "%d errors found in configuration file "
2312                            "'%s'", errors, fname);
2313                 hostapd_config_free(conf);
2314                 conf = NULL;
2315         }
2316
2317         return conf;
2318 }
2319
2320
2321 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
2322 {
2323         int i;
2324
2325         if (a->idx != b->idx || a->default_len != b->default_len)
2326                 return 1;
2327         for (i = 0; i < NUM_WEP_KEYS; i++)
2328                 if (a->len[i] != b->len[i] ||
2329                     os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
2330                         return 1;
2331         return 0;
2332 }
2333
2334
2335 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
2336                                        int num_servers)
2337 {
2338         int i;
2339
2340         for (i = 0; i < num_servers; i++) {
2341                 os_free(servers[i].shared_secret);
2342         }
2343         os_free(servers);
2344 }
2345
2346
2347 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
2348 {
2349         os_free(user->identity);
2350         os_free(user->password);
2351         os_free(user);
2352 }
2353
2354
2355 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
2356 {
2357         int i;
2358         for (i = 0; i < NUM_WEP_KEYS; i++) {
2359                 os_free(keys->key[i]);
2360                 keys->key[i] = NULL;
2361         }
2362 }
2363
2364
2365 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
2366 {
2367         struct hostapd_wpa_psk *psk, *prev;
2368         struct hostapd_eap_user *user, *prev_user;
2369
2370         if (conf == NULL)
2371                 return;
2372
2373         psk = conf->ssid.wpa_psk;
2374         while (psk) {
2375                 prev = psk;
2376                 psk = psk->next;
2377                 os_free(prev);
2378         }
2379
2380         os_free(conf->ssid.wpa_passphrase);
2381         os_free(conf->ssid.wpa_psk_file);
2382 #ifdef CONFIG_FULL_DYNAMIC_VLAN
2383         os_free(conf->ssid.vlan_tagged_interface);
2384 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
2385
2386         user = conf->eap_user;
2387         while (user) {
2388                 prev_user = user;
2389                 user = user->next;
2390                 hostapd_config_free_eap_user(prev_user);
2391         }
2392
2393         os_free(conf->dump_log_name);
2394         os_free(conf->eap_req_id_text);
2395         os_free(conf->accept_mac);
2396         os_free(conf->deny_mac);
2397         os_free(conf->nas_identifier);
2398         hostapd_config_free_radius(conf->radius->auth_servers,
2399                                    conf->radius->num_auth_servers);
2400         hostapd_config_free_radius(conf->radius->acct_servers,
2401                                    conf->radius->num_acct_servers);
2402         os_free(conf->rsn_preauth_interfaces);
2403         os_free(conf->ctrl_interface);
2404         os_free(conf->ca_cert);
2405         os_free(conf->server_cert);
2406         os_free(conf->private_key);
2407         os_free(conf->private_key_passwd);
2408         os_free(conf->dh_file);
2409         os_free(conf->pac_opaque_encr_key);
2410         os_free(conf->eap_fast_a_id);
2411         os_free(conf->eap_fast_a_id_info);
2412         os_free(conf->eap_sim_db);
2413         os_free(conf->radius_server_clients);
2414         os_free(conf->test_socket);
2415         os_free(conf->radius);
2416         hostapd_config_free_vlan(conf);
2417         if (conf->ssid.dyn_vlan_keys) {
2418                 struct hostapd_ssid *ssid = &conf->ssid;
2419                 size_t i;
2420                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
2421                         if (ssid->dyn_vlan_keys[i] == NULL)
2422                                 continue;
2423                         hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
2424                         os_free(ssid->dyn_vlan_keys[i]);
2425                 }
2426                 os_free(ssid->dyn_vlan_keys);
2427                 ssid->dyn_vlan_keys = NULL;
2428         }
2429
2430 #ifdef CONFIG_IEEE80211R
2431         {
2432                 struct ft_remote_r0kh *r0kh, *r0kh_prev;
2433                 struct ft_remote_r1kh *r1kh, *r1kh_prev;
2434
2435                 r0kh = conf->r0kh_list;
2436                 conf->r0kh_list = NULL;
2437                 while (r0kh) {
2438                         r0kh_prev = r0kh;
2439                         r0kh = r0kh->next;
2440                         os_free(r0kh_prev);
2441                 }
2442
2443                 r1kh = conf->r1kh_list;
2444                 conf->r1kh_list = NULL;
2445                 while (r1kh) {
2446                         r1kh_prev = r1kh;
2447                         r1kh = r1kh->next;
2448                         os_free(r1kh_prev);
2449                 }
2450         }
2451 #endif /* CONFIG_IEEE80211R */
2452
2453 #ifdef CONFIG_WPS
2454         os_free(conf->wps_pin_requests);
2455         os_free(conf->device_name);
2456         os_free(conf->manufacturer);
2457         os_free(conf->model_name);
2458         os_free(conf->model_number);
2459         os_free(conf->serial_number);
2460         os_free(conf->device_type);
2461         os_free(conf->config_methods);
2462         os_free(conf->ap_pin);
2463         os_free(conf->extra_cred);
2464         os_free(conf->ap_settings);
2465         os_free(conf->upnp_iface);
2466         os_free(conf->friendly_name);
2467         os_free(conf->manufacturer_url);
2468         os_free(conf->model_description);
2469         os_free(conf->model_url);
2470         os_free(conf->upc);
2471 #endif /* CONFIG_WPS */
2472 }
2473
2474
2475 /**
2476  * hostapd_config_free - Free hostapd configuration
2477  * @conf: Configuration data from hostapd_config_read().
2478  */
2479 void hostapd_config_free(struct hostapd_config *conf)
2480 {
2481         size_t i;
2482
2483         if (conf == NULL)
2484                 return;
2485
2486         for (i = 0; i < conf->num_bss; i++)
2487                 hostapd_config_free_bss(&conf->bss[i]);
2488         os_free(conf->bss);
2489
2490         os_free(conf);
2491 }
2492
2493
2494 /**
2495  * hostapd_maclist_found - Find a MAC address from a list
2496  * @list: MAC address list
2497  * @num_entries: Number of addresses in the list
2498  * @addr: Address to search for
2499  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
2500  * Returns: 1 if address is in the list or 0 if not.
2501  *
2502  * Perform a binary search for given MAC address from a pre-sorted list.
2503  */
2504 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
2505                           const u8 *addr, int *vlan_id)
2506 {
2507         int start, end, middle, res;
2508
2509         start = 0;
2510         end = num_entries - 1;
2511
2512         while (start <= end) {
2513                 middle = (start + end) / 2;
2514                 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
2515                 if (res == 0) {
2516                         if (vlan_id)
2517                                 *vlan_id = list[middle].vlan_id;
2518                         return 1;
2519                 }
2520                 if (res < 0)
2521                         start = middle + 1;
2522                 else
2523                         end = middle - 1;
2524         }
2525
2526         return 0;
2527 }
2528
2529
2530 int hostapd_rate_found(int *list, int rate)
2531 {
2532         int i;
2533
2534         if (list == NULL)
2535                 return 0;
2536
2537         for (i = 0; list[i] >= 0; i++)
2538                 if (list[i] == rate)
2539                         return 1;
2540
2541         return 0;
2542 }
2543
2544
2545 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
2546 {
2547         struct hostapd_vlan *v = vlan;
2548         while (v) {
2549                 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
2550                         return v->ifname;
2551                 v = v->next;
2552         }
2553         return NULL;
2554 }
2555
2556
2557 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
2558                            const u8 *addr, const u8 *prev_psk)
2559 {
2560         struct hostapd_wpa_psk *psk;
2561         int next_ok = prev_psk == NULL;
2562
2563         for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
2564                 if (next_ok &&
2565                     (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
2566                         return psk->psk;
2567
2568                 if (psk->psk == prev_psk)
2569                         next_ok = 1;
2570         }
2571
2572         return NULL;
2573 }
2574
2575
2576 const struct hostapd_eap_user *
2577 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
2578                      size_t identity_len, int phase2)
2579 {
2580         struct hostapd_eap_user *user = conf->eap_user;
2581
2582 #ifdef CONFIG_WPS
2583         if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
2584             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
2585                 static struct hostapd_eap_user wsc_enrollee;
2586                 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
2587                 wsc_enrollee.methods[0].method = eap_server_get_type(
2588                         "WSC", &wsc_enrollee.methods[0].vendor);
2589                 return &wsc_enrollee;
2590         }
2591
2592         if (conf->wps_state && conf->ap_pin &&
2593             identity_len == WSC_ID_REGISTRAR_LEN &&
2594             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
2595                 static struct hostapd_eap_user wsc_registrar;
2596                 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
2597                 wsc_registrar.methods[0].method = eap_server_get_type(
2598                         "WSC", &wsc_registrar.methods[0].vendor);
2599                 wsc_registrar.password = (u8 *) conf->ap_pin;
2600                 wsc_registrar.password_len = os_strlen(conf->ap_pin);
2601                 return &wsc_registrar;
2602         }
2603 #endif /* CONFIG_WPS */
2604
2605         while (user) {
2606                 if (!phase2 && user->identity == NULL) {
2607                         /* Wildcard match */
2608                         break;
2609                 }
2610
2611                 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
2612                     identity_len >= user->identity_len &&
2613                     os_memcmp(user->identity, identity, user->identity_len) ==
2614                     0) {
2615                         /* Wildcard prefix match */
2616                         break;
2617                 }
2618
2619                 if (user->phase2 == !!phase2 &&
2620                     user->identity_len == identity_len &&
2621                     os_memcmp(user->identity, identity, identity_len) == 0)
2622                         break;
2623                 user = user->next;
2624         }
2625
2626         return user;
2627 }