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