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