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