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