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