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