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