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