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