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