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