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