Rename EAP server defines from EAP_* to EAP_SERVER_*
[wpasupplicant] / 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         int auto_addr = 0;
1016
1017         if (hostapd_drv_none(hapd))
1018                 return 0;
1019
1020         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1021
1022         /* Determine the bits necessary to cover the number of BSSIDs. */
1023         for (i--; i; i >>= 1)
1024                 bits++;
1025
1026         /* Determine the bits necessary to any configured BSSIDs,
1027            if they are higher than the number of BSSIDs. */
1028         for (j = 0; j < iface->conf->num_bss; j++) {
1029                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
1030                         if (j)
1031                                 auto_addr++;
1032                         continue;
1033                 }
1034
1035                 for (i = 0; i < ETH_ALEN; i++) {
1036                         mask[i] |=
1037                                 iface->conf->bss[j].bssid[i] ^
1038                                 hapd->own_addr[i];
1039                 }
1040         }
1041
1042         if (!auto_addr)
1043                 goto skip_mask_ext;
1044
1045         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1046                 ;
1047         j = 0;
1048         if (i < ETH_ALEN) {
1049                 j = (5 - i) * 8;
1050
1051                 while (mask[i] != 0) {
1052                         mask[i] >>= 1;
1053                         j++;
1054                 }
1055         }
1056
1057         if (bits < j)
1058                 bits = j;
1059
1060         if (bits > 40) {
1061                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
1062                            bits);
1063                 return -1;
1064         }
1065
1066         os_memset(mask, 0xff, ETH_ALEN);
1067         j = bits / 8;
1068         for (i = 5; i > 5 - j; i--)
1069                 mask[i] = 0;
1070         j = bits % 8;
1071         while (j--)
1072                 mask[i] <<= 1;
1073
1074 skip_mask_ext:
1075         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1076                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1077
1078         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1079         if (res == 0)
1080                 return 0;
1081
1082         if (res < 0) {
1083                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1084                            MACSTR " for start address " MACSTR ".",
1085                            MAC2STR(mask), MAC2STR(hapd->own_addr));
1086                 return -1;
1087         }
1088
1089         if (!auto_addr)
1090                 return 0;
1091
1092         for (i = 0; i < ETH_ALEN; i++) {
1093                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1094                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1095                                    " for start address " MACSTR ".",
1096                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
1097                         wpa_printf(MSG_ERROR, "Start address must be the "
1098                                    "first address in the block (i.e., addr "
1099                                    "AND mask == addr).");
1100                         return -1;
1101                 }
1102         }
1103
1104         return 0;
1105 }
1106
1107
1108 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1109 {
1110         size_t i;
1111
1112         for (i = 0; i < conf->num_bss; i++) {
1113                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1114                         return 1;
1115                 }
1116         }
1117
1118         return 0;
1119 }
1120
1121
1122 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1123 {
1124         struct wpa_auth_config _conf;
1125         struct wpa_auth_callbacks cb;
1126         const u8 *wpa_ie;
1127         size_t wpa_ie_len;
1128
1129         hostapd_wpa_auth_conf(hapd->conf, &_conf);
1130         os_memset(&cb, 0, sizeof(cb));
1131         cb.ctx = hapd;
1132         cb.logger = hostapd_wpa_auth_logger;
1133         cb.disconnect = hostapd_wpa_auth_disconnect;
1134         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1135         cb.set_eapol = hostapd_wpa_auth_set_eapol;
1136         cb.get_eapol = hostapd_wpa_auth_get_eapol;
1137         cb.get_psk = hostapd_wpa_auth_get_psk;
1138         cb.get_msk = hostapd_wpa_auth_get_msk;
1139         cb.set_key = hostapd_wpa_auth_set_key;
1140         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1141         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1142         cb.send_eapol = hostapd_wpa_auth_send_eapol;
1143         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1144         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1145         cb.send_ether = hostapd_wpa_auth_send_ether;
1146 #ifdef CONFIG_IEEE80211R
1147         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1148         cb.add_sta = hostapd_wpa_auth_add_sta;
1149 #endif /* CONFIG_IEEE80211R */
1150         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1151         if (hapd->wpa_auth == NULL) {
1152                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1153                 return -1;
1154         }
1155
1156         if (hostapd_set_privacy(hapd, 1)) {
1157                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1158                            "for interface %s", hapd->conf->iface);
1159                 return -1;
1160         }
1161
1162         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1163         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1164                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1165                            "the kernel driver.");
1166                 return -1;
1167         }
1168
1169         if (rsn_preauth_iface_init(hapd)) {
1170                 wpa_printf(MSG_ERROR, "Initialization of RSN "
1171                            "pre-authentication failed.");
1172                 return -1;
1173         }
1174
1175         return 0;
1176
1177 }
1178
1179
1180 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1181                                     struct hostapd_bss_config *conf)
1182 {
1183         struct radius_server_conf srv;
1184         os_memset(&srv, 0, sizeof(srv));
1185         srv.client_file = conf->radius_server_clients;
1186         srv.auth_port = conf->radius_server_auth_port;
1187         srv.conf_ctx = conf;
1188         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1189         srv.ssl_ctx = hapd->ssl_ctx;
1190         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1191         srv.eap_fast_a_id = conf->eap_fast_a_id;
1192         srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1193         srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1194         srv.eap_fast_prov = conf->eap_fast_prov;
1195         srv.pac_key_lifetime = conf->pac_key_lifetime;
1196         srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1197         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1198         srv.tnc = conf->tnc;
1199         srv.wps = hapd->wps;
1200         srv.ipv6 = conf->radius_server_ipv6;
1201         srv.get_eap_user = hostapd_radius_get_eap_user;
1202         srv.eap_req_id_text = conf->eap_req_id_text;
1203         srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1204
1205         hapd->radius_srv = radius_server_init(&srv);
1206         if (hapd->radius_srv == NULL) {
1207                 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1208                 return -1;
1209         }
1210
1211         return 0;
1212 }
1213
1214
1215 /**
1216  * hostapd_setup_bss - Per-BSS setup (initialization)
1217  * @hapd: Pointer to BSS data
1218  * @first: Whether this BSS is the first BSS of an interface
1219  *
1220  * This function is used to initialize all per-BSS data structures and
1221  * resources. This gets called in a loop for each BSS when an interface is
1222  * initialized. Most of the modules that are initialized here will be
1223  * deinitialized in hostapd_cleanup().
1224  */
1225 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1226 {
1227         struct hostapd_bss_config *conf = hapd->conf;
1228         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1229         int ssid_len, set_ssid;
1230
1231         if (!first) {
1232                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1233                         /* Allocate the next available BSSID. */
1234                         do {
1235                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1236                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1237                 } else {
1238                         /* Allocate the configured BSSID. */
1239                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1240
1241                         if (hostapd_mac_comp(hapd->own_addr,
1242                                              hapd->iface->bss[0]->own_addr) ==
1243                             0) {
1244                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1245                                            "BSSID set to the MAC address of "
1246                                            "the radio", hapd->conf->iface);
1247                                 return -1;
1248                         }
1249                 }
1250
1251                 hapd->interface_added = 1;
1252                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1253                                     hapd->own_addr)) {
1254                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1255                                    MACSTR ")", MAC2STR(hapd->own_addr));
1256                         return -1;
1257                 }
1258         }
1259
1260         /*
1261          * Fetch the SSID from the system and use it or,
1262          * if one was specified in the config file, verify they
1263          * match.
1264          */
1265         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1266         if (ssid_len < 0) {
1267                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1268                 return -1;
1269         }
1270         if (conf->ssid.ssid_set) {
1271                 /*
1272                  * If SSID is specified in the config file and it differs
1273                  * from what is being used then force installation of the
1274                  * new SSID.
1275                  */
1276                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1277                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1278         } else {
1279                 /*
1280                  * No SSID in the config file; just use the one we got
1281                  * from the system.
1282                  */
1283                 set_ssid = 0;
1284                 conf->ssid.ssid_len = ssid_len;
1285                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1286                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1287         }
1288
1289         if (!hostapd_drv_none(hapd)) {
1290                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1291                            " and ssid '%s'",
1292                            hapd->conf->iface, MAC2STR(hapd->own_addr),
1293                            hapd->conf->ssid.ssid);
1294         }
1295
1296         if (hostapd_setup_wpa_psk(conf)) {
1297                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1298                 return -1;
1299         }
1300
1301         /* Set flag for whether SSID is broadcast in beacons */
1302         if (hostapd_set_broadcast_ssid(hapd,
1303                                        !!hapd->conf->ignore_broadcast_ssid)) {
1304                 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1305                            "kernel driver");
1306                 return -1;
1307         }
1308
1309         /* Set SSID for the kernel driver (to be used in beacon and probe
1310          * response frames) */
1311         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1312                                          conf->ssid.ssid_len)) {
1313                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1314                 return -1;
1315         }
1316
1317         if (wpa_debug_level == MSG_MSGDUMP)
1318                 conf->radius->msg_dumps = 1;
1319         hapd->radius = radius_client_init(hapd, conf->radius);
1320         if (hapd->radius == NULL) {
1321                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1322                 return -1;
1323         }
1324
1325         if (hostapd_acl_init(hapd)) {
1326                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1327                 return -1;
1328         }
1329         if (hostapd_init_wps(hapd, conf))
1330                 return -1;
1331
1332         if (ieee802_1x_init(hapd)) {
1333                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1334                 return -1;
1335         }
1336
1337         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1338                 return -1;
1339
1340         if (accounting_init(hapd)) {
1341                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1342                 return -1;
1343         }
1344
1345         if (hapd->conf->ieee802_11f &&
1346             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1347                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1348                            "failed.");
1349                 return -1;
1350         }
1351
1352         if (hostapd_ctrl_iface_init(hapd)) {
1353                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1354                 return -1;
1355         }
1356
1357         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1358                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1359                 return -1;
1360         }
1361
1362 #ifdef CONFIG_IEEE80211R
1363         if (!hostapd_drv_none(hapd)) {
1364                 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1365                                           hostapd_rrb_receive, hapd, 0);
1366                 if (hapd->l2 == NULL &&
1367                     (hapd->driver == NULL ||
1368                      hapd->driver->send_ether == NULL)) {
1369                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1370                                    "interface");
1371                         return -1;
1372                 }
1373         }
1374 #endif /* CONFIG_IEEE80211R */
1375
1376         ieee802_11_set_beacon(hapd);
1377
1378         if (conf->radius_server_clients &&
1379             hostapd_setup_radius_srv(hapd, conf))
1380                 return -1;
1381
1382         return 0;
1383 }
1384
1385
1386 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1387 {
1388         struct hostapd_data *hapd = iface->bss[0];
1389         int i;
1390         struct hostapd_tx_queue_params *p;
1391
1392         for (i = 0; i < NUM_TX_QUEUES; i++) {
1393                 p = &iface->conf->tx_queue[i];
1394
1395                 if (!p->configured)
1396                         continue;
1397
1398                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1399                                                 p->cwmax, p->burst)) {
1400                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1401                                    "parameters for queue %d.", i);
1402                         /* Continue anyway */
1403                 }
1404         }
1405 }
1406
1407
1408 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1409                                        size_t identity_len, int phase2,
1410                                        struct eap_user *user)
1411 {
1412         const struct hostapd_eap_user *eap_user;
1413         int i, count;
1414
1415         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1416         if (eap_user == NULL)
1417                 return -1;
1418
1419         if (user == NULL)
1420                 return 0;
1421
1422         os_memset(user, 0, sizeof(*user));
1423         count = EAP_USER_MAX_METHODS;
1424         if (count > EAP_MAX_METHODS)
1425                 count = EAP_MAX_METHODS;
1426         for (i = 0; i < count; i++) {
1427                 user->methods[i].vendor = eap_user->methods[i].vendor;
1428                 user->methods[i].method = eap_user->methods[i].method;
1429         }
1430
1431         if (eap_user->password) {
1432                 user->password = os_malloc(eap_user->password_len);
1433                 if (user->password == NULL)
1434                         return -1;
1435                 os_memcpy(user->password, eap_user->password,
1436                           eap_user->password_len);
1437                 user->password_len = eap_user->password_len;
1438                 user->password_hash = eap_user->password_hash;
1439         }
1440         user->force_version = eap_user->force_version;
1441         user->ttls_auth = eap_user->ttls_auth;
1442
1443         return 0;
1444 }
1445
1446
1447 static int setup_interface(struct hostapd_iface *iface)
1448 {
1449         struct hostapd_data *hapd = iface->bss[0];
1450         struct hostapd_bss_config *conf = hapd->conf;
1451         size_t i;
1452         char country[4];
1453         u8 *b = conf->bssid;
1454         int freq;
1455         size_t j;
1456         int ret = 0;
1457         u8 *prev_addr;
1458
1459         /*
1460          * Initialize the driver interface and make sure that all BSSes get
1461          * configured with a pointer to this driver interface.
1462          */
1463         if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1464                 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1465         } else {
1466                 hapd->drv_priv = hostapd_driver_init(hapd);
1467         }
1468
1469         if (hapd->drv_priv == NULL) {
1470                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1471                            hapd->driver ? hapd->driver->name : "Unknown");
1472                 hapd->driver = NULL;
1473                 return -1;
1474         }
1475         for (i = 0; i < iface->num_bss; i++) {
1476                 iface->bss[i]->driver = hapd->driver;
1477                 iface->bss[i]->drv_priv = hapd->drv_priv;
1478         }
1479
1480         if (hostapd_validate_bssid_configuration(iface))
1481                 return -1;
1482
1483 #ifdef CONFIG_IEEE80211N
1484         SET_2BIT_LE16(&iface->ht_op_mode,
1485                       HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1486                       OP_MODE_PURE);
1487 #endif /* CONFIG_IEEE80211N */
1488
1489         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1490                 os_memcpy(country, hapd->iconf->country, 3);
1491                 country[3] = '\0';
1492                 if (hostapd_set_country(hapd, country) < 0) {
1493                         wpa_printf(MSG_ERROR, "Failed to set country code");
1494                         return -1;
1495                 }
1496         }
1497
1498         if (hapd->iconf->ieee80211d &&
1499             hostapd_set_ieee80211d(hapd, 1) < 0) {
1500                 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1501                            hapd->iconf->ieee80211d);
1502                 return -1;
1503         }
1504
1505         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1506             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1507                 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1508                            "kernel driver");
1509                 return -1;
1510         }
1511
1512         /* TODO: merge with hostapd_driver_init() ? */
1513         if (hostapd_wireless_event_init(hapd) < 0)
1514                 return -1;
1515
1516         if (hostapd_get_hw_features(iface)) {
1517                 /* Not all drivers support this yet, so continue without hw
1518                  * feature data. */
1519         } else {
1520                 int ret = hostapd_select_hw_mode(iface);
1521                 if (ret < 0) {
1522                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1523                                    "channel. (%d)", ret);
1524                         return -1;
1525                 }
1526         }
1527
1528         hostapd_flush_old_stations(hapd);
1529         hostapd_set_privacy(hapd, 0);
1530
1531         if (hapd->iconf->channel) {
1532                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1533                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1534                            "Frequency: %d MHz",
1535                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1536                            hapd->iconf->channel, freq);
1537
1538                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1539                                      hapd->iconf->ieee80211n,
1540                                      hapd->iconf->secondary_channel)) {
1541                         wpa_printf(MSG_ERROR, "Could not set channel for "
1542                                    "kernel driver");
1543                         return -1;
1544                 }
1545         }
1546
1547         hostapd_broadcast_wep_clear(hapd);
1548         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1549                 return -1;
1550
1551         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1552         ieee802_11_set_beacon(hapd);
1553
1554         if (hapd->iconf->rts_threshold > -1 &&
1555             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1556                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1557                            "kernel driver");
1558                 return -1;
1559         }
1560
1561         if (hapd->iconf->fragm_threshold > -1 &&
1562             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1563                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1564                            "for kernel driver");
1565                 return -1;
1566         }
1567
1568         prev_addr = hapd->own_addr;
1569
1570         for (j = 0; j < iface->num_bss; j++) {
1571                 hapd = iface->bss[j];
1572                 if (j)
1573                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1574                 if (hostapd_setup_bss(hapd, j == 0))
1575                         return -1;
1576                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1577                         prev_addr = hapd->own_addr;
1578         }
1579
1580         hostapd_tx_queue_params(iface);
1581
1582         ap_list_init(iface);
1583
1584         if (hostapd_driver_commit(hapd) < 0) {
1585                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1586                            "configuration", __func__);
1587                 return -1;
1588         }
1589
1590         return ret;
1591 }
1592
1593
1594 /**
1595  * hostapd_setup_interface - Setup of an interface
1596  * @iface: Pointer to interface data.
1597  * Returns: 0 on success, -1 on failure
1598  *
1599  * Initializes the driver interface, validates the configuration,
1600  * and sets driver parameters based on the configuration.
1601  * Flushes old stations, sets the channel, encryption,
1602  * beacons, and WDS links based on the configuration.
1603  */
1604 static int hostapd_setup_interface(struct hostapd_iface *iface)
1605 {
1606         int ret;
1607
1608         ret = setup_interface(iface);
1609         if (ret) {
1610                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1611                            iface->bss[0]->conf->iface);
1612                 eloop_terminate();
1613                 return -1;
1614         } else if (!hostapd_drv_none(iface->bss[0])) {
1615                 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1616                            iface->bss[0]->conf->iface);
1617         }
1618
1619         return 0;
1620 }
1621
1622
1623 static void show_version(void)
1624 {
1625         fprintf(stderr,
1626                 "hostapd v" VERSION_STR "\n"
1627                 "User space daemon for IEEE 802.11 AP management,\n"
1628                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1629                 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1630                 "and contributors\n");
1631 }
1632
1633
1634 static void usage(void)
1635 {
1636         show_version();
1637         fprintf(stderr,
1638                 "\n"
1639                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1640                 "<configuration file(s)>\n"
1641                 "\n"
1642                 "options:\n"
1643                 "   -h   show this usage\n"
1644                 "   -d   show more debug messages (-dd for even more)\n"
1645                 "   -B   run daemon in the background\n"
1646                 "   -P   PID file\n"
1647                 "   -K   include key data in debug messages\n"
1648                 "   -t   include timestamps in some debug messages\n"
1649                 "   -v   show hostapd version\n");
1650
1651         exit(1);
1652 }
1653
1654
1655 /**
1656  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1657  * @hapd_iface: Pointer to interface data
1658  * @conf: Pointer to per-interface configuration
1659  * @bss: Pointer to per-BSS configuration for this BSS
1660  * Returns: Pointer to allocated BSS data
1661  *
1662  * This function is used to allocate per-BSS data structure. This data will be
1663  * freed after hostapd_cleanup() is called for it during interface
1664  * deinitialization.
1665  */
1666 static struct hostapd_data *
1667 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1668                        struct hostapd_config *conf,
1669                        struct hostapd_bss_config *bss)
1670 {
1671         struct hostapd_data *hapd;
1672
1673         hapd = os_zalloc(sizeof(*hapd));
1674         if (hapd == NULL)
1675                 return NULL;
1676
1677         hapd->iconf = conf;
1678         hapd->conf = bss;
1679         hapd->iface = hapd_iface;
1680
1681         if (hapd->conf->individual_wep_key_len > 0) {
1682                 /* use key0 in individual key and key1 in broadcast key */
1683                 hapd->default_wep_key_idx = 1;
1684         }
1685
1686 #ifdef EAP_TLS_FUNCS
1687         if (hapd->conf->eap_server &&
1688             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1689              hapd->conf->dh_file)) {
1690                 struct tls_connection_params params;
1691
1692                 hapd->ssl_ctx = tls_init(NULL);
1693                 if (hapd->ssl_ctx == NULL) {
1694                         wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1695                         goto fail;
1696                 }
1697
1698                 os_memset(&params, 0, sizeof(params));
1699                 params.ca_cert = hapd->conf->ca_cert;
1700                 params.client_cert = hapd->conf->server_cert;
1701                 params.private_key = hapd->conf->private_key;
1702                 params.private_key_passwd = hapd->conf->private_key_passwd;
1703                 params.dh_file = hapd->conf->dh_file;
1704
1705                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1706                         wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1707                         goto fail;
1708                 }
1709
1710                 if (tls_global_set_verify(hapd->ssl_ctx,
1711                                           hapd->conf->check_crl)) {
1712                         wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1713                         goto fail;
1714                 }
1715         }
1716 #endif /* EAP_TLS_FUNCS */
1717
1718 #ifdef EAP_SERVER
1719         if (hapd->conf->eap_sim_db) {
1720                 hapd->eap_sim_db_priv =
1721                         eap_sim_db_init(hapd->conf->eap_sim_db,
1722                                         hostapd_sim_db_cb, hapd);
1723                 if (hapd->eap_sim_db_priv == NULL) {
1724                         wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1725                                    "database interface");
1726                         goto fail;
1727                 }
1728         }
1729 #endif /* EAP_SERVER */
1730
1731         hapd->driver = hapd->iconf->driver;
1732
1733         return hapd;
1734
1735 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1736 fail:
1737 #endif
1738         /* TODO: cleanup allocated resources(?) */
1739         os_free(hapd);
1740         return NULL;
1741 }
1742
1743
1744 /**
1745  * hostapd_init - Allocate and initialize per-interface data
1746  * @config_file: Path to the configuration file
1747  * Returns: Pointer to the allocated interface data or %NULL on failure
1748  *
1749  * This function is used to allocate main data structures for per-interface
1750  * data. The allocated data buffer will be freed by calling
1751  * hostapd_cleanup_iface().
1752  */
1753 static struct hostapd_iface * hostapd_init(const char *config_file)
1754 {
1755         struct hostapd_iface *hapd_iface = NULL;
1756         struct hostapd_config *conf = NULL;
1757         struct hostapd_data *hapd;
1758         size_t i;
1759
1760         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1761         if (hapd_iface == NULL)
1762                 goto fail;
1763
1764         hapd_iface->config_fname = os_strdup(config_file);
1765         if (hapd_iface->config_fname == NULL)
1766                 goto fail;
1767
1768         conf = hostapd_config_read(hapd_iface->config_fname);
1769         if (conf == NULL)
1770                 goto fail;
1771         hapd_iface->conf = conf;
1772
1773         hapd_iface->num_bss = conf->num_bss;
1774         hapd_iface->bss = os_zalloc(conf->num_bss *
1775                                     sizeof(struct hostapd_data *));
1776         if (hapd_iface->bss == NULL)
1777                 goto fail;
1778
1779         for (i = 0; i < conf->num_bss; i++) {
1780                 hapd = hapd_iface->bss[i] =
1781                         hostapd_alloc_bss_data(hapd_iface, conf,
1782                                                &conf->bss[i]);
1783                 if (hapd == NULL)
1784                         goto fail;
1785         }
1786
1787         return hapd_iface;
1788
1789 fail:
1790         if (conf)
1791                 hostapd_config_free(conf);
1792         if (hapd_iface) {
1793                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1794                         hapd = hapd_iface->bss[i];
1795                         if (hapd && hapd->ssl_ctx)
1796                                 tls_deinit(hapd->ssl_ctx);
1797                 }
1798
1799                 os_free(hapd_iface->config_fname);
1800                 os_free(hapd_iface->bss);
1801                 os_free(hapd_iface);
1802         }
1803         return NULL;
1804 }
1805
1806
1807 static int hostapd_global_init(struct hapd_interfaces *interfaces)
1808 {
1809         hostapd_logger_register_cb(hostapd_logger_cb);
1810
1811         if (eap_server_register_methods()) {
1812                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1813                 return -1;
1814         }
1815
1816         if (eloop_init(interfaces)) {
1817                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1818                 return -1;
1819         }
1820
1821 #ifndef CONFIG_NATIVE_WINDOWS
1822         eloop_register_signal(SIGHUP, handle_reload, NULL);
1823         eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1824 #endif /* CONFIG_NATIVE_WINDOWS */
1825         eloop_register_signal_terminate(handle_term, NULL);
1826
1827 #ifndef CONFIG_NATIVE_WINDOWS
1828         openlog("hostapd", 0, LOG_DAEMON);
1829 #endif /* CONFIG_NATIVE_WINDOWS */
1830
1831         return 0;
1832 }
1833
1834
1835 static void hostapd_global_deinit(const char *pid_file)
1836 {
1837 #ifdef EAP_SERVER_TNC
1838         tncs_global_deinit();
1839 #endif /* EAP_SERVER_TNC */
1840
1841         eloop_destroy();
1842
1843 #ifndef CONFIG_NATIVE_WINDOWS
1844         closelog();
1845 #endif /* CONFIG_NATIVE_WINDOWS */
1846
1847         eap_server_unregister_methods();
1848
1849         os_daemonize_terminate(pid_file);
1850 }
1851
1852
1853 static void hostapd_interface_deinit(struct hostapd_iface *iface)
1854 {
1855         size_t j;
1856
1857         if (iface == NULL)
1858                 return;
1859
1860         hostapd_cleanup_iface_pre(iface);
1861         for (j = 0; j < iface->num_bss; j++) {
1862                 struct hostapd_data *hapd = iface->bss[j];
1863                 hostapd_free_stas(hapd);
1864                 hostapd_flush_old_stations(hapd);
1865                 hostapd_cleanup(hapd);
1866                 if (j == iface->num_bss - 1 && hapd->driver)
1867                         hostapd_driver_deinit(hapd);
1868         }
1869         for (j = 0; j < iface->num_bss; j++)
1870                 os_free(iface->bss[j]);
1871         hostapd_cleanup_iface(iface);
1872 }
1873
1874
1875 static struct hostapd_iface * hostapd_interface_init(const char *config_fname,
1876                                                      int debug)
1877 {
1878         struct hostapd_iface *iface;
1879         int k;
1880
1881         wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
1882         iface = hostapd_init(config_fname);
1883         if (!iface)
1884                 return NULL;
1885
1886         for (k = 0; k < debug; k++) {
1887                 if (iface->bss[0]->conf->logger_stdout_level > 0)
1888                         iface->bss[0]->conf->logger_stdout_level--;
1889         }
1890
1891         if (hostapd_setup_interface(iface)) {
1892                 hostapd_interface_deinit(iface);
1893                 return NULL;
1894         }
1895
1896         return iface;
1897 }
1898
1899
1900 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
1901                               const char *pid_file)
1902 {
1903 #ifdef EAP_SERVER_TNC
1904         int tnc = 0;
1905         size_t i, k;
1906
1907         for (i = 0; !tnc && i < ifaces->count; i++) {
1908                 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
1909                         if (ifaces->iface[i]->bss[0]->conf->tnc) {
1910                                 tnc++;
1911                                 break;
1912                         }
1913                 }
1914         }
1915
1916         if (tnc && tncs_global_init() < 0) {
1917                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1918                 return -1;
1919         }
1920 #endif /* EAP_SERVER_TNC */
1921
1922         if (daemonize && os_daemonize(pid_file)) {
1923                 perror("daemon");
1924                 return -1;
1925         }
1926
1927         eloop_run();
1928
1929         return 0;
1930 }
1931
1932
1933 int main(int argc, char *argv[])
1934 {
1935         struct hapd_interfaces interfaces;
1936         int ret = 1;
1937         size_t i;
1938         int c, debug = 0, daemonize = 0;
1939         const char *pid_file = NULL;
1940
1941         for (;;) {
1942                 c = getopt(argc, argv, "BdhKP:tv");
1943                 if (c < 0)
1944                         break;
1945                 switch (c) {
1946                 case 'h':
1947                         usage();
1948                         break;
1949                 case 'd':
1950                         debug++;
1951                         if (wpa_debug_level > 0)
1952                                 wpa_debug_level--;
1953                         break;
1954                 case 'B':
1955                         daemonize++;
1956                         break;
1957                 case 'K':
1958                         wpa_debug_show_keys++;
1959                         break;
1960                 case 'P':
1961                         pid_file = optarg;
1962                         break;
1963                 case 't':
1964                         wpa_debug_timestamp++;
1965                         break;
1966                 case 'v':
1967                         show_version();
1968                         exit(1);
1969                         break;
1970
1971                 default:
1972                         usage();
1973                         break;
1974                 }
1975         }
1976
1977         if (optind == argc)
1978                 usage();
1979
1980         interfaces.count = argc - optind;
1981         interfaces.iface = os_malloc(interfaces.count *
1982                                      sizeof(struct hostapd_iface *));
1983         if (interfaces.iface == NULL) {
1984                 wpa_printf(MSG_ERROR, "malloc failed\n");
1985                 return -1;
1986         }
1987
1988         if (hostapd_global_init(&interfaces))
1989                 return -1;
1990
1991         /* Initialize interfaces */
1992         for (i = 0; i < interfaces.count; i++) {
1993                 interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
1994                                                              debug);
1995                 if (!interfaces.iface[i])
1996                         goto out;
1997         }
1998
1999         if (hostapd_global_run(&interfaces, daemonize, pid_file))
2000                 goto out;
2001
2002         ret = 0;
2003
2004  out:
2005         /* Deinitialize all interfaces */
2006         for (i = 0; i < interfaces.count; i++)
2007                 hostapd_interface_deinit(interfaces.iface[i]);
2008         os_free(interfaces.iface);
2009
2010         hostapd_global_deinit(pid_file);
2011
2012         return ret;
2013 }