Remove shadowed variable
[libeap.git] / hostapd / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "eloop.h"
18 #include "hostapd.h"
19 #include "ieee802_1x.h"
20 #include "beacon.h"
21 #include "hw_features.h"
22 #include "accounting.h"
23 #include "eapol_sm.h"
24 #include "iapp.h"
25 #include "ieee802_11_defs.h"
26 #include "ieee802_11_auth.h"
27 #include "sta_info.h"
28 #include "ap_list.h"
29 #include "driver_i.h"
30 #include "radius/radius_client.h"
31 #include "radius/radius_server.h"
32 #include "wpa.h"
33 #include "preauth.h"
34 #include "vlan_init.h"
35 #include "ctrl_iface.h"
36 #include "tls.h"
37 #include "eap_server/eap_sim_db.h"
38 #include "eap_server/eap.h"
39 #include "eap_server/tncs.h"
40 #include "version.h"
41 #include "l2_packet/l2_packet.h"
42 #include "wps_hostapd.h"
43 #include "tkip_countermeasures.h"
44
45
46 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
47                                        size_t identity_len, int phase2,
48                                        struct eap_user *user);
49 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
50 static int hostapd_setup_wpa(struct hostapd_data *hapd);
51 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
52
53 extern int wpa_debug_level;
54
55
56 #ifdef EAP_SERVER
57 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
58                                  struct sta_info *sta, void *ctx)
59 {
60         if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
61                 return 1;
62         return 0;
63 }
64
65
66 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
67 {
68         struct hostapd_data *hapd = ctx;
69         if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
70                 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
71 }
72 #endif /* EAP_SERVER */
73
74
75 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
76                                   struct wpa_auth_config *wconf)
77 {
78         wconf->wpa = conf->wpa;
79         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
80         wconf->wpa_pairwise = conf->wpa_pairwise;
81         wconf->wpa_group = conf->wpa_group;
82         wconf->wpa_group_rekey = conf->wpa_group_rekey;
83         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
84         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
85         wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
86         wconf->rsn_pairwise = conf->rsn_pairwise;
87         wconf->rsn_preauth = conf->rsn_preauth;
88         wconf->eapol_version = conf->eapol_version;
89         wconf->peerkey = conf->peerkey;
90         wconf->wmm_enabled = conf->wmm_enabled;
91         wconf->okc = conf->okc;
92 #ifdef CONFIG_IEEE80211W
93         wconf->ieee80211w = conf->ieee80211w;
94 #endif /* CONFIG_IEEE80211W */
95 #ifdef CONFIG_IEEE80211R
96         wconf->ssid_len = conf->ssid.ssid_len;
97         if (wconf->ssid_len > SSID_LEN)
98                 wconf->ssid_len = SSID_LEN;
99         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
100         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
101                   MOBILITY_DOMAIN_ID_LEN);
102         if (conf->nas_identifier &&
103             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
104                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
105                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
106                           wconf->r0_key_holder_len);
107         }
108         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
109         wconf->r0_key_lifetime = conf->r0_key_lifetime;
110         wconf->reassociation_deadline = conf->reassociation_deadline;
111         wconf->r0kh_list = conf->r0kh_list;
112         wconf->r1kh_list = conf->r1kh_list;
113         wconf->pmk_r1_push = conf->pmk_r1_push;
114 #endif /* CONFIG_IEEE80211R */
115 }
116
117
118 int hostapd_reload_config(struct hostapd_iface *iface)
119 {
120         struct hostapd_data *hapd = iface->bss[0];
121         struct hostapd_config *newconf, *oldconf;
122         struct wpa_auth_config wpa_auth_conf;
123
124         newconf = hostapd_config_read(iface->config_fname);
125         if (newconf == NULL)
126                 return -1;
127
128         /*
129          * Deauthenticate all stations since the new configuration may not
130          * allow them to use the BSS anymore.
131          */
132         hostapd_flush_old_stations(hapd);
133
134         /* TODO: update dynamic data based on changed configuration
135          * items (e.g., open/close sockets, etc.) */
136         radius_client_flush(hapd->radius, 0);
137
138         oldconf = hapd->iconf;
139         hapd->iconf = newconf;
140         hapd->conf = &newconf->bss[0];
141         iface->conf = newconf;
142
143         if (hostapd_setup_wpa_psk(hapd->conf)) {
144                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
145                            "after reloading configuration");
146         }
147
148         if (hapd->conf->wpa && hapd->wpa_auth == NULL)
149                 hostapd_setup_wpa(hapd);
150         else if (hapd->conf->wpa) {
151                 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
152                 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
153         } else if (hapd->wpa_auth) {
154                 wpa_deinit(hapd->wpa_auth);
155                 hapd->wpa_auth = NULL;
156                 hostapd_set_privacy(hapd, 0);
157                 hostapd_setup_encryption(hapd->conf->iface, hapd);
158         }
159
160         ieee802_11_set_beacon(hapd);
161
162         hostapd_config_free(oldconf);
163
164         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
165
166         return 0;
167 }
168
169
170 int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
171 {
172         if (hostapd_reload_config(iface) < 0) {
173                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
174                            "file - continuing with old.");
175         }
176         return 0;
177 }
178
179
180 #ifdef HOSTAPD_DUMP_STATE
181 /**
182  * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
183  */
184 static void hostapd_dump_state(struct hostapd_data *hapd)
185 {
186         FILE *f;
187         time_t now;
188         struct sta_info *sta;
189         int i;
190         char *buf;
191
192         if (!hapd->conf->dump_log_name) {
193                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
194                            "request");
195                 return;
196         }
197
198         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
199                    hapd->conf->dump_log_name);
200         f = fopen(hapd->conf->dump_log_name, "w");
201         if (f == NULL) {
202                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
203                            "writing.", hapd->conf->dump_log_name);
204                 return;
205         }
206
207         time(&now);
208         fprintf(f, "hostapd state dump - %s", ctime(&now));
209         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
210                 "num_sta_no_short_slot_time=%d\n"
211                 "num_sta_no_short_preamble=%d\n",
212                 hapd->num_sta, hapd->iface->num_sta_non_erp,
213                 hapd->iface->num_sta_no_short_slot_time,
214                 hapd->iface->num_sta_no_short_preamble);
215
216         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
217                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
218
219                 fprintf(f,
220                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
221                         "  capability=0x%x listen_interval=%d\n",
222                         sta->aid,
223                         sta->flags,
224                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
225                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
226                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
227                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
228                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
229                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
230                          ""),
231                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
232                          ""),
233                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
234                          "[SHORT_PREAMBLE]" : ""),
235                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
236                         (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
237                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
238                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
239                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
240                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
241                         sta->capability,
242                         sta->listen_interval);
243
244                 fprintf(f, "  supported_rates=");
245                 for (i = 0; i < sta->supported_rates_len; i++)
246                         fprintf(f, "%02x ", sta->supported_rates[i]);
247                 fprintf(f, "\n");
248
249                 fprintf(f,
250                         "  timeout_next=%s\n",
251                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
252                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
253                           "DEAUTH")));
254
255                 ieee802_1x_dump_state(f, "  ", sta);
256         }
257
258         buf = os_malloc(4096);
259         if (buf) {
260                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
261                 if (count < 0)
262                         count = 0;
263                 else if (count > 4095)
264                         count = 4095;
265                 buf[count] = '\0';
266                 fprintf(f, "%s", buf);
267
268                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
269                 if (count < 0)
270                         count = 0;
271                 else if (count > 4095)
272                         count = 4095;
273                 buf[count] = '\0';
274                 fprintf(f, "%s", buf);
275                 os_free(buf);
276         }
277         fclose(f);
278 }
279
280
281 int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
282 {
283         size_t i;
284
285         for (i = 0; i < iface->num_bss; i++)
286                 hostapd_dump_state(iface->bss[i]);
287
288         return 0;
289 }
290 #endif /* HOSTAPD_DUMP_STATE */
291
292
293 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
294                                               char *ifname)
295 {
296         int i;
297
298         for (i = 0; i < NUM_WEP_KEYS; i++) {
299                 if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
300                                     i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
301                         wpa_printf(MSG_DEBUG, "Failed to clear default "
302                                    "encryption keys (ifname=%s keyidx=%d)",
303                                    ifname, i);
304                 }
305         }
306 #ifdef CONFIG_IEEE80211W
307         if (hapd->conf->ieee80211w) {
308                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
309                         if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL,
310                                             i, i == 0 ? 1 : 0, NULL, 0,
311                                             NULL, 0)) {
312                                 wpa_printf(MSG_DEBUG, "Failed to clear "
313                                            "default mgmt encryption keys "
314                                            "(ifname=%s keyidx=%d)", ifname, i);
315                         }
316                 }
317         }
318 #endif /* CONFIG_IEEE80211W */
319 }
320
321
322 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
323 {
324         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
325         return 0;
326 }
327
328
329 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
330 {
331         int errors = 0, idx;
332         struct hostapd_ssid *ssid = &hapd->conf->ssid;
333
334         idx = ssid->wep.idx;
335         if (ssid->wep.default_len &&
336             hostapd_set_key(hapd->conf->iface,
337                             hapd, WPA_ALG_WEP, NULL, idx, idx == ssid->wep.idx,
338                             NULL, 0, ssid->wep.key[idx], ssid->wep.len[idx])) {
339                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
340                 errors++;
341         }
342
343         if (ssid->dyn_vlan_keys) {
344                 size_t i;
345                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
346                         const char *ifname;
347                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
348                         if (key == NULL)
349                                 continue;
350                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
351                                                             i);
352                         if (ifname == NULL)
353                                 continue;
354
355                         idx = key->idx;
356                         if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL,
357                                             idx, idx == key->idx, NULL, 0,
358                                             key->key[idx], key->len[idx])) {
359                                 wpa_printf(MSG_WARNING, "Could not set "
360                                            "dynamic VLAN WEP encryption.");
361                                 errors++;
362                         }
363                 }
364         }
365
366         return errors;
367 }
368
369 /**
370  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
371  * @hapd: Pointer to BSS data
372  *
373  * This function is used to free all per-BSS data structures and resources.
374  * This gets called in a loop for each BSS between calls to
375  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
376  * is deinitialized. Most of the modules that are initialized in
377  * hostapd_setup_bss() are deinitialized here.
378  */
379 static void hostapd_cleanup(struct hostapd_data *hapd)
380 {
381         hostapd_ctrl_iface_deinit(hapd);
382
383         os_free(hapd->default_wep_key);
384         hapd->default_wep_key = NULL;
385         iapp_deinit(hapd->iapp);
386         hapd->iapp = NULL;
387         accounting_deinit(hapd);
388         rsn_preauth_iface_deinit(hapd);
389         if (hapd->wpa_auth) {
390                 wpa_deinit(hapd->wpa_auth);
391                 hapd->wpa_auth = NULL;
392
393                 if (hostapd_set_privacy(hapd, 0)) {
394                         wpa_printf(MSG_DEBUG, "Could not disable "
395                                    "PrivacyInvoked for interface %s",
396                                    hapd->conf->iface);
397                 }
398
399                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
400                         wpa_printf(MSG_DEBUG, "Could not remove generic "
401                                    "information element from interface %s",
402                                    hapd->conf->iface);
403                 }
404         }
405         ieee802_1x_deinit(hapd);
406         vlan_deinit(hapd);
407         hostapd_acl_deinit(hapd);
408         radius_client_deinit(hapd->radius);
409         hapd->radius = NULL;
410         radius_server_deinit(hapd->radius_srv);
411         hapd->radius_srv = NULL;
412
413 #ifdef CONFIG_IEEE80211R
414         l2_packet_deinit(hapd->l2);
415 #endif /* CONFIG_IEEE80211R */
416
417         hostapd_deinit_wps(hapd);
418
419         hostapd_wireless_event_deinit(hapd);
420
421 #ifdef EAP_TLS_FUNCS
422         if (hapd->ssl_ctx) {
423                 tls_deinit(hapd->ssl_ctx);
424                 hapd->ssl_ctx = NULL;
425         }
426 #endif /* EAP_TLS_FUNCS */
427
428 #ifdef EAP_SERVER
429         if (hapd->eap_sim_db_priv) {
430                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
431                 hapd->eap_sim_db_priv = NULL;
432         }
433 #endif /* EAP_SERVER */
434
435         if (hapd->interface_added &&
436             hostapd_bss_remove(hapd, hapd->conf->iface)) {
437                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
438                            hapd->conf->iface);
439         }
440 }
441
442
443 /**
444  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
445  * @iface: Pointer to interface data
446  *
447  * This function is called before per-BSS data structures are deinitialized
448  * with hostapd_cleanup().
449  */
450 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
451 {
452 }
453
454
455 /**
456  * hostapd_cleanup_iface - Complete per-interface cleanup
457  * @iface: Pointer to interface data
458  *
459  * This function is called after per-BSS data structures are deinitialized
460  * with hostapd_cleanup().
461  */
462 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
463 {
464         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
465         iface->hw_features = NULL;
466         os_free(iface->current_rates);
467         iface->current_rates = NULL;
468         ap_list_deinit(iface);
469         hostapd_config_free(iface->conf);
470         iface->conf = NULL;
471
472         os_free(iface->config_fname);
473         os_free(iface->bss);
474         os_free(iface);
475 }
476
477
478 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
479 {
480         int i;
481
482         hostapd_broadcast_wep_set(hapd);
483
484         if (hapd->conf->ssid.wep.default_len)
485                 return 0;
486
487         for (i = 0; i < 4; i++) {
488                 if (hapd->conf->ssid.wep.key[i] &&
489                     hostapd_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
490                                     i == hapd->conf->ssid.wep.idx, NULL, 0,
491                                     hapd->conf->ssid.wep.key[i],
492                                     hapd->conf->ssid.wep.len[i])) {
493                         wpa_printf(MSG_WARNING, "Could not set WEP "
494                                    "encryption.");
495                         return -1;
496                 }
497                 if (hapd->conf->ssid.wep.key[i] &&
498                     i == hapd->conf->ssid.wep.idx)
499                         hostapd_set_privacy(hapd, 1);
500         }
501
502         return 0;
503 }
504
505
506 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
507 {
508         int ret = 0;
509
510         if (hostapd_drv_none(hapd))
511                 return 0;
512
513         wpa_printf(MSG_DEBUG, "Flushing old station entries");
514         if (hostapd_flush(hapd)) {
515                 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
516                 ret = -1;
517         }
518         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
519
520         /* New Prism2.5/3 STA firmware versions seem to have issues with this
521          * broadcast deauth frame. This gets the firmware in odd state where
522          * nothing works correctly, so let's skip sending this for the hostap
523          * driver. */
524         if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
525                 u8 addr[ETH_ALEN];
526                 os_memset(addr, 0xff, ETH_ALEN);
527                 hostapd_sta_deauth(hapd, addr,
528                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
529         }
530
531         return ret;
532 }
533
534
535 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
536                                     logger_level level, const char *txt)
537 {
538 #ifndef CONFIG_NO_HOSTAPD_LOGGER
539         struct hostapd_data *hapd = ctx;
540         int hlevel;
541
542         switch (level) {
543         case LOGGER_WARNING:
544                 hlevel = HOSTAPD_LEVEL_WARNING;
545                 break;
546         case LOGGER_INFO:
547                 hlevel = HOSTAPD_LEVEL_INFO;
548                 break;
549         case LOGGER_DEBUG:
550         default:
551                 hlevel = HOSTAPD_LEVEL_DEBUG;
552                 break;
553         }
554
555         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
556 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
557 }
558
559
560 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
561                                         u16 reason)
562 {
563         struct hostapd_data *hapd = ctx;
564         struct sta_info *sta;
565
566         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
567                    "STA " MACSTR " reason %d",
568                    __func__, MAC2STR(addr), reason);
569
570         sta = ap_get_sta(hapd, addr);
571         hostapd_sta_deauth(hapd, addr, reason);
572         if (sta == NULL)
573                 return;
574         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
575         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
576         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
577         sta->timeout_next = STA_REMOVE;
578 }
579
580
581 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
582 {
583         struct hostapd_data *hapd = ctx;
584         michael_mic_failure(hapd, addr, 0);
585 }
586
587
588 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
589                                        wpa_eapol_variable var, int value)
590 {
591         struct hostapd_data *hapd = ctx;
592         struct sta_info *sta = ap_get_sta(hapd, addr);
593         if (sta == NULL)
594                 return;
595         switch (var) {
596         case WPA_EAPOL_portEnabled:
597                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
598                 break;
599         case WPA_EAPOL_portValid:
600                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
601                 break;
602         case WPA_EAPOL_authorized:
603                 ieee802_1x_set_sta_authorized(hapd, sta, value);
604                 break;
605         case WPA_EAPOL_portControl_Auto:
606                 if (sta->eapol_sm)
607                         sta->eapol_sm->portControl = Auto;
608                 break;
609         case WPA_EAPOL_keyRun:
610                 if (sta->eapol_sm)
611                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
612                 break;
613         case WPA_EAPOL_keyAvailable:
614                 if (sta->eapol_sm)
615                         sta->eapol_sm->eap_if->eapKeyAvailable =
616                                 value ? TRUE : FALSE;
617                 break;
618         case WPA_EAPOL_keyDone:
619                 if (sta->eapol_sm)
620                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
621                 break;
622         case WPA_EAPOL_inc_EapolFramesTx:
623                 if (sta->eapol_sm)
624                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
625                 break;
626         }
627 }
628
629
630 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
631                                       wpa_eapol_variable var)
632 {
633         struct hostapd_data *hapd = ctx;
634         struct sta_info *sta = ap_get_sta(hapd, addr);
635         if (sta == NULL || sta->eapol_sm == NULL)
636                 return -1;
637         switch (var) {
638         case WPA_EAPOL_keyRun:
639                 return sta->eapol_sm->keyRun;
640         case WPA_EAPOL_keyAvailable:
641                 return sta->eapol_sm->eap_if->eapKeyAvailable;
642         default:
643                 return -1;
644         }
645 }
646
647
648 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
649                                            const u8 *prev_psk)
650 {
651         struct hostapd_data *hapd = ctx;
652         return hostapd_get_psk(hapd->conf, addr, prev_psk);
653 }
654
655
656 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
657                                     size_t *len)
658 {
659         struct hostapd_data *hapd = ctx;
660         const u8 *key;
661         size_t keylen;
662         struct sta_info *sta;
663
664         sta = ap_get_sta(hapd, addr);
665         if (sta == NULL)
666                 return -1;
667
668         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
669         if (key == NULL)
670                 return -1;
671
672         if (keylen > *len)
673                 keylen = *len;
674         os_memcpy(msk, key, keylen);
675         *len = keylen;
676
677         return 0;
678 }
679
680
681 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
682                                     const u8 *addr, int idx, u8 *key,
683                                     size_t key_len)
684 {
685         struct hostapd_data *hapd = ctx;
686         const char *ifname = hapd->conf->iface;
687
688         if (vlan_id > 0) {
689                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
690                 if (ifname == NULL)
691                         return -1;
692         }
693
694         return hostapd_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
695                                key, key_len);
696 }
697
698
699 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
700                                        u8 *seq)
701 {
702         struct hostapd_data *hapd = ctx;
703         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
704 }
705
706
707 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
708                                             u8 *seq)
709 {
710         struct hostapd_data *hapd = ctx;
711         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
712                                        seq);
713 }
714
715
716 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
717                                        const u8 *data, size_t data_len,
718                                        int encrypt)
719 {
720         struct hostapd_data *hapd = ctx;
721         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
722 }
723
724
725 static int hostapd_wpa_auth_for_each_sta(
726         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
727         void *cb_ctx)
728 {
729         struct hostapd_data *hapd = ctx;
730         struct sta_info *sta;
731
732         for (sta = hapd->sta_list; sta; sta = sta->next) {
733                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
734                         return 1;
735         }
736         return 0;
737 }
738
739
740 struct wpa_auth_iface_iter_data {
741         int (*cb)(struct wpa_authenticator *sm, void *ctx);
742         void *cb_ctx;
743 };
744
745 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
746 {
747         struct wpa_auth_iface_iter_data *data = ctx;
748         size_t i;
749         for (i = 0; i < iface->num_bss; i++) {
750                 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
751                         return 1;
752         }
753         return 0;
754 }
755
756
757 static int hostapd_wpa_auth_for_each_auth(
758         void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
759         void *cb_ctx)
760 {
761         struct wpa_auth_iface_iter_data data;
762         data.cb = cb;
763         data.cb_ctx = cb_ctx;
764         return hostapd_for_each_interface(wpa_auth_iface_iter, &data);
765 }
766
767
768 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
769                                        const u8 *data, size_t data_len)
770 {
771         struct hostapd_data *hapd = ctx;
772
773         if (hapd->driver && hapd->driver->send_ether)
774                 return hapd->driver->send_ether(hapd->drv_priv, dst,
775                                                 hapd->own_addr, proto,
776                                                 data, data_len);
777         if (hapd->l2 == NULL)
778                 return -1;
779         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
780 }
781
782
783 #ifdef CONFIG_IEEE80211R
784
785 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
786                                            const u8 *data, size_t data_len)
787 {
788         struct hostapd_data *hapd = ctx;
789         int res;
790         struct ieee80211_mgmt *m;
791         size_t mlen;
792         struct sta_info *sta;
793
794         sta = ap_get_sta(hapd, dst);
795         if (sta == NULL || sta->wpa_sm == NULL)
796                 return -1;
797
798         m = os_zalloc(sizeof(*m) + data_len);
799         if (m == NULL)
800                 return -1;
801         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
802         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
803                                         WLAN_FC_STYPE_ACTION);
804         os_memcpy(m->da, dst, ETH_ALEN);
805         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
806         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
807         os_memcpy(&m->u, data, data_len);
808
809         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
810         os_free(m);
811         return res;
812 }
813
814
815 static struct wpa_state_machine *
816 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
817 {
818         struct hostapd_data *hapd = ctx;
819         struct sta_info *sta;
820
821         sta = ap_sta_add(hapd, sta_addr);
822         if (sta == NULL)
823                 return NULL;
824         if (sta->wpa_sm)
825                 return sta->wpa_sm;
826
827         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
828         if (sta->wpa_sm == NULL) {
829                 ap_free_sta(hapd, sta);
830                 return NULL;
831         }
832         sta->auth_alg = WLAN_AUTH_FT;
833
834         return sta->wpa_sm;
835 }
836
837
838 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
839                                 size_t len)
840 {
841         struct hostapd_data *hapd = ctx;
842         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
843 }
844
845 #endif /* CONFIG_IEEE80211R */
846
847
848 /**
849  * hostapd_validate_bssid_configuration - Validate BSSID configuration
850  * @iface: Pointer to interface data
851  * Returns: 0 on success, -1 on failure
852  *
853  * This function is used to validate that the configured BSSIDs are valid.
854  */
855 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
856 {
857         u8 mask[ETH_ALEN] = { 0 };
858         struct hostapd_data *hapd = iface->bss[0];
859         unsigned int i = iface->conf->num_bss, bits = 0, j;
860         int res;
861         int auto_addr = 0;
862
863         if (hostapd_drv_none(hapd))
864                 return 0;
865
866         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
867
868         /* Determine the bits necessary to cover the number of BSSIDs. */
869         for (i--; i; i >>= 1)
870                 bits++;
871
872         /* Determine the bits necessary to any configured BSSIDs,
873            if they are higher than the number of BSSIDs. */
874         for (j = 0; j < iface->conf->num_bss; j++) {
875                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
876                         if (j)
877                                 auto_addr++;
878                         continue;
879                 }
880
881                 for (i = 0; i < ETH_ALEN; i++) {
882                         mask[i] |=
883                                 iface->conf->bss[j].bssid[i] ^
884                                 hapd->own_addr[i];
885                 }
886         }
887
888         if (!auto_addr)
889                 goto skip_mask_ext;
890
891         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
892                 ;
893         j = 0;
894         if (i < ETH_ALEN) {
895                 j = (5 - i) * 8;
896
897                 while (mask[i] != 0) {
898                         mask[i] >>= 1;
899                         j++;
900                 }
901         }
902
903         if (bits < j)
904                 bits = j;
905
906         if (bits > 40) {
907                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
908                            bits);
909                 return -1;
910         }
911
912         os_memset(mask, 0xff, ETH_ALEN);
913         j = bits / 8;
914         for (i = 5; i > 5 - j; i--)
915                 mask[i] = 0;
916         j = bits % 8;
917         while (j--)
918                 mask[i] <<= 1;
919
920 skip_mask_ext:
921         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
922                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
923
924         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
925         if (res == 0)
926                 return 0;
927
928         if (res < 0) {
929                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
930                            MACSTR " for start address " MACSTR ".",
931                            MAC2STR(mask), MAC2STR(hapd->own_addr));
932                 return -1;
933         }
934
935         if (!auto_addr)
936                 return 0;
937
938         for (i = 0; i < ETH_ALEN; i++) {
939                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
940                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
941                                    " for start address " MACSTR ".",
942                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
943                         wpa_printf(MSG_ERROR, "Start address must be the "
944                                    "first address in the block (i.e., addr "
945                                    "AND mask == addr).");
946                         return -1;
947                 }
948         }
949
950         return 0;
951 }
952
953
954 static int mac_in_conf(struct hostapd_config *conf, const void *a)
955 {
956         size_t i;
957
958         for (i = 0; i < conf->num_bss; i++) {
959                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
960                         return 1;
961                 }
962         }
963
964         return 0;
965 }
966
967
968 static int hostapd_setup_wpa(struct hostapd_data *hapd)
969 {
970         struct wpa_auth_config _conf;
971         struct wpa_auth_callbacks cb;
972         const u8 *wpa_ie;
973         size_t wpa_ie_len;
974
975         hostapd_wpa_auth_conf(hapd->conf, &_conf);
976         os_memset(&cb, 0, sizeof(cb));
977         cb.ctx = hapd;
978         cb.logger = hostapd_wpa_auth_logger;
979         cb.disconnect = hostapd_wpa_auth_disconnect;
980         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
981         cb.set_eapol = hostapd_wpa_auth_set_eapol;
982         cb.get_eapol = hostapd_wpa_auth_get_eapol;
983         cb.get_psk = hostapd_wpa_auth_get_psk;
984         cb.get_msk = hostapd_wpa_auth_get_msk;
985         cb.set_key = hostapd_wpa_auth_set_key;
986         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
987         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
988         cb.send_eapol = hostapd_wpa_auth_send_eapol;
989         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
990         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
991         cb.send_ether = hostapd_wpa_auth_send_ether;
992 #ifdef CONFIG_IEEE80211R
993         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
994         cb.add_sta = hostapd_wpa_auth_add_sta;
995 #endif /* CONFIG_IEEE80211R */
996         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
997         if (hapd->wpa_auth == NULL) {
998                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
999                 return -1;
1000         }
1001
1002         if (hostapd_set_privacy(hapd, 1)) {
1003                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1004                            "for interface %s", hapd->conf->iface);
1005                 return -1;
1006         }
1007
1008         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1009         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1010                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1011                            "the kernel driver.");
1012                 return -1;
1013         }
1014
1015         if (rsn_preauth_iface_init(hapd)) {
1016                 wpa_printf(MSG_ERROR, "Initialization of RSN "
1017                            "pre-authentication failed.");
1018                 return -1;
1019         }
1020
1021         return 0;
1022
1023 }
1024
1025
1026 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1027                                     struct hostapd_bss_config *conf)
1028 {
1029         struct radius_server_conf srv;
1030         os_memset(&srv, 0, sizeof(srv));
1031         srv.client_file = conf->radius_server_clients;
1032         srv.auth_port = conf->radius_server_auth_port;
1033         srv.conf_ctx = conf;
1034         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1035         srv.ssl_ctx = hapd->ssl_ctx;
1036         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1037         srv.eap_fast_a_id = conf->eap_fast_a_id;
1038         srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1039         srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1040         srv.eap_fast_prov = conf->eap_fast_prov;
1041         srv.pac_key_lifetime = conf->pac_key_lifetime;
1042         srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1043         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1044         srv.tnc = conf->tnc;
1045         srv.wps = hapd->wps;
1046         srv.ipv6 = conf->radius_server_ipv6;
1047         srv.get_eap_user = hostapd_radius_get_eap_user;
1048         srv.eap_req_id_text = conf->eap_req_id_text;
1049         srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1050
1051         hapd->radius_srv = radius_server_init(&srv);
1052         if (hapd->radius_srv == NULL) {
1053                 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1054                 return -1;
1055         }
1056
1057         return 0;
1058 }
1059
1060
1061 /**
1062  * hostapd_setup_bss - Per-BSS setup (initialization)
1063  * @hapd: Pointer to BSS data
1064  * @first: Whether this BSS is the first BSS of an interface
1065  *
1066  * This function is used to initialize all per-BSS data structures and
1067  * resources. This gets called in a loop for each BSS when an interface is
1068  * initialized. Most of the modules that are initialized here will be
1069  * deinitialized in hostapd_cleanup().
1070  */
1071 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1072 {
1073         struct hostapd_bss_config *conf = hapd->conf;
1074         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1075         int ssid_len, set_ssid;
1076
1077         if (!first) {
1078                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1079                         /* Allocate the next available BSSID. */
1080                         do {
1081                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1082                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1083                 } else {
1084                         /* Allocate the configured BSSID. */
1085                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1086
1087                         if (hostapd_mac_comp(hapd->own_addr,
1088                                              hapd->iface->bss[0]->own_addr) ==
1089                             0) {
1090                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1091                                            "BSSID set to the MAC address of "
1092                                            "the radio", hapd->conf->iface);
1093                                 return -1;
1094                         }
1095                 }
1096
1097                 hapd->interface_added = 1;
1098                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1099                                     hapd->own_addr)) {
1100                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1101                                    MACSTR ")", MAC2STR(hapd->own_addr));
1102                         return -1;
1103                 }
1104         }
1105
1106         /*
1107          * Fetch the SSID from the system and use it or,
1108          * if one was specified in the config file, verify they
1109          * match.
1110          */
1111         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1112         if (ssid_len < 0) {
1113                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1114                 return -1;
1115         }
1116         if (conf->ssid.ssid_set) {
1117                 /*
1118                  * If SSID is specified in the config file and it differs
1119                  * from what is being used then force installation of the
1120                  * new SSID.
1121                  */
1122                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1123                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1124         } else {
1125                 /*
1126                  * No SSID in the config file; just use the one we got
1127                  * from the system.
1128                  */
1129                 set_ssid = 0;
1130                 conf->ssid.ssid_len = ssid_len;
1131                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1132                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1133         }
1134
1135         if (!hostapd_drv_none(hapd)) {
1136                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1137                            " and ssid '%s'",
1138                            hapd->conf->iface, MAC2STR(hapd->own_addr),
1139                            hapd->conf->ssid.ssid);
1140         }
1141
1142         if (hostapd_setup_wpa_psk(conf)) {
1143                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1144                 return -1;
1145         }
1146
1147         /* Set flag for whether SSID is broadcast in beacons */
1148         if (hostapd_set_broadcast_ssid(hapd,
1149                                        !!hapd->conf->ignore_broadcast_ssid)) {
1150                 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1151                            "kernel driver");
1152                 return -1;
1153         }
1154
1155         /* Set SSID for the kernel driver (to be used in beacon and probe
1156          * response frames) */
1157         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1158                                          conf->ssid.ssid_len)) {
1159                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1160                 return -1;
1161         }
1162
1163         if (wpa_debug_level == MSG_MSGDUMP)
1164                 conf->radius->msg_dumps = 1;
1165         hapd->radius = radius_client_init(hapd, conf->radius);
1166         if (hapd->radius == NULL) {
1167                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1168                 return -1;
1169         }
1170
1171         if (hostapd_acl_init(hapd)) {
1172                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1173                 return -1;
1174         }
1175         if (hostapd_init_wps(hapd, conf))
1176                 return -1;
1177
1178         if (ieee802_1x_init(hapd)) {
1179                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1180                 return -1;
1181         }
1182
1183         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1184                 return -1;
1185
1186         if (accounting_init(hapd)) {
1187                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1188                 return -1;
1189         }
1190
1191         if (hapd->conf->ieee802_11f &&
1192             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1193                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1194                            "failed.");
1195                 return -1;
1196         }
1197
1198         if (hostapd_ctrl_iface_init(hapd)) {
1199                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1200                 return -1;
1201         }
1202
1203         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1204                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1205                 return -1;
1206         }
1207
1208 #ifdef CONFIG_IEEE80211R
1209         if (!hostapd_drv_none(hapd)) {
1210                 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1211                                           hostapd_rrb_receive, hapd, 0);
1212                 if (hapd->l2 == NULL &&
1213                     (hapd->driver == NULL ||
1214                      hapd->driver->send_ether == NULL)) {
1215                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1216                                    "interface");
1217                         return -1;
1218                 }
1219         }
1220 #endif /* CONFIG_IEEE80211R */
1221
1222         ieee802_11_set_beacon(hapd);
1223
1224         if (conf->radius_server_clients &&
1225             hostapd_setup_radius_srv(hapd, conf))
1226                 return -1;
1227
1228         return 0;
1229 }
1230
1231
1232 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1233 {
1234         struct hostapd_data *hapd = iface->bss[0];
1235         int i;
1236         struct hostapd_tx_queue_params *p;
1237
1238         for (i = 0; i < NUM_TX_QUEUES; i++) {
1239                 p = &iface->conf->tx_queue[i];
1240
1241                 if (!p->configured)
1242                         continue;
1243
1244                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1245                                                 p->cwmax, p->burst)) {
1246                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1247                                    "parameters for queue %d.", i);
1248                         /* Continue anyway */
1249                 }
1250         }
1251 }
1252
1253
1254 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1255                                        size_t identity_len, int phase2,
1256                                        struct eap_user *user)
1257 {
1258         const struct hostapd_eap_user *eap_user;
1259         int i, count;
1260
1261         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1262         if (eap_user == NULL)
1263                 return -1;
1264
1265         if (user == NULL)
1266                 return 0;
1267
1268         os_memset(user, 0, sizeof(*user));
1269         count = EAP_USER_MAX_METHODS;
1270         if (count > EAP_MAX_METHODS)
1271                 count = EAP_MAX_METHODS;
1272         for (i = 0; i < count; i++) {
1273                 user->methods[i].vendor = eap_user->methods[i].vendor;
1274                 user->methods[i].method = eap_user->methods[i].method;
1275         }
1276
1277         if (eap_user->password) {
1278                 user->password = os_malloc(eap_user->password_len);
1279                 if (user->password == NULL)
1280                         return -1;
1281                 os_memcpy(user->password, eap_user->password,
1282                           eap_user->password_len);
1283                 user->password_len = eap_user->password_len;
1284                 user->password_hash = eap_user->password_hash;
1285         }
1286         user->force_version = eap_user->force_version;
1287         user->ttls_auth = eap_user->ttls_auth;
1288
1289         return 0;
1290 }
1291
1292
1293 static int setup_interface(struct hostapd_iface *iface)
1294 {
1295         struct hostapd_data *hapd = iface->bss[0];
1296         struct hostapd_bss_config *conf = hapd->conf;
1297         size_t i;
1298         char country[4];
1299         u8 *b = conf->bssid;
1300         int freq;
1301         size_t j;
1302         u8 *prev_addr;
1303
1304         /*
1305          * Initialize the driver interface and make sure that all BSSes get
1306          * configured with a pointer to this driver interface.
1307          */
1308         if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1309                 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1310         } else {
1311                 hapd->drv_priv = hostapd_driver_init(hapd);
1312         }
1313
1314         if (hapd->drv_priv == NULL) {
1315                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1316                            hapd->driver ? hapd->driver->name : "Unknown");
1317                 hapd->driver = NULL;
1318                 return -1;
1319         }
1320         for (i = 0; i < iface->num_bss; i++) {
1321                 iface->bss[i]->driver = hapd->driver;
1322                 iface->bss[i]->drv_priv = hapd->drv_priv;
1323         }
1324
1325         if (hostapd_validate_bssid_configuration(iface))
1326                 return -1;
1327
1328 #ifdef CONFIG_IEEE80211N
1329         SET_2BIT_LE16(&iface->ht_op_mode,
1330                       HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1331                       OP_MODE_PURE);
1332 #endif /* CONFIG_IEEE80211N */
1333
1334         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1335                 os_memcpy(country, hapd->iconf->country, 3);
1336                 country[3] = '\0';
1337                 if (hostapd_set_country(hapd, country) < 0) {
1338                         wpa_printf(MSG_ERROR, "Failed to set country code");
1339                         return -1;
1340                 }
1341         }
1342
1343         if (hapd->iconf->ieee80211d &&
1344             hostapd_set_ieee80211d(hapd, 1) < 0) {
1345                 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1346                            hapd->iconf->ieee80211d);
1347                 return -1;
1348         }
1349
1350         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1351             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1352                 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1353                            "kernel driver");
1354                 return -1;
1355         }
1356
1357         /* TODO: merge with hostapd_driver_init() ? */
1358         if (hostapd_wireless_event_init(hapd) < 0)
1359                 return -1;
1360
1361         if (hostapd_get_hw_features(iface)) {
1362                 /* Not all drivers support this yet, so continue without hw
1363                  * feature data. */
1364         } else {
1365                 int ret = hostapd_select_hw_mode(iface);
1366                 if (ret < 0) {
1367                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1368                                    "channel. (%d)", ret);
1369                         return -1;
1370                 }
1371         }
1372
1373         hostapd_flush_old_stations(hapd);
1374         hostapd_set_privacy(hapd, 0);
1375
1376         if (hapd->iconf->channel) {
1377                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1378                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1379                            "Frequency: %d MHz",
1380                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1381                            hapd->iconf->channel, freq);
1382
1383                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1384                                      hapd->iconf->ieee80211n,
1385                                      hapd->iconf->secondary_channel)) {
1386                         wpa_printf(MSG_ERROR, "Could not set channel for "
1387                                    "kernel driver");
1388                         return -1;
1389                 }
1390         }
1391
1392         hostapd_broadcast_wep_clear(hapd);
1393         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1394                 return -1;
1395
1396         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1397         ieee802_11_set_beacon(hapd);
1398
1399         if (hapd->iconf->rts_threshold > -1 &&
1400             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1401                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1402                            "kernel driver");
1403                 return -1;
1404         }
1405
1406         if (hapd->iconf->fragm_threshold > -1 &&
1407             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1408                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1409                            "for kernel driver");
1410                 return -1;
1411         }
1412
1413         prev_addr = hapd->own_addr;
1414
1415         for (j = 0; j < iface->num_bss; j++) {
1416                 hapd = iface->bss[j];
1417                 if (j)
1418                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1419                 if (hostapd_setup_bss(hapd, j == 0))
1420                         return -1;
1421                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1422                         prev_addr = hapd->own_addr;
1423         }
1424
1425         hostapd_tx_queue_params(iface);
1426
1427         ap_list_init(iface);
1428
1429         if (hostapd_driver_commit(hapd) < 0) {
1430                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1431                            "configuration", __func__);
1432                 return -1;
1433         }
1434
1435         return 0;
1436 }
1437
1438
1439 /**
1440  * hostapd_setup_interface - Setup of an interface
1441  * @iface: Pointer to interface data.
1442  * Returns: 0 on success, -1 on failure
1443  *
1444  * Initializes the driver interface, validates the configuration,
1445  * and sets driver parameters based on the configuration.
1446  * Flushes old stations, sets the channel, encryption,
1447  * beacons, and WDS links based on the configuration.
1448  */
1449 int hostapd_setup_interface(struct hostapd_iface *iface)
1450 {
1451         int ret;
1452
1453         ret = setup_interface(iface);
1454         if (ret) {
1455                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1456                            iface->bss[0]->conf->iface);
1457                 eloop_terminate();
1458                 return -1;
1459         } else if (!hostapd_drv_none(iface->bss[0])) {
1460                 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1461                            iface->bss[0]->conf->iface);
1462         }
1463
1464         return 0;
1465 }
1466
1467
1468 /**
1469  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1470  * @hapd_iface: Pointer to interface data
1471  * @conf: Pointer to per-interface configuration
1472  * @bss: Pointer to per-BSS configuration for this BSS
1473  * Returns: Pointer to allocated BSS data
1474  *
1475  * This function is used to allocate per-BSS data structure. This data will be
1476  * freed after hostapd_cleanup() is called for it during interface
1477  * deinitialization.
1478  */
1479 static struct hostapd_data *
1480 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1481                        struct hostapd_config *conf,
1482                        struct hostapd_bss_config *bss)
1483 {
1484         struct hostapd_data *hapd;
1485
1486         hapd = os_zalloc(sizeof(*hapd));
1487         if (hapd == NULL)
1488                 return NULL;
1489
1490         hapd->iconf = conf;
1491         hapd->conf = bss;
1492         hapd->iface = hapd_iface;
1493
1494         if (hapd->conf->individual_wep_key_len > 0) {
1495                 /* use key0 in individual key and key1 in broadcast key */
1496                 hapd->default_wep_key_idx = 1;
1497         }
1498
1499 #ifdef EAP_TLS_FUNCS
1500         if (hapd->conf->eap_server &&
1501             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1502              hapd->conf->dh_file)) {
1503                 struct tls_connection_params params;
1504
1505                 hapd->ssl_ctx = tls_init(NULL);
1506                 if (hapd->ssl_ctx == NULL) {
1507                         wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1508                         goto fail;
1509                 }
1510
1511                 os_memset(&params, 0, sizeof(params));
1512                 params.ca_cert = hapd->conf->ca_cert;
1513                 params.client_cert = hapd->conf->server_cert;
1514                 params.private_key = hapd->conf->private_key;
1515                 params.private_key_passwd = hapd->conf->private_key_passwd;
1516                 params.dh_file = hapd->conf->dh_file;
1517
1518                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1519                         wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1520                         goto fail;
1521                 }
1522
1523                 if (tls_global_set_verify(hapd->ssl_ctx,
1524                                           hapd->conf->check_crl)) {
1525                         wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1526                         goto fail;
1527                 }
1528         }
1529 #endif /* EAP_TLS_FUNCS */
1530
1531 #ifdef EAP_SERVER
1532         if (hapd->conf->eap_sim_db) {
1533                 hapd->eap_sim_db_priv =
1534                         eap_sim_db_init(hapd->conf->eap_sim_db,
1535                                         hostapd_sim_db_cb, hapd);
1536                 if (hapd->eap_sim_db_priv == NULL) {
1537                         wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1538                                    "database interface");
1539                         goto fail;
1540                 }
1541         }
1542 #endif /* EAP_SERVER */
1543
1544         hapd->driver = hapd->iconf->driver;
1545
1546         return hapd;
1547
1548 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1549 fail:
1550 #endif
1551         /* TODO: cleanup allocated resources(?) */
1552         os_free(hapd);
1553         return NULL;
1554 }
1555
1556
1557 /**
1558  * hostapd_init - Allocate and initialize per-interface data
1559  * @config_file: Path to the configuration file
1560  * Returns: Pointer to the allocated interface data or %NULL on failure
1561  *
1562  * This function is used to allocate main data structures for per-interface
1563  * data. The allocated data buffer will be freed by calling
1564  * hostapd_cleanup_iface().
1565  */
1566 struct hostapd_iface * hostapd_init(const char *config_file)
1567 {
1568         struct hostapd_iface *hapd_iface = NULL;
1569         struct hostapd_config *conf = NULL;
1570         struct hostapd_data *hapd;
1571         size_t i;
1572
1573         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1574         if (hapd_iface == NULL)
1575                 goto fail;
1576
1577         hapd_iface->config_fname = os_strdup(config_file);
1578         if (hapd_iface->config_fname == NULL)
1579                 goto fail;
1580
1581         conf = hostapd_config_read(hapd_iface->config_fname);
1582         if (conf == NULL)
1583                 goto fail;
1584         hapd_iface->conf = conf;
1585
1586         hapd_iface->num_bss = conf->num_bss;
1587         hapd_iface->bss = os_zalloc(conf->num_bss *
1588                                     sizeof(struct hostapd_data *));
1589         if (hapd_iface->bss == NULL)
1590                 goto fail;
1591
1592         for (i = 0; i < conf->num_bss; i++) {
1593                 hapd = hapd_iface->bss[i] =
1594                         hostapd_alloc_bss_data(hapd_iface, conf,
1595                                                &conf->bss[i]);
1596                 if (hapd == NULL)
1597                         goto fail;
1598         }
1599
1600         return hapd_iface;
1601
1602 fail:
1603         if (conf)
1604                 hostapd_config_free(conf);
1605         if (hapd_iface) {
1606                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1607                         hapd = hapd_iface->bss[i];
1608                         if (hapd && hapd->ssl_ctx)
1609                                 tls_deinit(hapd->ssl_ctx);
1610                 }
1611
1612                 os_free(hapd_iface->config_fname);
1613                 os_free(hapd_iface->bss);
1614                 os_free(hapd_iface);
1615         }
1616         return NULL;
1617 }
1618
1619
1620 void hostapd_interface_deinit(struct hostapd_iface *iface)
1621 {
1622         size_t j;
1623
1624         if (iface == NULL)
1625                 return;
1626
1627         hostapd_cleanup_iface_pre(iface);
1628         for (j = 0; j < iface->num_bss; j++) {
1629                 struct hostapd_data *hapd = iface->bss[j];
1630                 hostapd_free_stas(hapd);
1631                 hostapd_flush_old_stations(hapd);
1632                 hostapd_cleanup(hapd);
1633                 if (j == iface->num_bss - 1 && hapd->driver)
1634                         hostapd_driver_deinit(hapd);
1635         }
1636         for (j = 0; j < iface->num_bss; j++)
1637                 os_free(iface->bss[j]);
1638         hostapd_cleanup_iface(iface);
1639 }