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