e8d846056a72ba296a9b5b6e5821b5376c525c69
[libeap.git] / hostapd / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
17 #include <syslog.h>
18 #endif /* CONFIG_NATIVE_WINDOWS */
19
20 #include "eloop.h"
21 #include "hostapd.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "beacon.h"
25 #include "hw_features.h"
26 #include "accounting.h"
27 #include "eapol_sm.h"
28 #include "iapp.h"
29 #include "ap.h"
30 #include "ieee802_11_auth.h"
31 #include "ap_list.h"
32 #include "sta_info.h"
33 #include "driver.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
36 #include "wpa.h"
37 #include "preauth.h"
38 #include "wme.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
41 #include "tls.h"
42 #include "eap_server/eap_sim_db.h"
43 #include "eap_server/eap.h"
44 #include "eap_server/tncs.h"
45 #include "version.h"
46 #include "l2_packet/l2_packet.h"
47
48
49 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
50                                        size_t identity_len, int phase2,
51                                        struct eap_user *user);
52
53 struct hapd_interfaces {
54         size_t count;
55         struct hostapd_iface **iface;
56 };
57
58 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
59
60
61 extern int wpa_debug_level;
62 extern int wpa_debug_show_keys;
63 extern int wpa_debug_timestamp;
64
65
66 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
67                               int level, const char *txt, size_t len)
68 {
69         struct hostapd_data *hapd = ctx;
70         char *format, *module_str;
71         int maxlen;
72         int conf_syslog_level, conf_stdout_level;
73         unsigned int conf_syslog, conf_stdout;
74
75         maxlen = len + 100;
76         format = os_malloc(maxlen);
77         if (!format)
78                 return;
79
80         if (hapd && hapd->conf) {
81                 conf_syslog_level = hapd->conf->logger_syslog_level;
82                 conf_stdout_level = hapd->conf->logger_stdout_level;
83                 conf_syslog = hapd->conf->logger_syslog;
84                 conf_stdout = hapd->conf->logger_stdout;
85         } else {
86                 conf_syslog_level = conf_stdout_level = 0;
87                 conf_syslog = conf_stdout = (unsigned int) -1;
88         }
89
90         switch (module) {
91         case HOSTAPD_MODULE_IEEE80211:
92                 module_str = "IEEE 802.11";
93                 break;
94         case HOSTAPD_MODULE_IEEE8021X:
95                 module_str = "IEEE 802.1X";
96                 break;
97         case HOSTAPD_MODULE_RADIUS:
98                 module_str = "RADIUS";
99                 break;
100         case HOSTAPD_MODULE_WPA:
101                 module_str = "WPA";
102                 break;
103         case HOSTAPD_MODULE_DRIVER:
104                 module_str = "DRIVER";
105                 break;
106         case HOSTAPD_MODULE_IAPP:
107                 module_str = "IAPP";
108                 break;
109         case HOSTAPD_MODULE_MLME:
110                 module_str = "MLME";
111                 break;
112         default:
113                 module_str = NULL;
114                 break;
115         }
116
117         if (hapd && hapd->conf && addr)
118                 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
119                             hapd->conf->iface, MAC2STR(addr),
120                             module_str ? " " : "", module_str, txt);
121         else if (hapd && hapd->conf)
122                 os_snprintf(format, maxlen, "%s:%s%s %s",
123                             hapd->conf->iface, module_str ? " " : "",
124                             module_str, txt);
125         else if (addr)
126                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
127                             MAC2STR(addr), module_str ? " " : "",
128                             module_str, txt);
129         else
130                 os_snprintf(format, maxlen, "%s%s%s",
131                             module_str, module_str ? ": " : "", txt);
132
133         if ((conf_stdout & module) && level >= conf_stdout_level) {
134                 wpa_debug_print_timestamp();
135                 printf("%s\n", format);
136         }
137
138 #ifndef CONFIG_NATIVE_WINDOWS
139         if ((conf_syslog & module) && level >= conf_syslog_level) {
140                 int priority;
141                 switch (level) {
142                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
143                 case HOSTAPD_LEVEL_DEBUG:
144                         priority = LOG_DEBUG;
145                         break;
146                 case HOSTAPD_LEVEL_INFO:
147                         priority = LOG_INFO;
148                         break;
149                 case HOSTAPD_LEVEL_NOTICE:
150                         priority = LOG_NOTICE;
151                         break;
152                 case HOSTAPD_LEVEL_WARNING:
153                         priority = LOG_WARNING;
154                         break;
155                 default:
156                         priority = LOG_INFO;
157                         break;
158                 }
159                 syslog(priority, "%s", format);
160         }
161 #endif /* CONFIG_NATIVE_WINDOWS */
162
163         os_free(format);
164 }
165
166
167 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
168 {
169         u8 addr[ETH_ALEN];
170
171         /* New Prism2.5/3 STA firmware versions seem to have issues with this
172          * broadcast deauth frame. This gets the firmware in odd state where
173          * nothing works correctly, so let's skip sending this for the hostap
174          * driver. */
175
176         if (os_strcmp(hapd->driver->name, "hostap") != 0) {
177                 os_memset(addr, 0xff, ETH_ALEN);
178                 hostapd_sta_deauth(hapd, addr,
179                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
180         }
181 }
182
183
184 /**
185  * hostapd_prune_associations - Remove extraneous associations
186  * @hapd: Pointer to BSS data for the most recent association
187  * @sta: Pointer to the associated STA data
188  *
189  * This function looks through all radios and BSS's for previous
190  * (stale) associations of STA. If any are found they are removed.
191  */
192 static void hostapd_prune_associations(struct hostapd_data *hapd,
193                                        struct sta_info *sta)
194 {
195         struct sta_info *osta;
196         struct hostapd_data *ohapd;
197         size_t i, j;
198         struct hapd_interfaces *interfaces = eloop_get_user_data();
199
200         for (i = 0; i < interfaces->count; i++) {
201                 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
202                         ohapd = interfaces->iface[i]->bss[j];
203                         if (ohapd == hapd)
204                                 continue;
205                         osta = ap_get_sta(ohapd, sta->addr);
206                         if (!osta)
207                                 continue;
208
209                         ap_sta_disassociate(ohapd, osta,
210                                             WLAN_REASON_UNSPECIFIED);
211                 }
212         }
213 }
214
215
216 /**
217  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
218  * @hapd: Pointer to BSS data
219  * @sta: Pointer to the associated STA data
220  * @reassoc: 1 to indicate this was a re-association; 0 = first association
221  *
222  * This function will be called whenever a station associates with the AP. It
223  * can be called for ieee802_11.c for drivers that export MLME to hostapd and
224  * from driver_*.c for drivers that take care of management frames (IEEE 802.11
225  * authentication and association) internally.
226  */
227 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
228                            int reassoc)
229 {
230         if (hapd->tkip_countermeasures) {
231                 hostapd_sta_deauth(hapd, sta->addr,
232                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
233                 return;
234         }
235
236         hostapd_prune_associations(hapd, sta);
237
238         /* IEEE 802.11F (IAPP) */
239         if (hapd->conf->ieee802_11f)
240                 iapp_new_station(hapd->iapp, sta);
241
242         /* Start accounting here, if IEEE 802.1X and WPA are not used.
243          * IEEE 802.1X/WPA code will start accounting after the station has
244          * been authorized. */
245         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
246                 accounting_sta_start(hapd, sta);
247
248         hostapd_wme_sta_config(hapd, sta);
249
250         /* Start IEEE 802.1X authentication process for new stations */
251         ieee802_1x_new_station(hapd, sta);
252         if (reassoc) {
253                 if (sta->auth_alg != WLAN_AUTH_FT)
254                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
255         } else
256                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
257 }
258
259
260 #ifdef EAP_SERVER
261 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
262                                  struct sta_info *sta, void *ctx)
263 {
264         if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
265                 return 1;
266         return 0;
267 }
268
269
270 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
271 {
272         struct hostapd_data *hapd = ctx;
273         if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
274                 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
275 }
276 #endif /* EAP_SERVER */
277
278
279 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
280 {
281         printf("Signal %d received - terminating\n", sig);
282         eloop_terminate();
283 }
284
285
286 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
287                                   struct wpa_auth_config *wconf)
288 {
289         wconf->wpa = conf->wpa;
290         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
291         wconf->wpa_pairwise = conf->wpa_pairwise;
292         wconf->wpa_group = conf->wpa_group;
293         wconf->wpa_group_rekey = conf->wpa_group_rekey;
294         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
295         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
296         wconf->rsn_pairwise = conf->rsn_pairwise;
297         wconf->rsn_preauth = conf->rsn_preauth;
298         wconf->eapol_version = conf->eapol_version;
299         wconf->peerkey = conf->peerkey;
300         wconf->wme_enabled = conf->wme_enabled;
301 #ifdef CONFIG_IEEE80211W
302         wconf->ieee80211w = conf->ieee80211w;
303 #endif /* CONFIG_IEEE80211W */
304 #ifdef CONFIG_IEEE80211R
305         wconf->ssid_len = conf->ssid.ssid_len;
306         if (wconf->ssid_len > SSID_LEN)
307                 wconf->ssid_len = SSID_LEN;
308         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
309         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
310                   MOBILITY_DOMAIN_ID_LEN);
311         if (conf->nas_identifier &&
312             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
313                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
314                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
315                           wconf->r0_key_holder_len);
316         }
317         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
318         wconf->r0_key_lifetime = conf->r0_key_lifetime;
319         wconf->reassociation_deadline = conf->reassociation_deadline;
320         wconf->r0kh_list = conf->r0kh_list;
321         wconf->r1kh_list = conf->r1kh_list;
322         wconf->pmk_r1_push = conf->pmk_r1_push;
323 #endif /* CONFIG_IEEE80211R */
324 }
325
326
327 #ifndef CONFIG_NATIVE_WINDOWS
328 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
329 {
330         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
331         struct hostapd_config *newconf;
332         size_t i;
333         struct wpa_auth_config wpa_auth_conf;
334
335         printf("Signal %d received - reloading configuration\n", sig);
336
337         for (i = 0; i < hapds->count; i++) {
338                 struct hostapd_data *hapd = hapds->iface[i]->bss[0];
339                 newconf = hostapd_config_read(hapds->iface[i]->config_fname);
340                 if (newconf == NULL) {
341                         printf("Failed to read new configuration file - "
342                                "continuing with old.\n");
343                         continue;
344                 }
345                 /* TODO: update dynamic data based on changed configuration
346                  * items (e.g., open/close sockets, remove stations added to
347                  * deny list, etc.) */
348                 radius_client_flush(hapd->radius, 0);
349                 hostapd_config_free(hapd->iconf);
350
351                 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
352                 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
353
354                 hapd->iconf = newconf;
355                 hapd->conf = &newconf->bss[0];
356                 hapds->iface[i]->conf = newconf;
357
358                 if (hostapd_setup_wpa_psk(hapd->conf)) {
359                         wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
360                                    "after reloading configuration");
361                 }
362         }
363 }
364
365
366 #ifdef HOSTAPD_DUMP_STATE
367 static void hostapd_dump_state(struct hostapd_data *hapd)
368 {
369         FILE *f;
370         time_t now;
371         struct sta_info *sta;
372         int i;
373         char *buf;
374
375         if (!hapd->conf->dump_log_name) {
376                 printf("Dump file not defined - ignoring dump request\n");
377                 return;
378         }
379
380         printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
381         f = fopen(hapd->conf->dump_log_name, "w");
382         if (f == NULL) {
383                 printf("Could not open dump file '%s' for writing.\n",
384                        hapd->conf->dump_log_name);
385                 return;
386         }
387
388         time(&now);
389         fprintf(f, "hostapd state dump - %s", ctime(&now));
390         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
391                 "num_sta_no_short_slot_time=%d\n"
392                 "num_sta_no_short_preamble=%d\n",
393                 hapd->num_sta, hapd->iface->num_sta_non_erp,
394                 hapd->iface->num_sta_no_short_slot_time,
395                 hapd->iface->num_sta_no_short_preamble);
396
397         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
398                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
399
400                 fprintf(f,
401                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
402                         "  capability=0x%x listen_interval=%d\n",
403                         sta->aid,
404                         sta->flags,
405                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
406                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
407                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
408                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
409                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
410                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
411                          ""),
412                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
413                          ""),
414                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
415                          "[SHORT_PREAMBLE]" : ""),
416                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
417                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
418                         sta->capability,
419                         sta->listen_interval);
420
421                 fprintf(f, "  supported_rates=");
422                 for (i = 0; i < sta->supported_rates_len; i++)
423                         fprintf(f, "%02x ", sta->supported_rates[i]);
424                 fprintf(f, "\n");
425
426                 fprintf(f,
427                         "  timeout_next=%s\n",
428                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
429                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
430                           "DEAUTH")));
431
432                 ieee802_1x_dump_state(f, "  ", sta);
433         }
434
435         buf = os_malloc(4096);
436         if (buf) {
437                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
438                 if (count < 0)
439                         count = 0;
440                 else if (count > 4095)
441                         count = 4095;
442                 buf[count] = '\0';
443                 fprintf(f, "%s", buf);
444
445                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
446                 if (count < 0)
447                         count = 0;
448                 else if (count > 4095)
449                         count = 4095;
450                 buf[count] = '\0';
451                 fprintf(f, "%s", buf);
452                 os_free(buf);
453         }
454         fclose(f);
455 }
456 #endif /* HOSTAPD_DUMP_STATE */
457
458
459 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
460 {
461 #ifdef HOSTAPD_DUMP_STATE
462         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
463         size_t i, j;
464
465         for (i = 0; i < hapds->count; i++) {
466                 struct hostapd_iface *hapd_iface = hapds->iface[i];
467                 for (j = 0; j < hapd_iface->num_bss; j++)
468                         hostapd_dump_state(hapd_iface->bss[j]);
469         }
470 #endif /* HOSTAPD_DUMP_STATE */
471 }
472 #endif /* CONFIG_NATIVE_WINDOWS */
473
474 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
475                                               char *ifname)
476 {
477         int i;
478
479         for (i = 0; i < NUM_WEP_KEYS; i++) {
480                 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
481                                            0, i == 0 ? 1 : 0)) {
482                         printf("Failed to clear default encryption keys "
483                                "(ifname=%s keyidx=%d)\n", ifname, i);
484                 }
485         }
486 }
487
488
489 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
490 {
491         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
492         return 0;
493 }
494
495
496 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
497 {
498         int errors = 0, idx;
499         struct hostapd_ssid *ssid = &hapd->conf->ssid;
500
501         idx = ssid->wep.idx;
502         if (ssid->wep.default_len &&
503             hostapd_set_encryption(hapd->conf->iface,
504                                    hapd, "WEP", NULL, idx,
505                                    ssid->wep.key[idx],
506                                    ssid->wep.len[idx],
507                                    idx == ssid->wep.idx)) {
508                 printf("Could not set WEP encryption.\n");
509                 errors++;
510         }
511
512         if (ssid->dyn_vlan_keys) {
513                 size_t i;
514                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
515                         const char *ifname;
516                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
517                         if (key == NULL)
518                                 continue;
519                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
520                                                             i);
521                         if (ifname == NULL)
522                                 continue;
523
524                         idx = key->idx;
525                         if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
526                                                    idx, key->key[idx],
527                                                    key->len[idx],
528                                                    idx == key->idx)) {
529                                 printf("Could not set dynamic VLAN WEP "
530                                        "encryption.\n");
531                                 errors++;
532                         }
533                 }
534         }
535
536         return errors;
537 }
538
539 /**
540  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
541  * @hapd: Pointer to BSS data
542  *
543  * This function is used to free all per-BSS data structures and resources.
544  * This gets called in a loop for each BSS between calls to
545  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
546  * is deinitialized. Most of the modules that are initialized in
547  * hostapd_setup_bss() are deinitialized here.
548  */
549 static void hostapd_cleanup(struct hostapd_data *hapd)
550 {
551         hostapd_ctrl_iface_deinit(hapd);
552
553         os_free(hapd->default_wep_key);
554         hapd->default_wep_key = NULL;
555         iapp_deinit(hapd->iapp);
556         hapd->iapp = NULL;
557         accounting_deinit(hapd);
558         rsn_preauth_iface_deinit(hapd);
559         if (hapd->wpa_auth) {
560                 wpa_deinit(hapd->wpa_auth);
561                 hapd->wpa_auth = NULL;
562
563                 if (hostapd_set_privacy(hapd, 0)) {
564                         wpa_printf(MSG_DEBUG, "Could not disable "
565                                    "PrivacyInvoked for interface %s",
566                                    hapd->conf->iface);
567                 }
568
569                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
570                         wpa_printf(MSG_DEBUG, "Could not remove generic "
571                                    "information element from interface %s",
572                                    hapd->conf->iface);
573                 }
574         }
575         ieee802_1x_deinit(hapd);
576         vlan_deinit(hapd);
577         hostapd_acl_deinit(hapd);
578         radius_client_deinit(hapd->radius);
579         hapd->radius = NULL;
580         radius_server_deinit(hapd->radius_srv);
581         hapd->radius_srv = NULL;
582
583 #ifdef CONFIG_IEEE80211R
584         l2_packet_deinit(hapd->l2);
585 #endif /* CONFIG_IEEE80211R */
586
587         hostapd_wireless_event_deinit(hapd);
588
589 #ifdef EAP_TLS_FUNCS
590         if (hapd->ssl_ctx) {
591                 tls_deinit(hapd->ssl_ctx);
592                 hapd->ssl_ctx = NULL;
593         }
594 #endif /* EAP_TLS_FUNCS */
595
596 #ifdef EAP_SERVER
597         if (hapd->eap_sim_db_priv) {
598                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
599                 hapd->eap_sim_db_priv = NULL;
600         }
601 #endif /* EAP_SERVER */
602
603         if (hapd->interface_added &&
604             hostapd_bss_remove(hapd, hapd->conf->iface)) {
605                 printf("Failed to remove BSS interface %s\n",
606                        hapd->conf->iface);
607         }
608 }
609
610
611 /**
612  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
613  * @iface: Pointer to interface data
614  *
615  * This function is called before per-BSS data structures are deinitialized
616  * with hostapd_cleanup().
617  */
618 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
619 {
620 }
621
622
623 /**
624  * hostapd_cleanup_iface - Complete per-interface cleanup
625  * @iface: Pointer to interface data
626  *
627  * This function is called after per-BSS data structures are deinitialized
628  * with hostapd_cleanup().
629  */
630 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
631 {
632         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
633         iface->hw_features = NULL;
634         os_free(iface->current_rates);
635         iface->current_rates = NULL;
636         ap_list_deinit(iface);
637         hostapd_config_free(iface->conf);
638         iface->conf = NULL;
639
640         os_free(iface->config_fname);
641         os_free(iface->bss);
642         os_free(iface);
643 }
644
645
646 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
647 {
648         int i;
649
650         hostapd_broadcast_wep_set(hapd);
651
652         if (hapd->conf->ssid.wep.default_len)
653                 return 0;
654
655         for (i = 0; i < 4; i++) {
656                 if (hapd->conf->ssid.wep.key[i] &&
657                     hostapd_set_encryption(iface, hapd, "WEP", NULL,
658                                            i, hapd->conf->ssid.wep.key[i],
659                                            hapd->conf->ssid.wep.len[i],
660                                            i == hapd->conf->ssid.wep.idx)) {
661                         printf("Could not set WEP encryption.\n");
662                         return -1;
663                 }
664                 if (hapd->conf->ssid.wep.key[i] &&
665                     i == hapd->conf->ssid.wep.idx)
666                         hostapd_set_privacy(hapd, 1);
667         }
668
669         return 0;
670 }
671
672
673 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
674 {
675         int ret = 0;
676
677         wpa_printf(MSG_DEBUG, "Flushing old station entries");
678         if (hostapd_flush(hapd)) {
679                 printf("Could not connect to kernel driver.\n");
680                 ret = -1;
681         }
682         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
683         hostapd_deauth_all_stas(hapd);
684
685         return ret;
686 }
687
688
689 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
690                                     logger_level level, const char *txt)
691 {
692         struct hostapd_data *hapd = ctx;
693         int hlevel;
694
695         switch (level) {
696         case LOGGER_WARNING:
697                 hlevel = HOSTAPD_LEVEL_WARNING;
698                 break;
699         case LOGGER_INFO:
700                 hlevel = HOSTAPD_LEVEL_INFO;
701                 break;
702         case LOGGER_DEBUG:
703         default:
704                 hlevel = HOSTAPD_LEVEL_DEBUG;
705                 break;
706         }
707
708         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
709 }
710
711
712 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
713                                         u16 reason)
714 {
715         struct hostapd_data *hapd = ctx;
716         struct sta_info *sta;
717
718         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
719                    "STA " MACSTR " reason %d",
720                    __func__, MAC2STR(addr), reason);
721
722         sta = ap_get_sta(hapd, addr);
723         hostapd_sta_deauth(hapd, addr, reason);
724         if (sta == NULL)
725                 return;
726         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
727         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
728         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
729         sta->timeout_next = STA_REMOVE;
730 }
731
732
733 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
734 {
735         struct hostapd_data *hapd = ctx;
736         ieee80211_michael_mic_failure(hapd, addr, 0);
737 }
738
739
740 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
741                                        wpa_eapol_variable var, int value)
742 {
743         struct hostapd_data *hapd = ctx;
744         struct sta_info *sta = ap_get_sta(hapd, addr);
745         if (sta == NULL)
746                 return;
747         switch (var) {
748         case WPA_EAPOL_portEnabled:
749                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
750                 break;
751         case WPA_EAPOL_portValid:
752                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
753                 break;
754         case WPA_EAPOL_authorized:
755                 ieee802_1x_set_sta_authorized(hapd, sta, value);
756                 break;
757         case WPA_EAPOL_portControl_Auto:
758                 if (sta->eapol_sm)
759                         sta->eapol_sm->portControl = Auto;
760                 break;
761         case WPA_EAPOL_keyRun:
762                 if (sta->eapol_sm)
763                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
764                 break;
765         case WPA_EAPOL_keyAvailable:
766                 if (sta->eapol_sm)
767                         sta->eapol_sm->eap_if->eapKeyAvailable =
768                                 value ? TRUE : FALSE;
769                 break;
770         case WPA_EAPOL_keyDone:
771                 if (sta->eapol_sm)
772                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
773                 break;
774         case WPA_EAPOL_inc_EapolFramesTx:
775                 if (sta->eapol_sm)
776                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
777                 break;
778         }
779 }
780
781
782 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
783                                       wpa_eapol_variable var)
784 {
785         struct hostapd_data *hapd = ctx;
786         struct sta_info *sta = ap_get_sta(hapd, addr);
787         if (sta == NULL || sta->eapol_sm == NULL)
788                 return -1;
789         switch (var) {
790         case WPA_EAPOL_keyRun:
791                 return sta->eapol_sm->keyRun;
792         case WPA_EAPOL_keyAvailable:
793                 return sta->eapol_sm->eap_if->eapKeyAvailable;
794         default:
795                 return -1;
796         }
797 }
798
799
800 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
801                                            const u8 *prev_psk)
802 {
803         struct hostapd_data *hapd = ctx;
804         return hostapd_get_psk(hapd->conf, addr, prev_psk);
805 }
806
807
808 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
809                                     size_t *len)
810 {
811         struct hostapd_data *hapd = ctx;
812         const u8 *key;
813         size_t keylen;
814         struct sta_info *sta;
815
816         sta = ap_get_sta(hapd, addr);
817         if (sta == NULL)
818                 return -1;
819
820         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
821         if (key == NULL)
822                 return -1;
823
824         if (keylen > *len)
825                 keylen = *len;
826         os_memcpy(msk, key, keylen);
827         *len = keylen;
828
829         return 0;
830 }
831
832
833 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
834                                     const u8 *addr, int idx, u8 *key,
835                                     size_t key_len)
836 {
837         struct hostapd_data *hapd = ctx;
838         const char *ifname = hapd->conf->iface;
839
840         if (vlan_id > 0) {
841                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
842                 if (ifname == NULL)
843                         return -1;
844         }
845
846         return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
847                                       key, key_len, 1);
848 }
849
850
851 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
852                                        u8 *seq)
853 {
854         struct hostapd_data *hapd = ctx;
855         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
856 }
857
858
859 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
860                                             u8 *seq)
861 {
862         struct hostapd_data *hapd = ctx;
863         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
864                                        seq);
865 }
866
867
868 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
869                                        const u8 *data, size_t data_len,
870                                        int encrypt)
871 {
872         struct hostapd_data *hapd = ctx;
873         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
874 }
875
876
877 static int hostapd_wpa_auth_for_each_sta(
878         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
879         void *cb_ctx)
880 {
881         struct hostapd_data *hapd = ctx;
882         struct sta_info *sta;
883
884         for (sta = hapd->sta_list; sta; sta = sta->next) {
885                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
886                         return 1;
887         }
888         return 0;
889 }
890
891
892 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
893                                        const u8 *data, size_t data_len)
894 {
895         struct hostapd_data *hapd = ctx;
896
897         if (hapd->driver && hapd->driver->send_ether)
898                 return hapd->driver->send_ether(hapd->drv_priv, dst,
899                                                 hapd->own_addr, proto,
900                                                 data, data_len);
901         if (hapd->l2 == NULL)
902                 return -1;
903         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
904 }
905
906
907 #ifdef CONFIG_IEEE80211R
908
909 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
910                                            const u8 *data, size_t data_len)
911 {
912         struct hostapd_data *hapd = ctx;
913         int res;
914         struct ieee80211_mgmt *m;
915         size_t mlen;
916         struct sta_info *sta;
917
918         sta = ap_get_sta(hapd, dst);
919         if (sta == NULL || sta->wpa_sm == NULL)
920                 return -1;
921
922         m = os_zalloc(sizeof(*m) + data_len);
923         if (m == NULL)
924                 return -1;
925         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
926         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
927                                         WLAN_FC_STYPE_ACTION);
928         os_memcpy(m->da, dst, ETH_ALEN);
929         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
930         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
931         os_memcpy(&m->u, data, data_len);
932
933         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
934         os_free(m);
935         return res;
936 }
937
938
939 static struct wpa_state_machine *
940 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
941 {
942         struct hostapd_data *hapd = ctx;
943         struct sta_info *sta;
944
945         sta = ap_sta_add(hapd, sta_addr);
946         if (sta == NULL)
947                 return NULL;
948         if (sta->wpa_sm)
949                 return sta->wpa_sm;
950
951         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
952         if (sta->wpa_sm == NULL) {
953                 ap_free_sta(hapd, sta);
954                 return NULL;
955         }
956         sta->auth_alg = WLAN_AUTH_FT;
957
958         return sta->wpa_sm;
959 }
960
961
962 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
963                                 size_t len)
964 {
965         struct hostapd_data *hapd = ctx;
966         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
967 }
968
969 #endif /* CONFIG_IEEE80211R */
970
971
972 /**
973  * hostapd_validate_bssid_configuration - Validate BSSID configuration
974  * @iface: Pointer to interface data
975  * Returns: 0 on success, -1 on failure
976  *
977  * This function is used to validate that the configured BSSIDs are valid.
978  */
979 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
980 {
981         u8 mask[ETH_ALEN] = { 0 };
982         struct hostapd_data *hapd = iface->bss[0];
983         unsigned int i = iface->conf->num_bss, bits = 0, j;
984         int res;
985
986         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
987
988         /* Determine the bits necessary to cover the number of BSSIDs. */
989         for (i--; i; i >>= 1)
990                 bits++;
991
992         /* Determine the bits necessary to any configured BSSIDs,
993            if they are higher than the number of BSSIDs. */
994         for (j = 0; j < iface->conf->num_bss; j++) {
995                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
996                         continue;
997
998                 for (i = 0; i < ETH_ALEN; i++) {
999                         mask[i] |=
1000                                 iface->conf->bss[j].bssid[i] ^
1001                                 hapd->own_addr[i];
1002                 }
1003         }
1004
1005         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1006                 ;
1007         j = 0;
1008         if (i < ETH_ALEN) {
1009                 j = (5 - i) * 8;
1010
1011                 while (mask[i] != 0) {
1012                         mask[i] >>= 1;
1013                         j++;
1014                 }
1015         }
1016
1017         if (bits < j)
1018                 bits = j;
1019
1020         if (bits > 40)
1021                 return -1;
1022
1023         os_memset(mask, 0xff, ETH_ALEN);
1024         j = bits / 8;
1025         for (i = 5; i > 5 - j; i--)
1026                 mask[i] = 0;
1027         j = bits % 8;
1028         while (j--)
1029                 mask[i] <<= 1;
1030
1031         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1032                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1033
1034         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1035         if (res == 0)
1036                 return 0;
1037
1038         if (res < 0) {
1039                 printf("Driver did not accept BSSID mask " MACSTR " for start "
1040                        "address " MACSTR ".\n",
1041                        MAC2STR(mask), MAC2STR(hapd->own_addr));
1042                 return -1;
1043         }
1044
1045         for (i = 0; i < ETH_ALEN; i++) {
1046                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1047                         printf("Invalid BSSID mask " MACSTR " for start "
1048                                "address " MACSTR ".\n"
1049                                "Start address must be the first address in the"
1050                                " block (i.e., addr AND mask == addr).\n",
1051                                MAC2STR(mask), MAC2STR(hapd->own_addr));
1052                         return -1;
1053                 }
1054         }
1055
1056         return 0;
1057 }
1058
1059
1060 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1061 {
1062         size_t i;
1063
1064         for (i = 0; i < conf->num_bss; i++) {
1065                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1066                         return 1;
1067                 }
1068         }
1069
1070         return 0;
1071 }
1072
1073
1074 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1075 {
1076         struct wpa_auth_config _conf;
1077         struct wpa_auth_callbacks cb;
1078         const u8 *wpa_ie;
1079         size_t wpa_ie_len;
1080
1081         hostapd_wpa_auth_conf(hapd->conf, &_conf);
1082         os_memset(&cb, 0, sizeof(cb));
1083         cb.ctx = hapd;
1084         cb.logger = hostapd_wpa_auth_logger;
1085         cb.disconnect = hostapd_wpa_auth_disconnect;
1086         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1087         cb.set_eapol = hostapd_wpa_auth_set_eapol;
1088         cb.get_eapol = hostapd_wpa_auth_get_eapol;
1089         cb.get_psk = hostapd_wpa_auth_get_psk;
1090         cb.get_msk = hostapd_wpa_auth_get_msk;
1091         cb.set_key = hostapd_wpa_auth_set_key;
1092         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1093         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1094         cb.send_eapol = hostapd_wpa_auth_send_eapol;
1095         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1096         cb.send_ether = hostapd_wpa_auth_send_ether;
1097 #ifdef CONFIG_IEEE80211R
1098         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1099         cb.add_sta = hostapd_wpa_auth_add_sta;
1100 #endif /* CONFIG_IEEE80211R */
1101         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1102         if (hapd->wpa_auth == NULL) {
1103                 printf("WPA initialization failed.\n");
1104                 return -1;
1105         }
1106
1107         if (hostapd_set_privacy(hapd, 1)) {
1108                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1109                            "for interface %s", hapd->conf->iface);
1110                 return -1;
1111         }
1112
1113         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1114         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1115                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1116                            "the kernel driver.");
1117                 return -1;
1118         }
1119
1120         if (rsn_preauth_iface_init(hapd)) {
1121                 printf("Initialization of RSN pre-authentication "
1122                        "failed.\n");
1123                 return -1;
1124         }
1125
1126         return 0;
1127
1128 }
1129
1130
1131 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1132                                     struct hostapd_bss_config *conf)
1133 {
1134         struct radius_server_conf srv;
1135         os_memset(&srv, 0, sizeof(srv));
1136         srv.client_file = conf->radius_server_clients;
1137         srv.auth_port = conf->radius_server_auth_port;
1138         srv.conf_ctx = conf;
1139         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1140         srv.ssl_ctx = hapd->ssl_ctx;
1141         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1142         srv.eap_fast_a_id = conf->eap_fast_a_id;
1143         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1144         srv.tnc = conf->tnc;
1145         srv.ipv6 = conf->radius_server_ipv6;
1146         srv.get_eap_user = hostapd_radius_get_eap_user;
1147
1148         hapd->radius_srv = radius_server_init(&srv);
1149         if (hapd->radius_srv == NULL) {
1150                 printf("RADIUS server initialization failed.\n");
1151                 return -1;
1152         }
1153
1154         return 0;
1155 }
1156
1157
1158 /**
1159  * hostapd_setup_bss - Per-BSS setup (initialization)
1160  * @hapd: Pointer to BSS data
1161  * @first: Whether this BSS is the first BSS of an interface
1162  *
1163  * This function is used to initialize all per-BSS data structures and
1164  * resources. This gets called in a loop for each BSS when an interface is
1165  * initialized. Most of the modules that are initialized here will be
1166  * deinitialized in hostapd_cleanup().
1167  */
1168 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1169 {
1170         struct hostapd_bss_config *conf = hapd->conf;
1171         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1172         int ssid_len, set_ssid;
1173
1174         if (!first) {
1175                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1176                         /* Allocate the next available BSSID. */
1177                         do {
1178                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1179                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1180                 } else {
1181                         /* Allocate the configured BSSID. */
1182                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1183
1184                         if (hostapd_mac_comp(hapd->own_addr,
1185                                              hapd->iface->bss[0]->own_addr) ==
1186                             0) {
1187                                 printf("BSS '%s' may not have BSSID "
1188                                        "set to the MAC address of the radio\n",
1189                                        hapd->conf->iface);
1190                                 return -1;
1191                         }
1192                 }
1193
1194                 hapd->interface_added = 1;
1195                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1196                                     hapd->own_addr)) {
1197                         printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1198                                MAC2STR(hapd->own_addr));
1199                         return -1;
1200                 }
1201         }
1202
1203         /*
1204          * Fetch the SSID from the system and use it or,
1205          * if one was specified in the config file, verify they
1206          * match.
1207          */
1208         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1209         if (ssid_len < 0) {
1210                 printf("Could not read SSID from system\n");
1211                 return -1;
1212         }
1213         if (conf->ssid.ssid_set) {
1214                 /*
1215                  * If SSID is specified in the config file and it differs
1216                  * from what is being used then force installation of the
1217                  * new SSID.
1218                  */
1219                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1220                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1221         } else {
1222                 /*
1223                  * No SSID in the config file; just use the one we got
1224                  * from the system.
1225                  */
1226                 set_ssid = 0;
1227                 conf->ssid.ssid_len = ssid_len;
1228                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1229                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1230         }
1231
1232         printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1233                hapd->conf->iface, MAC2STR(hapd->own_addr),
1234                hapd->conf->ssid.ssid);
1235
1236         if (hostapd_setup_wpa_psk(conf)) {
1237                 printf("WPA-PSK setup failed.\n");
1238                 return -1;
1239         }
1240
1241         /* Set flag for whether SSID is broadcast in beacons */
1242         if (hostapd_set_broadcast_ssid(hapd,
1243                                        !!hapd->conf->ignore_broadcast_ssid)) {
1244                 printf("Could not set broadcast SSID flag for kernel "
1245                        "driver\n");
1246                 return -1;
1247         }
1248
1249         if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1250                 printf("Could not set DTIM period for kernel driver\n");
1251                 return -1;
1252         }
1253
1254         /* Set SSID for the kernel driver (to be used in beacon and probe
1255          * response frames) */
1256         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1257                                          conf->ssid.ssid_len)) {
1258                 printf("Could not set SSID for kernel driver\n");
1259                 return -1;
1260         }
1261
1262         if (wpa_debug_level == MSG_MSGDUMP)
1263                 conf->radius->msg_dumps = 1;
1264         hapd->radius = radius_client_init(hapd, conf->radius);
1265         if (hapd->radius == NULL) {
1266                 printf("RADIUS client initialization failed.\n");
1267                 return -1;
1268         }
1269
1270         if (hostapd_acl_init(hapd)) {
1271                 printf("ACL initialization failed.\n");
1272                 return -1;
1273         }
1274
1275         if (ieee802_1x_init(hapd)) {
1276                 printf("IEEE 802.1X initialization failed.\n");
1277                 return -1;
1278         }
1279
1280         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1281                 return -1;
1282
1283         if (accounting_init(hapd)) {
1284                 printf("Accounting initialization failed.\n");
1285                 return -1;
1286         }
1287
1288         if (hapd->conf->ieee802_11f &&
1289             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1290                 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1291                 return -1;
1292         }
1293
1294         if (hostapd_ctrl_iface_init(hapd)) {
1295                 printf("Failed to setup control interface\n");
1296                 return -1;
1297         }
1298
1299         if (vlan_init(hapd)) {
1300                 printf("VLAN initialization failed.\n");
1301                 return -1;
1302         }
1303
1304 #ifdef CONFIG_IEEE80211R
1305         hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1306                                   hostapd_rrb_receive, hapd, 0);
1307         if (hapd->l2 == NULL &&
1308             (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
1309                 printf("Failed to open l2_packet interface\n");
1310                 return -1;
1311         }
1312 #endif /* CONFIG_IEEE80211R */
1313
1314         ieee802_11_set_beacon(hapd);
1315
1316         if (conf->radius_server_clients &&
1317             hostapd_setup_radius_srv(hapd, conf))
1318                 return -1;
1319
1320         return 0;
1321 }
1322
1323
1324 /**
1325  * setup_interface2 - Setup (initialize) an interface (part 2)
1326  * @iface: Pointer to interface data.
1327  * Returns: 0 on success; -1 on failure.
1328  *
1329  * Flushes old stations, sets the channel, DFS parameters, encryption,
1330  * beacons, and WDS links based on the configuration.
1331  */
1332 static int setup_interface2(struct hostapd_iface *iface)
1333 {
1334         struct hostapd_data *hapd = iface->bss[0];
1335         int freq;
1336         size_t j;
1337         int ret = 0;
1338         u8 *prev_addr;
1339
1340         hostapd_flush_old_stations(hapd);
1341         hostapd_set_privacy(hapd, 0);
1342
1343         if (hapd->iconf->channel) {
1344                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1345                 printf("Mode: %s  Channel: %d  Frequency: %d MHz\n",
1346                        hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1347                        hapd->iconf->channel, freq);
1348
1349                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1350                         printf("Could not set channel for kernel driver\n");
1351                         return -1;
1352                 }
1353         }
1354
1355         hostapd_broadcast_wep_clear(hapd);
1356         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1357                 return -1;
1358
1359         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1360         ieee802_11_set_beacon(hapd);
1361
1362         if (hapd->iconf->rts_threshold > -1 &&
1363             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1364                 printf("Could not set RTS threshold for kernel driver\n");
1365                 return -1;
1366         }
1367
1368         if (hapd->iconf->fragm_threshold > -1 &&
1369             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1370                 printf("Could not set fragmentation threshold for kernel "
1371                        "driver\n");
1372                 return -1;
1373         }
1374
1375         prev_addr = hapd->own_addr;
1376
1377         for (j = 0; j < iface->num_bss; j++) {
1378                 hapd = iface->bss[j];
1379                 if (j)
1380                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1381                 if (hostapd_setup_bss(hapd, j == 0))
1382                         return -1;
1383                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1384                         prev_addr = hapd->own_addr;
1385         }
1386
1387         ap_list_init(iface);
1388
1389         if (hostapd_driver_commit(hapd) < 0) {
1390                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1391                            "configuration", __func__);
1392                 return -1;
1393         }
1394
1395         return ret;
1396 }
1397
1398
1399 static void setup_interface_start(void *eloop_data, void *user_ctx);
1400 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1401
1402 /**
1403  * setup_interface_finalize - Finish setup interface & call the callback
1404  * @iface: Pointer to interface data.
1405  * @status: Status of the setup interface (0 on success; -1 on failure).
1406  * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1407  */
1408 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1409 {
1410         hostapd_iface_cb cb;
1411
1412         if (!iface->setup_cb)
1413                 return -1;
1414         
1415         eloop_cancel_timeout(setup_interface_start, iface, NULL);
1416         eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1417         hostapd_select_hw_mode_stop(iface);
1418
1419         cb = iface->setup_cb;
1420
1421         iface->setup_cb = NULL;
1422
1423         cb(iface, status);
1424
1425         return 0;
1426 }
1427
1428
1429 /**
1430  * setup_interface2_wrapper - Wrapper for setup_interface2()
1431  * @iface: Pointer to interface data.
1432  * @status: Status of the hw mode select.
1433  *
1434  * Wrapper for setup_interface2() to calls finalize function upon completion.
1435  */
1436 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1437 {
1438         int ret = status;
1439         if (ret)
1440                 printf("Could not select hw_mode and channel. (%d)\n", ret);
1441         else
1442                 ret = setup_interface2(iface);
1443
1444         setup_interface_finalize(iface, ret);
1445 }
1446
1447
1448 /**
1449  * setup_interface2_handler - Used for immediate call of setup_interface2
1450  * @eloop_data: Stores the struct hostapd_iface * for the interface.
1451  * @user_ctx: Unused.
1452  */
1453 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1454 {
1455         struct hostapd_iface *iface = eloop_data;
1456
1457         setup_interface2_wrapper(iface, 0);
1458 }
1459
1460
1461 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1462                                        size_t identity_len, int phase2,
1463                                        struct eap_user *user)
1464 {
1465         const struct hostapd_eap_user *eap_user;
1466         int i, count;
1467
1468         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1469         if (eap_user == NULL)
1470                 return -1;
1471
1472         if (user == NULL)
1473                 return 0;
1474
1475         os_memset(user, 0, sizeof(*user));
1476         count = EAP_USER_MAX_METHODS;
1477         if (count > EAP_MAX_METHODS)
1478                 count = EAP_MAX_METHODS;
1479         for (i = 0; i < count; i++) {
1480                 user->methods[i].vendor = eap_user->methods[i].vendor;
1481                 user->methods[i].method = eap_user->methods[i].method;
1482         }
1483
1484         if (eap_user->password) {
1485                 user->password = os_malloc(eap_user->password_len);
1486                 if (user->password == NULL)
1487                         return -1;
1488                 os_memcpy(user->password, eap_user->password,
1489                           eap_user->password_len);
1490                 user->password_len = eap_user->password_len;
1491                 user->password_hash = eap_user->password_hash;
1492         }
1493         user->force_version = eap_user->force_version;
1494         user->ttls_auth = eap_user->ttls_auth;
1495
1496         return 0;
1497 }
1498
1499
1500 /**
1501  * setup_interface1 - Setup (initialize) an interface (part 1)
1502  * @iface: Pointer to interface data
1503  * Returns: 0 on success, -1 on failure
1504  *
1505  * Initializes the driver interface, validates the configuration,
1506  * and sets driver parameters based on the configuration.
1507  * Schedules setup_interface2() to be called immediately or after
1508  * hardware mode setup takes place. 
1509  */
1510 static int setup_interface1(struct hostapd_iface *iface)
1511 {
1512         struct hostapd_data *hapd = iface->bss[0];
1513         struct hostapd_bss_config *conf = hapd->conf;
1514         size_t i;
1515         char country[4];
1516         u8 *b = conf->bssid;
1517
1518         /*
1519          * Initialize the driver interface and make sure that all BSSes get
1520          * configured with a pointer to this driver interface.
1521          */
1522         if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1523                 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1524         } else {
1525                 hapd->drv_priv = hostapd_driver_init(hapd);
1526         }
1527
1528         if (hapd->drv_priv == NULL) {
1529                 printf("%s driver initialization failed.\n",
1530                         hapd->driver ? hapd->driver->name : "Unknown");
1531                 hapd->driver = NULL;
1532                 return -1;
1533         }
1534         for (i = 0; i < iface->num_bss; i++) {
1535                 iface->bss[i]->driver = hapd->driver;
1536                 iface->bss[i]->drv_priv = hapd->drv_priv;
1537         }
1538
1539         if (hostapd_validate_bssid_configuration(iface))
1540                 return -1;
1541
1542         os_memcpy(country, hapd->iconf->country, 3);
1543         country[3] = '\0';
1544         if (hostapd_set_country(hapd, country) < 0) {
1545                 printf("Failed to set country code\n");
1546                 return -1;
1547         }
1548
1549         if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1550                 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1551                         printf("Failed to set ieee80211d (%d)\n",
1552                                hapd->iconf->ieee80211d);
1553                         return -1;
1554                 }
1555         }
1556
1557         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1558             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1559                 printf("Failed to set bridge_packets for kernel driver\n");
1560                 return -1;
1561         }
1562
1563         /* TODO: merge with hostapd_driver_init() ? */
1564         if (hostapd_wireless_event_init(hapd) < 0)
1565                 return -1;
1566
1567         if (hostapd_get_hw_features(iface)) {
1568                 /* Not all drivers support this yet, so continue without hw
1569                  * feature data. */
1570         } else {
1571                 return hostapd_select_hw_mode_start(iface,
1572                                                     setup_interface2_wrapper);
1573         }
1574
1575         eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1576         return 0;
1577 }
1578
1579
1580 /**
1581  * setup_interface_start - Handler to start setup interface
1582  * @eloop_data: Stores the struct hostapd_iface * for the interface.
1583  * @user_ctx: Unused.
1584  *
1585  * An eloop handler is used so that all errors can be processed by the
1586  * callback without introducing stack recursion.
1587  */
1588 static void setup_interface_start(void *eloop_data, void *user_ctx)
1589 {
1590         struct hostapd_iface *iface = eloop_data;
1591
1592         int ret;
1593
1594         ret = setup_interface1(iface);
1595         if (ret)
1596                 setup_interface_finalize(iface, ret);
1597 }
1598
1599
1600 /**
1601  * hostapd_setup_interface_start - Start the setup of an interface
1602  * @iface: Pointer to interface data.
1603  * @cb: The function to callback when done.
1604  * Returns:  0 if it starts successfully; cb will be called when done.
1605  *          -1 on failure; cb will not be called.
1606  *
1607  * Initializes the driver interface, validates the configuration,
1608  * and sets driver parameters based on the configuration.
1609  * Flushes old stations, sets the channel, DFS parameters, encryption,
1610  * beacons, and WDS links based on the configuration.
1611  */
1612 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1613                                   hostapd_iface_cb cb)
1614 {
1615         if (iface->setup_cb) {
1616                 wpa_printf(MSG_DEBUG,
1617                            "%s: Interface setup already in progress.\n",
1618                            iface->bss[0]->conf->iface);
1619                 return -1;
1620         }
1621
1622         iface->setup_cb = cb;
1623
1624         eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1625
1626         return 0;
1627 }
1628
1629
1630 /**
1631  * hostapd_setup_interace_stop - Stops the setup of an interface
1632  * @iface: Pointer to interface data
1633  * Returns:  0 if successfully stopped;
1634  *          -1 on failure (i.e., was not in progress)
1635  */
1636 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1637 {
1638         return setup_interface_finalize(iface, -1);
1639 }
1640
1641
1642 static void show_version(void)
1643 {
1644         fprintf(stderr,
1645                 "hostapd v" VERSION_STR "\n"
1646                 "User space daemon for IEEE 802.11 AP management,\n"
1647                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1648                 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1649                 "and contributors\n");
1650 }
1651
1652
1653 static void usage(void)
1654 {
1655         show_version();
1656         fprintf(stderr,
1657                 "\n"
1658                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1659                 "<configuration file(s)>\n"
1660                 "\n"
1661                 "options:\n"
1662                 "   -h   show this usage\n"
1663                 "   -d   show more debug messages (-dd for even more)\n"
1664                 "   -B   run daemon in the background\n"
1665                 "   -P   PID file\n"
1666                 "   -K   include key data in debug messages\n"
1667                 "   -t   include timestamps in some debug messages\n"
1668                 "   -v   show hostapd version\n");
1669
1670         exit(1);
1671 }
1672
1673
1674 /**
1675  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1676  * @hapd_iface: Pointer to interface data
1677  * @conf: Pointer to per-interface configuration
1678  * @bss: Pointer to per-BSS configuration for this BSS
1679  * Returns: Pointer to allocated BSS data
1680  *
1681  * This function is used to allocate per-BSS data structure. This data will be
1682  * freed after hostapd_cleanup() is called for it during interface
1683  * deinitialization.
1684  */
1685 static struct hostapd_data *
1686 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1687                        struct hostapd_config *conf,
1688                        struct hostapd_bss_config *bss)
1689 {
1690         struct hostapd_data *hapd;
1691
1692         hapd = os_zalloc(sizeof(*hapd));
1693         if (hapd == NULL)
1694                 return NULL;
1695
1696         hapd->iconf = conf;
1697         hapd->conf = bss;
1698         hapd->iface = hapd_iface;
1699
1700         if (hapd->conf->individual_wep_key_len > 0) {
1701                 /* use key0 in individual key and key1 in broadcast key */
1702                 hapd->default_wep_key_idx = 1;
1703         }
1704
1705 #ifdef EAP_TLS_FUNCS
1706         if (hapd->conf->eap_server &&
1707             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1708              hapd->conf->dh_file)) {
1709                 struct tls_connection_params params;
1710
1711                 hapd->ssl_ctx = tls_init(NULL);
1712                 if (hapd->ssl_ctx == NULL) {
1713                         printf("Failed to initialize TLS\n");
1714                         goto fail;
1715                 }
1716
1717                 os_memset(&params, 0, sizeof(params));
1718                 params.ca_cert = hapd->conf->ca_cert;
1719                 params.client_cert = hapd->conf->server_cert;
1720                 params.private_key = hapd->conf->private_key;
1721                 params.private_key_passwd = hapd->conf->private_key_passwd;
1722                 params.dh_file = hapd->conf->dh_file;
1723
1724                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1725                         printf("Failed to set TLS parameters\n");
1726                         goto fail;
1727                 }
1728
1729                 if (tls_global_set_verify(hapd->ssl_ctx,
1730                                           hapd->conf->check_crl)) {
1731                         printf("Failed to enable check_crl\n");
1732                         goto fail;
1733                 }
1734         }
1735 #endif /* EAP_TLS_FUNCS */
1736
1737 #ifdef EAP_SERVER
1738         if (hapd->conf->eap_sim_db) {
1739                 hapd->eap_sim_db_priv =
1740                         eap_sim_db_init(hapd->conf->eap_sim_db,
1741                                         hostapd_sim_db_cb, hapd);
1742                 if (hapd->eap_sim_db_priv == NULL) {
1743                         printf("Failed to initialize EAP-SIM database "
1744                                "interface\n");
1745                         goto fail;
1746                 }
1747         }
1748 #endif /* EAP_SERVER */
1749
1750         if (hapd->conf->assoc_ap)
1751                 hapd->assoc_ap_state = WAIT_BEACON;
1752
1753         hapd->driver = hapd->iconf->driver;
1754
1755         return hapd;
1756
1757 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1758 fail:
1759 #endif
1760         /* TODO: cleanup allocated resources(?) */
1761         os_free(hapd);
1762         return NULL;
1763 }
1764
1765
1766 /**
1767  * hostapd_init - Allocate and initialize per-interface data
1768  * @config_file: Path to the configuration file
1769  * Returns: Pointer to the allocated interface data or %NULL on failure
1770  *
1771  * This function is used to allocate main data structures for per-interface
1772  * data. The allocated data buffer will be freed by calling
1773  * hostapd_cleanup_iface().
1774  */
1775 static struct hostapd_iface * hostapd_init(const char *config_file)
1776 {
1777         struct hostapd_iface *hapd_iface = NULL;
1778         struct hostapd_config *conf = NULL;
1779         struct hostapd_data *hapd;
1780         size_t i;
1781
1782         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1783         if (hapd_iface == NULL)
1784                 goto fail;
1785
1786         hapd_iface->config_fname = os_strdup(config_file);
1787         if (hapd_iface->config_fname == NULL)
1788                 goto fail;
1789
1790         conf = hostapd_config_read(hapd_iface->config_fname);
1791         if (conf == NULL)
1792                 goto fail;
1793         hapd_iface->conf = conf;
1794
1795         hapd_iface->num_bss = conf->num_bss;
1796         hapd_iface->bss = os_zalloc(conf->num_bss *
1797                                     sizeof(struct hostapd_data *));
1798         if (hapd_iface->bss == NULL)
1799                 goto fail;
1800
1801         for (i = 0; i < conf->num_bss; i++) {
1802                 hapd = hapd_iface->bss[i] =
1803                         hostapd_alloc_bss_data(hapd_iface, conf,
1804                                                &conf->bss[i]);
1805                 if (hapd == NULL)
1806                         goto fail;
1807         }
1808
1809         return hapd_iface;
1810
1811 fail:
1812         if (conf)
1813                 hostapd_config_free(conf);
1814         if (hapd_iface) {
1815                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1816                         hapd = hapd_iface->bss[i];
1817                         if (hapd && hapd->ssl_ctx)
1818                                 tls_deinit(hapd->ssl_ctx);
1819                 }
1820
1821                 os_free(hapd_iface->config_fname);
1822                 os_free(hapd_iface->bss);
1823                 os_free(hapd_iface);
1824         }
1825         return NULL;
1826 }
1827
1828
1829 /**
1830  * register_drivers - Register driver interfaces
1831  *
1832  * This function is generated by Makefile (into driver_conf.c) to call all
1833  * configured driver interfaces to register them to core hostapd.
1834  */
1835 void register_drivers(void);
1836
1837
1838 /**
1839  * setup_interface_done - Callback when an interface is done being setup.
1840  * @iface: Pointer to interface data.
1841  * @status: Status of the interface setup (0 on success; -1 on failure).
1842  */
1843 static void setup_interface_done(struct hostapd_iface *iface, int status)
1844 {
1845         if (status) {
1846                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1847                            iface->bss[0]->conf->iface);
1848                 eloop_terminate();
1849         } else
1850                 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1851                            iface->bss[0]->conf->iface);
1852 }
1853
1854
1855 int main(int argc, char *argv[])
1856 {
1857         struct hapd_interfaces interfaces;
1858         int ret = 1, k;
1859         size_t i, j;
1860         int c, debug = 0, daemonize = 0, tnc = 0;
1861         const char *pid_file = NULL;
1862
1863         hostapd_logger_register_cb(hostapd_logger_cb);
1864
1865         for (;;) {
1866                 c = getopt(argc, argv, "BdhKP:tv");
1867                 if (c < 0)
1868                         break;
1869                 switch (c) {
1870                 case 'h':
1871                         usage();
1872                         break;
1873                 case 'd':
1874                         debug++;
1875                         if (wpa_debug_level > 0)
1876                                 wpa_debug_level--;
1877                         break;
1878                 case 'B':
1879                         daemonize++;
1880                         break;
1881                 case 'K':
1882                         wpa_debug_show_keys++;
1883                         break;
1884                 case 'P':
1885                         pid_file = optarg;
1886                         break;
1887                 case 't':
1888                         wpa_debug_timestamp++;
1889                         break;
1890                 case 'v':
1891                         show_version();
1892                         exit(1);
1893                         break;
1894
1895                 default:
1896                         usage();
1897                         break;
1898                 }
1899         }
1900
1901         if (optind == argc)
1902                 usage();
1903
1904         if (eap_server_register_methods()) {
1905                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1906                 return -1;
1907         }
1908
1909         interfaces.count = argc - optind;
1910
1911         interfaces.iface = os_malloc(interfaces.count *
1912                                      sizeof(struct hostapd_iface *));
1913         if (interfaces.iface == NULL) {
1914                 wpa_printf(MSG_ERROR, "malloc failed\n");
1915                 return -1;
1916         }
1917
1918         if (eloop_init(&interfaces)) {
1919                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1920                 return -1;
1921         }
1922
1923 #ifndef CONFIG_NATIVE_WINDOWS
1924         eloop_register_signal(SIGHUP, handle_reload, NULL);
1925         eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1926 #endif /* CONFIG_NATIVE_WINDOWS */
1927         eloop_register_signal_terminate(handle_term, NULL);
1928
1929         /* Initialize interfaces */
1930         for (i = 0; i < interfaces.count; i++) {
1931                 printf("Configuration file: %s\n", argv[optind + i]);
1932                 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1933                 if (!interfaces.iface[i])
1934                         goto out;
1935                 for (k = 0; k < debug; k++) {
1936                         if (interfaces.iface[i]->bss[0]->conf->
1937                             logger_stdout_level > 0)
1938                                 interfaces.iface[i]->bss[0]->conf->
1939                                         logger_stdout_level--;
1940                 }
1941
1942                 ret = hostapd_setup_interface_start(interfaces.iface[i],
1943                                                     setup_interface_done);
1944                 if (ret)
1945                         goto out;
1946
1947                 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1948                         if (interfaces.iface[i]->bss[0]->conf->tnc)
1949                                 tnc++;
1950                 }
1951         }
1952
1953 #ifdef EAP_TNC
1954         if (tnc && tncs_global_init() < 0) {
1955                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1956                 goto out;
1957         }
1958 #endif /* EAP_TNC */
1959
1960         if (daemonize && os_daemonize(pid_file)) {
1961                 perror("daemon");
1962                 goto out;
1963         }
1964
1965 #ifndef CONFIG_NATIVE_WINDOWS
1966         openlog("hostapd", 0, LOG_DAEMON);
1967 #endif /* CONFIG_NATIVE_WINDOWS */
1968
1969         eloop_run();
1970
1971         /* Disconnect associated stations from all interfaces and BSSes */
1972         for (i = 0; i < interfaces.count; i++) {
1973                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1974                         struct hostapd_data *hapd =
1975                                 interfaces.iface[i]->bss[j];
1976                         hostapd_free_stas(hapd);
1977                         hostapd_flush_old_stations(hapd);
1978                 }
1979         }
1980
1981         ret = 0;
1982
1983  out:
1984         /* Deinitialize all interfaces */
1985         for (i = 0; i < interfaces.count; i++) {
1986                 if (!interfaces.iface[i])
1987                         continue;
1988                 hostapd_setup_interface_stop(interfaces.iface[i]);
1989                 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1990                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1991                         struct hostapd_data *hapd =
1992                                 interfaces.iface[i]->bss[j];
1993                         hostapd_cleanup(hapd);
1994                         if (j == interfaces.iface[i]->num_bss - 1 &&
1995                             hapd->driver)
1996                                 hostapd_driver_deinit(hapd);
1997                 }
1998                 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1999                         os_free(interfaces.iface[i]->bss[j]);
2000                 hostapd_cleanup_iface(interfaces.iface[i]);
2001         }
2002         os_free(interfaces.iface);
2003
2004 #ifdef EAP_TNC
2005         tncs_global_deinit();
2006 #endif /* EAP_TNC */
2007
2008         eloop_destroy();
2009
2010 #ifndef CONFIG_NATIVE_WINDOWS
2011         closelog();
2012 #endif /* CONFIG_NATIVE_WINDOWS */
2013
2014         eap_server_unregister_methods();
2015
2016         os_daemonize_terminate(pid_file);
2017
2018         return ret;
2019 }