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