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