Extend offloaded ACS QCA vendor command to support VHT
[mech_eap.git] / hostapd / config_file.c
1 /*
2  * hostapd / Configuration file parser
3  * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
11 #include <grp.h>
12 #endif /* CONFIG_NATIVE_WINDOWS */
13
14 #include "utils/common.h"
15 #include "utils/uuid.h"
16 #include "common/ieee802_11_defs.h"
17 #include "drivers/driver.h"
18 #include "eap_server/eap.h"
19 #include "radius/radius_client.h"
20 #include "ap/wpa_auth.h"
21 #include "ap/ap_config.h"
22 #include "config_file.h"
23
24
25 #ifndef CONFIG_NO_RADIUS
26 #ifdef EAP_SERVER
27 static struct hostapd_radius_attr *
28 hostapd_parse_radius_attr(const char *value);
29 #endif /* EAP_SERVER */
30 #endif /* CONFIG_NO_RADIUS */
31
32
33 #ifndef CONFIG_NO_VLAN
34 static int hostapd_config_read_vlan_file(struct hostapd_bss_config *bss,
35                                          const char *fname)
36 {
37         FILE *f;
38         char buf[128], *pos, *pos2;
39         int line = 0, vlan_id;
40         struct hostapd_vlan *vlan;
41
42         f = fopen(fname, "r");
43         if (!f) {
44                 wpa_printf(MSG_ERROR, "VLAN file '%s' not readable.", fname);
45                 return -1;
46         }
47
48         while (fgets(buf, sizeof(buf), f)) {
49                 line++;
50
51                 if (buf[0] == '#')
52                         continue;
53                 pos = buf;
54                 while (*pos != '\0') {
55                         if (*pos == '\n') {
56                                 *pos = '\0';
57                                 break;
58                         }
59                         pos++;
60                 }
61                 if (buf[0] == '\0')
62                         continue;
63
64                 if (buf[0] == '*') {
65                         vlan_id = VLAN_ID_WILDCARD;
66                         pos = buf + 1;
67                 } else {
68                         vlan_id = strtol(buf, &pos, 10);
69                         if (buf == pos || vlan_id < 1 ||
70                             vlan_id > MAX_VLAN_ID) {
71                                 wpa_printf(MSG_ERROR, "Invalid VLAN ID at "
72                                            "line %d in '%s'", line, fname);
73                                 fclose(f);
74                                 return -1;
75                         }
76                 }
77
78                 while (*pos == ' ' || *pos == '\t')
79                         pos++;
80                 pos2 = pos;
81                 while (*pos2 != ' ' && *pos2 != '\t' && *pos2 != '\0')
82                         pos2++;
83                 *pos2 = '\0';
84                 if (*pos == '\0' || os_strlen(pos) > IFNAMSIZ) {
85                         wpa_printf(MSG_ERROR, "Invalid VLAN ifname at line %d "
86                                    "in '%s'", line, fname);
87                         fclose(f);
88                         return -1;
89                 }
90
91                 vlan = os_zalloc(sizeof(*vlan));
92                 if (vlan == NULL) {
93                         wpa_printf(MSG_ERROR, "Out of memory while reading "
94                                    "VLAN interfaces from '%s'", fname);
95                         fclose(f);
96                         return -1;
97                 }
98
99                 vlan->vlan_id = vlan_id;
100                 os_strlcpy(vlan->ifname, pos, sizeof(vlan->ifname));
101                 vlan->next = bss->vlan;
102                 bss->vlan = vlan;
103         }
104
105         fclose(f);
106
107         return 0;
108 }
109 #endif /* CONFIG_NO_VLAN */
110
111
112 static int hostapd_acl_comp(const void *a, const void *b)
113 {
114         const struct mac_acl_entry *aa = a;
115         const struct mac_acl_entry *bb = b;
116         return os_memcmp(aa->addr, bb->addr, sizeof(macaddr));
117 }
118
119
120 static int hostapd_config_read_maclist(const char *fname,
121                                        struct mac_acl_entry **acl, int *num)
122 {
123         FILE *f;
124         char buf[128], *pos;
125         int line = 0;
126         u8 addr[ETH_ALEN];
127         struct mac_acl_entry *newacl;
128         int vlan_id;
129
130         if (!fname)
131                 return 0;
132
133         f = fopen(fname, "r");
134         if (!f) {
135                 wpa_printf(MSG_ERROR, "MAC list file '%s' not found.", fname);
136                 return -1;
137         }
138
139         while (fgets(buf, sizeof(buf), f)) {
140                 int i, rem = 0;
141
142                 line++;
143
144                 if (buf[0] == '#')
145                         continue;
146                 pos = buf;
147                 while (*pos != '\0') {
148                         if (*pos == '\n') {
149                                 *pos = '\0';
150                                 break;
151                         }
152                         pos++;
153                 }
154                 if (buf[0] == '\0')
155                         continue;
156                 pos = buf;
157                 if (buf[0] == '-') {
158                         rem = 1;
159                         pos++;
160                 }
161
162                 if (hwaddr_aton(pos, addr)) {
163                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' at "
164                                    "line %d in '%s'", pos, line, fname);
165                         fclose(f);
166                         return -1;
167                 }
168
169                 if (rem) {
170                         i = 0;
171                         while (i < *num) {
172                                 if (os_memcmp((*acl)[i].addr, addr, ETH_ALEN) ==
173                                     0) {
174                                         os_remove_in_array(*acl, *num,
175                                                            sizeof(**acl), i);
176                                         (*num)--;
177                                 } else
178                                         i++;
179                         }
180                         continue;
181                 }
182                 vlan_id = 0;
183                 pos = buf;
184                 while (*pos != '\0' && *pos != ' ' && *pos != '\t')
185                         pos++;
186                 while (*pos == ' ' || *pos == '\t')
187                         pos++;
188                 if (*pos != '\0')
189                         vlan_id = atoi(pos);
190
191                 newacl = os_realloc_array(*acl, *num + 1, sizeof(**acl));
192                 if (newacl == NULL) {
193                         wpa_printf(MSG_ERROR, "MAC list reallocation failed");
194                         fclose(f);
195                         return -1;
196                 }
197
198                 *acl = newacl;
199                 os_memcpy((*acl)[*num].addr, addr, ETH_ALEN);
200                 (*acl)[*num].vlan_id = vlan_id;
201                 (*num)++;
202         }
203
204         fclose(f);
205
206         qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
207
208         return 0;
209 }
210
211
212 #ifdef EAP_SERVER
213 static int hostapd_config_read_eap_user(const char *fname,
214                                         struct hostapd_bss_config *conf)
215 {
216         FILE *f;
217         char buf[512], *pos, *start, *pos2;
218         int line = 0, ret = 0, num_methods;
219         struct hostapd_eap_user *user = NULL, *tail = NULL, *new_user = NULL;
220
221         if (!fname)
222                 return 0;
223
224         if (os_strncmp(fname, "sqlite:", 7) == 0) {
225                 os_free(conf->eap_user_sqlite);
226                 conf->eap_user_sqlite = os_strdup(fname + 7);
227                 return 0;
228         }
229
230         f = fopen(fname, "r");
231         if (!f) {
232                 wpa_printf(MSG_ERROR, "EAP user file '%s' not found.", fname);
233                 return -1;
234         }
235
236         /* Lines: "user" METHOD,METHOD2 "password" (password optional) */
237         while (fgets(buf, sizeof(buf), f)) {
238                 line++;
239
240                 if (buf[0] == '#')
241                         continue;
242                 pos = buf;
243                 while (*pos != '\0') {
244                         if (*pos == '\n') {
245                                 *pos = '\0';
246                                 break;
247                         }
248                         pos++;
249                 }
250                 if (buf[0] == '\0')
251                         continue;
252
253 #ifndef CONFIG_NO_RADIUS
254                 if (user && os_strncmp(buf, "radius_accept_attr=", 19) == 0) {
255                         struct hostapd_radius_attr *attr, *a;
256                         attr = hostapd_parse_radius_attr(buf + 19);
257                         if (attr == NULL) {
258                                 wpa_printf(MSG_ERROR, "Invalid radius_auth_req_attr: %s",
259                                            buf + 19);
260                                 user = NULL; /* already in the BSS list */
261                                 goto failed;
262                         }
263                         if (user->accept_attr == NULL) {
264                                 user->accept_attr = attr;
265                         } else {
266                                 a = user->accept_attr;
267                                 while (a->next)
268                                         a = a->next;
269                                 a->next = attr;
270                         }
271                         continue;
272                 }
273 #endif /* CONFIG_NO_RADIUS */
274
275                 user = NULL;
276
277                 if (buf[0] != '"' && buf[0] != '*') {
278                         wpa_printf(MSG_ERROR, "Invalid EAP identity (no \" in "
279                                    "start) on line %d in '%s'", line, fname);
280                         goto failed;
281                 }
282
283                 user = os_zalloc(sizeof(*user));
284                 if (user == NULL) {
285                         wpa_printf(MSG_ERROR, "EAP user allocation failed");
286                         goto failed;
287                 }
288                 user->force_version = -1;
289
290                 if (buf[0] == '*') {
291                         pos = buf;
292                 } else {
293                         pos = buf + 1;
294                         start = pos;
295                         while (*pos != '"' && *pos != '\0')
296                                 pos++;
297                         if (*pos == '\0') {
298                                 wpa_printf(MSG_ERROR, "Invalid EAP identity "
299                                            "(no \" in end) on line %d in '%s'",
300                                            line, fname);
301                                 goto failed;
302                         }
303
304                         user->identity = os_malloc(pos - start);
305                         if (user->identity == NULL) {
306                                 wpa_printf(MSG_ERROR, "Failed to allocate "
307                                            "memory for EAP identity");
308                                 goto failed;
309                         }
310                         os_memcpy(user->identity, start, pos - start);
311                         user->identity_len = pos - start;
312
313                         if (pos[0] == '"' && pos[1] == '*') {
314                                 user->wildcard_prefix = 1;
315                                 pos++;
316                         }
317                 }
318                 pos++;
319                 while (*pos == ' ' || *pos == '\t')
320                         pos++;
321
322                 if (*pos == '\0') {
323                         wpa_printf(MSG_ERROR, "No EAP method on line %d in "
324                                    "'%s'", line, fname);
325                         goto failed;
326                 }
327
328                 start = pos;
329                 while (*pos != ' ' && *pos != '\t' && *pos != '\0')
330                         pos++;
331                 if (*pos == '\0') {
332                         pos = NULL;
333                 } else {
334                         *pos = '\0';
335                         pos++;
336                 }
337                 num_methods = 0;
338                 while (*start) {
339                         char *pos3 = os_strchr(start, ',');
340                         if (pos3) {
341                                 *pos3++ = '\0';
342                         }
343                         user->methods[num_methods].method =
344                                 eap_server_get_type(
345                                         start,
346                                         &user->methods[num_methods].vendor);
347                         if (user->methods[num_methods].vendor ==
348                             EAP_VENDOR_IETF &&
349                             user->methods[num_methods].method == EAP_TYPE_NONE)
350                         {
351                                 if (os_strcmp(start, "TTLS-PAP") == 0) {
352                                         user->ttls_auth |= EAP_TTLS_AUTH_PAP;
353                                         goto skip_eap;
354                                 }
355                                 if (os_strcmp(start, "TTLS-CHAP") == 0) {
356                                         user->ttls_auth |= EAP_TTLS_AUTH_CHAP;
357                                         goto skip_eap;
358                                 }
359                                 if (os_strcmp(start, "TTLS-MSCHAP") == 0) {
360                                         user->ttls_auth |=
361                                                 EAP_TTLS_AUTH_MSCHAP;
362                                         goto skip_eap;
363                                 }
364                                 if (os_strcmp(start, "TTLS-MSCHAPV2") == 0) {
365                                         user->ttls_auth |=
366                                                 EAP_TTLS_AUTH_MSCHAPV2;
367                                         goto skip_eap;
368                                 }
369                                 if (os_strcmp(start, "MACACL") == 0) {
370                                         user->macacl = 1;
371                                         goto skip_eap;
372                                 }
373                                 wpa_printf(MSG_ERROR, "Unsupported EAP type "
374                                            "'%s' on line %d in '%s'",
375                                            start, line, fname);
376                                 goto failed;
377                         }
378
379                         num_methods++;
380                         if (num_methods >= EAP_MAX_METHODS)
381                                 break;
382                 skip_eap:
383                         if (pos3 == NULL)
384                                 break;
385                         start = pos3;
386                 }
387                 if (num_methods == 0 && user->ttls_auth == 0 && !user->macacl) {
388                         wpa_printf(MSG_ERROR, "No EAP types configured on "
389                                    "line %d in '%s'", line, fname);
390                         goto failed;
391                 }
392
393                 if (pos == NULL)
394                         goto done;
395
396                 while (*pos == ' ' || *pos == '\t')
397                         pos++;
398                 if (*pos == '\0')
399                         goto done;
400
401                 if (os_strncmp(pos, "[ver=0]", 7) == 0) {
402                         user->force_version = 0;
403                         goto done;
404                 }
405
406                 if (os_strncmp(pos, "[ver=1]", 7) == 0) {
407                         user->force_version = 1;
408                         goto done;
409                 }
410
411                 if (os_strncmp(pos, "[2]", 3) == 0) {
412                         user->phase2 = 1;
413                         goto done;
414                 }
415
416                 if (*pos == '"') {
417                         pos++;
418                         start = pos;
419                         while (*pos != '"' && *pos != '\0')
420                                 pos++;
421                         if (*pos == '\0') {
422                                 wpa_printf(MSG_ERROR, "Invalid EAP password "
423                                            "(no \" in end) on line %d in '%s'",
424                                            line, fname);
425                                 goto failed;
426                         }
427
428                         user->password = os_malloc(pos - start);
429                         if (user->password == NULL) {
430                                 wpa_printf(MSG_ERROR, "Failed to allocate "
431                                            "memory for EAP password");
432                                 goto failed;
433                         }
434                         os_memcpy(user->password, start, pos - start);
435                         user->password_len = pos - start;
436
437                         pos++;
438                 } else if (os_strncmp(pos, "hash:", 5) == 0) {
439                         pos += 5;
440                         pos2 = pos;
441                         while (*pos2 != '\0' && *pos2 != ' ' &&
442                                *pos2 != '\t' && *pos2 != '#')
443                                 pos2++;
444                         if (pos2 - pos != 32) {
445                                 wpa_printf(MSG_ERROR, "Invalid password hash "
446                                            "on line %d in '%s'", line, fname);
447                                 goto failed;
448                         }
449                         user->password = os_malloc(16);
450                         if (user->password == NULL) {
451                                 wpa_printf(MSG_ERROR, "Failed to allocate "
452                                            "memory for EAP password hash");
453                                 goto failed;
454                         }
455                         if (hexstr2bin(pos, user->password, 16) < 0) {
456                                 wpa_printf(MSG_ERROR, "Invalid hash password "
457                                            "on line %d in '%s'", line, fname);
458                                 goto failed;
459                         }
460                         user->password_len = 16;
461                         user->password_hash = 1;
462                         pos = pos2;
463                 } else {
464                         pos2 = pos;
465                         while (*pos2 != '\0' && *pos2 != ' ' &&
466                                *pos2 != '\t' && *pos2 != '#')
467                                 pos2++;
468                         if ((pos2 - pos) & 1) {
469                                 wpa_printf(MSG_ERROR, "Invalid hex password "
470                                            "on line %d in '%s'", line, fname);
471                                 goto failed;
472                         }
473                         user->password = os_malloc((pos2 - pos) / 2);
474                         if (user->password == NULL) {
475                                 wpa_printf(MSG_ERROR, "Failed to allocate "
476                                            "memory for EAP password");
477                                 goto failed;
478                         }
479                         if (hexstr2bin(pos, user->password,
480                                        (pos2 - pos) / 2) < 0) {
481                                 wpa_printf(MSG_ERROR, "Invalid hex password "
482                                            "on line %d in '%s'", line, fname);
483                                 goto failed;
484                         }
485                         user->password_len = (pos2 - pos) / 2;
486                         pos = pos2;
487                 }
488
489                 while (*pos == ' ' || *pos == '\t')
490                         pos++;
491                 if (os_strncmp(pos, "[2]", 3) == 0) {
492                         user->phase2 = 1;
493                 }
494
495         done:
496                 if (tail == NULL) {
497                         tail = new_user = user;
498                 } else {
499                         tail->next = user;
500                         tail = user;
501                 }
502                 continue;
503
504         failed:
505                 if (user)
506                         hostapd_config_free_eap_user(user);
507                 ret = -1;
508                 break;
509         }
510
511         fclose(f);
512
513         if (ret == 0) {
514                 user = conf->eap_user;
515                 while (user) {
516                         struct hostapd_eap_user *prev;
517
518                         prev = user;
519                         user = user->next;
520                         hostapd_config_free_eap_user(prev);
521                 }
522                 conf->eap_user = new_user;
523         }
524
525         return ret;
526 }
527 #endif /* EAP_SERVER */
528
529
530 #ifndef CONFIG_NO_RADIUS
531 static int
532 hostapd_config_read_radius_addr(struct hostapd_radius_server **server,
533                                 int *num_server, const char *val, int def_port,
534                                 struct hostapd_radius_server **curr_serv)
535 {
536         struct hostapd_radius_server *nserv;
537         int ret;
538         static int server_index = 1;
539
540         nserv = os_realloc_array(*server, *num_server + 1, sizeof(*nserv));
541         if (nserv == NULL)
542                 return -1;
543
544         *server = nserv;
545         nserv = &nserv[*num_server];
546         (*num_server)++;
547         (*curr_serv) = nserv;
548
549         os_memset(nserv, 0, sizeof(*nserv));
550         nserv->port = def_port;
551         ret = hostapd_parse_ip_addr(val, &nserv->addr);
552         nserv->index = server_index++;
553
554         return ret;
555 }
556
557
558 static struct hostapd_radius_attr *
559 hostapd_parse_radius_attr(const char *value)
560 {
561         const char *pos;
562         char syntax;
563         struct hostapd_radius_attr *attr;
564         size_t len;
565
566         attr = os_zalloc(sizeof(*attr));
567         if (attr == NULL)
568                 return NULL;
569
570         attr->type = atoi(value);
571
572         pos = os_strchr(value, ':');
573         if (pos == NULL) {
574                 attr->val = wpabuf_alloc(1);
575                 if (attr->val == NULL) {
576                         os_free(attr);
577                         return NULL;
578                 }
579                 wpabuf_put_u8(attr->val, 0);
580                 return attr;
581         }
582
583         pos++;
584         if (pos[0] == '\0' || pos[1] != ':') {
585                 os_free(attr);
586                 return NULL;
587         }
588         syntax = *pos++;
589         pos++;
590
591         switch (syntax) {
592         case 's':
593                 attr->val = wpabuf_alloc_copy(pos, os_strlen(pos));
594                 break;
595         case 'x':
596                 len = os_strlen(pos);
597                 if (len & 1)
598                         break;
599                 len /= 2;
600                 attr->val = wpabuf_alloc(len);
601                 if (attr->val == NULL)
602                         break;
603                 if (hexstr2bin(pos, wpabuf_put(attr->val, len), len) < 0) {
604                         wpabuf_free(attr->val);
605                         os_free(attr);
606                         return NULL;
607                 }
608                 break;
609         case 'd':
610                 attr->val = wpabuf_alloc(4);
611                 if (attr->val)
612                         wpabuf_put_be32(attr->val, atoi(pos));
613                 break;
614         default:
615                 os_free(attr);
616                 return NULL;
617         }
618
619         if (attr->val == NULL) {
620                 os_free(attr);
621                 return NULL;
622         }
623
624         return attr;
625 }
626
627
628 static int hostapd_parse_das_client(struct hostapd_bss_config *bss,
629                                     const char *val)
630 {
631         char *secret;
632
633         secret = os_strchr(val, ' ');
634         if (secret == NULL)
635                 return -1;
636
637         secret++;
638
639         if (hostapd_parse_ip_addr(val, &bss->radius_das_client_addr))
640                 return -1;
641
642         os_free(bss->radius_das_shared_secret);
643         bss->radius_das_shared_secret = (u8 *) os_strdup(secret);
644         if (bss->radius_das_shared_secret == NULL)
645                 return -1;
646         bss->radius_das_shared_secret_len = os_strlen(secret);
647
648         return 0;
649 }
650 #endif /* CONFIG_NO_RADIUS */
651
652
653 static int hostapd_config_parse_key_mgmt(int line, const char *value)
654 {
655         int val = 0, last;
656         char *start, *end, *buf;
657
658         buf = os_strdup(value);
659         if (buf == NULL)
660                 return -1;
661         start = buf;
662
663         while (*start != '\0') {
664                 while (*start == ' ' || *start == '\t')
665                         start++;
666                 if (*start == '\0')
667                         break;
668                 end = start;
669                 while (*end != ' ' && *end != '\t' && *end != '\0')
670                         end++;
671                 last = *end == '\0';
672                 *end = '\0';
673                 if (os_strcmp(start, "WPA-PSK") == 0)
674                         val |= WPA_KEY_MGMT_PSK;
675                 else if (os_strcmp(start, "WPA-EAP") == 0)
676                         val |= WPA_KEY_MGMT_IEEE8021X;
677 #ifdef CONFIG_IEEE80211R
678                 else if (os_strcmp(start, "FT-PSK") == 0)
679                         val |= WPA_KEY_MGMT_FT_PSK;
680                 else if (os_strcmp(start, "FT-EAP") == 0)
681                         val |= WPA_KEY_MGMT_FT_IEEE8021X;
682 #endif /* CONFIG_IEEE80211R */
683 #ifdef CONFIG_IEEE80211W
684                 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
685                         val |= WPA_KEY_MGMT_PSK_SHA256;
686                 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
687                         val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
688 #endif /* CONFIG_IEEE80211W */
689 #ifdef CONFIG_SAE
690                 else if (os_strcmp(start, "SAE") == 0)
691                         val |= WPA_KEY_MGMT_SAE;
692                 else if (os_strcmp(start, "FT-SAE") == 0)
693                         val |= WPA_KEY_MGMT_FT_SAE;
694 #endif /* CONFIG_SAE */
695 #ifdef CONFIG_SUITEB
696                 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
697                         val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
698 #endif /* CONFIG_SUITEB */
699 #ifdef CONFIG_SUITEB192
700                 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
701                         val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
702 #endif /* CONFIG_SUITEB192 */
703                 else {
704                         wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
705                                    line, start);
706                         os_free(buf);
707                         return -1;
708                 }
709
710                 if (last)
711                         break;
712                 start = end + 1;
713         }
714
715         os_free(buf);
716         if (val == 0) {
717                 wpa_printf(MSG_ERROR, "Line %d: no key_mgmt values "
718                            "configured.", line);
719                 return -1;
720         }
721
722         return val;
723 }
724
725
726 static int hostapd_config_parse_cipher(int line, const char *value)
727 {
728         int val = wpa_parse_cipher(value);
729         if (val < 0) {
730                 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
731                            line, value);
732                 return -1;
733         }
734         if (val == 0) {
735                 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
736                            line);
737                 return -1;
738         }
739         return val;
740 }
741
742
743 static int hostapd_config_read_wep(struct hostapd_wep_keys *wep, int keyidx,
744                                    char *val)
745 {
746         size_t len = os_strlen(val);
747
748         if (keyidx < 0 || keyidx > 3 || wep->key[keyidx] != NULL)
749                 return -1;
750
751         if (val[0] == '"') {
752                 if (len < 2 || val[len - 1] != '"')
753                         return -1;
754                 len -= 2;
755                 wep->key[keyidx] = os_malloc(len);
756                 if (wep->key[keyidx] == NULL)
757                         return -1;
758                 os_memcpy(wep->key[keyidx], val + 1, len);
759                 wep->len[keyidx] = len;
760         } else {
761                 if (len & 1)
762                         return -1;
763                 len /= 2;
764                 wep->key[keyidx] = os_malloc(len);
765                 if (wep->key[keyidx] == NULL)
766                         return -1;
767                 wep->len[keyidx] = len;
768                 if (hexstr2bin(val, wep->key[keyidx], len) < 0)
769                         return -1;
770         }
771
772         wep->keys_set++;
773
774         return 0;
775 }
776
777
778 static int hostapd_parse_chanlist(struct hostapd_config *conf, char *val)
779 {
780         char *pos;
781
782         /* for backwards compatibility, translate ' ' in conf str to ',' */
783         pos = val;
784         while (pos) {
785                 pos = os_strchr(pos, ' ');
786                 if (pos)
787                         *pos++ = ',';
788         }
789         if (freq_range_list_parse(&conf->acs_ch_list, val))
790                 return -1;
791
792         return 0;
793 }
794
795
796 static int hostapd_parse_intlist(int **int_list, char *val)
797 {
798         int *list;
799         int count;
800         char *pos, *end;
801
802         os_free(*int_list);
803         *int_list = NULL;
804
805         pos = val;
806         count = 0;
807         while (*pos != '\0') {
808                 if (*pos == ' ')
809                         count++;
810                 pos++;
811         }
812
813         list = os_malloc(sizeof(int) * (count + 2));
814         if (list == NULL)
815                 return -1;
816         pos = val;
817         count = 0;
818         while (*pos != '\0') {
819                 end = os_strchr(pos, ' ');
820                 if (end)
821                         *end = '\0';
822
823                 list[count++] = atoi(pos);
824                 if (!end)
825                         break;
826                 pos = end + 1;
827         }
828         list[count] = -1;
829
830         *int_list = list;
831         return 0;
832 }
833
834
835 static int hostapd_config_bss(struct hostapd_config *conf, const char *ifname)
836 {
837         struct hostapd_bss_config **all, *bss;
838
839         if (*ifname == '\0')
840                 return -1;
841
842         all = os_realloc_array(conf->bss, conf->num_bss + 1,
843                                sizeof(struct hostapd_bss_config *));
844         if (all == NULL) {
845                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
846                            "multi-BSS entry");
847                 return -1;
848         }
849         conf->bss = all;
850
851         bss = os_zalloc(sizeof(*bss));
852         if (bss == NULL)
853                 return -1;
854         bss->radius = os_zalloc(sizeof(*bss->radius));
855         if (bss->radius == NULL) {
856                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
857                            "multi-BSS RADIUS data");
858                 os_free(bss);
859                 return -1;
860         }
861
862         conf->bss[conf->num_bss++] = bss;
863         conf->last_bss = bss;
864
865         hostapd_config_defaults_bss(bss);
866         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
867         os_memcpy(bss->ssid.vlan, bss->iface, IFNAMSIZ + 1);
868
869         return 0;
870 }
871
872
873 /* convert floats with one decimal place to value*10 int, i.e.,
874  * "1.5" will return 15 */
875 static int hostapd_config_read_int10(const char *value)
876 {
877         int i, d;
878         char *pos;
879
880         i = atoi(value);
881         pos = os_strchr(value, '.');
882         d = 0;
883         if (pos) {
884                 pos++;
885                 if (*pos >= '0' && *pos <= '9')
886                         d = *pos - '0';
887         }
888
889         return i * 10 + d;
890 }
891
892
893 static int valid_cw(int cw)
894 {
895         return (cw == 1 || cw == 3 || cw == 7 || cw == 15 || cw == 31 ||
896                 cw == 63 || cw == 127 || cw == 255 || cw == 511 || cw == 1023);
897 }
898
899
900 enum {
901         IEEE80211_TX_QUEUE_DATA0 = 0, /* used for EDCA AC_VO data */
902         IEEE80211_TX_QUEUE_DATA1 = 1, /* used for EDCA AC_VI data */
903         IEEE80211_TX_QUEUE_DATA2 = 2, /* used for EDCA AC_BE data */
904         IEEE80211_TX_QUEUE_DATA3 = 3 /* used for EDCA AC_BK data */
905 };
906
907 static int hostapd_config_tx_queue(struct hostapd_config *conf, char *name,
908                                    char *val)
909 {
910         int num;
911         char *pos;
912         struct hostapd_tx_queue_params *queue;
913
914         /* skip 'tx_queue_' prefix */
915         pos = name + 9;
916         if (os_strncmp(pos, "data", 4) == 0 &&
917             pos[4] >= '0' && pos[4] <= '9' && pos[5] == '_') {
918                 num = pos[4] - '0';
919                 pos += 6;
920         } else if (os_strncmp(pos, "after_beacon_", 13) == 0 ||
921                    os_strncmp(pos, "beacon_", 7) == 0) {
922                 wpa_printf(MSG_INFO, "DEPRECATED: '%s' not used", name);
923                 return 0;
924         } else {
925                 wpa_printf(MSG_ERROR, "Unknown tx_queue name '%s'", pos);
926                 return -1;
927         }
928
929         if (num >= NUM_TX_QUEUES) {
930                 /* for backwards compatibility, do not trigger failure */
931                 wpa_printf(MSG_INFO, "DEPRECATED: '%s' not used", name);
932                 return 0;
933         }
934
935         queue = &conf->tx_queue[num];
936
937         if (os_strcmp(pos, "aifs") == 0) {
938                 queue->aifs = atoi(val);
939                 if (queue->aifs < 0 || queue->aifs > 255) {
940                         wpa_printf(MSG_ERROR, "Invalid AIFS value %d",
941                                    queue->aifs);
942                         return -1;
943                 }
944         } else if (os_strcmp(pos, "cwmin") == 0) {
945                 queue->cwmin = atoi(val);
946                 if (!valid_cw(queue->cwmin)) {
947                         wpa_printf(MSG_ERROR, "Invalid cwMin value %d",
948                                    queue->cwmin);
949                         return -1;
950                 }
951         } else if (os_strcmp(pos, "cwmax") == 0) {
952                 queue->cwmax = atoi(val);
953                 if (!valid_cw(queue->cwmax)) {
954                         wpa_printf(MSG_ERROR, "Invalid cwMax value %d",
955                                    queue->cwmax);
956                         return -1;
957                 }
958         } else if (os_strcmp(pos, "burst") == 0) {
959                 queue->burst = hostapd_config_read_int10(val);
960         } else {
961                 wpa_printf(MSG_ERROR, "Unknown tx_queue field '%s'", pos);
962                 return -1;
963         }
964
965         return 0;
966 }
967
968
969 #ifdef CONFIG_IEEE80211R
970 static int add_r0kh(struct hostapd_bss_config *bss, char *value)
971 {
972         struct ft_remote_r0kh *r0kh;
973         char *pos, *next;
974
975         r0kh = os_zalloc(sizeof(*r0kh));
976         if (r0kh == NULL)
977                 return -1;
978
979         /* 02:01:02:03:04:05 a.example.com 000102030405060708090a0b0c0d0e0f */
980         pos = value;
981         next = os_strchr(pos, ' ');
982         if (next)
983                 *next++ = '\0';
984         if (next == NULL || hwaddr_aton(pos, r0kh->addr)) {
985                 wpa_printf(MSG_ERROR, "Invalid R0KH MAC address: '%s'", pos);
986                 os_free(r0kh);
987                 return -1;
988         }
989
990         pos = next;
991         next = os_strchr(pos, ' ');
992         if (next)
993                 *next++ = '\0';
994         if (next == NULL || next - pos > FT_R0KH_ID_MAX_LEN) {
995                 wpa_printf(MSG_ERROR, "Invalid R0KH-ID: '%s'", pos);
996                 os_free(r0kh);
997                 return -1;
998         }
999         r0kh->id_len = next - pos - 1;
1000         os_memcpy(r0kh->id, pos, r0kh->id_len);
1001
1002         pos = next;
1003         if (hexstr2bin(pos, r0kh->key, sizeof(r0kh->key))) {
1004                 wpa_printf(MSG_ERROR, "Invalid R0KH key: '%s'", pos);
1005                 os_free(r0kh);
1006                 return -1;
1007         }
1008
1009         r0kh->next = bss->r0kh_list;
1010         bss->r0kh_list = r0kh;
1011
1012         return 0;
1013 }
1014
1015
1016 static int add_r1kh(struct hostapd_bss_config *bss, char *value)
1017 {
1018         struct ft_remote_r1kh *r1kh;
1019         char *pos, *next;
1020
1021         r1kh = os_zalloc(sizeof(*r1kh));
1022         if (r1kh == NULL)
1023                 return -1;
1024
1025         /* 02:01:02:03:04:05 02:01:02:03:04:05
1026          * 000102030405060708090a0b0c0d0e0f */
1027         pos = value;
1028         next = os_strchr(pos, ' ');
1029         if (next)
1030                 *next++ = '\0';
1031         if (next == NULL || hwaddr_aton(pos, r1kh->addr)) {
1032                 wpa_printf(MSG_ERROR, "Invalid R1KH MAC address: '%s'", pos);
1033                 os_free(r1kh);
1034                 return -1;
1035         }
1036
1037         pos = next;
1038         next = os_strchr(pos, ' ');
1039         if (next)
1040                 *next++ = '\0';
1041         if (next == NULL || hwaddr_aton(pos, r1kh->id)) {
1042                 wpa_printf(MSG_ERROR, "Invalid R1KH-ID: '%s'", pos);
1043                 os_free(r1kh);
1044                 return -1;
1045         }
1046
1047         pos = next;
1048         if (hexstr2bin(pos, r1kh->key, sizeof(r1kh->key))) {
1049                 wpa_printf(MSG_ERROR, "Invalid R1KH key: '%s'", pos);
1050                 os_free(r1kh);
1051                 return -1;
1052         }
1053
1054         r1kh->next = bss->r1kh_list;
1055         bss->r1kh_list = r1kh;
1056
1057         return 0;
1058 }
1059 #endif /* CONFIG_IEEE80211R */
1060
1061
1062 #ifdef CONFIG_IEEE80211N
1063 static int hostapd_config_ht_capab(struct hostapd_config *conf,
1064                                    const char *capab)
1065 {
1066         if (os_strstr(capab, "[LDPC]"))
1067                 conf->ht_capab |= HT_CAP_INFO_LDPC_CODING_CAP;
1068         if (os_strstr(capab, "[HT40-]")) {
1069                 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1070                 conf->secondary_channel = -1;
1071         }
1072         if (os_strstr(capab, "[HT40+]")) {
1073                 conf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1074                 conf->secondary_channel = 1;
1075         }
1076         if (os_strstr(capab, "[SMPS-STATIC]")) {
1077                 conf->ht_capab &= ~HT_CAP_INFO_SMPS_MASK;
1078                 conf->ht_capab |= HT_CAP_INFO_SMPS_STATIC;
1079         }
1080         if (os_strstr(capab, "[SMPS-DYNAMIC]")) {
1081                 conf->ht_capab &= ~HT_CAP_INFO_SMPS_MASK;
1082                 conf->ht_capab |= HT_CAP_INFO_SMPS_DYNAMIC;
1083         }
1084         if (os_strstr(capab, "[GF]"))
1085                 conf->ht_capab |= HT_CAP_INFO_GREEN_FIELD;
1086         if (os_strstr(capab, "[SHORT-GI-20]"))
1087                 conf->ht_capab |= HT_CAP_INFO_SHORT_GI20MHZ;
1088         if (os_strstr(capab, "[SHORT-GI-40]"))
1089                 conf->ht_capab |= HT_CAP_INFO_SHORT_GI40MHZ;
1090         if (os_strstr(capab, "[TX-STBC]"))
1091                 conf->ht_capab |= HT_CAP_INFO_TX_STBC;
1092         if (os_strstr(capab, "[RX-STBC1]")) {
1093                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1094                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_1;
1095         }
1096         if (os_strstr(capab, "[RX-STBC12]")) {
1097                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1098                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_12;
1099         }
1100         if (os_strstr(capab, "[RX-STBC123]")) {
1101                 conf->ht_capab &= ~HT_CAP_INFO_RX_STBC_MASK;
1102                 conf->ht_capab |= HT_CAP_INFO_RX_STBC_123;
1103         }
1104         if (os_strstr(capab, "[DELAYED-BA]"))
1105                 conf->ht_capab |= HT_CAP_INFO_DELAYED_BA;
1106         if (os_strstr(capab, "[MAX-AMSDU-7935]"))
1107                 conf->ht_capab |= HT_CAP_INFO_MAX_AMSDU_SIZE;
1108         if (os_strstr(capab, "[DSSS_CCK-40]"))
1109                 conf->ht_capab |= HT_CAP_INFO_DSSS_CCK40MHZ;
1110         if (os_strstr(capab, "[40-INTOLERANT]"))
1111                 conf->ht_capab |= HT_CAP_INFO_40MHZ_INTOLERANT;
1112         if (os_strstr(capab, "[LSIG-TXOP-PROT]"))
1113                 conf->ht_capab |= HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT;
1114
1115         return 0;
1116 }
1117 #endif /* CONFIG_IEEE80211N */
1118
1119
1120 #ifdef CONFIG_IEEE80211AC
1121 static int hostapd_config_vht_capab(struct hostapd_config *conf,
1122                                     const char *capab)
1123 {
1124         if (os_strstr(capab, "[MAX-MPDU-7991]"))
1125                 conf->vht_capab |= VHT_CAP_MAX_MPDU_LENGTH_7991;
1126         if (os_strstr(capab, "[MAX-MPDU-11454]"))
1127                 conf->vht_capab |= VHT_CAP_MAX_MPDU_LENGTH_11454;
1128         if (os_strstr(capab, "[VHT160]"))
1129                 conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
1130         if (os_strstr(capab, "[VHT160-80PLUS80]"))
1131                 conf->vht_capab |= VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
1132         if (os_strstr(capab, "[RXLDPC]"))
1133                 conf->vht_capab |= VHT_CAP_RXLDPC;
1134         if (os_strstr(capab, "[SHORT-GI-80]"))
1135                 conf->vht_capab |= VHT_CAP_SHORT_GI_80;
1136         if (os_strstr(capab, "[SHORT-GI-160]"))
1137                 conf->vht_capab |= VHT_CAP_SHORT_GI_160;
1138         if (os_strstr(capab, "[TX-STBC-2BY1]"))
1139                 conf->vht_capab |= VHT_CAP_TXSTBC;
1140         if (os_strstr(capab, "[RX-STBC-1]"))
1141                 conf->vht_capab |= VHT_CAP_RXSTBC_1;
1142         if (os_strstr(capab, "[RX-STBC-12]"))
1143                 conf->vht_capab |= VHT_CAP_RXSTBC_2;
1144         if (os_strstr(capab, "[RX-STBC-123]"))
1145                 conf->vht_capab |= VHT_CAP_RXSTBC_3;
1146         if (os_strstr(capab, "[RX-STBC-1234]"))
1147                 conf->vht_capab |= VHT_CAP_RXSTBC_4;
1148         if (os_strstr(capab, "[SU-BEAMFORMER]"))
1149                 conf->vht_capab |= VHT_CAP_SU_BEAMFORMER_CAPABLE;
1150         if (os_strstr(capab, "[SU-BEAMFORMEE]"))
1151                 conf->vht_capab |= VHT_CAP_SU_BEAMFORMEE_CAPABLE;
1152         if (os_strstr(capab, "[BF-ANTENNA-2]") &&
1153             (conf->vht_capab & VHT_CAP_SU_BEAMFORMEE_CAPABLE))
1154                 conf->vht_capab |= (1 << VHT_CAP_BEAMFORMEE_STS_OFFSET);
1155         if (os_strstr(capab, "[SOUNDING-DIMENSION-2]") &&
1156             (conf->vht_capab & VHT_CAP_SU_BEAMFORMER_CAPABLE))
1157                 conf->vht_capab |= (1 << VHT_CAP_SOUNDING_DIMENSION_OFFSET);
1158         if (os_strstr(capab, "[MU-BEAMFORMER]"))
1159                 conf->vht_capab |= VHT_CAP_MU_BEAMFORMER_CAPABLE;
1160         if (os_strstr(capab, "[MU-BEAMFORMEE]"))
1161                 conf->vht_capab |= VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1162         if (os_strstr(capab, "[VHT-TXOP-PS]"))
1163                 conf->vht_capab |= VHT_CAP_VHT_TXOP_PS;
1164         if (os_strstr(capab, "[HTC-VHT]"))
1165                 conf->vht_capab |= VHT_CAP_HTC_VHT;
1166         if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP7]"))
1167                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX;
1168         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP6]"))
1169                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_6;
1170         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP5]"))
1171                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_5;
1172         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP4]"))
1173                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_4;
1174         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP3]"))
1175                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_3;
1176         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP2]"))
1177                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_2;
1178         else if (os_strstr(capab, "[MAX-A-MPDU-LEN-EXP1]"))
1179                 conf->vht_capab |= VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_1;
1180         if (os_strstr(capab, "[VHT-LINK-ADAPT2]") &&
1181             (conf->vht_capab & VHT_CAP_HTC_VHT))
1182                 conf->vht_capab |= VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB;
1183         if (os_strstr(capab, "[VHT-LINK-ADAPT3]") &&
1184             (conf->vht_capab & VHT_CAP_HTC_VHT))
1185                 conf->vht_capab |= VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB;
1186         if (os_strstr(capab, "[RX-ANTENNA-PATTERN]"))
1187                 conf->vht_capab |= VHT_CAP_RX_ANTENNA_PATTERN;
1188         if (os_strstr(capab, "[TX-ANTENNA-PATTERN]"))
1189                 conf->vht_capab |= VHT_CAP_TX_ANTENNA_PATTERN;
1190         return 0;
1191 }
1192 #endif /* CONFIG_IEEE80211AC */
1193
1194
1195 #ifdef CONFIG_INTERWORKING
1196 static int parse_roaming_consortium(struct hostapd_bss_config *bss, char *pos,
1197                                     int line)
1198 {
1199         size_t len = os_strlen(pos);
1200         u8 oi[MAX_ROAMING_CONSORTIUM_LEN];
1201
1202         struct hostapd_roaming_consortium *rc;
1203
1204         if ((len & 1) || len < 2 * 3 || len / 2 > MAX_ROAMING_CONSORTIUM_LEN ||
1205             hexstr2bin(pos, oi, len / 2)) {
1206                 wpa_printf(MSG_ERROR, "Line %d: invalid roaming_consortium "
1207                            "'%s'", line, pos);
1208                 return -1;
1209         }
1210         len /= 2;
1211
1212         rc = os_realloc_array(bss->roaming_consortium,
1213                               bss->roaming_consortium_count + 1,
1214                               sizeof(struct hostapd_roaming_consortium));
1215         if (rc == NULL)
1216                 return -1;
1217
1218         os_memcpy(rc[bss->roaming_consortium_count].oi, oi, len);
1219         rc[bss->roaming_consortium_count].len = len;
1220
1221         bss->roaming_consortium = rc;
1222         bss->roaming_consortium_count++;
1223
1224         return 0;
1225 }
1226
1227
1228 static int parse_lang_string(struct hostapd_lang_string **array,
1229                              unsigned int *count, char *pos)
1230 {
1231         char *sep, *str = NULL;
1232         size_t clen, nlen, slen;
1233         struct hostapd_lang_string *ls;
1234         int ret = -1;
1235
1236         if (*pos == '"' || (*pos == 'P' && pos[1] == '"')) {
1237                 str = wpa_config_parse_string(pos, &slen);
1238                 if (!str)
1239                         return -1;
1240                 pos = str;
1241         }
1242
1243         sep = os_strchr(pos, ':');
1244         if (sep == NULL)
1245                 goto fail;
1246         *sep++ = '\0';
1247
1248         clen = os_strlen(pos);
1249         if (clen < 2 || clen > sizeof(ls->lang))
1250                 goto fail;
1251         nlen = os_strlen(sep);
1252         if (nlen > 252)
1253                 goto fail;
1254
1255         ls = os_realloc_array(*array, *count + 1,
1256                               sizeof(struct hostapd_lang_string));
1257         if (ls == NULL)
1258                 goto fail;
1259
1260         *array = ls;
1261         ls = &(*array)[*count];
1262         (*count)++;
1263
1264         os_memset(ls->lang, 0, sizeof(ls->lang));
1265         os_memcpy(ls->lang, pos, clen);
1266         ls->name_len = nlen;
1267         os_memcpy(ls->name, sep, nlen);
1268
1269         ret = 0;
1270 fail:
1271         os_free(str);
1272         return ret;
1273 }
1274
1275
1276 static int parse_venue_name(struct hostapd_bss_config *bss, char *pos,
1277                             int line)
1278 {
1279         if (parse_lang_string(&bss->venue_name, &bss->venue_name_count, pos)) {
1280                 wpa_printf(MSG_ERROR, "Line %d: Invalid venue_name '%s'",
1281                            line, pos);
1282                 return -1;
1283         }
1284         return 0;
1285 }
1286
1287
1288 static int parse_3gpp_cell_net(struct hostapd_bss_config *bss, char *buf,
1289                                int line)
1290 {
1291         size_t count;
1292         char *pos;
1293         u8 *info = NULL, *ipos;
1294
1295         /* format: <MCC1,MNC1>[;<MCC2,MNC2>][;...] */
1296
1297         count = 1;
1298         for (pos = buf; *pos; pos++) {
1299                 if ((*pos < '0' || *pos > '9') && *pos != ';' && *pos != ',')
1300                         goto fail;
1301                 if (*pos == ';')
1302                         count++;
1303         }
1304         if (1 + count * 3 > 0x7f)
1305                 goto fail;
1306
1307         info = os_zalloc(2 + 3 + count * 3);
1308         if (info == NULL)
1309                 return -1;
1310
1311         ipos = info;
1312         *ipos++ = 0; /* GUD - Version 1 */
1313         *ipos++ = 3 + count * 3; /* User Data Header Length (UDHL) */
1314         *ipos++ = 0; /* PLMN List IEI */
1315         /* ext(b8) | Length of PLMN List value contents(b7..1) */
1316         *ipos++ = 1 + count * 3;
1317         *ipos++ = count; /* Number of PLMNs */
1318
1319         pos = buf;
1320         while (pos && *pos) {
1321                 char *mcc, *mnc;
1322                 size_t mnc_len;
1323
1324                 mcc = pos;
1325                 mnc = os_strchr(pos, ',');
1326                 if (mnc == NULL)
1327                         goto fail;
1328                 *mnc++ = '\0';
1329                 pos = os_strchr(mnc, ';');
1330                 if (pos)
1331                         *pos++ = '\0';
1332
1333                 mnc_len = os_strlen(mnc);
1334                 if (os_strlen(mcc) != 3 || (mnc_len != 2 && mnc_len != 3))
1335                         goto fail;
1336
1337                 /* BC coded MCC,MNC */
1338                 /* MCC digit 2 | MCC digit 1 */
1339                 *ipos++ = ((mcc[1] - '0') << 4) | (mcc[0] - '0');
1340                 /* MNC digit 3 | MCC digit 3 */
1341                 *ipos++ = (((mnc_len == 2) ? 0xf0 : ((mnc[2] - '0') << 4))) |
1342                         (mcc[2] - '0');
1343                 /* MNC digit 2 | MNC digit 1 */
1344                 *ipos++ = ((mnc[1] - '0') << 4) | (mnc[0] - '0');
1345         }
1346
1347         os_free(bss->anqp_3gpp_cell_net);
1348         bss->anqp_3gpp_cell_net = info;
1349         bss->anqp_3gpp_cell_net_len = 2 + 3 + 3 * count;
1350         wpa_hexdump(MSG_MSGDUMP, "3GPP Cellular Network information",
1351                     bss->anqp_3gpp_cell_net, bss->anqp_3gpp_cell_net_len);
1352
1353         return 0;
1354
1355 fail:
1356         wpa_printf(MSG_ERROR, "Line %d: Invalid anqp_3gpp_cell_net: %s",
1357                    line, buf);
1358         os_free(info);
1359         return -1;
1360 }
1361
1362
1363 static int parse_nai_realm(struct hostapd_bss_config *bss, char *buf, int line)
1364 {
1365         struct hostapd_nai_realm_data *realm;
1366         size_t i, j, len;
1367         int *offsets;
1368         char *pos, *end, *rpos;
1369
1370         offsets = os_calloc(bss->nai_realm_count * MAX_NAI_REALMS,
1371                             sizeof(int));
1372         if (offsets == NULL)
1373                 return -1;
1374
1375         for (i = 0; i < bss->nai_realm_count; i++) {
1376                 realm = &bss->nai_realm_data[i];
1377                 for (j = 0; j < MAX_NAI_REALMS; j++) {
1378                         offsets[i * MAX_NAI_REALMS + j] =
1379                                 realm->realm[j] ?
1380                                 realm->realm[j] - realm->realm_buf : -1;
1381                 }
1382         }
1383
1384         realm = os_realloc_array(bss->nai_realm_data, bss->nai_realm_count + 1,
1385                                  sizeof(struct hostapd_nai_realm_data));
1386         if (realm == NULL) {
1387                 os_free(offsets);
1388                 return -1;
1389         }
1390         bss->nai_realm_data = realm;
1391
1392         /* patch the pointers after realloc */
1393         for (i = 0; i < bss->nai_realm_count; i++) {
1394                 realm = &bss->nai_realm_data[i];
1395                 for (j = 0; j < MAX_NAI_REALMS; j++) {
1396                         int offs = offsets[i * MAX_NAI_REALMS + j];
1397                         if (offs >= 0)
1398                                 realm->realm[j] = realm->realm_buf + offs;
1399                         else
1400                                 realm->realm[j] = NULL;
1401                 }
1402         }
1403         os_free(offsets);
1404
1405         realm = &bss->nai_realm_data[bss->nai_realm_count];
1406         os_memset(realm, 0, sizeof(*realm));
1407
1408         pos = buf;
1409         realm->encoding = atoi(pos);
1410         pos = os_strchr(pos, ',');
1411         if (pos == NULL)
1412                 goto fail;
1413         pos++;
1414
1415         end = os_strchr(pos, ',');
1416         if (end) {
1417                 len = end - pos;
1418                 *end = '\0';
1419         } else {
1420                 len = os_strlen(pos);
1421         }
1422
1423         if (len > MAX_NAI_REALMLEN) {
1424                 wpa_printf(MSG_ERROR, "Too long a realm string (%d > max %d "
1425                            "characters)", (int) len, MAX_NAI_REALMLEN);
1426                 goto fail;
1427         }
1428         os_memcpy(realm->realm_buf, pos, len);
1429
1430         if (end)
1431                 pos = end + 1;
1432         else
1433                 pos = NULL;
1434
1435         while (pos && *pos) {
1436                 struct hostapd_nai_realm_eap *eap;
1437
1438                 if (realm->eap_method_count >= MAX_NAI_EAP_METHODS) {
1439                         wpa_printf(MSG_ERROR, "Too many EAP methods");
1440                         goto fail;
1441                 }
1442
1443                 eap = &realm->eap_method[realm->eap_method_count];
1444                 realm->eap_method_count++;
1445
1446                 end = os_strchr(pos, ',');
1447                 if (end == NULL)
1448                         end = pos + os_strlen(pos);
1449
1450                 eap->eap_method = atoi(pos);
1451                 for (;;) {
1452                         pos = os_strchr(pos, '[');
1453                         if (pos == NULL || pos > end)
1454                                 break;
1455                         pos++;
1456                         if (eap->num_auths >= MAX_NAI_AUTH_TYPES) {
1457                                 wpa_printf(MSG_ERROR, "Too many auth params");
1458                                 goto fail;
1459                         }
1460                         eap->auth_id[eap->num_auths] = atoi(pos);
1461                         pos = os_strchr(pos, ':');
1462                         if (pos == NULL || pos > end)
1463                                 goto fail;
1464                         pos++;
1465                         eap->auth_val[eap->num_auths] = atoi(pos);
1466                         pos = os_strchr(pos, ']');
1467                         if (pos == NULL || pos > end)
1468                                 goto fail;
1469                         pos++;
1470                         eap->num_auths++;
1471                 }
1472
1473                 if (*end != ',')
1474                         break;
1475
1476                 pos = end + 1;
1477         }
1478
1479         /* Split realm list into null terminated realms */
1480         rpos = realm->realm_buf;
1481         i = 0;
1482         while (*rpos) {
1483                 if (i >= MAX_NAI_REALMS) {
1484                         wpa_printf(MSG_ERROR, "Too many realms");
1485                         goto fail;
1486                 }
1487                 realm->realm[i++] = rpos;
1488                 rpos = os_strchr(rpos, ';');
1489                 if (rpos == NULL)
1490                         break;
1491                 *rpos++ = '\0';
1492         }
1493
1494         bss->nai_realm_count++;
1495
1496         return 0;
1497
1498 fail:
1499         wpa_printf(MSG_ERROR, "Line %d: invalid nai_realm '%s'", line, buf);
1500         return -1;
1501 }
1502
1503
1504 static int parse_qos_map_set(struct hostapd_bss_config *bss,
1505                              char *buf, int line)
1506 {
1507         u8 qos_map_set[16 + 2 * 21], count = 0;
1508         char *pos = buf;
1509         int val;
1510
1511         for (;;) {
1512                 if (count == sizeof(qos_map_set)) {
1513                         wpa_printf(MSG_ERROR, "Line %d: Too many qos_map_set "
1514                                    "parameters '%s'", line, buf);
1515                         return -1;
1516                 }
1517
1518                 val = atoi(pos);
1519                 if (val > 255 || val < 0) {
1520                         wpa_printf(MSG_ERROR, "Line %d: Invalid qos_map_set "
1521                                    "'%s'", line, buf);
1522                         return -1;
1523                 }
1524
1525                 qos_map_set[count++] = val;
1526                 pos = os_strchr(pos, ',');
1527                 if (!pos)
1528                         break;
1529                 pos++;
1530         }
1531
1532         if (count < 16 || count & 1) {
1533                 wpa_printf(MSG_ERROR, "Line %d: Invalid qos_map_set '%s'",
1534                            line, buf);
1535                 return -1;
1536         }
1537
1538         os_memcpy(bss->qos_map_set, qos_map_set, count);
1539         bss->qos_map_set_len = count;
1540
1541         return 0;
1542 }
1543
1544 #endif /* CONFIG_INTERWORKING */
1545
1546
1547 #ifdef CONFIG_HS20
1548 static int hs20_parse_conn_capab(struct hostapd_bss_config *bss, char *buf,
1549                                  int line)
1550 {
1551         u8 *conn_cap;
1552         char *pos;
1553
1554         if (bss->hs20_connection_capability_len >= 0xfff0)
1555                 return -1;
1556
1557         conn_cap = os_realloc(bss->hs20_connection_capability,
1558                               bss->hs20_connection_capability_len + 4);
1559         if (conn_cap == NULL)
1560                 return -1;
1561
1562         bss->hs20_connection_capability = conn_cap;
1563         conn_cap += bss->hs20_connection_capability_len;
1564         pos = buf;
1565         conn_cap[0] = atoi(pos);
1566         pos = os_strchr(pos, ':');
1567         if (pos == NULL)
1568                 return -1;
1569         pos++;
1570         WPA_PUT_LE16(conn_cap + 1, atoi(pos));
1571         pos = os_strchr(pos, ':');
1572         if (pos == NULL)
1573                 return -1;
1574         pos++;
1575         conn_cap[3] = atoi(pos);
1576         bss->hs20_connection_capability_len += 4;
1577
1578         return 0;
1579 }
1580
1581
1582 static int hs20_parse_wan_metrics(struct hostapd_bss_config *bss, char *buf,
1583                                   int line)
1584 {
1585         u8 *wan_metrics;
1586         char *pos;
1587
1588         /* <WAN Info>:<DL Speed>:<UL Speed>:<DL Load>:<UL Load>:<LMD> */
1589
1590         wan_metrics = os_zalloc(13);
1591         if (wan_metrics == NULL)
1592                 return -1;
1593
1594         pos = buf;
1595         /* WAN Info */
1596         if (hexstr2bin(pos, wan_metrics, 1) < 0)
1597                 goto fail;
1598         pos += 2;
1599         if (*pos != ':')
1600                 goto fail;
1601         pos++;
1602
1603         /* Downlink Speed */
1604         WPA_PUT_LE32(wan_metrics + 1, atoi(pos));
1605         pos = os_strchr(pos, ':');
1606         if (pos == NULL)
1607                 goto fail;
1608         pos++;
1609
1610         /* Uplink Speed */
1611         WPA_PUT_LE32(wan_metrics + 5, atoi(pos));
1612         pos = os_strchr(pos, ':');
1613         if (pos == NULL)
1614                 goto fail;
1615         pos++;
1616
1617         /* Downlink Load */
1618         wan_metrics[9] = atoi(pos);
1619         pos = os_strchr(pos, ':');
1620         if (pos == NULL)
1621                 goto fail;
1622         pos++;
1623
1624         /* Uplink Load */
1625         wan_metrics[10] = atoi(pos);
1626         pos = os_strchr(pos, ':');
1627         if (pos == NULL)
1628                 goto fail;
1629         pos++;
1630
1631         /* LMD */
1632         WPA_PUT_LE16(wan_metrics + 11, atoi(pos));
1633
1634         os_free(bss->hs20_wan_metrics);
1635         bss->hs20_wan_metrics = wan_metrics;
1636
1637         return 0;
1638
1639 fail:
1640         wpa_printf(MSG_ERROR, "Line %d: Invalid hs20_wan_metrics '%s'",
1641                    line, buf);
1642         os_free(wan_metrics);
1643         return -1;
1644 }
1645
1646
1647 static int hs20_parse_oper_friendly_name(struct hostapd_bss_config *bss,
1648                                          char *pos, int line)
1649 {
1650         if (parse_lang_string(&bss->hs20_oper_friendly_name,
1651                               &bss->hs20_oper_friendly_name_count, pos)) {
1652                 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1653                            "hs20_oper_friendly_name '%s'", line, pos);
1654                 return -1;
1655         }
1656         return 0;
1657 }
1658
1659
1660 static int hs20_parse_icon(struct hostapd_bss_config *bss, char *pos)
1661 {
1662         struct hs20_icon *icon;
1663         char *end;
1664
1665         icon = os_realloc_array(bss->hs20_icons, bss->hs20_icons_count + 1,
1666                                 sizeof(struct hs20_icon));
1667         if (icon == NULL)
1668                 return -1;
1669         bss->hs20_icons = icon;
1670         icon = &bss->hs20_icons[bss->hs20_icons_count];
1671         os_memset(icon, 0, sizeof(*icon));
1672
1673         icon->width = atoi(pos);
1674         pos = os_strchr(pos, ':');
1675         if (pos == NULL)
1676                 return -1;
1677         pos++;
1678
1679         icon->height = atoi(pos);
1680         pos = os_strchr(pos, ':');
1681         if (pos == NULL)
1682                 return -1;
1683         pos++;
1684
1685         end = os_strchr(pos, ':');
1686         if (end == NULL || end - pos > 3)
1687                 return -1;
1688         os_memcpy(icon->language, pos, end - pos);
1689         pos = end + 1;
1690
1691         end = os_strchr(pos, ':');
1692         if (end == NULL || end - pos > 255)
1693                 return -1;
1694         os_memcpy(icon->type, pos, end - pos);
1695         pos = end + 1;
1696
1697         end = os_strchr(pos, ':');
1698         if (end == NULL || end - pos > 255)
1699                 return -1;
1700         os_memcpy(icon->name, pos, end - pos);
1701         pos = end + 1;
1702
1703         if (os_strlen(pos) > 255)
1704                 return -1;
1705         os_memcpy(icon->file, pos, os_strlen(pos));
1706
1707         bss->hs20_icons_count++;
1708
1709         return 0;
1710 }
1711
1712
1713 static int hs20_parse_osu_ssid(struct hostapd_bss_config *bss,
1714                                char *pos, int line)
1715 {
1716         size_t slen;
1717         char *str;
1718
1719         str = wpa_config_parse_string(pos, &slen);
1720         if (str == NULL || slen < 1 || slen > HOSTAPD_MAX_SSID_LEN) {
1721                 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID '%s'", line, pos);
1722                 os_free(str);
1723                 return -1;
1724         }
1725
1726         os_memcpy(bss->osu_ssid, str, slen);
1727         bss->osu_ssid_len = slen;
1728         os_free(str);
1729
1730         return 0;
1731 }
1732
1733
1734 static int hs20_parse_osu_server_uri(struct hostapd_bss_config *bss,
1735                                      char *pos, int line)
1736 {
1737         struct hs20_osu_provider *p;
1738
1739         p = os_realloc_array(bss->hs20_osu_providers,
1740                              bss->hs20_osu_providers_count + 1, sizeof(*p));
1741         if (p == NULL)
1742                 return -1;
1743
1744         bss->hs20_osu_providers = p;
1745         bss->last_osu = &bss->hs20_osu_providers[bss->hs20_osu_providers_count];
1746         bss->hs20_osu_providers_count++;
1747         os_memset(bss->last_osu, 0, sizeof(*p));
1748         bss->last_osu->server_uri = os_strdup(pos);
1749
1750         return 0;
1751 }
1752
1753
1754 static int hs20_parse_osu_friendly_name(struct hostapd_bss_config *bss,
1755                                         char *pos, int line)
1756 {
1757         if (bss->last_osu == NULL) {
1758                 wpa_printf(MSG_ERROR, "Line %d: Unexpected OSU field", line);
1759                 return -1;
1760         }
1761
1762         if (parse_lang_string(&bss->last_osu->friendly_name,
1763                               &bss->last_osu->friendly_name_count, pos)) {
1764                 wpa_printf(MSG_ERROR, "Line %d: Invalid osu_friendly_name '%s'",
1765                            line, pos);
1766                 return -1;
1767         }
1768
1769         return 0;
1770 }
1771
1772
1773 static int hs20_parse_osu_nai(struct hostapd_bss_config *bss,
1774                               char *pos, int line)
1775 {
1776         if (bss->last_osu == NULL) {
1777                 wpa_printf(MSG_ERROR, "Line %d: Unexpected OSU field", line);
1778                 return -1;
1779         }
1780
1781         os_free(bss->last_osu->osu_nai);
1782         bss->last_osu->osu_nai = os_strdup(pos);
1783         if (bss->last_osu->osu_nai == NULL)
1784                 return -1;
1785
1786         return 0;
1787 }
1788
1789
1790 static int hs20_parse_osu_method_list(struct hostapd_bss_config *bss, char *pos,
1791                                       int line)
1792 {
1793         if (bss->last_osu == NULL) {
1794                 wpa_printf(MSG_ERROR, "Line %d: Unexpected OSU field", line);
1795                 return -1;
1796         }
1797
1798         if (hostapd_parse_intlist(&bss->last_osu->method_list, pos)) {
1799                 wpa_printf(MSG_ERROR, "Line %d: Invalid osu_method_list", line);
1800                 return -1;
1801         }
1802
1803         return 0;
1804 }
1805
1806
1807 static int hs20_parse_osu_icon(struct hostapd_bss_config *bss, char *pos,
1808                                int line)
1809 {
1810         char **n;
1811         struct hs20_osu_provider *p = bss->last_osu;
1812
1813         if (p == NULL) {
1814                 wpa_printf(MSG_ERROR, "Line %d: Unexpected OSU field", line);
1815                 return -1;
1816         }
1817
1818         n = os_realloc_array(p->icons, p->icons_count + 1, sizeof(char *));
1819         if (n == NULL)
1820                 return -1;
1821         p->icons = n;
1822         p->icons[p->icons_count] = os_strdup(pos);
1823         if (p->icons[p->icons_count] == NULL)
1824                 return -1;
1825         p->icons_count++;
1826
1827         return 0;
1828 }
1829
1830
1831 static int hs20_parse_osu_service_desc(struct hostapd_bss_config *bss,
1832                                        char *pos, int line)
1833 {
1834         if (bss->last_osu == NULL) {
1835                 wpa_printf(MSG_ERROR, "Line %d: Unexpected OSU field", line);
1836                 return -1;
1837         }
1838
1839         if (parse_lang_string(&bss->last_osu->service_desc,
1840                               &bss->last_osu->service_desc_count, pos)) {
1841                 wpa_printf(MSG_ERROR, "Line %d: Invalid osu_service_desc '%s'",
1842                            line, pos);
1843                 return -1;
1844         }
1845
1846         return 0;
1847 }
1848
1849 #endif /* CONFIG_HS20 */
1850
1851
1852 #ifdef CONFIG_WPS_NFC
1853 static struct wpabuf * hostapd_parse_bin(const char *buf)
1854 {
1855         size_t len;
1856         struct wpabuf *ret;
1857
1858         len = os_strlen(buf);
1859         if (len & 0x01)
1860                 return NULL;
1861         len /= 2;
1862
1863         ret = wpabuf_alloc(len);
1864         if (ret == NULL)
1865                 return NULL;
1866
1867         if (hexstr2bin(buf, wpabuf_put(ret, len), len)) {
1868                 wpabuf_free(ret);
1869                 return NULL;
1870         }
1871
1872         return ret;
1873 }
1874 #endif /* CONFIG_WPS_NFC */
1875
1876
1877 #ifdef CONFIG_ACS
1878 static int hostapd_config_parse_acs_chan_bias(struct hostapd_config *conf,
1879                                               char *pos)
1880 {
1881         struct acs_bias *bias = NULL, *tmp;
1882         unsigned int num = 0;
1883         char *end;
1884
1885         while (*pos) {
1886                 tmp = os_realloc_array(bias, num + 1, sizeof(*bias));
1887                 if (!tmp)
1888                         goto fail;
1889                 bias = tmp;
1890
1891                 bias[num].channel = atoi(pos);
1892                 if (bias[num].channel <= 0)
1893                         goto fail;
1894                 pos = os_strchr(pos, ':');
1895                 if (!pos)
1896                         goto fail;
1897                 pos++;
1898                 bias[num].bias = strtod(pos, &end);
1899                 if (end == pos || bias[num].bias < 0.0)
1900                         goto fail;
1901                 pos = end;
1902                 if (*pos != ' ' && *pos != '\0')
1903                         goto fail;
1904                 num++;
1905         }
1906
1907         os_free(conf->acs_chan_bias);
1908         conf->acs_chan_bias = bias;
1909         conf->num_acs_chan_bias = num;
1910
1911         return 0;
1912 fail:
1913         os_free(bias);
1914         return -1;
1915 }
1916 #endif /* CONFIG_ACS */
1917
1918
1919 static int hostapd_config_fill(struct hostapd_config *conf,
1920                                struct hostapd_bss_config *bss,
1921                                char *buf, char *pos, int line)
1922 {
1923         if (os_strcmp(buf, "interface") == 0) {
1924                 os_strlcpy(conf->bss[0]->iface, pos,
1925                            sizeof(conf->bss[0]->iface));
1926         } else if (os_strcmp(buf, "bridge") == 0) {
1927                 os_strlcpy(bss->bridge, pos, sizeof(bss->bridge));
1928         } else if (os_strcmp(buf, "vlan_bridge") == 0) {
1929                 os_strlcpy(bss->vlan_bridge, pos, sizeof(bss->vlan_bridge));
1930         } else if (os_strcmp(buf, "wds_bridge") == 0) {
1931                 os_strlcpy(bss->wds_bridge, pos, sizeof(bss->wds_bridge));
1932         } else if (os_strcmp(buf, "driver") == 0) {
1933                 int j;
1934                 /* clear to get error below if setting is invalid */
1935                 conf->driver = NULL;
1936                 for (j = 0; wpa_drivers[j]; j++) {
1937                         if (os_strcmp(pos, wpa_drivers[j]->name) == 0) {
1938                                 conf->driver = wpa_drivers[j];
1939                                 break;
1940                         }
1941                 }
1942                 if (conf->driver == NULL) {
1943                         wpa_printf(MSG_ERROR,
1944                                    "Line %d: invalid/unknown driver '%s'",
1945                                    line, pos);
1946                         return 1;
1947                 }
1948         } else if (os_strcmp(buf, "driver_params") == 0) {
1949                 os_free(conf->driver_params);
1950                 conf->driver_params = os_strdup(pos);
1951         } else if (os_strcmp(buf, "debug") == 0) {
1952                 wpa_printf(MSG_DEBUG, "Line %d: DEPRECATED: 'debug' configuration variable is not used anymore",
1953                            line);
1954         } else if (os_strcmp(buf, "logger_syslog_level") == 0) {
1955                 bss->logger_syslog_level = atoi(pos);
1956         } else if (os_strcmp(buf, "logger_stdout_level") == 0) {
1957                 bss->logger_stdout_level = atoi(pos);
1958         } else if (os_strcmp(buf, "logger_syslog") == 0) {
1959                 bss->logger_syslog = atoi(pos);
1960         } else if (os_strcmp(buf, "logger_stdout") == 0) {
1961                 bss->logger_stdout = atoi(pos);
1962         } else if (os_strcmp(buf, "dump_file") == 0) {
1963                 wpa_printf(MSG_INFO, "Line %d: DEPRECATED: 'dump_file' configuration variable is not used anymore",
1964                            line);
1965         } else if (os_strcmp(buf, "ssid") == 0) {
1966                 bss->ssid.ssid_len = os_strlen(pos);
1967                 if (bss->ssid.ssid_len > HOSTAPD_MAX_SSID_LEN ||
1968                     bss->ssid.ssid_len < 1) {
1969                         wpa_printf(MSG_ERROR, "Line %d: invalid SSID '%s'",
1970                                    line, pos);
1971                         return 1;
1972                 }
1973                 os_memcpy(bss->ssid.ssid, pos, bss->ssid.ssid_len);
1974                 bss->ssid.ssid_set = 1;
1975         } else if (os_strcmp(buf, "ssid2") == 0) {
1976                 size_t slen;
1977                 char *str = wpa_config_parse_string(pos, &slen);
1978                 if (str == NULL || slen < 1 || slen > HOSTAPD_MAX_SSID_LEN) {
1979                         wpa_printf(MSG_ERROR, "Line %d: invalid SSID '%s'",
1980                                    line, pos);
1981                         os_free(str);
1982                         return 1;
1983                 }
1984                 os_memcpy(bss->ssid.ssid, str, slen);
1985                 bss->ssid.ssid_len = slen;
1986                 bss->ssid.ssid_set = 1;
1987                 os_free(str);
1988         } else if (os_strcmp(buf, "utf8_ssid") == 0) {
1989                 bss->ssid.utf8_ssid = atoi(pos) > 0;
1990         } else if (os_strcmp(buf, "macaddr_acl") == 0) {
1991                 bss->macaddr_acl = atoi(pos);
1992                 if (bss->macaddr_acl != ACCEPT_UNLESS_DENIED &&
1993                     bss->macaddr_acl != DENY_UNLESS_ACCEPTED &&
1994                     bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
1995                         wpa_printf(MSG_ERROR, "Line %d: unknown macaddr_acl %d",
1996                                    line, bss->macaddr_acl);
1997                 }
1998         } else if (os_strcmp(buf, "accept_mac_file") == 0) {
1999                 if (hostapd_config_read_maclist(pos, &bss->accept_mac,
2000                                                 &bss->num_accept_mac)) {
2001                         wpa_printf(MSG_ERROR, "Line %d: Failed to read accept_mac_file '%s'",
2002                                    line, pos);
2003                         return 1;
2004                 }
2005         } else if (os_strcmp(buf, "deny_mac_file") == 0) {
2006                 if (hostapd_config_read_maclist(pos, &bss->deny_mac,
2007                                                 &bss->num_deny_mac)) {
2008                         wpa_printf(MSG_ERROR, "Line %d: Failed to read deny_mac_file '%s'",
2009                                    line, pos);
2010                         return 1;
2011                 }
2012         } else if (os_strcmp(buf, "wds_sta") == 0) {
2013                 bss->wds_sta = atoi(pos);
2014         } else if (os_strcmp(buf, "start_disabled") == 0) {
2015                 bss->start_disabled = atoi(pos);
2016         } else if (os_strcmp(buf, "ap_isolate") == 0) {
2017                 bss->isolate = atoi(pos);
2018         } else if (os_strcmp(buf, "ap_max_inactivity") == 0) {
2019                 bss->ap_max_inactivity = atoi(pos);
2020         } else if (os_strcmp(buf, "skip_inactivity_poll") == 0) {
2021                 bss->skip_inactivity_poll = atoi(pos);
2022         } else if (os_strcmp(buf, "country_code") == 0) {
2023                 os_memcpy(conf->country, pos, 2);
2024                 /* FIX: make this configurable */
2025                 conf->country[2] = ' ';
2026         } else if (os_strcmp(buf, "ieee80211d") == 0) {
2027                 conf->ieee80211d = atoi(pos);
2028         } else if (os_strcmp(buf, "ieee80211h") == 0) {
2029                 conf->ieee80211h = atoi(pos);
2030         } else if (os_strcmp(buf, "ieee8021x") == 0) {
2031                 bss->ieee802_1x = atoi(pos);
2032         } else if (os_strcmp(buf, "eapol_version") == 0) {
2033                 bss->eapol_version = atoi(pos);
2034                 if (bss->eapol_version < 1 || bss->eapol_version > 2) {
2035                         wpa_printf(MSG_ERROR,
2036                                    "Line %d: invalid EAPOL version (%d): '%s'.",
2037                                    line, bss->eapol_version, pos);
2038                         return 1;
2039                 }
2040                 wpa_printf(MSG_DEBUG, "eapol_version=%d", bss->eapol_version);
2041 #ifdef EAP_SERVER
2042         } else if (os_strcmp(buf, "eap_authenticator") == 0) {
2043                 bss->eap_server = atoi(pos);
2044                 wpa_printf(MSG_ERROR, "Line %d: obsolete eap_authenticator used; this has been renamed to eap_server", line);
2045         } else if (os_strcmp(buf, "eap_server") == 0) {
2046                 bss->eap_server = atoi(pos);
2047         } else if (os_strcmp(buf, "eap_user_file") == 0) {
2048                 if (hostapd_config_read_eap_user(pos, bss))
2049                         return 1;
2050         } else if (os_strcmp(buf, "ca_cert") == 0) {
2051                 os_free(bss->ca_cert);
2052                 bss->ca_cert = os_strdup(pos);
2053         } else if (os_strcmp(buf, "server_cert") == 0) {
2054                 os_free(bss->server_cert);
2055                 bss->server_cert = os_strdup(pos);
2056         } else if (os_strcmp(buf, "private_key") == 0) {
2057                 os_free(bss->private_key);
2058                 bss->private_key = os_strdup(pos);
2059         } else if (os_strcmp(buf, "private_key_passwd") == 0) {
2060                 os_free(bss->private_key_passwd);
2061                 bss->private_key_passwd = os_strdup(pos);
2062         } else if (os_strcmp(buf, "check_crl") == 0) {
2063                 bss->check_crl = atoi(pos);
2064         } else if (os_strcmp(buf, "ocsp_stapling_response") == 0) {
2065                 os_free(bss->ocsp_stapling_response);
2066                 bss->ocsp_stapling_response = os_strdup(pos);
2067         } else if (os_strcmp(buf, "dh_file") == 0) {
2068                 os_free(bss->dh_file);
2069                 bss->dh_file = os_strdup(pos);
2070         } else if (os_strcmp(buf, "openssl_ciphers") == 0) {
2071                 os_free(bss->openssl_ciphers);
2072                 bss->openssl_ciphers = os_strdup(pos);
2073         } else if (os_strcmp(buf, "fragment_size") == 0) {
2074                 bss->fragment_size = atoi(pos);
2075 #ifdef EAP_SERVER_FAST
2076         } else if (os_strcmp(buf, "pac_opaque_encr_key") == 0) {
2077                 os_free(bss->pac_opaque_encr_key);
2078                 bss->pac_opaque_encr_key = os_malloc(16);
2079                 if (bss->pac_opaque_encr_key == NULL) {
2080                         wpa_printf(MSG_ERROR,
2081                                    "Line %d: No memory for pac_opaque_encr_key",
2082                                    line);
2083                         return 1;
2084                 } else if (hexstr2bin(pos, bss->pac_opaque_encr_key, 16)) {
2085                         wpa_printf(MSG_ERROR, "Line %d: Invalid pac_opaque_encr_key",
2086                                    line);
2087                         return 1;
2088                 }
2089         } else if (os_strcmp(buf, "eap_fast_a_id") == 0) {
2090                 size_t idlen = os_strlen(pos);
2091                 if (idlen & 1) {
2092                         wpa_printf(MSG_ERROR, "Line %d: Invalid eap_fast_a_id",
2093                                    line);
2094                         return 1;
2095                 }
2096                 os_free(bss->eap_fast_a_id);
2097                 bss->eap_fast_a_id = os_malloc(idlen / 2);
2098                 if (bss->eap_fast_a_id == NULL ||
2099                     hexstr2bin(pos, bss->eap_fast_a_id, idlen / 2)) {
2100                         wpa_printf(MSG_ERROR, "Line %d: Failed to parse eap_fast_a_id",
2101                                    line);
2102                         os_free(bss->eap_fast_a_id);
2103                         bss->eap_fast_a_id = NULL;
2104                         return 1;
2105                 } else {
2106                         bss->eap_fast_a_id_len = idlen / 2;
2107                 }
2108         } else if (os_strcmp(buf, "eap_fast_a_id_info") == 0) {
2109                 os_free(bss->eap_fast_a_id_info);
2110                 bss->eap_fast_a_id_info = os_strdup(pos);
2111         } else if (os_strcmp(buf, "eap_fast_prov") == 0) {
2112                 bss->eap_fast_prov = atoi(pos);
2113         } else if (os_strcmp(buf, "pac_key_lifetime") == 0) {
2114                 bss->pac_key_lifetime = atoi(pos);
2115         } else if (os_strcmp(buf, "pac_key_refresh_time") == 0) {
2116                 bss->pac_key_refresh_time = atoi(pos);
2117 #endif /* EAP_SERVER_FAST */
2118 #ifdef EAP_SERVER_SIM
2119         } else if (os_strcmp(buf, "eap_sim_db") == 0) {
2120                 os_free(bss->eap_sim_db);
2121                 bss->eap_sim_db = os_strdup(pos);
2122         } else if (os_strcmp(buf, "eap_sim_aka_result_ind") == 0) {
2123                 bss->eap_sim_aka_result_ind = atoi(pos);
2124 #endif /* EAP_SERVER_SIM */
2125 #ifdef EAP_SERVER_TNC
2126         } else if (os_strcmp(buf, "tnc") == 0) {
2127                 bss->tnc = atoi(pos);
2128 #endif /* EAP_SERVER_TNC */
2129 #ifdef EAP_SERVER_PWD
2130         } else if (os_strcmp(buf, "pwd_group") == 0) {
2131                 bss->pwd_group = atoi(pos);
2132 #endif /* EAP_SERVER_PWD */
2133         } else if (os_strcmp(buf, "eap_server_erp") == 0) {
2134                 bss->eap_server_erp = atoi(pos);
2135 #endif /* EAP_SERVER */
2136         } else if (os_strcmp(buf, "eap_message") == 0) {
2137                 char *term;
2138                 os_free(bss->eap_req_id_text);
2139                 bss->eap_req_id_text = os_strdup(pos);
2140                 if (bss->eap_req_id_text == NULL) {
2141                         wpa_printf(MSG_ERROR, "Line %d: Failed to allocate memory for eap_req_id_text",
2142                                    line);
2143                         return 1;
2144                 }
2145                 bss->eap_req_id_text_len = os_strlen(bss->eap_req_id_text);
2146                 term = os_strstr(bss->eap_req_id_text, "\\0");
2147                 if (term) {
2148                         *term++ = '\0';
2149                         os_memmove(term, term + 1,
2150                                    bss->eap_req_id_text_len -
2151                                    (term - bss->eap_req_id_text) - 1);
2152                         bss->eap_req_id_text_len--;
2153                 }
2154         } else if (os_strcmp(buf, "erp_send_reauth_start") == 0) {
2155                 bss->erp_send_reauth_start = atoi(pos);
2156         } else if (os_strcmp(buf, "erp_domain") == 0) {
2157                 os_free(bss->erp_domain);
2158                 bss->erp_domain = os_strdup(pos);
2159         } else if (os_strcmp(buf, "wep_key_len_broadcast") == 0) {
2160                 bss->default_wep_key_len = atoi(pos);
2161                 if (bss->default_wep_key_len > 13) {
2162                         wpa_printf(MSG_ERROR, "Line %d: invalid WEP key len %lu (= %lu bits)",
2163                                    line,
2164                                    (unsigned long) bss->default_wep_key_len,
2165                                    (unsigned long)
2166                                    bss->default_wep_key_len * 8);
2167                         return 1;
2168                 }
2169         } else if (os_strcmp(buf, "wep_key_len_unicast") == 0) {
2170                 bss->individual_wep_key_len = atoi(pos);
2171                 if (bss->individual_wep_key_len < 0 ||
2172                     bss->individual_wep_key_len > 13) {
2173                         wpa_printf(MSG_ERROR, "Line %d: invalid WEP key len %d (= %d bits)",
2174                                    line, bss->individual_wep_key_len,
2175                                    bss->individual_wep_key_len * 8);
2176                         return 1;
2177                 }
2178         } else if (os_strcmp(buf, "wep_rekey_period") == 0) {
2179                 bss->wep_rekeying_period = atoi(pos);
2180                 if (bss->wep_rekeying_period < 0) {
2181                         wpa_printf(MSG_ERROR, "Line %d: invalid period %d",
2182                                    line, bss->wep_rekeying_period);
2183                         return 1;
2184                 }
2185         } else if (os_strcmp(buf, "eap_reauth_period") == 0) {
2186                 bss->eap_reauth_period = atoi(pos);
2187                 if (bss->eap_reauth_period < 0) {
2188                         wpa_printf(MSG_ERROR, "Line %d: invalid period %d",
2189                                    line, bss->eap_reauth_period);
2190                         return 1;
2191                 }
2192         } else if (os_strcmp(buf, "eapol_key_index_workaround") == 0) {
2193                 bss->eapol_key_index_workaround = atoi(pos);
2194 #ifdef CONFIG_IAPP
2195         } else if (os_strcmp(buf, "iapp_interface") == 0) {
2196                 bss->ieee802_11f = 1;
2197                 os_strlcpy(bss->iapp_iface, pos, sizeof(bss->iapp_iface));
2198 #endif /* CONFIG_IAPP */
2199         } else if (os_strcmp(buf, "own_ip_addr") == 0) {
2200                 if (hostapd_parse_ip_addr(pos, &bss->own_ip_addr)) {
2201                         wpa_printf(MSG_ERROR,
2202                                    "Line %d: invalid IP address '%s'",
2203                                    line, pos);
2204                         return 1;
2205                 }
2206         } else if (os_strcmp(buf, "nas_identifier") == 0) {
2207                 os_free(bss->nas_identifier);
2208                 bss->nas_identifier = os_strdup(pos);
2209 #ifndef CONFIG_NO_RADIUS
2210         } else if (os_strcmp(buf, "radius_client_addr") == 0) {
2211                 if (hostapd_parse_ip_addr(pos, &bss->radius->client_addr)) {
2212                         wpa_printf(MSG_ERROR,
2213                                    "Line %d: invalid IP address '%s'",
2214                                    line, pos);
2215                         return 1;
2216                 }
2217                 bss->radius->force_client_addr = 1;
2218         } else if (os_strcmp(buf, "auth_server_addr") == 0) {
2219                 if (hostapd_config_read_radius_addr(
2220                             &bss->radius->auth_servers,
2221                             &bss->radius->num_auth_servers, pos, 1812,
2222                             &bss->radius->auth_server)) {
2223                         wpa_printf(MSG_ERROR,
2224                                    "Line %d: invalid IP address '%s'",
2225                                    line, pos);
2226                         return 1;
2227                 }
2228         } else if (bss->radius->auth_server &&
2229                    os_strcmp(buf, "auth_server_addr_replace") == 0) {
2230                 if (hostapd_parse_ip_addr(pos,
2231                                           &bss->radius->auth_server->addr)) {
2232                         wpa_printf(MSG_ERROR,
2233                                    "Line %d: invalid IP address '%s'",
2234                                    line, pos);
2235                         return 1;
2236                 }
2237         } else if (bss->radius->auth_server &&
2238                    os_strcmp(buf, "auth_server_port") == 0) {
2239                 bss->radius->auth_server->port = atoi(pos);
2240         } else if (bss->radius->auth_server &&
2241                    os_strcmp(buf, "auth_server_shared_secret") == 0) {
2242                 int len = os_strlen(pos);
2243                 if (len == 0) {
2244                         /* RFC 2865, Ch. 3 */
2245                         wpa_printf(MSG_ERROR, "Line %d: empty shared secret is not allowed",
2246                                    line);
2247                         return 1;
2248                 }
2249                 os_free(bss->radius->auth_server->shared_secret);
2250                 bss->radius->auth_server->shared_secret = (u8 *) os_strdup(pos);
2251                 bss->radius->auth_server->shared_secret_len = len;
2252         } else if (os_strcmp(buf, "acct_server_addr") == 0) {
2253                 if (hostapd_config_read_radius_addr(
2254                             &bss->radius->acct_servers,
2255                             &bss->radius->num_acct_servers, pos, 1813,
2256                             &bss->radius->acct_server)) {
2257                         wpa_printf(MSG_ERROR,
2258                                    "Line %d: invalid IP address '%s'",
2259                                    line, pos);
2260                         return 1;
2261                 }
2262         } else if (bss->radius->acct_server &&
2263                    os_strcmp(buf, "acct_server_addr_replace") == 0) {
2264                 if (hostapd_parse_ip_addr(pos,
2265                                           &bss->radius->acct_server->addr)) {
2266                         wpa_printf(MSG_ERROR,
2267                                    "Line %d: invalid IP address '%s'",
2268                                    line, pos);
2269                         return 1;
2270                 }
2271         } else if (bss->radius->acct_server &&
2272                    os_strcmp(buf, "acct_server_port") == 0) {
2273                 bss->radius->acct_server->port = atoi(pos);
2274         } else if (bss->radius->acct_server &&
2275                    os_strcmp(buf, "acct_server_shared_secret") == 0) {
2276                 int len = os_strlen(pos);
2277                 if (len == 0) {
2278                         /* RFC 2865, Ch. 3 */
2279                         wpa_printf(MSG_ERROR, "Line %d: empty shared secret is not allowed",
2280                                    line);
2281                         return 1;
2282                 }
2283                 os_free(bss->radius->acct_server->shared_secret);
2284                 bss->radius->acct_server->shared_secret = (u8 *) os_strdup(pos);
2285                 bss->radius->acct_server->shared_secret_len = len;
2286         } else if (os_strcmp(buf, "radius_retry_primary_interval") == 0) {
2287                 bss->radius->retry_primary_interval = atoi(pos);
2288         } else if (os_strcmp(buf, "radius_acct_interim_interval") == 0) {
2289                 bss->acct_interim_interval = atoi(pos);
2290         } else if (os_strcmp(buf, "radius_request_cui") == 0) {
2291                 bss->radius_request_cui = atoi(pos);
2292         } else if (os_strcmp(buf, "radius_auth_req_attr") == 0) {
2293                 struct hostapd_radius_attr *attr, *a;
2294                 attr = hostapd_parse_radius_attr(pos);
2295                 if (attr == NULL) {
2296                         wpa_printf(MSG_ERROR,
2297                                    "Line %d: invalid radius_auth_req_attr",
2298                                    line);
2299                         return 1;
2300                 } else if (bss->radius_auth_req_attr == NULL) {
2301                         bss->radius_auth_req_attr = attr;
2302                 } else {
2303                         a = bss->radius_auth_req_attr;
2304                         while (a->next)
2305                                 a = a->next;
2306                         a->next = attr;
2307                 }
2308         } else if (os_strcmp(buf, "radius_acct_req_attr") == 0) {
2309                 struct hostapd_radius_attr *attr, *a;
2310                 attr = hostapd_parse_radius_attr(pos);
2311                 if (attr == NULL) {
2312                         wpa_printf(MSG_ERROR,
2313                                    "Line %d: invalid radius_acct_req_attr",
2314                                    line);
2315                         return 1;
2316                 } else if (bss->radius_acct_req_attr == NULL) {
2317                         bss->radius_acct_req_attr = attr;
2318                 } else {
2319                         a = bss->radius_acct_req_attr;
2320                         while (a->next)
2321                                 a = a->next;
2322                         a->next = attr;
2323                 }
2324         } else if (os_strcmp(buf, "radius_das_port") == 0) {
2325                 bss->radius_das_port = atoi(pos);
2326         } else if (os_strcmp(buf, "radius_das_client") == 0) {
2327                 if (hostapd_parse_das_client(bss, pos) < 0) {
2328                         wpa_printf(MSG_ERROR, "Line %d: invalid DAS client",
2329                                    line);
2330                         return 1;
2331                 }
2332         } else if (os_strcmp(buf, "radius_das_time_window") == 0) {
2333                 bss->radius_das_time_window = atoi(pos);
2334         } else if (os_strcmp(buf, "radius_das_require_event_timestamp") == 0) {
2335                 bss->radius_das_require_event_timestamp = atoi(pos);
2336 #endif /* CONFIG_NO_RADIUS */
2337         } else if (os_strcmp(buf, "auth_algs") == 0) {
2338                 bss->auth_algs = atoi(pos);
2339                 if (bss->auth_algs == 0) {
2340                         wpa_printf(MSG_ERROR, "Line %d: no authentication algorithms allowed",
2341                                    line);
2342                         return 1;
2343                 }
2344         } else if (os_strcmp(buf, "max_num_sta") == 0) {
2345                 bss->max_num_sta = atoi(pos);
2346                 if (bss->max_num_sta < 0 ||
2347                     bss->max_num_sta > MAX_STA_COUNT) {
2348                         wpa_printf(MSG_ERROR, "Line %d: Invalid max_num_sta=%d; allowed range 0..%d",
2349                                    line, bss->max_num_sta, MAX_STA_COUNT);
2350                         return 1;
2351                 }
2352         } else if (os_strcmp(buf, "wpa") == 0) {
2353                 bss->wpa = atoi(pos);
2354         } else if (os_strcmp(buf, "wpa_group_rekey") == 0) {
2355                 bss->wpa_group_rekey = atoi(pos);
2356         } else if (os_strcmp(buf, "wpa_strict_rekey") == 0) {
2357                 bss->wpa_strict_rekey = atoi(pos);
2358         } else if (os_strcmp(buf, "wpa_gmk_rekey") == 0) {
2359                 bss->wpa_gmk_rekey = atoi(pos);
2360         } else if (os_strcmp(buf, "wpa_ptk_rekey") == 0) {
2361                 bss->wpa_ptk_rekey = atoi(pos);
2362         } else if (os_strcmp(buf, "wpa_passphrase") == 0) {
2363                 int len = os_strlen(pos);
2364                 if (len < 8 || len > 63) {
2365                         wpa_printf(MSG_ERROR, "Line %d: invalid WPA passphrase length %d (expected 8..63)",
2366                                    line, len);
2367                         return 1;
2368                 }
2369                 os_free(bss->ssid.wpa_passphrase);
2370                 bss->ssid.wpa_passphrase = os_strdup(pos);
2371                 if (bss->ssid.wpa_passphrase) {
2372                         hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
2373                         bss->ssid.wpa_passphrase_set = 1;
2374                 }
2375         } else if (os_strcmp(buf, "wpa_psk") == 0) {
2376                 hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
2377                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
2378                 if (bss->ssid.wpa_psk == NULL)
2379                         return 1;
2380                 if (hexstr2bin(pos, bss->ssid.wpa_psk->psk, PMK_LEN) ||
2381                     pos[PMK_LEN * 2] != '\0') {
2382                         wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
2383                                    line, pos);
2384                         hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
2385                         return 1;
2386                 }
2387                 bss->ssid.wpa_psk->group = 1;
2388                 os_free(bss->ssid.wpa_passphrase);
2389                 bss->ssid.wpa_passphrase = NULL;
2390                 bss->ssid.wpa_psk_set = 1;
2391         } else if (os_strcmp(buf, "wpa_psk_file") == 0) {
2392                 os_free(bss->ssid.wpa_psk_file);
2393                 bss->ssid.wpa_psk_file = os_strdup(pos);
2394                 if (!bss->ssid.wpa_psk_file) {
2395                         wpa_printf(MSG_ERROR, "Line %d: allocation failed",
2396                                    line);
2397                         return 1;
2398                 }
2399         } else if (os_strcmp(buf, "wpa_key_mgmt") == 0) {
2400                 bss->wpa_key_mgmt = hostapd_config_parse_key_mgmt(line, pos);
2401                 if (bss->wpa_key_mgmt == -1)
2402                         return 1;
2403         } else if (os_strcmp(buf, "wpa_psk_radius") == 0) {
2404                 bss->wpa_psk_radius = atoi(pos);
2405                 if (bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
2406                     bss->wpa_psk_radius != PSK_RADIUS_ACCEPTED &&
2407                     bss->wpa_psk_radius != PSK_RADIUS_REQUIRED) {
2408                         wpa_printf(MSG_ERROR,
2409                                    "Line %d: unknown wpa_psk_radius %d",
2410                                    line, bss->wpa_psk_radius);
2411                         return 1;
2412                 }
2413         } else if (os_strcmp(buf, "wpa_pairwise") == 0) {
2414                 bss->wpa_pairwise = hostapd_config_parse_cipher(line, pos);
2415                 if (bss->wpa_pairwise == -1 || bss->wpa_pairwise == 0)
2416                         return 1;
2417                 if (bss->wpa_pairwise &
2418                     (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)) {
2419                         wpa_printf(MSG_ERROR, "Line %d: unsupported pairwise cipher suite '%s'",
2420                                    bss->wpa_pairwise, pos);
2421                         return 1;
2422                 }
2423         } else if (os_strcmp(buf, "rsn_pairwise") == 0) {
2424                 bss->rsn_pairwise = hostapd_config_parse_cipher(line, pos);
2425                 if (bss->rsn_pairwise == -1 || bss->rsn_pairwise == 0)
2426                         return 1;
2427                 if (bss->rsn_pairwise &
2428                     (WPA_CIPHER_NONE | WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)) {
2429                         wpa_printf(MSG_ERROR, "Line %d: unsupported pairwise cipher suite '%s'",
2430                                    bss->rsn_pairwise, pos);
2431                         return 1;
2432                 }
2433 #ifdef CONFIG_RSN_PREAUTH
2434         } else if (os_strcmp(buf, "rsn_preauth") == 0) {
2435                 bss->rsn_preauth = atoi(pos);
2436         } else if (os_strcmp(buf, "rsn_preauth_interfaces") == 0) {
2437                 os_free(bss->rsn_preauth_interfaces);
2438                 bss->rsn_preauth_interfaces = os_strdup(pos);
2439 #endif /* CONFIG_RSN_PREAUTH */
2440 #ifdef CONFIG_PEERKEY
2441         } else if (os_strcmp(buf, "peerkey") == 0) {
2442                 bss->peerkey = atoi(pos);
2443 #endif /* CONFIG_PEERKEY */
2444 #ifdef CONFIG_IEEE80211R
2445         } else if (os_strcmp(buf, "mobility_domain") == 0) {
2446                 if (os_strlen(pos) != 2 * MOBILITY_DOMAIN_ID_LEN ||
2447                     hexstr2bin(pos, bss->mobility_domain,
2448                                MOBILITY_DOMAIN_ID_LEN) != 0) {
2449                         wpa_printf(MSG_ERROR,
2450                                    "Line %d: Invalid mobility_domain '%s'",
2451                                    line, pos);
2452                         return 1;
2453                 }
2454         } else if (os_strcmp(buf, "r1_key_holder") == 0) {
2455                 if (os_strlen(pos) != 2 * FT_R1KH_ID_LEN ||
2456                     hexstr2bin(pos, bss->r1_key_holder, FT_R1KH_ID_LEN) != 0) {
2457                         wpa_printf(MSG_ERROR,
2458                                    "Line %d: Invalid r1_key_holder '%s'",
2459                                    line, pos);
2460                         return 1;
2461                 }
2462         } else if (os_strcmp(buf, "r0_key_lifetime") == 0) {
2463                 bss->r0_key_lifetime = atoi(pos);
2464         } else if (os_strcmp(buf, "reassociation_deadline") == 0) {
2465                 bss->reassociation_deadline = atoi(pos);
2466         } else if (os_strcmp(buf, "r0kh") == 0) {
2467                 if (add_r0kh(bss, pos) < 0) {
2468                         wpa_printf(MSG_DEBUG, "Line %d: Invalid r0kh '%s'",
2469                                    line, pos);
2470                         return 1;
2471                 }
2472         } else if (os_strcmp(buf, "r1kh") == 0) {
2473                 if (add_r1kh(bss, pos) < 0) {
2474                         wpa_printf(MSG_DEBUG, "Line %d: Invalid r1kh '%s'",
2475                                    line, pos);
2476                         return 1;
2477                 }
2478         } else if (os_strcmp(buf, "pmk_r1_push") == 0) {
2479                 bss->pmk_r1_push = atoi(pos);
2480         } else if (os_strcmp(buf, "ft_over_ds") == 0) {
2481                 bss->ft_over_ds = atoi(pos);
2482 #endif /* CONFIG_IEEE80211R */
2483 #ifndef CONFIG_NO_CTRL_IFACE
2484         } else if (os_strcmp(buf, "ctrl_interface") == 0) {
2485                 os_free(bss->ctrl_interface);
2486                 bss->ctrl_interface = os_strdup(pos);
2487         } else if (os_strcmp(buf, "ctrl_interface_group") == 0) {
2488 #ifndef CONFIG_NATIVE_WINDOWS
2489                 struct group *grp;
2490                 char *endp;
2491                 const char *group = pos;
2492
2493                 grp = getgrnam(group);
2494                 if (grp) {
2495                         bss->ctrl_interface_gid = grp->gr_gid;
2496                         bss->ctrl_interface_gid_set = 1;
2497                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d (from group name '%s')",
2498                                    bss->ctrl_interface_gid, group);
2499                         return 0;
2500                 }
2501
2502                 /* Group name not found - try to parse this as gid */
2503                 bss->ctrl_interface_gid = strtol(group, &endp, 10);
2504                 if (*group == '\0' || *endp != '\0') {
2505                         wpa_printf(MSG_DEBUG, "Line %d: Invalid group '%s'",
2506                                    line, group);
2507                         return 1;
2508                 }
2509                 bss->ctrl_interface_gid_set = 1;
2510                 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
2511                            bss->ctrl_interface_gid);
2512 #endif /* CONFIG_NATIVE_WINDOWS */
2513 #endif /* CONFIG_NO_CTRL_IFACE */
2514 #ifdef RADIUS_SERVER
2515         } else if (os_strcmp(buf, "radius_server_clients") == 0) {
2516                 os_free(bss->radius_server_clients);
2517                 bss->radius_server_clients = os_strdup(pos);
2518         } else if (os_strcmp(buf, "radius_server_auth_port") == 0) {
2519                 bss->radius_server_auth_port = atoi(pos);
2520         } else if (os_strcmp(buf, "radius_server_acct_port") == 0) {
2521                 bss->radius_server_acct_port = atoi(pos);
2522         } else if (os_strcmp(buf, "radius_server_ipv6") == 0) {
2523                 bss->radius_server_ipv6 = atoi(pos);
2524 #endif /* RADIUS_SERVER */
2525         } else if (os_strcmp(buf, "use_pae_group_addr") == 0) {
2526                 bss->use_pae_group_addr = atoi(pos);
2527         } else if (os_strcmp(buf, "hw_mode") == 0) {
2528                 if (os_strcmp(pos, "a") == 0)
2529                         conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
2530                 else if (os_strcmp(pos, "b") == 0)
2531                         conf->hw_mode = HOSTAPD_MODE_IEEE80211B;
2532                 else if (os_strcmp(pos, "g") == 0)
2533                         conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
2534                 else if (os_strcmp(pos, "ad") == 0)
2535                         conf->hw_mode = HOSTAPD_MODE_IEEE80211AD;
2536                 else {
2537                         wpa_printf(MSG_ERROR, "Line %d: unknown hw_mode '%s'",
2538                                    line, pos);
2539                         return 1;
2540                 }
2541         } else if (os_strcmp(buf, "wps_rf_bands") == 0) {
2542                 if (os_strcmp(pos, "a") == 0)
2543                         bss->wps_rf_bands = WPS_RF_50GHZ;
2544                 else if (os_strcmp(pos, "g") == 0 ||
2545                          os_strcmp(pos, "b") == 0)
2546                         bss->wps_rf_bands = WPS_RF_24GHZ;
2547                 else if (os_strcmp(pos, "ag") == 0 ||
2548                          os_strcmp(pos, "ga") == 0)
2549                         bss->wps_rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
2550                 else {
2551                         wpa_printf(MSG_ERROR,
2552                                    "Line %d: unknown wps_rf_band '%s'",
2553                                    line, pos);
2554                         return 1;
2555                 }
2556         } else if (os_strcmp(buf, "channel") == 0) {
2557                 if (os_strcmp(pos, "acs_survey") == 0) {
2558 #ifndef CONFIG_ACS
2559                         wpa_printf(MSG_ERROR, "Line %d: tries to enable ACS but CONFIG_ACS disabled",
2560                                    line);
2561                         return 1;
2562 #else /* CONFIG_ACS */
2563                         conf->acs = 1;
2564                         conf->channel = 0;
2565 #endif /* CONFIG_ACS */
2566                 } else {
2567                         conf->channel = atoi(pos);
2568                         conf->acs = conf->channel == 0;
2569                 }
2570         } else if (os_strcmp(buf, "chanlist") == 0) {
2571                 if (hostapd_parse_chanlist(conf, pos)) {
2572                         wpa_printf(MSG_ERROR, "Line %d: invalid channel list",
2573                                    line);
2574                         return 1;
2575                 }
2576         } else if (os_strcmp(buf, "beacon_int") == 0) {
2577                 int val = atoi(pos);
2578                 /* MIB defines range as 1..65535, but very small values
2579                  * cause problems with the current implementation.
2580                  * Since it is unlikely that this small numbers are
2581                  * useful in real life scenarios, do not allow beacon
2582                  * period to be set below 15 TU. */
2583                 if (val < 15 || val > 65535) {
2584                         wpa_printf(MSG_ERROR, "Line %d: invalid beacon_int %d (expected 15..65535)",
2585                                    line, val);
2586                         return 1;
2587                 }
2588                 conf->beacon_int = val;
2589 #ifdef CONFIG_ACS
2590         } else if (os_strcmp(buf, "acs_num_scans") == 0) {
2591                 int val = atoi(pos);
2592                 if (val <= 0 || val > 100) {
2593                         wpa_printf(MSG_ERROR, "Line %d: invalid acs_num_scans %d (expected 1..100)",
2594                                    line, val);
2595                         return 1;
2596                 }
2597                 conf->acs_num_scans = val;
2598         } else if (os_strcmp(buf, "acs_chan_bias") == 0) {
2599                 if (hostapd_config_parse_acs_chan_bias(conf, pos)) {
2600                         wpa_printf(MSG_ERROR, "Line %d: invalid acs_chan_bias",
2601                                    line);
2602                         return -1;
2603                 }
2604 #endif /* CONFIG_ACS */
2605         } else if (os_strcmp(buf, "dtim_period") == 0) {
2606                 bss->dtim_period = atoi(pos);
2607                 if (bss->dtim_period < 1 || bss->dtim_period > 255) {
2608                         wpa_printf(MSG_ERROR, "Line %d: invalid dtim_period %d",
2609                                    line, bss->dtim_period);
2610                         return 1;
2611                 }
2612         } else if (os_strcmp(buf, "bss_load_update_period") == 0) {
2613                 bss->bss_load_update_period = atoi(pos);
2614                 if (bss->bss_load_update_period < 0 ||
2615                     bss->bss_load_update_period > 100) {
2616                         wpa_printf(MSG_ERROR,
2617                                    "Line %d: invalid bss_load_update_period %d",
2618                                    line, bss->bss_load_update_period);
2619                         return 1;
2620                 }
2621         } else if (os_strcmp(buf, "rts_threshold") == 0) {
2622                 conf->rts_threshold = atoi(pos);
2623                 if (conf->rts_threshold < 0 || conf->rts_threshold > 2347) {
2624                         wpa_printf(MSG_ERROR,
2625                                    "Line %d: invalid rts_threshold %d",
2626                                    line, conf->rts_threshold);
2627                         return 1;
2628                 }
2629         } else if (os_strcmp(buf, "fragm_threshold") == 0) {
2630                 conf->fragm_threshold = atoi(pos);
2631                 if (conf->fragm_threshold < 256 ||
2632                     conf->fragm_threshold > 2346) {
2633                         wpa_printf(MSG_ERROR,
2634                                    "Line %d: invalid fragm_threshold %d",
2635                                    line, conf->fragm_threshold);
2636                         return 1;
2637                 }
2638         } else if (os_strcmp(buf, "send_probe_response") == 0) {
2639                 int val = atoi(pos);
2640                 if (val != 0 && val != 1) {
2641                         wpa_printf(MSG_ERROR, "Line %d: invalid send_probe_response %d (expected 0 or 1)",
2642                                    line, val);
2643                         return 1;
2644                 }
2645                 conf->send_probe_response = val;
2646         } else if (os_strcmp(buf, "supported_rates") == 0) {
2647                 if (hostapd_parse_intlist(&conf->supported_rates, pos)) {
2648                         wpa_printf(MSG_ERROR, "Line %d: invalid rate list",
2649                                    line);
2650                         return 1;
2651                 }
2652         } else if (os_strcmp(buf, "basic_rates") == 0) {
2653                 if (hostapd_parse_intlist(&conf->basic_rates, pos)) {
2654                         wpa_printf(MSG_ERROR, "Line %d: invalid rate list",
2655                                    line);
2656                         return 1;
2657                 }
2658         } else if (os_strcmp(buf, "preamble") == 0) {
2659                 if (atoi(pos))
2660                         conf->preamble = SHORT_PREAMBLE;
2661                 else
2662                         conf->preamble = LONG_PREAMBLE;
2663         } else if (os_strcmp(buf, "ignore_broadcast_ssid") == 0) {
2664                 bss->ignore_broadcast_ssid = atoi(pos);
2665         } else if (os_strcmp(buf, "wep_default_key") == 0) {
2666                 bss->ssid.wep.idx = atoi(pos);
2667                 if (bss->ssid.wep.idx > 3) {
2668                         wpa_printf(MSG_ERROR,
2669                                    "Invalid wep_default_key index %d",
2670                                    bss->ssid.wep.idx);
2671                         return 1;
2672                 }
2673         } else if (os_strcmp(buf, "wep_key0") == 0 ||
2674                    os_strcmp(buf, "wep_key1") == 0 ||
2675                    os_strcmp(buf, "wep_key2") == 0 ||
2676                    os_strcmp(buf, "wep_key3") == 0) {
2677                 if (hostapd_config_read_wep(&bss->ssid.wep,
2678                                             buf[7] - '0', pos)) {
2679                         wpa_printf(MSG_ERROR, "Line %d: invalid WEP key '%s'",
2680                                    line, buf);
2681                         return 1;
2682                 }
2683 #ifndef CONFIG_NO_VLAN
2684         } else if (os_strcmp(buf, "dynamic_vlan") == 0) {
2685                 bss->ssid.dynamic_vlan = atoi(pos);
2686         } else if (os_strcmp(buf, "vlan_file") == 0) {
2687                 if (hostapd_config_read_vlan_file(bss, pos)) {
2688                         wpa_printf(MSG_ERROR, "Line %d: failed to read VLAN file '%s'",
2689                                    line, pos);
2690                         return 1;
2691                 }
2692         } else if (os_strcmp(buf, "vlan_naming") == 0) {
2693                 bss->ssid.vlan_naming = atoi(pos);
2694                 if (bss->ssid.vlan_naming >= DYNAMIC_VLAN_NAMING_END ||
2695                     bss->ssid.vlan_naming < 0) {
2696                         wpa_printf(MSG_ERROR,
2697                                    "Line %d: invalid naming scheme %d",
2698                                    line, bss->ssid.vlan_naming);
2699                         return 1;
2700                 }
2701 #ifdef CONFIG_FULL_DYNAMIC_VLAN
2702         } else if (os_strcmp(buf, "vlan_tagged_interface") == 0) {
2703                 os_free(bss->ssid.vlan_tagged_interface);
2704                 bss->ssid.vlan_tagged_interface = os_strdup(pos);
2705 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
2706 #endif /* CONFIG_NO_VLAN */
2707         } else if (os_strcmp(buf, "ap_table_max_size") == 0) {
2708                 conf->ap_table_max_size = atoi(pos);
2709         } else if (os_strcmp(buf, "ap_table_expiration_time") == 0) {
2710                 conf->ap_table_expiration_time = atoi(pos);
2711         } else if (os_strncmp(buf, "tx_queue_", 9) == 0) {
2712                 if (hostapd_config_tx_queue(conf, buf, pos)) {
2713                         wpa_printf(MSG_ERROR, "Line %d: invalid TX queue item",
2714                                    line);
2715                         return 1;
2716                 }
2717         } else if (os_strcmp(buf, "wme_enabled") == 0 ||
2718                    os_strcmp(buf, "wmm_enabled") == 0) {
2719                 bss->wmm_enabled = atoi(pos);
2720         } else if (os_strcmp(buf, "uapsd_advertisement_enabled") == 0) {
2721                 bss->wmm_uapsd = atoi(pos);
2722         } else if (os_strncmp(buf, "wme_ac_", 7) == 0 ||
2723                    os_strncmp(buf, "wmm_ac_", 7) == 0) {
2724                 if (hostapd_config_wmm_ac(conf->wmm_ac_params, buf, pos)) {
2725                         wpa_printf(MSG_ERROR, "Line %d: invalid WMM ac item",
2726                                    line);
2727                         return 1;
2728                 }
2729         } else if (os_strcmp(buf, "bss") == 0) {
2730                 if (hostapd_config_bss(conf, pos)) {
2731                         wpa_printf(MSG_ERROR, "Line %d: invalid bss item",
2732                                    line);
2733                         return 1;
2734                 }
2735         } else if (os_strcmp(buf, "bssid") == 0) {
2736                 if (hwaddr_aton(pos, bss->bssid)) {
2737                         wpa_printf(MSG_ERROR, "Line %d: invalid bssid item",
2738                                    line);
2739                         return 1;
2740                 }
2741 #ifdef CONFIG_IEEE80211W
2742         } else if (os_strcmp(buf, "ieee80211w") == 0) {
2743                 bss->ieee80211w = atoi(pos);
2744         } else if (os_strcmp(buf, "group_mgmt_cipher") == 0) {
2745                 if (os_strcmp(pos, "AES-128-CMAC") == 0) {
2746                         bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
2747                 } else if (os_strcmp(pos, "BIP-GMAC-128") == 0) {
2748                         bss->group_mgmt_cipher = WPA_CIPHER_BIP_GMAC_128;
2749                 } else if (os_strcmp(pos, "BIP-GMAC-256") == 0) {
2750                         bss->group_mgmt_cipher = WPA_CIPHER_BIP_GMAC_256;
2751                 } else if (os_strcmp(pos, "BIP-CMAC-256") == 0) {
2752                         bss->group_mgmt_cipher = WPA_CIPHER_BIP_CMAC_256;
2753                 } else {
2754                         wpa_printf(MSG_ERROR, "Line %d: invalid group_mgmt_cipher: %s",
2755                                    line, pos);
2756                         return 1;
2757                 }
2758         } else if (os_strcmp(buf, "assoc_sa_query_max_timeout") == 0) {
2759                 bss->assoc_sa_query_max_timeout = atoi(pos);
2760                 if (bss->assoc_sa_query_max_timeout == 0) {
2761                         wpa_printf(MSG_ERROR, "Line %d: invalid assoc_sa_query_max_timeout",
2762                                    line);
2763                         return 1;
2764                 }
2765         } else if (os_strcmp(buf, "assoc_sa_query_retry_timeout") == 0) {
2766                 bss->assoc_sa_query_retry_timeout = atoi(pos);
2767                 if (bss->assoc_sa_query_retry_timeout == 0) {
2768                         wpa_printf(MSG_ERROR, "Line %d: invalid assoc_sa_query_retry_timeout",
2769                                    line);
2770                         return 1;
2771                 }
2772 #endif /* CONFIG_IEEE80211W */
2773 #ifdef CONFIG_IEEE80211N
2774         } else if (os_strcmp(buf, "ieee80211n") == 0) {
2775                 conf->ieee80211n = atoi(pos);
2776         } else if (os_strcmp(buf, "ht_capab") == 0) {
2777                 if (hostapd_config_ht_capab(conf, pos) < 0) {
2778                         wpa_printf(MSG_ERROR, "Line %d: invalid ht_capab",
2779                                    line);
2780                         return 1;
2781                 }
2782         } else if (os_strcmp(buf, "require_ht") == 0) {
2783                 conf->require_ht = atoi(pos);
2784         } else if (os_strcmp(buf, "obss_interval") == 0) {
2785                 conf->obss_interval = atoi(pos);
2786 #endif /* CONFIG_IEEE80211N */
2787 #ifdef CONFIG_IEEE80211AC
2788         } else if (os_strcmp(buf, "ieee80211ac") == 0) {
2789                 conf->ieee80211ac = atoi(pos);
2790         } else if (os_strcmp(buf, "vht_capab") == 0) {
2791                 if (hostapd_config_vht_capab(conf, pos) < 0) {
2792                         wpa_printf(MSG_ERROR, "Line %d: invalid vht_capab",
2793                                    line);
2794                         return 1;
2795                 }
2796         } else if (os_strcmp(buf, "require_vht") == 0) {
2797                 conf->require_vht = atoi(pos);
2798         } else if (os_strcmp(buf, "vht_oper_chwidth") == 0) {
2799                 conf->vht_oper_chwidth = atoi(pos);
2800         } else if (os_strcmp(buf, "vht_oper_centr_freq_seg0_idx") == 0) {
2801                 conf->vht_oper_centr_freq_seg0_idx = atoi(pos);
2802         } else if (os_strcmp(buf, "vht_oper_centr_freq_seg1_idx") == 0) {
2803                 conf->vht_oper_centr_freq_seg1_idx = atoi(pos);
2804         } else if (os_strcmp(buf, "vendor_vht") == 0) {
2805                 bss->vendor_vht = atoi(pos);
2806 #endif /* CONFIG_IEEE80211AC */
2807         } else if (os_strcmp(buf, "max_listen_interval") == 0) {
2808                 bss->max_listen_interval = atoi(pos);
2809         } else if (os_strcmp(buf, "disable_pmksa_caching") == 0) {
2810                 bss->disable_pmksa_caching = atoi(pos);
2811         } else if (os_strcmp(buf, "okc") == 0) {
2812                 bss->okc = atoi(pos);
2813 #ifdef CONFIG_WPS
2814         } else if (os_strcmp(buf, "wps_state") == 0) {
2815                 bss->wps_state = atoi(pos);
2816                 if (bss->wps_state < 0 || bss->wps_state > 2) {
2817                         wpa_printf(MSG_ERROR, "Line %d: invalid wps_state",
2818                                    line);
2819                         return 1;
2820                 }
2821         } else if (os_strcmp(buf, "wps_independent") == 0) {
2822                 bss->wps_independent = atoi(pos);
2823         } else if (os_strcmp(buf, "ap_setup_locked") == 0) {
2824                 bss->ap_setup_locked = atoi(pos);
2825         } else if (os_strcmp(buf, "uuid") == 0) {
2826                 if (uuid_str2bin(pos, bss->uuid)) {
2827                         wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2828                         return 1;
2829                 }
2830         } else if (os_strcmp(buf, "wps_pin_requests") == 0) {
2831                 os_free(bss->wps_pin_requests);
2832                 bss->wps_pin_requests = os_strdup(pos);
2833         } else if (os_strcmp(buf, "device_name") == 0) {
2834                 if (os_strlen(pos) > 32) {
2835                         wpa_printf(MSG_ERROR, "Line %d: Too long "
2836                                    "device_name", line);
2837                         return 1;
2838                 }
2839                 os_free(bss->device_name);
2840                 bss->device_name = os_strdup(pos);
2841         } else if (os_strcmp(buf, "manufacturer") == 0) {
2842                 if (os_strlen(pos) > 64) {
2843                         wpa_printf(MSG_ERROR, "Line %d: Too long manufacturer",
2844                                    line);
2845                         return 1;
2846                 }
2847                 os_free(bss->manufacturer);
2848                 bss->manufacturer = os_strdup(pos);
2849         } else if (os_strcmp(buf, "model_name") == 0) {
2850                 if (os_strlen(pos) > 32) {
2851                         wpa_printf(MSG_ERROR, "Line %d: Too long model_name",
2852                                    line);
2853                         return 1;
2854                 }
2855                 os_free(bss->model_name);
2856                 bss->model_name = os_strdup(pos);
2857         } else if (os_strcmp(buf, "model_number") == 0) {
2858                 if (os_strlen(pos) > 32) {
2859                         wpa_printf(MSG_ERROR, "Line %d: Too long model_number",
2860                                    line);
2861                         return 1;
2862                 }
2863                 os_free(bss->model_number);
2864                 bss->model_number = os_strdup(pos);
2865         } else if (os_strcmp(buf, "serial_number") == 0) {
2866                 if (os_strlen(pos) > 32) {
2867                         wpa_printf(MSG_ERROR, "Line %d: Too long serial_number",
2868                                    line);
2869                         return 1;
2870                 }
2871                 os_free(bss->serial_number);
2872                 bss->serial_number = os_strdup(pos);
2873         } else if (os_strcmp(buf, "device_type") == 0) {
2874                 if (wps_dev_type_str2bin(pos, bss->device_type))
2875                         return 1;
2876         } else if (os_strcmp(buf, "config_methods") == 0) {
2877                 os_free(bss->config_methods);
2878                 bss->config_methods = os_strdup(pos);
2879         } else if (os_strcmp(buf, "os_version") == 0) {
2880                 if (hexstr2bin(pos, bss->os_version, 4)) {
2881                         wpa_printf(MSG_ERROR, "Line %d: invalid os_version",
2882                                    line);
2883                         return 1;
2884                 }
2885         } else if (os_strcmp(buf, "ap_pin") == 0) {
2886                 os_free(bss->ap_pin);
2887                 bss->ap_pin = os_strdup(pos);
2888         } else if (os_strcmp(buf, "skip_cred_build") == 0) {
2889                 bss->skip_cred_build = atoi(pos);
2890         } else if (os_strcmp(buf, "extra_cred") == 0) {
2891                 os_free(bss->extra_cred);
2892                 bss->extra_cred = (u8 *) os_readfile(pos, &bss->extra_cred_len);
2893                 if (bss->extra_cred == NULL) {
2894                         wpa_printf(MSG_ERROR, "Line %d: could not read Credentials from '%s'",
2895                                    line, pos);
2896                         return 1;
2897                 }
2898         } else if (os_strcmp(buf, "wps_cred_processing") == 0) {
2899                 bss->wps_cred_processing = atoi(pos);
2900         } else if (os_strcmp(buf, "ap_settings") == 0) {
2901                 os_free(bss->ap_settings);
2902                 bss->ap_settings =
2903                         (u8 *) os_readfile(pos, &bss->ap_settings_len);
2904                 if (bss->ap_settings == NULL) {
2905                         wpa_printf(MSG_ERROR, "Line %d: could not read AP Settings from '%s'",
2906                                    line, pos);
2907                         return 1;
2908                 }
2909         } else if (os_strcmp(buf, "upnp_iface") == 0) {
2910                 os_free(bss->upnp_iface);
2911                 bss->upnp_iface = os_strdup(pos);
2912         } else if (os_strcmp(buf, "friendly_name") == 0) {
2913                 os_free(bss->friendly_name);
2914                 bss->friendly_name = os_strdup(pos);
2915         } else if (os_strcmp(buf, "manufacturer_url") == 0) {
2916                 os_free(bss->manufacturer_url);
2917                 bss->manufacturer_url = os_strdup(pos);
2918         } else if (os_strcmp(buf, "model_description") == 0) {
2919                 os_free(bss->model_description);
2920                 bss->model_description = os_strdup(pos);
2921         } else if (os_strcmp(buf, "model_url") == 0) {
2922                 os_free(bss->model_url);
2923                 bss->model_url = os_strdup(pos);
2924         } else if (os_strcmp(buf, "upc") == 0) {
2925                 os_free(bss->upc);
2926                 bss->upc = os_strdup(pos);
2927         } else if (os_strcmp(buf, "pbc_in_m1") == 0) {
2928                 bss->pbc_in_m1 = atoi(pos);
2929         } else if (os_strcmp(buf, "server_id") == 0) {
2930                 os_free(bss->server_id);
2931                 bss->server_id = os_strdup(pos);
2932 #ifdef CONFIG_WPS_NFC
2933         } else if (os_strcmp(buf, "wps_nfc_dev_pw_id") == 0) {
2934                 bss->wps_nfc_dev_pw_id = atoi(pos);
2935                 if (bss->wps_nfc_dev_pw_id < 0x10 ||
2936                     bss->wps_nfc_dev_pw_id > 0xffff) {
2937                         wpa_printf(MSG_ERROR, "Line %d: Invalid wps_nfc_dev_pw_id value",
2938                                    line);
2939                         return 1;
2940                 }
2941                 bss->wps_nfc_pw_from_config = 1;
2942         } else if (os_strcmp(buf, "wps_nfc_dh_pubkey") == 0) {
2943                 wpabuf_free(bss->wps_nfc_dh_pubkey);
2944                 bss->wps_nfc_dh_pubkey = hostapd_parse_bin(pos);
2945                 bss->wps_nfc_pw_from_config = 1;
2946         } else if (os_strcmp(buf, "wps_nfc_dh_privkey") == 0) {
2947                 wpabuf_free(bss->wps_nfc_dh_privkey);
2948                 bss->wps_nfc_dh_privkey = hostapd_parse_bin(pos);
2949                 bss->wps_nfc_pw_from_config = 1;
2950         } else if (os_strcmp(buf, "wps_nfc_dev_pw") == 0) {
2951                 wpabuf_free(bss->wps_nfc_dev_pw);
2952                 bss->wps_nfc_dev_pw = hostapd_parse_bin(pos);
2953                 bss->wps_nfc_pw_from_config = 1;
2954 #endif /* CONFIG_WPS_NFC */
2955 #endif /* CONFIG_WPS */
2956 #ifdef CONFIG_P2P_MANAGER
2957         } else if (os_strcmp(buf, "manage_p2p") == 0) {
2958                 if (atoi(pos))
2959                         bss->p2p |= P2P_MANAGE;
2960                 else
2961                         bss->p2p &= ~P2P_MANAGE;
2962         } else if (os_strcmp(buf, "allow_cross_connection") == 0) {
2963                 if (atoi(pos))
2964                         bss->p2p |= P2P_ALLOW_CROSS_CONNECTION;
2965                 else
2966                         bss->p2p &= ~P2P_ALLOW_CROSS_CONNECTION;
2967 #endif /* CONFIG_P2P_MANAGER */
2968         } else if (os_strcmp(buf, "disassoc_low_ack") == 0) {
2969                 bss->disassoc_low_ack = atoi(pos);
2970         } else if (os_strcmp(buf, "tdls_prohibit") == 0) {
2971                 if (atoi(pos))
2972                         bss->tdls |= TDLS_PROHIBIT;
2973                 else
2974                         bss->tdls &= ~TDLS_PROHIBIT;
2975         } else if (os_strcmp(buf, "tdls_prohibit_chan_switch") == 0) {
2976                 if (atoi(pos))
2977                         bss->tdls |= TDLS_PROHIBIT_CHAN_SWITCH;
2978                 else
2979                         bss->tdls &= ~TDLS_PROHIBIT_CHAN_SWITCH;
2980 #ifdef CONFIG_RSN_TESTING
2981         } else if (os_strcmp(buf, "rsn_testing") == 0) {
2982                 extern int rsn_testing;
2983                 rsn_testing = atoi(pos);
2984 #endif /* CONFIG_RSN_TESTING */
2985         } else if (os_strcmp(buf, "time_advertisement") == 0) {
2986                 bss->time_advertisement = atoi(pos);
2987         } else if (os_strcmp(buf, "time_zone") == 0) {
2988                 size_t tz_len = os_strlen(pos);
2989                 if (tz_len < 4 || tz_len > 255) {
2990                         wpa_printf(MSG_DEBUG, "Line %d: invalid time_zone",
2991                                    line);
2992                         return 1;
2993                 }
2994                 os_free(bss->time_zone);
2995                 bss->time_zone = os_strdup(pos);
2996                 if (bss->time_zone == NULL)
2997                         return 1;
2998 #ifdef CONFIG_WNM
2999         } else if (os_strcmp(buf, "wnm_sleep_mode") == 0) {
3000                 bss->wnm_sleep_mode = atoi(pos);
3001         } else if (os_strcmp(buf, "bss_transition") == 0) {
3002                 bss->bss_transition = atoi(pos);
3003 #endif /* CONFIG_WNM */
3004 #ifdef CONFIG_INTERWORKING
3005         } else if (os_strcmp(buf, "interworking") == 0) {
3006                 bss->interworking = atoi(pos);
3007         } else if (os_strcmp(buf, "access_network_type") == 0) {
3008                 bss->access_network_type = atoi(pos);
3009                 if (bss->access_network_type < 0 ||
3010                     bss->access_network_type > 15) {
3011                         wpa_printf(MSG_ERROR,
3012                                    "Line %d: invalid access_network_type",
3013                                    line);
3014                         return 1;
3015                 }
3016         } else if (os_strcmp(buf, "internet") == 0) {
3017                 bss->internet = atoi(pos);
3018         } else if (os_strcmp(buf, "asra") == 0) {
3019                 bss->asra = atoi(pos);
3020         } else if (os_strcmp(buf, "esr") == 0) {
3021                 bss->esr = atoi(pos);
3022         } else if (os_strcmp(buf, "uesa") == 0) {
3023                 bss->uesa = atoi(pos);
3024         } else if (os_strcmp(buf, "venue_group") == 0) {
3025                 bss->venue_group = atoi(pos);
3026                 bss->venue_info_set = 1;
3027         } else if (os_strcmp(buf, "venue_type") == 0) {
3028                 bss->venue_type = atoi(pos);
3029                 bss->venue_info_set = 1;
3030         } else if (os_strcmp(buf, "hessid") == 0) {
3031                 if (hwaddr_aton(pos, bss->hessid)) {
3032                         wpa_printf(MSG_ERROR, "Line %d: invalid hessid", line);
3033                         return 1;
3034                 }
3035         } else if (os_strcmp(buf, "roaming_consortium") == 0) {
3036                 if (parse_roaming_consortium(bss, pos, line) < 0)
3037                         return 1;
3038         } else if (os_strcmp(buf, "venue_name") == 0) {
3039                 if (parse_venue_name(bss, pos, line) < 0)
3040                         return 1;
3041         } else if (os_strcmp(buf, "network_auth_type") == 0) {
3042                 u8 auth_type;
3043                 u16 redirect_url_len;
3044                 if (hexstr2bin(pos, &auth_type, 1)) {
3045                         wpa_printf(MSG_ERROR,
3046                                    "Line %d: Invalid network_auth_type '%s'",
3047                                    line, pos);
3048                         return 1;
3049                 }
3050                 if (auth_type == 0 || auth_type == 2)
3051                         redirect_url_len = os_strlen(pos + 2);
3052                 else
3053                         redirect_url_len = 0;
3054                 os_free(bss->network_auth_type);
3055                 bss->network_auth_type = os_malloc(redirect_url_len + 3 + 1);
3056                 if (bss->network_auth_type == NULL)
3057                         return 1;
3058                 *bss->network_auth_type = auth_type;
3059                 WPA_PUT_LE16(bss->network_auth_type + 1, redirect_url_len);
3060                 if (redirect_url_len)
3061                         os_memcpy(bss->network_auth_type + 3, pos + 2,
3062                                   redirect_url_len);
3063                 bss->network_auth_type_len = 3 + redirect_url_len;
3064         } else if (os_strcmp(buf, "ipaddr_type_availability") == 0) {
3065                 if (hexstr2bin(pos, &bss->ipaddr_type_availability, 1)) {
3066                         wpa_printf(MSG_ERROR, "Line %d: Invalid ipaddr_type_availability '%s'",
3067                                    line, pos);
3068                         bss->ipaddr_type_configured = 0;
3069                         return 1;
3070                 }
3071                 bss->ipaddr_type_configured = 1;
3072         } else if (os_strcmp(buf, "domain_name") == 0) {
3073                 int j, num_domains, domain_len, domain_list_len = 0;
3074                 char *tok_start, *tok_prev;
3075                 u8 *domain_list, *domain_ptr;
3076
3077                 domain_list_len = os_strlen(pos) + 1;
3078                 domain_list = os_malloc(domain_list_len);
3079                 if (domain_list == NULL)
3080                         return 1;
3081
3082                 domain_ptr = domain_list;
3083                 tok_prev = pos;
3084                 num_domains = 1;
3085                 while ((tok_prev = os_strchr(tok_prev, ','))) {
3086                         num_domains++;
3087                         tok_prev++;
3088                 }
3089                 tok_prev = pos;
3090                 for (j = 0; j < num_domains; j++) {
3091                         tok_start = os_strchr(tok_prev, ',');
3092                         if (tok_start) {
3093                                 domain_len = tok_start - tok_prev;
3094                                 *domain_ptr = domain_len;
3095                                 os_memcpy(domain_ptr + 1, tok_prev, domain_len);
3096                                 domain_ptr += domain_len + 1;
3097                                 tok_prev = ++tok_start;
3098                         } else {
3099                                 domain_len = os_strlen(tok_prev);
3100                                 *domain_ptr = domain_len;
3101                                 os_memcpy(domain_ptr + 1, tok_prev, domain_len);
3102                                 domain_ptr += domain_len + 1;
3103                         }
3104                 }
3105
3106                 os_free(bss->domain_name);
3107                 bss->domain_name = domain_list;
3108                 bss->domain_name_len = domain_list_len;
3109         } else if (os_strcmp(buf, "anqp_3gpp_cell_net") == 0) {
3110                 if (parse_3gpp_cell_net(bss, pos, line) < 0)
3111                         return 1;
3112         } else if (os_strcmp(buf, "nai_realm") == 0) {
3113                 if (parse_nai_realm(bss, pos, line) < 0)
3114                         return 1;
3115         } else if (os_strcmp(buf, "gas_frag_limit") == 0) {
3116                 bss->gas_frag_limit = atoi(pos);
3117         } else if (os_strcmp(buf, "gas_comeback_delay") == 0) {
3118                 bss->gas_comeback_delay = atoi(pos);
3119         } else if (os_strcmp(buf, "qos_map_set") == 0) {
3120                 if (parse_qos_map_set(bss, pos, line) < 0)
3121                         return 1;
3122 #endif /* CONFIG_INTERWORKING */
3123 #ifdef CONFIG_RADIUS_TEST
3124         } else if (os_strcmp(buf, "dump_msk_file") == 0) {
3125                 os_free(bss->dump_msk_file);
3126                 bss->dump_msk_file = os_strdup(pos);
3127 #endif /* CONFIG_RADIUS_TEST */
3128 #ifdef CONFIG_HS20
3129         } else if (os_strcmp(buf, "hs20") == 0) {
3130                 bss->hs20 = atoi(pos);
3131         } else if (os_strcmp(buf, "disable_dgaf") == 0) {
3132                 bss->disable_dgaf = atoi(pos);
3133         } else if (os_strcmp(buf, "proxy_arp") == 0) {
3134                 bss->proxy_arp = atoi(pos);
3135         } else if (os_strcmp(buf, "osen") == 0) {
3136                 bss->osen = atoi(pos);
3137         } else if (os_strcmp(buf, "anqp_domain_id") == 0) {
3138                 bss->anqp_domain_id = atoi(pos);
3139         } else if (os_strcmp(buf, "hs20_deauth_req_timeout") == 0) {
3140                 bss->hs20_deauth_req_timeout = atoi(pos);
3141         } else if (os_strcmp(buf, "hs20_oper_friendly_name") == 0) {
3142                 if (hs20_parse_oper_friendly_name(bss, pos, line) < 0)
3143                         return 1;
3144         } else if (os_strcmp(buf, "hs20_wan_metrics") == 0) {
3145                 if (hs20_parse_wan_metrics(bss, pos, line) < 0)
3146                         return 1;
3147         } else if (os_strcmp(buf, "hs20_conn_capab") == 0) {
3148                 if (hs20_parse_conn_capab(bss, pos, line) < 0) {
3149                         return 1;
3150                 }
3151         } else if (os_strcmp(buf, "hs20_operating_class") == 0) {
3152                 u8 *oper_class;
3153                 size_t oper_class_len;
3154                 oper_class_len = os_strlen(pos);
3155                 if (oper_class_len < 2 || (oper_class_len & 0x01)) {
3156                         wpa_printf(MSG_ERROR,
3157                                    "Line %d: Invalid hs20_operating_class '%s'",
3158                                    line, pos);
3159                         return 1;
3160                 }
3161                 oper_class_len /= 2;
3162                 oper_class = os_malloc(oper_class_len);
3163                 if (oper_class == NULL)
3164                         return 1;
3165                 if (hexstr2bin(pos, oper_class, oper_class_len)) {
3166                         wpa_printf(MSG_ERROR,
3167                                    "Line %d: Invalid hs20_operating_class '%s'",
3168                                    line, pos);
3169                         os_free(oper_class);
3170                         return 1;
3171                 }
3172                 os_free(bss->hs20_operating_class);
3173                 bss->hs20_operating_class = oper_class;
3174                 bss->hs20_operating_class_len = oper_class_len;
3175         } else if (os_strcmp(buf, "hs20_icon") == 0) {
3176                 if (hs20_parse_icon(bss, pos) < 0) {
3177                         wpa_printf(MSG_ERROR, "Line %d: Invalid hs20_icon '%s'",
3178                                    line, pos);
3179                         return 1;
3180                 }
3181         } else if (os_strcmp(buf, "osu_ssid") == 0) {
3182                 if (hs20_parse_osu_ssid(bss, pos, line) < 0)
3183                         return 1;
3184         } else if (os_strcmp(buf, "osu_server_uri") == 0) {
3185                 if (hs20_parse_osu_server_uri(bss, pos, line) < 0)
3186                         return 1;
3187         } else if (os_strcmp(buf, "osu_friendly_name") == 0) {
3188                 if (hs20_parse_osu_friendly_name(bss, pos, line) < 0)
3189                         return 1;
3190         } else if (os_strcmp(buf, "osu_nai") == 0) {
3191                 if (hs20_parse_osu_nai(bss, pos, line) < 0)
3192                         return 1;
3193         } else if (os_strcmp(buf, "osu_method_list") == 0) {
3194                 if (hs20_parse_osu_method_list(bss, pos, line) < 0)
3195                         return 1;
3196         } else if (os_strcmp(buf, "osu_icon") == 0) {
3197                 if (hs20_parse_osu_icon(bss, pos, line) < 0)
3198                         return 1;
3199         } else if (os_strcmp(buf, "osu_service_desc") == 0) {
3200                 if (hs20_parse_osu_service_desc(bss, pos, line) < 0)
3201                         return 1;
3202         } else if (os_strcmp(buf, "subscr_remediation_url") == 0) {
3203                 os_free(bss->subscr_remediation_url);
3204                 bss->subscr_remediation_url = os_strdup(pos);
3205         } else if (os_strcmp(buf, "subscr_remediation_method") == 0) {
3206                 bss->subscr_remediation_method = atoi(pos);
3207 #endif /* CONFIG_HS20 */
3208 #ifdef CONFIG_TESTING_OPTIONS
3209 #define PARSE_TEST_PROBABILITY(_val)                            \
3210         } else if (os_strcmp(buf, #_val) == 0) {                \
3211                 char *end;                                      \
3212                                                                 \
3213                 conf->_val = strtod(pos, &end);                 \
3214                 if (*end || conf->_val < 0.0 ||                 \
3215                     conf->_val > 1.0) {                         \
3216                         wpa_printf(MSG_ERROR,                   \
3217                                    "Line %d: Invalid value '%s'", \
3218                                    line, pos);                  \
3219                         return 1;                               \
3220                 }
3221         PARSE_TEST_PROBABILITY(ignore_probe_probability)
3222         PARSE_TEST_PROBABILITY(ignore_auth_probability)
3223         PARSE_TEST_PROBABILITY(ignore_assoc_probability)
3224         PARSE_TEST_PROBABILITY(ignore_reassoc_probability)
3225         PARSE_TEST_PROBABILITY(corrupt_gtk_rekey_mic_probability)
3226         } else if (os_strcmp(buf, "bss_load_test") == 0) {
3227                 WPA_PUT_LE16(bss->bss_load_test, atoi(pos));
3228                 pos = os_strchr(pos, ':');
3229                 if (pos == NULL) {
3230                         wpa_printf(MSG_ERROR, "Line %d: Invalid bss_load_test",
3231                                    line);
3232                         return 1;
3233                 }
3234                 pos++;
3235                 bss->bss_load_test[2] = atoi(pos);
3236                 pos = os_strchr(pos, ':');
3237                 if (pos == NULL) {
3238                         wpa_printf(MSG_ERROR, "Line %d: Invalid bss_load_test",
3239                                    line);
3240                         return 1;
3241                 }
3242                 pos++;
3243                 WPA_PUT_LE16(&bss->bss_load_test[3], atoi(pos));
3244                 bss->bss_load_test_set = 1;
3245         } else if (os_strcmp(buf, "radio_measurements") == 0) {
3246                 bss->radio_measurements = atoi(pos);
3247 #endif /* CONFIG_TESTING_OPTIONS */
3248         } else if (os_strcmp(buf, "vendor_elements") == 0) {
3249                 struct wpabuf *elems;
3250                 size_t len = os_strlen(pos);
3251                 if (len & 0x01) {
3252                         wpa_printf(MSG_ERROR,
3253                                    "Line %d: Invalid vendor_elements '%s'",
3254                                    line, pos);
3255                         return 1;
3256                 }
3257                 len /= 2;
3258                 if (len == 0) {
3259                         wpabuf_free(bss->vendor_elements);
3260                         bss->vendor_elements = NULL;
3261                         return 0;
3262                 }
3263
3264                 elems = wpabuf_alloc(len);
3265                 if (elems == NULL)
3266                         return 1;
3267
3268                 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
3269                         wpabuf_free(elems);
3270                         wpa_printf(MSG_ERROR,
3271                                    "Line %d: Invalid vendor_elements '%s'",
3272                                    line, pos);
3273                         return 1;
3274                 }
3275
3276                 wpabuf_free(bss->vendor_elements);
3277                 bss->vendor_elements = elems;
3278         } else if (os_strcmp(buf, "sae_anti_clogging_threshold") == 0) {
3279                 bss->sae_anti_clogging_threshold = atoi(pos);
3280         } else if (os_strcmp(buf, "sae_groups") == 0) {
3281                 if (hostapd_parse_intlist(&bss->sae_groups, pos)) {
3282                         wpa_printf(MSG_ERROR,
3283                                    "Line %d: Invalid sae_groups value '%s'",
3284                                    line, pos);
3285                         return 1;
3286                 }
3287         } else if (os_strcmp(buf, "local_pwr_constraint") == 0) {
3288                 int val = atoi(pos);
3289                 if (val < 0 || val > 255) {
3290                         wpa_printf(MSG_ERROR, "Line %d: Invalid local_pwr_constraint %d (expected 0..255)",
3291                                    line, val);
3292                         return 1;
3293                 }
3294                 conf->local_pwr_constraint = val;
3295         } else if (os_strcmp(buf, "spectrum_mgmt_required") == 0) {
3296                 conf->spectrum_mgmt_required = atoi(pos);
3297         } else if (os_strcmp(buf, "wowlan_triggers") == 0) {
3298                 os_free(bss->wowlan_triggers);
3299                 bss->wowlan_triggers = os_strdup(pos);
3300         } else {
3301                 wpa_printf(MSG_ERROR,
3302                            "Line %d: unknown configuration item '%s'",
3303                            line, buf);
3304                 return 1;
3305         }
3306
3307         return 0;
3308 }
3309
3310
3311 /**
3312  * hostapd_config_read - Read and parse a configuration file
3313  * @fname: Configuration file name (including path, if needed)
3314  * Returns: Allocated configuration data structure
3315  */
3316 struct hostapd_config * hostapd_config_read(const char *fname)
3317 {
3318         struct hostapd_config *conf;
3319         FILE *f;
3320         char buf[512], *pos;
3321         int line = 0;
3322         int errors = 0;
3323         size_t i;
3324
3325         f = fopen(fname, "r");
3326         if (f == NULL) {
3327                 wpa_printf(MSG_ERROR, "Could not open configuration file '%s' "
3328                            "for reading.", fname);
3329                 return NULL;
3330         }
3331
3332         conf = hostapd_config_defaults();
3333         if (conf == NULL) {
3334                 fclose(f);
3335                 return NULL;
3336         }
3337
3338         /* set default driver based on configuration */
3339         conf->driver = wpa_drivers[0];
3340         if (conf->driver == NULL) {
3341                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
3342                 hostapd_config_free(conf);
3343                 fclose(f);
3344                 return NULL;
3345         }
3346
3347         conf->last_bss = conf->bss[0];
3348
3349         while (fgets(buf, sizeof(buf), f)) {
3350                 struct hostapd_bss_config *bss;
3351
3352                 bss = conf->last_bss;
3353                 line++;
3354
3355                 if (buf[0] == '#')
3356                         continue;
3357                 pos = buf;
3358                 while (*pos != '\0') {
3359                         if (*pos == '\n') {
3360                                 *pos = '\0';
3361                                 break;
3362                         }
3363                         pos++;
3364                 }
3365                 if (buf[0] == '\0')
3366                         continue;
3367
3368                 pos = os_strchr(buf, '=');
3369                 if (pos == NULL) {
3370                         wpa_printf(MSG_ERROR, "Line %d: invalid line '%s'",
3371                                    line, buf);
3372                         errors++;
3373                         continue;
3374                 }
3375                 *pos = '\0';
3376                 pos++;
3377                 errors += hostapd_config_fill(conf, bss, buf, pos, line);
3378         }
3379
3380         fclose(f);
3381
3382         for (i = 0; i < conf->num_bss; i++)
3383                 hostapd_set_security_params(conf->bss[i], 1);
3384
3385         if (hostapd_config_check(conf, 1))
3386                 errors++;
3387
3388 #ifndef WPA_IGNORE_CONFIG_ERRORS
3389         if (errors) {
3390                 wpa_printf(MSG_ERROR, "%d errors found in configuration file "
3391                            "'%s'", errors, fname);
3392                 hostapd_config_free(conf);
3393                 conf = NULL;
3394         }
3395 #endif /* WPA_IGNORE_CONFIG_ERRORS */
3396
3397         return conf;
3398 }
3399
3400
3401 int hostapd_set_iface(struct hostapd_config *conf,
3402                       struct hostapd_bss_config *bss, char *field, char *value)
3403 {
3404         int errors;
3405         size_t i;
3406
3407         errors = hostapd_config_fill(conf, bss, field, value, 0);
3408         if (errors) {
3409                 wpa_printf(MSG_INFO, "Failed to set configuration field '%s' "
3410                            "to value '%s'", field, value);
3411                 return -1;
3412         }
3413
3414         for (i = 0; i < conf->num_bss; i++)
3415                 hostapd_set_security_params(conf->bss[i], 0);
3416
3417         if (hostapd_config_check(conf, 0)) {
3418                 wpa_printf(MSG_ERROR, "Configuration check failed");
3419                 return -1;
3420         }
3421
3422         return 0;
3423 }