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