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