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