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