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