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