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