Added WLAN_STA_MFP flag for driver wrappers so that they can configure the
[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%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_WME ? "[WME]" : ""),
418                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
419                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
420                         sta->capability,
421                         sta->listen_interval);
422
423                 fprintf(f, "  supported_rates=");
424                 for (i = 0; i < sta->supported_rates_len; i++)
425                         fprintf(f, "%02x ", sta->supported_rates[i]);
426                 fprintf(f, "\n");
427
428                 fprintf(f,
429                         "  timeout_next=%s\n",
430                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
431                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
432                           "DEAUTH")));
433
434                 ieee802_1x_dump_state(f, "  ", sta);
435         }
436
437         buf = os_malloc(4096);
438         if (buf) {
439                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
440                 if (count < 0)
441                         count = 0;
442                 else if (count > 4095)
443                         count = 4095;
444                 buf[count] = '\0';
445                 fprintf(f, "%s", buf);
446
447                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
448                 if (count < 0)
449                         count = 0;
450                 else if (count > 4095)
451                         count = 4095;
452                 buf[count] = '\0';
453                 fprintf(f, "%s", buf);
454                 os_free(buf);
455         }
456         fclose(f);
457 }
458 #endif /* HOSTAPD_DUMP_STATE */
459
460
461 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
462 {
463 #ifdef HOSTAPD_DUMP_STATE
464         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
465         size_t i, j;
466
467         for (i = 0; i < hapds->count; i++) {
468                 struct hostapd_iface *hapd_iface = hapds->iface[i];
469                 for (j = 0; j < hapd_iface->num_bss; j++)
470                         hostapd_dump_state(hapd_iface->bss[j]);
471         }
472 #endif /* HOSTAPD_DUMP_STATE */
473 }
474 #endif /* CONFIG_NATIVE_WINDOWS */
475
476 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
477                                               char *ifname)
478 {
479         int i;
480
481         for (i = 0; i < NUM_WEP_KEYS; i++) {
482                 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
483                                            0, i == 0 ? 1 : 0)) {
484                         printf("Failed to clear default encryption keys "
485                                "(ifname=%s keyidx=%d)\n", ifname, i);
486                 }
487         }
488 }
489
490
491 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
492 {
493         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
494         return 0;
495 }
496
497
498 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
499 {
500         int errors = 0, idx;
501         struct hostapd_ssid *ssid = &hapd->conf->ssid;
502
503         idx = ssid->wep.idx;
504         if (ssid->wep.default_len &&
505             hostapd_set_encryption(hapd->conf->iface,
506                                    hapd, "WEP", NULL, idx,
507                                    ssid->wep.key[idx],
508                                    ssid->wep.len[idx],
509                                    idx == ssid->wep.idx)) {
510                 printf("Could not set WEP encryption.\n");
511                 errors++;
512         }
513
514         if (ssid->dyn_vlan_keys) {
515                 size_t i;
516                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
517                         const char *ifname;
518                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
519                         if (key == NULL)
520                                 continue;
521                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
522                                                             i);
523                         if (ifname == NULL)
524                                 continue;
525
526                         idx = key->idx;
527                         if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
528                                                    idx, key->key[idx],
529                                                    key->len[idx],
530                                                    idx == key->idx)) {
531                                 printf("Could not set dynamic VLAN WEP "
532                                        "encryption.\n");
533                                 errors++;
534                         }
535                 }
536         }
537
538         return errors;
539 }
540
541 /**
542  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
543  * @hapd: Pointer to BSS data
544  *
545  * This function is used to free all per-BSS data structures and resources.
546  * This gets called in a loop for each BSS between calls to
547  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
548  * is deinitialized. Most of the modules that are initialized in
549  * hostapd_setup_bss() are deinitialized here.
550  */
551 static void hostapd_cleanup(struct hostapd_data *hapd)
552 {
553         hostapd_ctrl_iface_deinit(hapd);
554
555         os_free(hapd->default_wep_key);
556         hapd->default_wep_key = NULL;
557         iapp_deinit(hapd->iapp);
558         hapd->iapp = NULL;
559         accounting_deinit(hapd);
560         rsn_preauth_iface_deinit(hapd);
561         if (hapd->wpa_auth) {
562                 wpa_deinit(hapd->wpa_auth);
563                 hapd->wpa_auth = NULL;
564
565                 if (hostapd_set_privacy(hapd, 0)) {
566                         wpa_printf(MSG_DEBUG, "Could not disable "
567                                    "PrivacyInvoked for interface %s",
568                                    hapd->conf->iface);
569                 }
570
571                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
572                         wpa_printf(MSG_DEBUG, "Could not remove generic "
573                                    "information element from interface %s",
574                                    hapd->conf->iface);
575                 }
576         }
577         ieee802_1x_deinit(hapd);
578         vlan_deinit(hapd);
579         hostapd_acl_deinit(hapd);
580         radius_client_deinit(hapd->radius);
581         hapd->radius = NULL;
582         radius_server_deinit(hapd->radius_srv);
583         hapd->radius_srv = NULL;
584
585 #ifdef CONFIG_IEEE80211R
586         l2_packet_deinit(hapd->l2);
587 #endif /* CONFIG_IEEE80211R */
588
589         hostapd_wireless_event_deinit(hapd);
590
591 #ifdef EAP_TLS_FUNCS
592         if (hapd->ssl_ctx) {
593                 tls_deinit(hapd->ssl_ctx);
594                 hapd->ssl_ctx = NULL;
595         }
596 #endif /* EAP_TLS_FUNCS */
597
598 #ifdef EAP_SERVER
599         if (hapd->eap_sim_db_priv) {
600                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
601                 hapd->eap_sim_db_priv = NULL;
602         }
603 #endif /* EAP_SERVER */
604
605         if (hapd->interface_added &&
606             hostapd_bss_remove(hapd, hapd->conf->iface)) {
607                 printf("Failed to remove BSS interface %s\n",
608                        hapd->conf->iface);
609         }
610 }
611
612
613 /**
614  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
615  * @iface: Pointer to interface data
616  *
617  * This function is called before per-BSS data structures are deinitialized
618  * with hostapd_cleanup().
619  */
620 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
621 {
622 }
623
624
625 /**
626  * hostapd_cleanup_iface - Complete per-interface cleanup
627  * @iface: Pointer to interface data
628  *
629  * This function is called after per-BSS data structures are deinitialized
630  * with hostapd_cleanup().
631  */
632 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
633 {
634         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
635         iface->hw_features = NULL;
636         os_free(iface->current_rates);
637         iface->current_rates = NULL;
638         ap_list_deinit(iface);
639         hostapd_config_free(iface->conf);
640         iface->conf = NULL;
641
642         os_free(iface->config_fname);
643         os_free(iface->bss);
644         os_free(iface);
645 }
646
647
648 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
649 {
650         int i;
651
652         hostapd_broadcast_wep_set(hapd);
653
654         if (hapd->conf->ssid.wep.default_len)
655                 return 0;
656
657         for (i = 0; i < 4; i++) {
658                 if (hapd->conf->ssid.wep.key[i] &&
659                     hostapd_set_encryption(iface, hapd, "WEP", NULL,
660                                            i, hapd->conf->ssid.wep.key[i],
661                                            hapd->conf->ssid.wep.len[i],
662                                            i == hapd->conf->ssid.wep.idx)) {
663                         printf("Could not set WEP encryption.\n");
664                         return -1;
665                 }
666                 if (hapd->conf->ssid.wep.key[i] &&
667                     i == hapd->conf->ssid.wep.idx)
668                         hostapd_set_privacy(hapd, 1);
669         }
670
671         return 0;
672 }
673
674
675 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
676 {
677         int ret = 0;
678
679         wpa_printf(MSG_DEBUG, "Flushing old station entries");
680         if (hostapd_flush(hapd)) {
681                 printf("Could not connect to kernel driver.\n");
682                 ret = -1;
683         }
684         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
685         hostapd_deauth_all_stas(hapd);
686
687         return ret;
688 }
689
690
691 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
692                                     logger_level level, const char *txt)
693 {
694         struct hostapd_data *hapd = ctx;
695         int hlevel;
696
697         switch (level) {
698         case LOGGER_WARNING:
699                 hlevel = HOSTAPD_LEVEL_WARNING;
700                 break;
701         case LOGGER_INFO:
702                 hlevel = HOSTAPD_LEVEL_INFO;
703                 break;
704         case LOGGER_DEBUG:
705         default:
706                 hlevel = HOSTAPD_LEVEL_DEBUG;
707                 break;
708         }
709
710         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
711 }
712
713
714 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
715                                         u16 reason)
716 {
717         struct hostapd_data *hapd = ctx;
718         struct sta_info *sta;
719
720         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
721                    "STA " MACSTR " reason %d",
722                    __func__, MAC2STR(addr), reason);
723
724         sta = ap_get_sta(hapd, addr);
725         hostapd_sta_deauth(hapd, addr, reason);
726         if (sta == NULL)
727                 return;
728         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
729         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
730         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
731         sta->timeout_next = STA_REMOVE;
732 }
733
734
735 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
736 {
737         struct hostapd_data *hapd = ctx;
738         ieee80211_michael_mic_failure(hapd, addr, 0);
739 }
740
741
742 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
743                                        wpa_eapol_variable var, int value)
744 {
745         struct hostapd_data *hapd = ctx;
746         struct sta_info *sta = ap_get_sta(hapd, addr);
747         if (sta == NULL)
748                 return;
749         switch (var) {
750         case WPA_EAPOL_portEnabled:
751                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
752                 break;
753         case WPA_EAPOL_portValid:
754                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
755                 break;
756         case WPA_EAPOL_authorized:
757                 ieee802_1x_set_sta_authorized(hapd, sta, value);
758                 break;
759         case WPA_EAPOL_portControl_Auto:
760                 if (sta->eapol_sm)
761                         sta->eapol_sm->portControl = Auto;
762                 break;
763         case WPA_EAPOL_keyRun:
764                 if (sta->eapol_sm)
765                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
766                 break;
767         case WPA_EAPOL_keyAvailable:
768                 if (sta->eapol_sm)
769                         sta->eapol_sm->eap_if->eapKeyAvailable =
770                                 value ? TRUE : FALSE;
771                 break;
772         case WPA_EAPOL_keyDone:
773                 if (sta->eapol_sm)
774                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
775                 break;
776         case WPA_EAPOL_inc_EapolFramesTx:
777                 if (sta->eapol_sm)
778                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
779                 break;
780         }
781 }
782
783
784 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
785                                       wpa_eapol_variable var)
786 {
787         struct hostapd_data *hapd = ctx;
788         struct sta_info *sta = ap_get_sta(hapd, addr);
789         if (sta == NULL || sta->eapol_sm == NULL)
790                 return -1;
791         switch (var) {
792         case WPA_EAPOL_keyRun:
793                 return sta->eapol_sm->keyRun;
794         case WPA_EAPOL_keyAvailable:
795                 return sta->eapol_sm->eap_if->eapKeyAvailable;
796         default:
797                 return -1;
798         }
799 }
800
801
802 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
803                                            const u8 *prev_psk)
804 {
805         struct hostapd_data *hapd = ctx;
806         return hostapd_get_psk(hapd->conf, addr, prev_psk);
807 }
808
809
810 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
811                                     size_t *len)
812 {
813         struct hostapd_data *hapd = ctx;
814         const u8 *key;
815         size_t keylen;
816         struct sta_info *sta;
817
818         sta = ap_get_sta(hapd, addr);
819         if (sta == NULL)
820                 return -1;
821
822         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
823         if (key == NULL)
824                 return -1;
825
826         if (keylen > *len)
827                 keylen = *len;
828         os_memcpy(msk, key, keylen);
829         *len = keylen;
830
831         return 0;
832 }
833
834
835 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
836                                     const u8 *addr, int idx, u8 *key,
837                                     size_t key_len)
838 {
839         struct hostapd_data *hapd = ctx;
840         const char *ifname = hapd->conf->iface;
841
842         if (vlan_id > 0) {
843                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
844                 if (ifname == NULL)
845                         return -1;
846         }
847
848         return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
849                                       key, key_len, 1);
850 }
851
852
853 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
854                                        u8 *seq)
855 {
856         struct hostapd_data *hapd = ctx;
857         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
858 }
859
860
861 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
862                                             u8 *seq)
863 {
864         struct hostapd_data *hapd = ctx;
865         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
866                                        seq);
867 }
868
869
870 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
871                                        const u8 *data, size_t data_len,
872                                        int encrypt)
873 {
874         struct hostapd_data *hapd = ctx;
875         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
876 }
877
878
879 static int hostapd_wpa_auth_for_each_sta(
880         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
881         void *cb_ctx)
882 {
883         struct hostapd_data *hapd = ctx;
884         struct sta_info *sta;
885
886         for (sta = hapd->sta_list; sta; sta = sta->next) {
887                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
888                         return 1;
889         }
890         return 0;
891 }
892
893
894 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
895                                        const u8 *data, size_t data_len)
896 {
897         struct hostapd_data *hapd = ctx;
898
899         if (hapd->driver && hapd->driver->send_ether)
900                 return hapd->driver->send_ether(hapd->drv_priv, dst,
901                                                 hapd->own_addr, proto,
902                                                 data, data_len);
903         if (hapd->l2 == NULL)
904                 return -1;
905         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
906 }
907
908
909 #ifdef CONFIG_IEEE80211R
910
911 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
912                                            const u8 *data, size_t data_len)
913 {
914         struct hostapd_data *hapd = ctx;
915         int res;
916         struct ieee80211_mgmt *m;
917         size_t mlen;
918         struct sta_info *sta;
919
920         sta = ap_get_sta(hapd, dst);
921         if (sta == NULL || sta->wpa_sm == NULL)
922                 return -1;
923
924         m = os_zalloc(sizeof(*m) + data_len);
925         if (m == NULL)
926                 return -1;
927         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
928         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
929                                         WLAN_FC_STYPE_ACTION);
930         os_memcpy(m->da, dst, ETH_ALEN);
931         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
932         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
933         os_memcpy(&m->u, data, data_len);
934
935         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
936         os_free(m);
937         return res;
938 }
939
940
941 static struct wpa_state_machine *
942 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
943 {
944         struct hostapd_data *hapd = ctx;
945         struct sta_info *sta;
946
947         sta = ap_sta_add(hapd, sta_addr);
948         if (sta == NULL)
949                 return NULL;
950         if (sta->wpa_sm)
951                 return sta->wpa_sm;
952
953         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
954         if (sta->wpa_sm == NULL) {
955                 ap_free_sta(hapd, sta);
956                 return NULL;
957         }
958         sta->auth_alg = WLAN_AUTH_FT;
959
960         return sta->wpa_sm;
961 }
962
963
964 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
965                                 size_t len)
966 {
967         struct hostapd_data *hapd = ctx;
968         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
969 }
970
971 #endif /* CONFIG_IEEE80211R */
972
973
974 /**
975  * hostapd_validate_bssid_configuration - Validate BSSID configuration
976  * @iface: Pointer to interface data
977  * Returns: 0 on success, -1 on failure
978  *
979  * This function is used to validate that the configured BSSIDs are valid.
980  */
981 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
982 {
983         u8 mask[ETH_ALEN] = { 0 };
984         struct hostapd_data *hapd = iface->bss[0];
985         unsigned int i = iface->conf->num_bss, bits = 0, j;
986         int res;
987
988         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
989
990         /* Determine the bits necessary to cover the number of BSSIDs. */
991         for (i--; i; i >>= 1)
992                 bits++;
993
994         /* Determine the bits necessary to any configured BSSIDs,
995            if they are higher than the number of BSSIDs. */
996         for (j = 0; j < iface->conf->num_bss; j++) {
997                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
998                         continue;
999
1000                 for (i = 0; i < ETH_ALEN; i++) {
1001                         mask[i] |=
1002                                 iface->conf->bss[j].bssid[i] ^
1003                                 hapd->own_addr[i];
1004                 }
1005         }
1006
1007         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1008                 ;
1009         j = 0;
1010         if (i < ETH_ALEN) {
1011                 j = (5 - i) * 8;
1012
1013                 while (mask[i] != 0) {
1014                         mask[i] >>= 1;
1015                         j++;
1016                 }
1017         }
1018
1019         if (bits < j)
1020                 bits = j;
1021
1022         if (bits > 40)
1023                 return -1;
1024
1025         os_memset(mask, 0xff, ETH_ALEN);
1026         j = bits / 8;
1027         for (i = 5; i > 5 - j; i--)
1028                 mask[i] = 0;
1029         j = bits % 8;
1030         while (j--)
1031                 mask[i] <<= 1;
1032
1033         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1034                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1035
1036         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1037         if (res == 0)
1038                 return 0;
1039
1040         if (res < 0) {
1041                 printf("Driver did not accept BSSID mask " MACSTR " for start "
1042                        "address " MACSTR ".\n",
1043                        MAC2STR(mask), MAC2STR(hapd->own_addr));
1044                 return -1;
1045         }
1046
1047         for (i = 0; i < ETH_ALEN; i++) {
1048                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1049                         printf("Invalid BSSID mask " MACSTR " for start "
1050                                "address " MACSTR ".\n"
1051                                "Start address must be the first address in the"
1052                                " block (i.e., addr AND mask == addr).\n",
1053                                MAC2STR(mask), MAC2STR(hapd->own_addr));
1054                         return -1;
1055                 }
1056         }
1057
1058         return 0;
1059 }
1060
1061
1062 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1063 {
1064         size_t i;
1065
1066         for (i = 0; i < conf->num_bss; i++) {
1067                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1068                         return 1;
1069                 }
1070         }
1071
1072         return 0;
1073 }
1074
1075
1076 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1077 {
1078         struct wpa_auth_config _conf;
1079         struct wpa_auth_callbacks cb;
1080         const u8 *wpa_ie;
1081         size_t wpa_ie_len;
1082
1083         hostapd_wpa_auth_conf(hapd->conf, &_conf);
1084         os_memset(&cb, 0, sizeof(cb));
1085         cb.ctx = hapd;
1086         cb.logger = hostapd_wpa_auth_logger;
1087         cb.disconnect = hostapd_wpa_auth_disconnect;
1088         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1089         cb.set_eapol = hostapd_wpa_auth_set_eapol;
1090         cb.get_eapol = hostapd_wpa_auth_get_eapol;
1091         cb.get_psk = hostapd_wpa_auth_get_psk;
1092         cb.get_msk = hostapd_wpa_auth_get_msk;
1093         cb.set_key = hostapd_wpa_auth_set_key;
1094         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1095         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1096         cb.send_eapol = hostapd_wpa_auth_send_eapol;
1097         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1098         cb.send_ether = hostapd_wpa_auth_send_ether;
1099 #ifdef CONFIG_IEEE80211R
1100         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1101         cb.add_sta = hostapd_wpa_auth_add_sta;
1102 #endif /* CONFIG_IEEE80211R */
1103         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1104         if (hapd->wpa_auth == NULL) {
1105                 printf("WPA initialization failed.\n");
1106                 return -1;
1107         }
1108
1109         if (hostapd_set_privacy(hapd, 1)) {
1110                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1111                            "for interface %s", hapd->conf->iface);
1112                 return -1;
1113         }
1114
1115         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1116         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1117                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1118                            "the kernel driver.");
1119                 return -1;
1120         }
1121
1122         if (rsn_preauth_iface_init(hapd)) {
1123                 printf("Initialization of RSN pre-authentication "
1124                        "failed.\n");
1125                 return -1;
1126         }
1127
1128         return 0;
1129
1130 }
1131
1132
1133 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1134                                     struct hostapd_bss_config *conf)
1135 {
1136         struct radius_server_conf srv;
1137         os_memset(&srv, 0, sizeof(srv));
1138         srv.client_file = conf->radius_server_clients;
1139         srv.auth_port = conf->radius_server_auth_port;
1140         srv.conf_ctx = conf;
1141         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1142         srv.ssl_ctx = hapd->ssl_ctx;
1143         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1144         srv.eap_fast_a_id = conf->eap_fast_a_id;
1145         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1146         srv.tnc = conf->tnc;
1147         srv.ipv6 = conf->radius_server_ipv6;
1148         srv.get_eap_user = hostapd_radius_get_eap_user;
1149
1150         hapd->radius_srv = radius_server_init(&srv);
1151         if (hapd->radius_srv == NULL) {
1152                 printf("RADIUS server initialization failed.\n");
1153                 return -1;
1154         }
1155
1156         return 0;
1157 }
1158
1159
1160 /**
1161  * hostapd_setup_bss - Per-BSS setup (initialization)
1162  * @hapd: Pointer to BSS data
1163  * @first: Whether this BSS is the first BSS of an interface
1164  *
1165  * This function is used to initialize all per-BSS data structures and
1166  * resources. This gets called in a loop for each BSS when an interface is
1167  * initialized. Most of the modules that are initialized here will be
1168  * deinitialized in hostapd_cleanup().
1169  */
1170 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1171 {
1172         struct hostapd_bss_config *conf = hapd->conf;
1173         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1174         int ssid_len, set_ssid;
1175
1176         if (!first) {
1177                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1178                         /* Allocate the next available BSSID. */
1179                         do {
1180                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1181                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1182                 } else {
1183                         /* Allocate the configured BSSID. */
1184                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1185
1186                         if (hostapd_mac_comp(hapd->own_addr,
1187                                              hapd->iface->bss[0]->own_addr) ==
1188                             0) {
1189                                 printf("BSS '%s' may not have BSSID "
1190                                        "set to the MAC address of the radio\n",
1191                                        hapd->conf->iface);
1192                                 return -1;
1193                         }
1194                 }
1195
1196                 hapd->interface_added = 1;
1197                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1198                                     hapd->own_addr)) {
1199                         printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1200                                MAC2STR(hapd->own_addr));
1201                         return -1;
1202                 }
1203         }
1204
1205         /*
1206          * Fetch the SSID from the system and use it or,
1207          * if one was specified in the config file, verify they
1208          * match.
1209          */
1210         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1211         if (ssid_len < 0) {
1212                 printf("Could not read SSID from system\n");
1213                 return -1;
1214         }
1215         if (conf->ssid.ssid_set) {
1216                 /*
1217                  * If SSID is specified in the config file and it differs
1218                  * from what is being used then force installation of the
1219                  * new SSID.
1220                  */
1221                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1222                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1223         } else {
1224                 /*
1225                  * No SSID in the config file; just use the one we got
1226                  * from the system.
1227                  */
1228                 set_ssid = 0;
1229                 conf->ssid.ssid_len = ssid_len;
1230                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1231                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1232         }
1233
1234         printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1235                hapd->conf->iface, MAC2STR(hapd->own_addr),
1236                hapd->conf->ssid.ssid);
1237
1238         if (hostapd_setup_wpa_psk(conf)) {
1239                 printf("WPA-PSK setup failed.\n");
1240                 return -1;
1241         }
1242
1243         /* Set flag for whether SSID is broadcast in beacons */
1244         if (hostapd_set_broadcast_ssid(hapd,
1245                                        !!hapd->conf->ignore_broadcast_ssid)) {
1246                 printf("Could not set broadcast SSID flag for kernel "
1247                        "driver\n");
1248                 return -1;
1249         }
1250
1251         if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1252                 printf("Could not set DTIM period for kernel driver\n");
1253                 return -1;
1254         }
1255
1256         /* Set SSID for the kernel driver (to be used in beacon and probe
1257          * response frames) */
1258         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1259                                          conf->ssid.ssid_len)) {
1260                 printf("Could not set SSID for kernel driver\n");
1261                 return -1;
1262         }
1263
1264         if (wpa_debug_level == MSG_MSGDUMP)
1265                 conf->radius->msg_dumps = 1;
1266         hapd->radius = radius_client_init(hapd, conf->radius);
1267         if (hapd->radius == NULL) {
1268                 printf("RADIUS client initialization failed.\n");
1269                 return -1;
1270         }
1271
1272         if (hostapd_acl_init(hapd)) {
1273                 printf("ACL initialization failed.\n");
1274                 return -1;
1275         }
1276
1277         if (ieee802_1x_init(hapd)) {
1278                 printf("IEEE 802.1X initialization failed.\n");
1279                 return -1;
1280         }
1281
1282         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1283                 return -1;
1284
1285         if (accounting_init(hapd)) {
1286                 printf("Accounting initialization failed.\n");
1287                 return -1;
1288         }
1289
1290         if (hapd->conf->ieee802_11f &&
1291             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1292                 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1293                 return -1;
1294         }
1295
1296         if (hostapd_ctrl_iface_init(hapd)) {
1297                 printf("Failed to setup control interface\n");
1298                 return -1;
1299         }
1300
1301         if (vlan_init(hapd)) {
1302                 printf("VLAN initialization failed.\n");
1303                 return -1;
1304         }
1305
1306 #ifdef CONFIG_IEEE80211R
1307         hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1308                                   hostapd_rrb_receive, hapd, 0);
1309         if (hapd->l2 == NULL &&
1310             (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
1311                 printf("Failed to open l2_packet interface\n");
1312                 return -1;
1313         }
1314 #endif /* CONFIG_IEEE80211R */
1315
1316         ieee802_11_set_beacon(hapd);
1317
1318         if (conf->radius_server_clients &&
1319             hostapd_setup_radius_srv(hapd, conf))
1320                 return -1;
1321
1322         return 0;
1323 }
1324
1325
1326 /**
1327  * setup_interface2 - Setup (initialize) an interface (part 2)
1328  * @iface: Pointer to interface data.
1329  * Returns: 0 on success; -1 on failure.
1330  *
1331  * Flushes old stations, sets the channel, DFS parameters, encryption,
1332  * beacons, and WDS links based on the configuration.
1333  */
1334 static int setup_interface2(struct hostapd_iface *iface)
1335 {
1336         struct hostapd_data *hapd = iface->bss[0];
1337         int freq;
1338         size_t j;
1339         int ret = 0;
1340         u8 *prev_addr;
1341
1342         hostapd_flush_old_stations(hapd);
1343         hostapd_set_privacy(hapd, 0);
1344
1345         if (hapd->iconf->channel) {
1346                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1347                 printf("Mode: %s  Channel: %d  Frequency: %d MHz\n",
1348                        hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1349                        hapd->iconf->channel, freq);
1350
1351                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1352                         printf("Could not set channel for kernel driver\n");
1353                         return -1;
1354                 }
1355         }
1356
1357         hostapd_broadcast_wep_clear(hapd);
1358         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1359                 return -1;
1360
1361         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1362         ieee802_11_set_beacon(hapd);
1363
1364         if (hapd->iconf->rts_threshold > -1 &&
1365             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1366                 printf("Could not set RTS threshold for kernel driver\n");
1367                 return -1;
1368         }
1369
1370         if (hapd->iconf->fragm_threshold > -1 &&
1371             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1372                 printf("Could not set fragmentation threshold for kernel "
1373                        "driver\n");
1374                 return -1;
1375         }
1376
1377         prev_addr = hapd->own_addr;
1378
1379         for (j = 0; j < iface->num_bss; j++) {
1380                 hapd = iface->bss[j];
1381                 if (j)
1382                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1383                 if (hostapd_setup_bss(hapd, j == 0))
1384                         return -1;
1385                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1386                         prev_addr = hapd->own_addr;
1387         }
1388
1389         ap_list_init(iface);
1390
1391         if (hostapd_driver_commit(hapd) < 0) {
1392                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1393                            "configuration", __func__);
1394                 return -1;
1395         }
1396
1397         return ret;
1398 }
1399
1400
1401 static void setup_interface_start(void *eloop_data, void *user_ctx);
1402 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1403
1404 /**
1405  * setup_interface_finalize - Finish setup interface & call the callback
1406  * @iface: Pointer to interface data.
1407  * @status: Status of the setup interface (0 on success; -1 on failure).
1408  * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1409  */
1410 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1411 {
1412         hostapd_iface_cb cb;
1413
1414         if (!iface->setup_cb)
1415                 return -1;
1416         
1417         eloop_cancel_timeout(setup_interface_start, iface, NULL);
1418         eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1419         hostapd_select_hw_mode_stop(iface);
1420
1421         cb = iface->setup_cb;
1422
1423         iface->setup_cb = NULL;
1424
1425         cb(iface, status);
1426
1427         return 0;
1428 }
1429
1430
1431 /**
1432  * setup_interface2_wrapper - Wrapper for setup_interface2()
1433  * @iface: Pointer to interface data.
1434  * @status: Status of the hw mode select.
1435  *
1436  * Wrapper for setup_interface2() to calls finalize function upon completion.
1437  */
1438 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1439 {
1440         int ret = status;
1441         if (ret)
1442                 printf("Could not select hw_mode and channel. (%d)\n", ret);
1443         else
1444                 ret = setup_interface2(iface);
1445
1446         setup_interface_finalize(iface, ret);
1447 }
1448
1449
1450 /**
1451  * setup_interface2_handler - Used for immediate call of setup_interface2
1452  * @eloop_data: Stores the struct hostapd_iface * for the interface.
1453  * @user_ctx: Unused.
1454  */
1455 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1456 {
1457         struct hostapd_iface *iface = eloop_data;
1458
1459         setup_interface2_wrapper(iface, 0);
1460 }
1461
1462
1463 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1464                                        size_t identity_len, int phase2,
1465                                        struct eap_user *user)
1466 {
1467         const struct hostapd_eap_user *eap_user;
1468         int i, count;
1469
1470         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1471         if (eap_user == NULL)
1472                 return -1;
1473
1474         if (user == NULL)
1475                 return 0;
1476
1477         os_memset(user, 0, sizeof(*user));
1478         count = EAP_USER_MAX_METHODS;
1479         if (count > EAP_MAX_METHODS)
1480                 count = EAP_MAX_METHODS;
1481         for (i = 0; i < count; i++) {
1482                 user->methods[i].vendor = eap_user->methods[i].vendor;
1483                 user->methods[i].method = eap_user->methods[i].method;
1484         }
1485
1486         if (eap_user->password) {
1487                 user->password = os_malloc(eap_user->password_len);
1488                 if (user->password == NULL)
1489                         return -1;
1490                 os_memcpy(user->password, eap_user->password,
1491                           eap_user->password_len);
1492                 user->password_len = eap_user->password_len;
1493                 user->password_hash = eap_user->password_hash;
1494         }
1495         user->force_version = eap_user->force_version;
1496         user->ttls_auth = eap_user->ttls_auth;
1497
1498         return 0;
1499 }
1500
1501
1502 /**
1503  * setup_interface1 - Setup (initialize) an interface (part 1)
1504  * @iface: Pointer to interface data
1505  * Returns: 0 on success, -1 on failure
1506  *
1507  * Initializes the driver interface, validates the configuration,
1508  * and sets driver parameters based on the configuration.
1509  * Schedules setup_interface2() to be called immediately or after
1510  * hardware mode setup takes place. 
1511  */
1512 static int setup_interface1(struct hostapd_iface *iface)
1513 {
1514         struct hostapd_data *hapd = iface->bss[0];
1515         struct hostapd_bss_config *conf = hapd->conf;
1516         size_t i;
1517         char country[4];
1518         u8 *b = conf->bssid;
1519
1520         /*
1521          * Initialize the driver interface and make sure that all BSSes get
1522          * configured with a pointer to this driver interface.
1523          */
1524         if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1525                 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1526         } else {
1527                 hapd->drv_priv = hostapd_driver_init(hapd);
1528         }
1529
1530         if (hapd->drv_priv == NULL) {
1531                 printf("%s driver initialization failed.\n",
1532                         hapd->driver ? hapd->driver->name : "Unknown");
1533                 hapd->driver = NULL;
1534                 return -1;
1535         }
1536         for (i = 0; i < iface->num_bss; i++) {
1537                 iface->bss[i]->driver = hapd->driver;
1538                 iface->bss[i]->drv_priv = hapd->drv_priv;
1539         }
1540
1541         if (hostapd_validate_bssid_configuration(iface))
1542                 return -1;
1543
1544         os_memcpy(country, hapd->iconf->country, 3);
1545         country[3] = '\0';
1546         if (hostapd_set_country(hapd, country) < 0) {
1547                 printf("Failed to set country code\n");
1548                 return -1;
1549         }
1550
1551         if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1552                 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1553                         printf("Failed to set ieee80211d (%d)\n",
1554                                hapd->iconf->ieee80211d);
1555                         return -1;
1556                 }
1557         }
1558
1559         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1560             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1561                 printf("Failed to set bridge_packets for kernel driver\n");
1562                 return -1;
1563         }
1564
1565         /* TODO: merge with hostapd_driver_init() ? */
1566         if (hostapd_wireless_event_init(hapd) < 0)
1567                 return -1;
1568
1569         if (hostapd_get_hw_features(iface)) {
1570                 /* Not all drivers support this yet, so continue without hw
1571                  * feature data. */
1572         } else {
1573                 return hostapd_select_hw_mode_start(iface,
1574                                                     setup_interface2_wrapper);
1575         }
1576
1577         eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1578         return 0;
1579 }
1580
1581
1582 /**
1583  * setup_interface_start - Handler to start setup interface
1584  * @eloop_data: Stores the struct hostapd_iface * for the interface.
1585  * @user_ctx: Unused.
1586  *
1587  * An eloop handler is used so that all errors can be processed by the
1588  * callback without introducing stack recursion.
1589  */
1590 static void setup_interface_start(void *eloop_data, void *user_ctx)
1591 {
1592         struct hostapd_iface *iface = eloop_data;
1593
1594         int ret;
1595
1596         ret = setup_interface1(iface);
1597         if (ret)
1598                 setup_interface_finalize(iface, ret);
1599 }
1600
1601
1602 /**
1603  * hostapd_setup_interface_start - Start the setup of an interface
1604  * @iface: Pointer to interface data.
1605  * @cb: The function to callback when done.
1606  * Returns:  0 if it starts successfully; cb will be called when done.
1607  *          -1 on failure; cb will not be called.
1608  *
1609  * Initializes the driver interface, validates the configuration,
1610  * and sets driver parameters based on the configuration.
1611  * Flushes old stations, sets the channel, DFS parameters, encryption,
1612  * beacons, and WDS links based on the configuration.
1613  */
1614 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1615                                   hostapd_iface_cb cb)
1616 {
1617         if (iface->setup_cb) {
1618                 wpa_printf(MSG_DEBUG,
1619                            "%s: Interface setup already in progress.\n",
1620                            iface->bss[0]->conf->iface);
1621                 return -1;
1622         }
1623
1624         iface->setup_cb = cb;
1625
1626         eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1627
1628         return 0;
1629 }
1630
1631
1632 /**
1633  * hostapd_setup_interace_stop - Stops the setup of an interface
1634  * @iface: Pointer to interface data
1635  * Returns:  0 if successfully stopped;
1636  *          -1 on failure (i.e., was not in progress)
1637  */
1638 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1639 {
1640         return setup_interface_finalize(iface, -1);
1641 }
1642
1643
1644 static void show_version(void)
1645 {
1646         fprintf(stderr,
1647                 "hostapd v" VERSION_STR "\n"
1648                 "User space daemon for IEEE 802.11 AP management,\n"
1649                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1650                 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1651                 "and contributors\n");
1652 }
1653
1654
1655 static void usage(void)
1656 {
1657         show_version();
1658         fprintf(stderr,
1659                 "\n"
1660                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1661                 "<configuration file(s)>\n"
1662                 "\n"
1663                 "options:\n"
1664                 "   -h   show this usage\n"
1665                 "   -d   show more debug messages (-dd for even more)\n"
1666                 "   -B   run daemon in the background\n"
1667                 "   -P   PID file\n"
1668                 "   -K   include key data in debug messages\n"
1669                 "   -t   include timestamps in some debug messages\n"
1670                 "   -v   show hostapd version\n");
1671
1672         exit(1);
1673 }
1674
1675
1676 /**
1677  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1678  * @hapd_iface: Pointer to interface data
1679  * @conf: Pointer to per-interface configuration
1680  * @bss: Pointer to per-BSS configuration for this BSS
1681  * Returns: Pointer to allocated BSS data
1682  *
1683  * This function is used to allocate per-BSS data structure. This data will be
1684  * freed after hostapd_cleanup() is called for it during interface
1685  * deinitialization.
1686  */
1687 static struct hostapd_data *
1688 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1689                        struct hostapd_config *conf,
1690                        struct hostapd_bss_config *bss)
1691 {
1692         struct hostapd_data *hapd;
1693
1694         hapd = os_zalloc(sizeof(*hapd));
1695         if (hapd == NULL)
1696                 return NULL;
1697
1698         hapd->iconf = conf;
1699         hapd->conf = bss;
1700         hapd->iface = hapd_iface;
1701
1702         if (hapd->conf->individual_wep_key_len > 0) {
1703                 /* use key0 in individual key and key1 in broadcast key */
1704                 hapd->default_wep_key_idx = 1;
1705         }
1706
1707 #ifdef EAP_TLS_FUNCS
1708         if (hapd->conf->eap_server &&
1709             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1710              hapd->conf->dh_file)) {
1711                 struct tls_connection_params params;
1712
1713                 hapd->ssl_ctx = tls_init(NULL);
1714                 if (hapd->ssl_ctx == NULL) {
1715                         printf("Failed to initialize TLS\n");
1716                         goto fail;
1717                 }
1718
1719                 os_memset(&params, 0, sizeof(params));
1720                 params.ca_cert = hapd->conf->ca_cert;
1721                 params.client_cert = hapd->conf->server_cert;
1722                 params.private_key = hapd->conf->private_key;
1723                 params.private_key_passwd = hapd->conf->private_key_passwd;
1724                 params.dh_file = hapd->conf->dh_file;
1725
1726                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1727                         printf("Failed to set TLS parameters\n");
1728                         goto fail;
1729                 }
1730
1731                 if (tls_global_set_verify(hapd->ssl_ctx,
1732                                           hapd->conf->check_crl)) {
1733                         printf("Failed to enable check_crl\n");
1734                         goto fail;
1735                 }
1736         }
1737 #endif /* EAP_TLS_FUNCS */
1738
1739 #ifdef EAP_SERVER
1740         if (hapd->conf->eap_sim_db) {
1741                 hapd->eap_sim_db_priv =
1742                         eap_sim_db_init(hapd->conf->eap_sim_db,
1743                                         hostapd_sim_db_cb, hapd);
1744                 if (hapd->eap_sim_db_priv == NULL) {
1745                         printf("Failed to initialize EAP-SIM database "
1746                                "interface\n");
1747                         goto fail;
1748                 }
1749         }
1750 #endif /* EAP_SERVER */
1751
1752         if (hapd->conf->assoc_ap)
1753                 hapd->assoc_ap_state = WAIT_BEACON;
1754
1755         hapd->driver = hapd->iconf->driver;
1756
1757         return hapd;
1758
1759 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1760 fail:
1761 #endif
1762         /* TODO: cleanup allocated resources(?) */
1763         os_free(hapd);
1764         return NULL;
1765 }
1766
1767
1768 /**
1769  * hostapd_init - Allocate and initialize per-interface data
1770  * @config_file: Path to the configuration file
1771  * Returns: Pointer to the allocated interface data or %NULL on failure
1772  *
1773  * This function is used to allocate main data structures for per-interface
1774  * data. The allocated data buffer will be freed by calling
1775  * hostapd_cleanup_iface().
1776  */
1777 static struct hostapd_iface * hostapd_init(const char *config_file)
1778 {
1779         struct hostapd_iface *hapd_iface = NULL;
1780         struct hostapd_config *conf = NULL;
1781         struct hostapd_data *hapd;
1782         size_t i;
1783
1784         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1785         if (hapd_iface == NULL)
1786                 goto fail;
1787
1788         hapd_iface->config_fname = os_strdup(config_file);
1789         if (hapd_iface->config_fname == NULL)
1790                 goto fail;
1791
1792         conf = hostapd_config_read(hapd_iface->config_fname);
1793         if (conf == NULL)
1794                 goto fail;
1795         hapd_iface->conf = conf;
1796
1797         hapd_iface->num_bss = conf->num_bss;
1798         hapd_iface->bss = os_zalloc(conf->num_bss *
1799                                     sizeof(struct hostapd_data *));
1800         if (hapd_iface->bss == NULL)
1801                 goto fail;
1802
1803         for (i = 0; i < conf->num_bss; i++) {
1804                 hapd = hapd_iface->bss[i] =
1805                         hostapd_alloc_bss_data(hapd_iface, conf,
1806                                                &conf->bss[i]);
1807                 if (hapd == NULL)
1808                         goto fail;
1809         }
1810
1811         return hapd_iface;
1812
1813 fail:
1814         if (conf)
1815                 hostapd_config_free(conf);
1816         if (hapd_iface) {
1817                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1818                         hapd = hapd_iface->bss[i];
1819                         if (hapd && hapd->ssl_ctx)
1820                                 tls_deinit(hapd->ssl_ctx);
1821                 }
1822
1823                 os_free(hapd_iface->config_fname);
1824                 os_free(hapd_iface->bss);
1825                 os_free(hapd_iface);
1826         }
1827         return NULL;
1828 }
1829
1830
1831 /**
1832  * register_drivers - Register driver interfaces
1833  *
1834  * This function is generated by Makefile (into driver_conf.c) to call all
1835  * configured driver interfaces to register them to core hostapd.
1836  */
1837 void register_drivers(void);
1838
1839
1840 /**
1841  * setup_interface_done - Callback when an interface is done being setup.
1842  * @iface: Pointer to interface data.
1843  * @status: Status of the interface setup (0 on success; -1 on failure).
1844  */
1845 static void setup_interface_done(struct hostapd_iface *iface, int status)
1846 {
1847         if (status) {
1848                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1849                            iface->bss[0]->conf->iface);
1850                 eloop_terminate();
1851         } else
1852                 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1853                            iface->bss[0]->conf->iface);
1854 }
1855
1856
1857 int main(int argc, char *argv[])
1858 {
1859         struct hapd_interfaces interfaces;
1860         int ret = 1, k;
1861         size_t i, j;
1862         int c, debug = 0, daemonize = 0, tnc = 0;
1863         const char *pid_file = NULL;
1864
1865         hostapd_logger_register_cb(hostapd_logger_cb);
1866
1867         for (;;) {
1868                 c = getopt(argc, argv, "BdhKP:tv");
1869                 if (c < 0)
1870                         break;
1871                 switch (c) {
1872                 case 'h':
1873                         usage();
1874                         break;
1875                 case 'd':
1876                         debug++;
1877                         if (wpa_debug_level > 0)
1878                                 wpa_debug_level--;
1879                         break;
1880                 case 'B':
1881                         daemonize++;
1882                         break;
1883                 case 'K':
1884                         wpa_debug_show_keys++;
1885                         break;
1886                 case 'P':
1887                         pid_file = optarg;
1888                         break;
1889                 case 't':
1890                         wpa_debug_timestamp++;
1891                         break;
1892                 case 'v':
1893                         show_version();
1894                         exit(1);
1895                         break;
1896
1897                 default:
1898                         usage();
1899                         break;
1900                 }
1901         }
1902
1903         if (optind == argc)
1904                 usage();
1905
1906         if (eap_server_register_methods()) {
1907                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1908                 return -1;
1909         }
1910
1911         interfaces.count = argc - optind;
1912
1913         interfaces.iface = os_malloc(interfaces.count *
1914                                      sizeof(struct hostapd_iface *));
1915         if (interfaces.iface == NULL) {
1916                 wpa_printf(MSG_ERROR, "malloc failed\n");
1917                 return -1;
1918         }
1919
1920         if (eloop_init(&interfaces)) {
1921                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1922                 return -1;
1923         }
1924
1925 #ifndef CONFIG_NATIVE_WINDOWS
1926         eloop_register_signal(SIGHUP, handle_reload, NULL);
1927         eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1928 #endif /* CONFIG_NATIVE_WINDOWS */
1929         eloop_register_signal_terminate(handle_term, NULL);
1930
1931         /* Initialize interfaces */
1932         for (i = 0; i < interfaces.count; i++) {
1933                 printf("Configuration file: %s\n", argv[optind + i]);
1934                 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1935                 if (!interfaces.iface[i])
1936                         goto out;
1937                 for (k = 0; k < debug; k++) {
1938                         if (interfaces.iface[i]->bss[0]->conf->
1939                             logger_stdout_level > 0)
1940                                 interfaces.iface[i]->bss[0]->conf->
1941                                         logger_stdout_level--;
1942                 }
1943
1944                 ret = hostapd_setup_interface_start(interfaces.iface[i],
1945                                                     setup_interface_done);
1946                 if (ret)
1947                         goto out;
1948
1949                 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1950                         if (interfaces.iface[i]->bss[0]->conf->tnc)
1951                                 tnc++;
1952                 }
1953         }
1954
1955 #ifdef EAP_TNC
1956         if (tnc && tncs_global_init() < 0) {
1957                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1958                 goto out;
1959         }
1960 #endif /* EAP_TNC */
1961
1962         if (daemonize && os_daemonize(pid_file)) {
1963                 perror("daemon");
1964                 goto out;
1965         }
1966
1967 #ifndef CONFIG_NATIVE_WINDOWS
1968         openlog("hostapd", 0, LOG_DAEMON);
1969 #endif /* CONFIG_NATIVE_WINDOWS */
1970
1971         eloop_run();
1972
1973         /* Disconnect associated stations from all interfaces and BSSes */
1974         for (i = 0; i < interfaces.count; i++) {
1975                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1976                         struct hostapd_data *hapd =
1977                                 interfaces.iface[i]->bss[j];
1978                         hostapd_free_stas(hapd);
1979                         hostapd_flush_old_stations(hapd);
1980                 }
1981         }
1982
1983         ret = 0;
1984
1985  out:
1986         /* Deinitialize all interfaces */
1987         for (i = 0; i < interfaces.count; i++) {
1988                 if (!interfaces.iface[i])
1989                         continue;
1990                 hostapd_setup_interface_stop(interfaces.iface[i]);
1991                 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1992                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1993                         struct hostapd_data *hapd =
1994                                 interfaces.iface[i]->bss[j];
1995                         hostapd_cleanup(hapd);
1996                         if (j == interfaces.iface[i]->num_bss - 1 &&
1997                             hapd->driver)
1998                                 hostapd_driver_deinit(hapd);
1999                 }
2000                 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2001                         os_free(interfaces.iface[i]->bss[j]);
2002                 hostapd_cleanup_iface(interfaces.iface[i]);
2003         }
2004         os_free(interfaces.iface);
2005
2006 #ifdef EAP_TNC
2007         tncs_global_deinit();
2008 #endif /* EAP_TNC */
2009
2010         eloop_destroy();
2011
2012 #ifndef CONFIG_NATIVE_WINDOWS
2013         closelog();
2014 #endif /* CONFIG_NATIVE_WINDOWS */
2015
2016         eap_server_unregister_methods();
2017
2018         os_daemonize_terminate(pid_file);
2019
2020         return ret;
2021 }