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