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