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