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