libm: Import fdim{,f,l} functions from FreeBSD.
[dragonfly.git] / contrib / hostapd-0.5.8 / hostapd.c
blobd4cd488eb5c0afe12caaa5c515b285d685b83a62
1 /*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
17 #include <syslog.h>
18 #endif /* CONFIG_NATIVE_WINDOWS */
20 #include "eloop.h"
21 #include "hostapd.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "beacon.h"
25 #include "hw_features.h"
26 #include "accounting.h"
27 #include "eapol_sm.h"
28 #include "iapp.h"
29 #include "ap.h"
30 #include "ieee802_11_auth.h"
31 #include "ap_list.h"
32 #include "sta_info.h"
33 #include "driver.h"
34 #include "radius_client.h"
35 #include "radius_server.h"
36 #include "wpa.h"
37 #include "preauth.h"
38 #include "wme.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
41 #include "tls.h"
42 #include "eap_sim_db.h"
43 #include "eap.h"
44 #include "version.h"
47 struct hapd_interfaces {
48 size_t count;
49 struct hostapd_iface **iface;
52 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
55 extern int wpa_debug_level;
56 extern int wpa_debug_show_keys;
57 extern int wpa_debug_timestamp;
60 void hostapd_logger(struct hostapd_data *hapd, const u8 *addr,
61 unsigned int module, int level, const char *fmt, ...)
63 char *format, *module_str;
64 int maxlen;
65 va_list ap;
66 int conf_syslog_level, conf_stdout_level;
67 unsigned int conf_syslog, conf_stdout;
69 maxlen = strlen(fmt) + 100;
70 format = malloc(maxlen);
71 if (!format)
72 return;
74 if (hapd && hapd->conf) {
75 conf_syslog_level = hapd->conf->logger_syslog_level;
76 conf_stdout_level = hapd->conf->logger_stdout_level;
77 conf_syslog = hapd->conf->logger_syslog;
78 conf_stdout = hapd->conf->logger_stdout;
79 } else {
80 conf_syslog_level = conf_stdout_level = 0;
81 conf_syslog = conf_stdout = (unsigned int) -1;
84 switch (module) {
85 case HOSTAPD_MODULE_IEEE80211:
86 module_str = "IEEE 802.11";
87 break;
88 case HOSTAPD_MODULE_IEEE8021X:
89 module_str = "IEEE 802.1X";
90 break;
91 case HOSTAPD_MODULE_RADIUS:
92 module_str = "RADIUS";
93 break;
94 case HOSTAPD_MODULE_WPA:
95 module_str = "WPA";
96 break;
97 case HOSTAPD_MODULE_DRIVER:
98 module_str = "DRIVER";
99 break;
100 case HOSTAPD_MODULE_IAPP:
101 module_str = "IAPP";
102 break;
103 case HOSTAPD_MODULE_MLME:
104 module_str = "MLME";
105 break;
106 default:
107 module_str = NULL;
108 break;
111 if (hapd && hapd->conf && addr)
112 snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
113 hapd->conf->iface, MAC2STR(addr),
114 module_str ? " " : "", module_str, fmt);
115 else if (hapd && hapd->conf)
116 snprintf(format, maxlen, "%s:%s%s %s",
117 hapd->conf->iface, module_str ? " " : "",
118 module_str, fmt);
119 else if (addr)
120 snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
121 MAC2STR(addr), module_str ? " " : "",
122 module_str, fmt);
123 else
124 snprintf(format, maxlen, "%s%s%s",
125 module_str, module_str ? ": " : "", fmt);
127 if ((conf_stdout & module) && level >= conf_stdout_level) {
128 wpa_debug_print_timestamp();
129 va_start(ap, fmt);
130 vprintf(format, ap);
131 va_end(ap);
132 printf("\n");
135 #ifndef CONFIG_NATIVE_WINDOWS
136 if ((conf_syslog & module) && level >= conf_syslog_level) {
137 int priority;
138 switch (level) {
139 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
140 case HOSTAPD_LEVEL_DEBUG:
141 priority = LOG_DEBUG;
142 break;
143 case HOSTAPD_LEVEL_INFO:
144 priority = LOG_INFO;
145 break;
146 case HOSTAPD_LEVEL_NOTICE:
147 priority = LOG_NOTICE;
148 break;
149 case HOSTAPD_LEVEL_WARNING:
150 priority = LOG_WARNING;
151 break;
152 default:
153 priority = LOG_INFO;
154 break;
156 va_start(ap, fmt);
157 vsyslog(priority, format, ap);
158 va_end(ap);
160 #endif /* CONFIG_NATIVE_WINDOWS */
162 free(format);
166 const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
167 size_t buflen)
169 if (buflen == 0 || addr == NULL)
170 return NULL;
172 if (addr->af == AF_INET) {
173 snprintf(buf, buflen, "%s", inet_ntoa(addr->u.v4));
174 } else {
175 buf[0] = '\0';
177 #ifdef CONFIG_IPV6
178 if (addr->af == AF_INET6) {
179 if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
180 buf[0] = '\0';
182 #endif /* CONFIG_IPV6 */
184 return buf;
188 int hostapd_ip_diff(struct hostapd_ip_addr *a, struct hostapd_ip_addr *b)
190 if (a == NULL && b == NULL)
191 return 0;
192 if (a == NULL || b == NULL)
193 return 1;
195 switch (a->af) {
196 case AF_INET:
197 if (a->u.v4.s_addr != b->u.v4.s_addr)
198 return 1;
199 break;
200 #ifdef CONFIG_IPV6
201 case AF_INET6:
202 if (memcpy(&a->u.v6, &b->u.v6, sizeof(a->u.v6))
203 != 0)
204 return 1;
205 break;
206 #endif /* CONFIG_IPV6 */
209 return 0;
213 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
215 #if 0
216 u8 addr[ETH_ALEN];
218 memset(addr, 0xff, ETH_ALEN);
219 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
220 #else
221 /* New Prism2.5/3 STA firmware versions seem to have issues with this
222 * broadcast deauth frame. This gets the firmware in odd state where
223 * nothing works correctly, so let's skip sending this for a while
224 * until the issue has been resolved. */
225 #endif
230 * hostapd_prune_associations - Remove extraneous associations
231 * @hapd: Pointer to BSS data for the most recent association
232 * @sta: Pointer to the associated STA data
234 * This function looks through all radios and BSS's for previous
235 * (stale) associations of STA. If any are found they are removed.
237 static void hostapd_prune_associations(struct hostapd_data *hapd,
238 struct sta_info *sta)
240 struct sta_info *osta;
241 struct hostapd_data *ohapd;
242 size_t i, j;
243 struct hapd_interfaces *interfaces = eloop_get_user_data();
245 for (i = 0; i < interfaces->count; i++) {
246 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
247 ohapd = interfaces->iface[i]->bss[j];
248 if (ohapd == hapd)
249 continue;
250 osta = ap_get_sta(ohapd, sta->addr);
251 if (!osta)
252 continue;
254 ap_sta_disassociate(ohapd, osta,
255 WLAN_REASON_UNSPECIFIED);
262 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
263 * @hapd: Pointer to BSS data
264 * @sta: Pointer to the associated STA data
265 * @reassoc: 1 to indicate this was a re-association; 0 = first association
267 * This function will be called whenever a station associates with the AP. It
268 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
269 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
270 * authentication and association) internally.
272 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
273 int reassoc)
275 if (hapd->tkip_countermeasures) {
276 hostapd_sta_deauth(hapd, sta->addr,
277 WLAN_REASON_MICHAEL_MIC_FAILURE);
278 return;
281 hostapd_prune_associations(hapd, sta);
283 /* IEEE 802.11F (IAPP) */
284 if (hapd->conf->ieee802_11f)
285 iapp_new_station(hapd->iapp, sta);
287 /* Start accounting here, if IEEE 802.1X and WPA are not used.
288 * IEEE 802.1X/WPA code will start accounting after the station has
289 * been authorized. */
290 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
291 accounting_sta_start(hapd, sta);
293 hostapd_wme_sta_config(hapd, sta);
295 /* Start IEEE 802.1X authentication process for new stations */
296 ieee802_1x_new_station(hapd, sta);
297 if (reassoc)
298 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
299 else
300 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
304 #ifdef EAP_SERVER
305 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
306 struct sta_info *sta, void *ctx)
308 if (eapol_sm_eap_pending_cb(sta->eapol_sm, ctx) == 0)
309 return 1;
310 return 0;
314 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
316 struct hostapd_data *hapd = ctx;
317 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
318 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
320 #endif /* EAP_SERVER */
323 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
325 printf("Signal %d received - terminating\n", sig);
326 eloop_terminate();
330 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
331 struct wpa_auth_config *wconf)
333 wconf->wpa = conf->wpa;
334 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
335 wconf->wpa_pairwise = conf->wpa_pairwise;
336 wconf->wpa_group = conf->wpa_group;
337 wconf->wpa_group_rekey = conf->wpa_group_rekey;
338 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
339 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
340 wconf->rsn_preauth = conf->rsn_preauth;
341 wconf->eapol_version = conf->eapol_version;
342 wconf->peerkey = conf->peerkey;
343 wconf->wme_enabled = conf->wme_enabled;
344 #ifdef CONFIG_IEEE80211W
345 wconf->ieee80211w = conf->ieee80211w;
346 #endif /* CONFIG_IEEE80211W */
350 #ifndef CONFIG_NATIVE_WINDOWS
351 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
353 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
354 struct hostapd_config *newconf;
355 size_t i;
356 struct wpa_auth_config wpa_auth_conf;
358 printf("Signal %d received - reloading configuration\n", sig);
360 for (i = 0; i < hapds->count; i++) {
361 struct hostapd_data *hapd = hapds->iface[i]->bss[0];
362 newconf = hostapd_config_read(hapds->iface[i]->config_fname);
363 if (newconf == NULL) {
364 printf("Failed to read new configuration file - "
365 "continuing with old.\n");
366 continue;
368 /* TODO: update dynamic data based on changed configuration
369 * items (e.g., open/close sockets, remove stations added to
370 * deny list, etc.) */
371 radius_client_flush(hapd->radius, 0);
372 hostapd_config_free(hapd->iconf);
374 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
375 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
377 hapd->iconf = newconf;
378 hapd->conf = &newconf->bss[0];
379 hapds->iface[i]->conf = newconf;
381 if (hostapd_setup_wpa_psk(hapd->conf)) {
382 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
383 "after reloading configuration");
389 #ifdef HOSTAPD_DUMP_STATE
390 static void hostapd_dump_state(struct hostapd_data *hapd)
392 FILE *f;
393 time_t now;
394 struct sta_info *sta;
395 int i;
396 char *buf;
398 if (!hapd->conf->dump_log_name) {
399 printf("Dump file not defined - ignoring dump request\n");
400 return;
403 printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
404 f = fopen(hapd->conf->dump_log_name, "w");
405 if (f == NULL) {
406 printf("Could not open dump file '%s' for writing.\n",
407 hapd->conf->dump_log_name);
408 return;
411 time(&now);
412 fprintf(f, "hostapd state dump - %s", ctime(&now));
413 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
414 "num_sta_no_short_slot_time=%d\n"
415 "num_sta_no_short_preamble=%d\n",
416 hapd->num_sta, hapd->iface->num_sta_non_erp,
417 hapd->iface->num_sta_no_short_slot_time,
418 hapd->iface->num_sta_no_short_preamble);
420 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
421 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
423 fprintf(f,
424 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
425 " capability=0x%x listen_interval=%d\n",
426 sta->aid,
427 sta->flags,
428 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
429 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
430 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
431 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
432 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
433 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
434 ""),
435 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
436 ""),
437 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
438 "[SHORT_PREAMBLE]" : ""),
439 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
440 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
441 sta->capability,
442 sta->listen_interval);
444 fprintf(f, " supported_rates=");
445 for (i = 0; i < sta->supported_rates_len; i++)
446 fprintf(f, "%02x ", sta->supported_rates[i]);
447 fprintf(f, "\n");
449 fprintf(f,
450 " timeout_next=%s\n",
451 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
452 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
453 "DEAUTH")));
455 ieee802_1x_dump_state(f, " ", sta);
458 buf = malloc(4096);
459 if (buf) {
460 int count = radius_client_get_mib(hapd->radius, buf, 4096);
461 if (count < 0)
462 count = 0;
463 else if (count > 4095)
464 count = 4095;
465 buf[count] = '\0';
466 fprintf(f, "%s", buf);
468 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
469 if (count < 0)
470 count = 0;
471 else if (count > 4095)
472 count = 4095;
473 buf[count] = '\0';
474 fprintf(f, "%s", buf);
475 free(buf);
477 fclose(f);
479 #endif /* HOSTAPD_DUMP_STATE */
482 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
484 #ifdef HOSTAPD_DUMP_STATE
485 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
486 size_t i, j;
488 for (i = 0; i < hapds->count; i++) {
489 struct hostapd_iface *hapd_iface = hapds->iface[i];
490 for (j = 0; j < hapd_iface->num_bss; j++)
491 hostapd_dump_state(hapd_iface->bss[j]);
493 #endif /* HOSTAPD_DUMP_STATE */
495 #endif /* CONFIG_NATIVE_WINDOWS */
497 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
498 char *ifname)
500 int i;
502 for (i = 0; i < NUM_WEP_KEYS; i++) {
503 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
504 0, i == 0 ? 1 : 0)) {
505 printf("Failed to clear default encryption keys "
506 "(ifname=%s keyidx=%d)\n", ifname, i);
512 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
514 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
515 return 0;
519 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
521 int errors = 0, idx;
522 struct hostapd_ssid *ssid = &hapd->conf->ssid;
524 idx = ssid->wep.idx;
525 if (ssid->wep.default_len &&
526 hostapd_set_encryption(hapd->conf->iface,
527 hapd, "WEP", NULL, idx,
528 ssid->wep.key[idx],
529 ssid->wep.len[idx],
530 idx == ssid->wep.idx)) {
531 printf("Could not set WEP encryption.\n");
532 errors++;
535 if (ssid->dyn_vlan_keys) {
536 size_t i;
537 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
538 const char *ifname;
539 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
540 if (key == NULL)
541 continue;
542 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
544 if (ifname == NULL)
545 continue;
547 idx = key->idx;
548 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
549 idx, key->key[idx],
550 key->len[idx],
551 idx == key->idx)) {
552 printf("Could not set dynamic VLAN WEP "
553 "encryption.\n");
554 errors++;
559 return errors;
563 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
564 * @hapd: Pointer to BSS data
566 * This function is used to free all per-BSS data structures and resources.
567 * This gets called in a loop for each BSS between calls to
568 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
569 * is deinitialized. Most of the modules that are initialized in
570 * hostapd_setup_bss() are deinitialized here.
572 static void hostapd_cleanup(struct hostapd_data *hapd)
574 hostapd_ctrl_iface_deinit(hapd);
576 free(hapd->default_wep_key);
577 hapd->default_wep_key = NULL;
578 iapp_deinit(hapd->iapp);
579 hapd->iapp = NULL;
580 accounting_deinit(hapd);
581 rsn_preauth_iface_deinit(hapd);
582 if (hapd->wpa_auth) {
583 wpa_deinit(hapd->wpa_auth);
584 hapd->wpa_auth = NULL;
586 if (hostapd_set_privacy(hapd, 0)) {
587 wpa_printf(MSG_DEBUG, "Could not disable "
588 "PrivacyInvoked for interface %s",
589 hapd->conf->iface);
592 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
593 wpa_printf(MSG_DEBUG, "Could not remove generic "
594 "information element from interface %s",
595 hapd->conf->iface);
598 ieee802_1x_deinit(hapd);
599 vlan_deinit(hapd);
600 hostapd_acl_deinit(hapd);
601 radius_client_deinit(hapd->radius);
602 hapd->radius = NULL;
603 radius_server_deinit(hapd->radius_srv);
604 hapd->radius_srv = NULL;
606 hostapd_wireless_event_deinit(hapd);
608 #ifdef EAP_TLS_FUNCS
609 if (hapd->ssl_ctx) {
610 tls_deinit(hapd->ssl_ctx);
611 hapd->ssl_ctx = NULL;
613 #endif /* EAP_TLS_FUNCS */
615 #ifdef EAP_SERVER
616 if (hapd->eap_sim_db_priv) {
617 eap_sim_db_deinit(hapd->eap_sim_db_priv);
618 hapd->eap_sim_db_priv = NULL;
620 #endif /* EAP_SERVER */
622 if (hapd->interface_added &&
623 hostapd_bss_remove(hapd, hapd->conf->iface)) {
624 printf("Failed to remove BSS interface %s\n",
625 hapd->conf->iface);
631 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
632 * @iface: Pointer to interface data
634 * This function is called before per-BSS data structures are deinitialized
635 * with hostapd_cleanup().
637 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
643 * hostapd_cleanup_iface - Complete per-interface cleanup
644 * @iface: Pointer to interface data
646 * This function is called after per-BSS data structures are deinitialized
647 * with hostapd_cleanup().
649 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
651 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
652 iface->hw_features = NULL;
653 free(iface->current_rates);
654 iface->current_rates = NULL;
655 ap_list_deinit(iface);
656 hostapd_config_free(iface->conf);
657 iface->conf = NULL;
659 free(iface->config_fname);
660 free(iface->bss);
661 free(iface);
665 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
667 int i;
669 hostapd_broadcast_wep_set(hapd);
671 if (hapd->conf->ssid.wep.default_len)
672 return 0;
674 for (i = 0; i < 4; i++) {
675 if (hapd->conf->ssid.wep.key[i] &&
676 hostapd_set_encryption(iface, hapd, "WEP", NULL,
677 i, hapd->conf->ssid.wep.key[i],
678 hapd->conf->ssid.wep.len[i],
679 i == hapd->conf->ssid.wep.idx)) {
680 printf("Could not set WEP encryption.\n");
681 return -1;
683 if (hapd->conf->ssid.wep.key[i] &&
684 i == hapd->conf->ssid.wep.idx)
685 hostapd_set_privacy(hapd, 1);
688 return 0;
692 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
694 int ret = 0;
696 wpa_printf(MSG_DEBUG, "Flushing old station entries");
697 if (hostapd_flush(hapd)) {
698 printf("Could not connect to kernel driver.\n");
699 ret = -1;
701 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
702 hostapd_deauth_all_stas(hapd);
704 return ret;
708 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
709 logger_level level, const char *txt)
711 struct hostapd_data *hapd = ctx;
712 int hlevel;
714 switch (level) {
715 case LOGGER_WARNING:
716 hlevel = HOSTAPD_LEVEL_WARNING;
717 break;
718 case LOGGER_INFO:
719 hlevel = HOSTAPD_LEVEL_INFO;
720 break;
721 case LOGGER_DEBUG:
722 default:
723 hlevel = HOSTAPD_LEVEL_DEBUG;
724 break;
727 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
731 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
732 u16 reason)
734 struct hostapd_data *hapd = ctx;
735 struct sta_info *sta;
737 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
738 "STA " MACSTR " reason %d",
739 __func__, MAC2STR(addr), reason);
741 sta = ap_get_sta(hapd, addr);
742 hostapd_sta_deauth(hapd, addr, reason);
743 if (sta == NULL)
744 return;
745 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
746 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
747 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
748 sta->timeout_next = STA_REMOVE;
752 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
754 struct hostapd_data *hapd = ctx;
755 ieee80211_michael_mic_failure(hapd, addr, 0);
759 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
760 wpa_eapol_variable var, int value)
762 struct hostapd_data *hapd = ctx;
763 struct sta_info *sta = ap_get_sta(hapd, addr);
764 if (sta == NULL)
765 return;
766 switch (var) {
767 case WPA_EAPOL_portEnabled:
768 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
769 break;
770 case WPA_EAPOL_portValid:
771 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
772 break;
773 case WPA_EAPOL_authorized:
774 ieee802_1x_set_sta_authorized(hapd, sta, value);
775 break;
776 case WPA_EAPOL_portControl_Auto:
777 if (sta->eapol_sm)
778 sta->eapol_sm->portControl = Auto;
779 break;
780 case WPA_EAPOL_keyRun:
781 if (sta->eapol_sm)
782 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
783 break;
784 case WPA_EAPOL_keyAvailable:
785 if (sta->eapol_sm)
786 sta->eapol_sm->keyAvailable = value ? TRUE : FALSE;
787 break;
788 case WPA_EAPOL_keyDone:
789 if (sta->eapol_sm)
790 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
791 break;
792 case WPA_EAPOL_inc_EapolFramesTx:
793 if (sta->eapol_sm)
794 sta->eapol_sm->dot1xAuthEapolFramesTx++;
795 break;
800 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
801 wpa_eapol_variable var)
803 struct hostapd_data *hapd = ctx;
804 struct sta_info *sta = ap_get_sta(hapd, addr);
805 if (sta == NULL || sta->eapol_sm == NULL)
806 return -1;
807 switch (var) {
808 case WPA_EAPOL_keyRun:
809 return sta->eapol_sm->keyRun;
810 case WPA_EAPOL_keyAvailable:
811 return sta->eapol_sm->keyAvailable;
812 default:
813 return -1;
818 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
819 const u8 *prev_psk)
821 struct hostapd_data *hapd = ctx;
822 return hostapd_get_psk(hapd->conf, addr, prev_psk);
826 static int hostapd_wpa_auth_get_pmk(void *ctx, const u8 *addr, u8 *pmk,
827 size_t *len)
829 struct hostapd_data *hapd = ctx;
830 u8 *key;
831 size_t keylen;
832 struct sta_info *sta;
834 sta = ap_get_sta(hapd, addr);
835 if (sta == NULL)
836 return -1;
838 key = ieee802_1x_get_key_crypt(sta->eapol_sm, &keylen);
839 if (key == NULL)
840 return -1;
842 if (keylen > *len)
843 keylen = WPA_PMK_LEN;
844 memcpy(pmk, key, keylen);
845 *len = keylen;
846 return 0;
850 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
851 const u8 *addr, int idx, u8 *key,
852 size_t key_len)
854 struct hostapd_data *hapd = ctx;
855 const char *ifname = hapd->conf->iface;
857 if (vlan_id > 0) {
858 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
859 if (ifname == NULL)
860 return -1;
863 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
864 key, key_len, 1);
868 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
869 u8 *seq)
871 struct hostapd_data *hapd = ctx;
872 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
876 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
877 u8 *seq)
879 struct hostapd_data *hapd = ctx;
880 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
881 seq);
885 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
886 const u8 *data, size_t data_len,
887 int encrypt)
889 struct hostapd_data *hapd = ctx;
890 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
894 static int hostapd_wpa_auth_for_each_sta(
895 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
896 void *cb_ctx)
898 struct hostapd_data *hapd = ctx;
899 struct sta_info *sta;
901 for (sta = hapd->sta_list; sta; sta = sta->next) {
902 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
903 return 1;
905 return 0;
910 * hostapd_validate_bssid_configuration - Validate BSSID configuration
911 * @iface: Pointer to interface data
912 * Returns: 0 on success, -1 on failure
914 * This function is used to validate that the configured BSSIDs are valid.
916 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
918 u8 mask[ETH_ALEN] = { 0 };
919 struct hostapd_data *hapd = iface->bss[0];
920 unsigned int i = iface->conf->num_bss, bits = 0, j;
921 int res;
923 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
925 /* Determine the bits necessary to cover the number of BSSIDs. */
926 for (i--; i; i >>= 1)
927 bits++;
929 /* Determine the bits necessary to any configured BSSIDs,
930 if they are higher than the number of BSSIDs. */
931 for (j = 0; j < iface->conf->num_bss; j++) {
932 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
933 continue;
935 for (i = 0; i < ETH_ALEN; i++) {
936 mask[i] |=
937 iface->conf->bss[j].bssid[i] ^
938 hapd->own_addr[i];
942 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
944 j = 0;
945 if (i < ETH_ALEN) {
946 j = (5 - i) * 8;
948 while (mask[i] != 0) {
949 mask[i] >>= 1;
950 j++;
954 if (bits < j)
955 bits = j;
957 if (bits > 40)
958 return -1;
960 memset(mask, 0xff, ETH_ALEN);
961 j = bits / 8;
962 for (i = 5; i > 5 - j; i--)
963 mask[i] = 0;
964 j = bits % 8;
965 while (j--)
966 mask[i] <<= 1;
968 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "BSS count %lu, BSSID mask "
969 MACSTR " (%d bits)\n",
970 (unsigned long) iface->conf->num_bss, MAC2STR(mask),
971 bits);
973 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
974 if (res == 0)
975 return 0;
977 if (res < 0) {
978 printf("Driver did not accept BSSID mask " MACSTR " for start "
979 "address " MACSTR ".\n",
980 MAC2STR(mask), MAC2STR(hapd->own_addr));
981 return -1;
984 for (i = 0; i < ETH_ALEN; i++) {
985 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
986 printf("Invalid BSSID mask " MACSTR " for start "
987 "address " MACSTR ".\n"
988 "Start address must be the first address in the"
989 " block (i.e., addr AND mask == addr).\n",
990 MAC2STR(mask), MAC2STR(hapd->own_addr));
991 return -1;
995 return 0;
999 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1001 size_t i;
1003 for (i = 0; i < conf->num_bss; i++) {
1004 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1005 return 1;
1009 return 0;
1014 * hostapd_setup_bss - Per-BSS setup (initialization)
1015 * @hapd: Pointer to BSS data
1016 * @first: Whether this BSS is the first BSS of an interface
1018 * This function is used to initialize all per-BSS data structures and
1019 * resources. This gets called in a loop for each BSS when an interface is
1020 * initialized. Most of the modules that are initialized here will be
1021 * deinitialized in hostapd_cleanup().
1023 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1025 struct hostapd_bss_config *conf = hapd->conf;
1026 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1027 int ssid_len, set_ssid;
1029 if (!first) {
1030 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1031 /* Allocate the next available BSSID. */
1032 do {
1033 inc_byte_array(hapd->own_addr, ETH_ALEN);
1034 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1035 } else {
1036 /* Allocate the configured BSSID. */
1037 memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1039 if (hostapd_mac_comp(hapd->own_addr,
1040 hapd->iface->bss[0]->own_addr) ==
1041 0) {
1042 printf("BSS '%s' may not have BSSID "
1043 "set to the MAC address of the radio\n",
1044 hapd->conf->iface);
1045 return -1;
1049 hapd->interface_added = 1;
1050 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1051 hapd->own_addr)) {
1052 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1053 MAC2STR(hapd->own_addr));
1054 return -1;
1059 * Fetch the SSID from the system and use it or,
1060 * if one was specified in the config file, verify they
1061 * match.
1063 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1064 if (ssid_len < 0) {
1065 printf("Could not read SSID from system\n");
1066 return -1;
1068 if (conf->ssid.ssid_set) {
1070 * If SSID is specified in the config file and it differs
1071 * from what is being used then force installation of the
1072 * new SSID.
1074 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1075 memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1076 } else {
1078 * No SSID in the config file; just use the one we got
1079 * from the system.
1081 set_ssid = 0;
1082 conf->ssid.ssid_len = ssid_len;
1083 memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1084 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1087 printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1088 hapd->conf->iface, MAC2STR(hapd->own_addr),
1089 hapd->conf->ssid.ssid);
1091 if (hostapd_setup_wpa_psk(conf)) {
1092 printf("WPA-PSK setup failed.\n");
1093 return -1;
1096 /* Set flag for whether SSID is broadcast in beacons */
1097 if (hostapd_set_broadcast_ssid(hapd,
1098 !!hapd->conf->ignore_broadcast_ssid)) {
1099 printf("Could not set broadcast SSID flag for kernel "
1100 "driver\n");
1101 return -1;
1104 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1105 printf("Could not set DTIM period for kernel driver\n");
1106 return -1;
1109 /* Set SSID for the kernel driver (to be used in beacon and probe
1110 * response frames) */
1111 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1112 conf->ssid.ssid_len)) {
1113 printf("Could not set SSID for kernel driver\n");
1114 return -1;
1117 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS))
1118 conf->radius->msg_dumps = 1;
1119 hapd->radius = radius_client_init(hapd, conf->radius);
1120 if (hapd->radius == NULL) {
1121 printf("RADIUS client initialization failed.\n");
1122 return -1;
1125 if (hostapd_acl_init(hapd)) {
1126 printf("ACL initialization failed.\n");
1127 return -1;
1129 if (ieee802_1x_init(hapd)) {
1130 printf("IEEE 802.1X initialization failed.\n");
1131 return -1;
1134 if (hapd->conf->wpa) {
1135 struct wpa_auth_config _conf;
1136 struct wpa_auth_callbacks cb;
1137 const u8 *wpa_ie;
1138 size_t wpa_ie_len;
1140 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1141 memset(&cb, 0, sizeof(cb));
1142 cb.ctx = hapd;
1143 cb.logger = hostapd_wpa_auth_logger;
1144 cb.disconnect = hostapd_wpa_auth_disconnect;
1145 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1146 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1147 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1148 cb.get_psk = hostapd_wpa_auth_get_psk;
1149 cb.get_pmk = hostapd_wpa_auth_get_pmk;
1150 cb.set_key = hostapd_wpa_auth_set_key;
1151 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1152 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1153 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1154 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1155 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1156 if (hapd->wpa_auth == NULL) {
1157 printf("WPA initialization failed.\n");
1158 return -1;
1161 if (hostapd_set_privacy(hapd, 1)) {
1162 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1163 "for interface %s", hapd->conf->iface);
1164 return -1;
1167 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1168 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1169 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1170 "the kernel driver.");
1171 return -1;
1174 if (rsn_preauth_iface_init(hapd)) {
1175 printf("Initialization of RSN pre-authentication "
1176 "failed.\n");
1177 return -1;
1181 if (accounting_init(hapd)) {
1182 printf("Accounting initialization failed.\n");
1183 return -1;
1186 if (hapd->conf->ieee802_11f &&
1187 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1188 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1189 return -1;
1192 if (hostapd_ctrl_iface_init(hapd)) {
1193 printf("Failed to setup control interface\n");
1194 return -1;
1197 ieee802_11_set_beacon(hapd);
1199 if (vlan_init(hapd)) {
1200 printf("VLAN initialization failed.\n");
1201 return -1;
1204 return 0;
1209 * setup_interface2 - Setup (initialize) an interface (part 2)
1210 * @iface: Pointer to interface data.
1211 * Returns: 0 on success; -1 on failure.
1213 * Flushes old stations, sets the channel, DFS parameters, encryption,
1214 * beacons, and WDS links based on the configuration.
1216 static int setup_interface2(struct hostapd_iface *iface)
1218 struct hostapd_data *hapd = iface->bss[0];
1219 int freq;
1220 size_t j;
1221 int ret = 0;
1222 u8 *prev_addr;
1224 hostapd_flush_old_stations(hapd);
1225 hostapd_set_privacy(hapd, 0);
1227 if (hapd->iconf->channel) {
1228 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1229 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1230 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1231 hapd->iconf->channel, freq);
1233 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1234 printf("Could not set channel for kernel driver\n");
1235 return -1;
1239 hostapd_broadcast_wep_clear(hapd);
1240 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1241 return -1;
1243 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1244 ieee802_11_set_beacon(hapd);
1246 if (hapd->iconf->rts_threshold > -1 &&
1247 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1248 printf("Could not set RTS threshold for kernel driver\n");
1249 return -1;
1252 if (hapd->iconf->fragm_threshold > -1 &&
1253 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1254 printf("Could not set fragmentation threshold for kernel "
1255 "driver\n");
1256 return -1;
1259 prev_addr = hapd->own_addr;
1261 for (j = 0; j < iface->num_bss; j++) {
1262 hapd = iface->bss[j];
1263 if (j)
1264 memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1265 if (hostapd_setup_bss(hapd, j == 0))
1266 return -1;
1267 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1268 prev_addr = hapd->own_addr;
1271 ap_list_init(iface);
1273 if (hostapd_driver_commit(hapd) < 0) {
1274 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1275 "configuration", __func__);
1276 return -1;
1279 return ret;
1283 static void setup_interface_start(void *eloop_data, void *user_ctx);
1284 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1287 * setup_interface_finalize - Finish setup interface & call the callback
1288 * @iface: Pointer to interface data.
1289 * @status: Status of the setup interface (0 on success; -1 on failure).
1290 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1292 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1294 hostapd_iface_cb cb;
1296 if (!iface->setup_cb)
1297 return -1;
1299 eloop_cancel_timeout(setup_interface_start, iface, NULL);
1300 eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1301 hostapd_select_hw_mode_stop(iface);
1303 cb = iface->setup_cb;
1305 iface->setup_cb = NULL;
1307 cb(iface, status);
1309 return 0;
1314 * setup_interface2_wrapper - Wrapper for setup_interface2()
1315 * @iface: Pointer to interface data.
1316 * @status: Status of the hw mode select.
1318 * Wrapper for setup_interface2() to calls finalize function upon completion.
1320 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1322 int ret = status;
1323 if (ret)
1324 printf("Could not select hw_mode and channel. (%d)\n", ret);
1325 else
1326 ret = setup_interface2(iface);
1328 setup_interface_finalize(iface, ret);
1333 * setup_interface2_handler - Used for immediate call of setup_interface2
1334 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1335 * @user_ctx: Unused.
1337 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1339 struct hostapd_iface *iface = eloop_data;
1341 setup_interface2_wrapper(iface, 0);
1346 * setup_interface1 - Setup (initialize) an interface (part 1)
1347 * @iface: Pointer to interface data
1348 * Returns: 0 on success, -1 on failure
1350 * Initializes the driver interface, validates the configuration,
1351 * and sets driver parameters based on the configuration.
1352 * Schedules setup_interface2() to be called immediately or after
1353 * hardware mode setup takes place.
1355 static int setup_interface1(struct hostapd_iface *iface)
1357 struct hostapd_data *hapd = iface->bss[0];
1358 struct hostapd_bss_config *conf = hapd->conf;
1359 size_t i;
1360 char country[4];
1363 * Initialize the driver interface and make sure that all BSSes get
1364 * configured with a pointer to this driver interface.
1366 if (hostapd_driver_init(hapd)) {
1367 printf("%s driver initialization failed.\n",
1368 hapd->driver ? hapd->driver->name : "Unknown");
1369 hapd->driver = NULL;
1370 return -1;
1372 for (i = 0; i < iface->num_bss; i++)
1373 iface->bss[i]->driver = hapd->driver;
1375 if (hostapd_validate_bssid_configuration(iface))
1376 return -1;
1378 memcpy(country, hapd->iconf->country, 3);
1379 country[3] = '\0';
1380 if (hostapd_set_country(hapd, country) < 0) {
1381 printf("Failed to set country code\n");
1382 return -1;
1385 if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1386 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1387 printf("Failed to set ieee80211d (%d)\n",
1388 hapd->iconf->ieee80211d);
1389 return -1;
1393 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1394 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1395 printf("Failed to set bridge_packets for kernel driver\n");
1396 return -1;
1399 if (conf->radius_server_clients) {
1400 struct radius_server_conf srv;
1401 memset(&srv, 0, sizeof(srv));
1402 srv.client_file = conf->radius_server_clients;
1403 srv.auth_port = conf->radius_server_auth_port;
1404 srv.hostapd_conf = conf;
1405 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1406 srv.ssl_ctx = hapd->ssl_ctx;
1407 srv.ipv6 = conf->radius_server_ipv6;
1408 hapd->radius_srv = radius_server_init(&srv);
1409 if (hapd->radius_srv == NULL) {
1410 printf("RADIUS server initialization failed.\n");
1411 return -1;
1415 /* TODO: merge with hostapd_driver_init() ? */
1416 if (hostapd_wireless_event_init(hapd) < 0)
1417 return -1;
1419 if (hostapd_get_hw_features(iface)) {
1420 /* Not all drivers support this yet, so continue without hw
1421 * feature data. */
1422 } else {
1423 return hostapd_select_hw_mode_start(iface,
1424 setup_interface2_wrapper);
1427 eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1428 return 0;
1433 * setup_interface_start - Handler to start setup interface
1434 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1435 * @user_ctx: Unused.
1437 * An eloop handler is used so that all errors can be processed by the
1438 * callback without introducing stack recursion.
1440 static void setup_interface_start(void *eloop_data, void *user_ctx)
1442 struct hostapd_iface *iface = eloop_data;
1444 int ret;
1446 ret = setup_interface1(iface);
1447 if (ret)
1448 setup_interface_finalize(iface, ret);
1453 * hostapd_setup_interface_start - Start the setup of an interface
1454 * @iface: Pointer to interface data.
1455 * @cb: The function to callback when done.
1456 * Returns: 0 if it starts successfully; cb will be called when done.
1457 * -1 on failure; cb will not be called.
1459 * Initializes the driver interface, validates the configuration,
1460 * and sets driver parameters based on the configuration.
1461 * Flushes old stations, sets the channel, DFS parameters, encryption,
1462 * beacons, and WDS links based on the configuration.
1464 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1465 hostapd_iface_cb cb)
1467 if (iface->setup_cb) {
1468 wpa_printf(MSG_DEBUG,
1469 "%s: Interface setup already in progress.\n",
1470 iface->bss[0]->conf->iface);
1471 return -1;
1474 iface->setup_cb = cb;
1476 eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1478 return 0;
1483 * hostapd_setup_interace_stop - Stops the setup of an interface
1484 * @iface: Pointer to interface data
1485 * Returns: 0 if successfully stopped;
1486 * -1 on failure (i.e., was not in progress)
1488 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1490 return setup_interface_finalize(iface, -1);
1494 struct driver {
1495 struct driver *next;
1496 char *name;
1497 const struct driver_ops *ops;
1499 static struct driver *drivers = NULL;
1501 void driver_register(const char *name, const struct driver_ops *ops)
1503 struct driver *d;
1505 d = malloc(sizeof(struct driver));
1506 if (d == NULL) {
1507 printf("Failed to register driver %s!\n", name);
1508 return;
1510 d->name = strdup(name);
1511 if (d->name == NULL) {
1512 printf("Failed to register driver %s!\n", name);
1513 free(d);
1514 return;
1516 d->ops = ops;
1518 d->next = drivers;
1519 drivers = d;
1523 void driver_unregister(const char *name)
1525 struct driver *p, **pp;
1527 for (pp = &drivers; (p = *pp) != NULL; pp = &p->next) {
1528 if (strcasecmp(p->name, name) == 0) {
1529 *pp = p->next;
1530 p->next = NULL;
1531 free(p->name);
1532 free(p);
1533 break;
1539 static void driver_unregister_all(void)
1541 struct driver *p, *pp;
1542 p = drivers;
1543 drivers = NULL;
1544 while (p) {
1545 pp = p;
1546 p = p->next;
1547 free(pp->name);
1548 free(pp);
1553 const struct driver_ops * driver_lookup(const char *name)
1555 struct driver *p;
1557 if (strcmp(name, "default") == 0) {
1558 p = drivers;
1559 while (p && p->next)
1560 p = p->next;
1561 return p->ops;
1564 for (p = drivers; p != NULL; p = p->next) {
1565 if (strcasecmp(p->name, name) == 0)
1566 return p->ops;
1569 return NULL;
1573 static void show_version(void)
1575 fprintf(stderr,
1576 "hostapd v" VERSION_STR "\n"
1577 "User space daemon for IEEE 802.11 AP management,\n"
1578 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1579 "Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> "
1580 "and contributors\n");
1584 static void usage(void)
1586 show_version();
1587 fprintf(stderr,
1588 "\n"
1589 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1590 "<configuration file(s)>\n"
1591 "\n"
1592 "options:\n"
1593 " -h show this usage\n"
1594 " -d show more debug messages (-dd for even more)\n"
1595 " -B run daemon in the background\n"
1596 " -P PID file\n"
1597 " -K include key data in debug messages\n"
1598 " -t include timestamps in some debug messages\n"
1599 " -v show hostapd version\n");
1601 exit(1);
1606 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1607 * @hapd_iface: Pointer to interface data
1608 * @conf: Pointer to per-interface configuration
1609 * @bss: Pointer to per-BSS configuration for this BSS
1610 * Returns: Pointer to allocated BSS data
1612 * This function is used to allocate per-BSS data structure. This data will be
1613 * freed after hostapd_cleanup() is called for it during interface
1614 * deinitialization.
1616 static struct hostapd_data *
1617 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1618 struct hostapd_config *conf,
1619 struct hostapd_bss_config *bss)
1621 struct hostapd_data *hapd;
1623 hapd = wpa_zalloc(sizeof(*hapd));
1624 if (hapd == NULL)
1625 return NULL;
1627 hapd->iconf = conf;
1628 hapd->conf = bss;
1629 hapd->iface = hapd_iface;
1631 if (hapd->conf->individual_wep_key_len > 0) {
1632 /* use key0 in individual key and key1 in broadcast key */
1633 hapd->default_wep_key_idx = 1;
1636 #ifdef EAP_TLS_FUNCS
1637 if (hapd->conf->eap_server &&
1638 (hapd->conf->ca_cert || hapd->conf->server_cert)) {
1639 struct tls_connection_params params;
1641 hapd->ssl_ctx = tls_init(NULL);
1642 if (hapd->ssl_ctx == NULL) {
1643 printf("Failed to initialize TLS\n");
1644 goto fail;
1647 memset(&params, 0, sizeof(params));
1648 params.ca_cert = hapd->conf->ca_cert;
1649 params.client_cert = hapd->conf->server_cert;
1650 params.private_key = hapd->conf->private_key;
1651 params.private_key_passwd = hapd->conf->private_key_passwd;
1653 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1654 printf("Failed to set TLS parameters\n");
1655 goto fail;
1658 if (tls_global_set_verify(hapd->ssl_ctx,
1659 hapd->conf->check_crl)) {
1660 printf("Failed to enable check_crl\n");
1661 goto fail;
1664 #endif /* EAP_TLS_FUNCS */
1666 #ifdef EAP_SERVER
1667 if (hapd->conf->eap_sim_db) {
1668 hapd->eap_sim_db_priv =
1669 eap_sim_db_init(hapd->conf->eap_sim_db,
1670 hostapd_sim_db_cb, hapd);
1671 if (hapd->eap_sim_db_priv == NULL) {
1672 printf("Failed to initialize EAP-SIM database "
1673 "interface\n");
1674 goto fail;
1677 #endif /* EAP_SERVER */
1679 if (hapd->conf->assoc_ap)
1680 hapd->assoc_ap_state = WAIT_BEACON;
1682 /* FIX: need to fix this const vs. not */
1683 hapd->driver = (struct driver_ops *) hapd->iconf->driver;
1685 return hapd;
1687 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1688 fail:
1689 #endif
1690 /* TODO: cleanup allocated resources(?) */
1691 free(hapd);
1692 return NULL;
1697 * hostapd_init - Allocate and initialize per-interface data
1698 * @config_file: Path to the configuration file
1699 * Returns: Pointer to the allocated interface data or %NULL on failure
1701 * This function is used to allocate main data structures for per-interface
1702 * data. The allocated data buffer will be freed by calling
1703 * hostapd_cleanup_iface().
1705 static struct hostapd_iface * hostapd_init(const char *config_file)
1707 struct hostapd_iface *hapd_iface = NULL;
1708 struct hostapd_config *conf = NULL;
1709 struct hostapd_data *hapd;
1710 size_t i;
1712 hapd_iface = wpa_zalloc(sizeof(*hapd_iface));
1713 if (hapd_iface == NULL)
1714 goto fail;
1716 hapd_iface->config_fname = strdup(config_file);
1717 if (hapd_iface->config_fname == NULL)
1718 goto fail;
1720 conf = hostapd_config_read(hapd_iface->config_fname);
1721 if (conf == NULL)
1722 goto fail;
1723 hapd_iface->conf = conf;
1725 hapd_iface->num_bss = conf->num_bss;
1726 hapd_iface->bss = wpa_zalloc(conf->num_bss *
1727 sizeof(struct hostapd_data *));
1728 if (hapd_iface->bss == NULL)
1729 goto fail;
1731 for (i = 0; i < conf->num_bss; i++) {
1732 hapd = hapd_iface->bss[i] =
1733 hostapd_alloc_bss_data(hapd_iface, conf,
1734 &conf->bss[i]);
1735 if (hapd == NULL)
1736 goto fail;
1739 return hapd_iface;
1741 fail:
1742 if (conf)
1743 hostapd_config_free(conf);
1744 if (hapd_iface) {
1745 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1746 hapd = hapd_iface->bss[i];
1747 if (hapd && hapd->ssl_ctx)
1748 tls_deinit(hapd->ssl_ctx);
1751 free(hapd_iface->config_fname);
1752 free(hapd_iface->bss);
1753 free(hapd_iface);
1755 return NULL;
1760 * register_drivers - Register driver interfaces
1762 * This function is generated by Makefile (into driver_conf.c) to call all
1763 * configured driver interfaces to register them to core hostapd.
1765 void register_drivers(void);
1769 * setup_interface_done - Callback when an interface is done being setup.
1770 * @iface: Pointer to interface data.
1771 * @status: Status of the interface setup (0 on success; -1 on failure).
1773 static void setup_interface_done(struct hostapd_iface *iface, int status)
1775 if (status) {
1776 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1777 iface->bss[0]->conf->iface);
1778 eloop_terminate();
1779 } else
1780 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1781 iface->bss[0]->conf->iface);
1785 int main(int argc, char *argv[])
1787 struct hapd_interfaces interfaces;
1788 int ret = 1, k;
1789 size_t i, j;
1790 int c, debug = 0, daemonize = 0;
1791 const char *pid_file = NULL;
1793 for (;;) {
1794 c = getopt(argc, argv, "BdhKP:tv");
1795 if (c < 0)
1796 break;
1797 switch (c) {
1798 case 'h':
1799 usage();
1800 break;
1801 case 'd':
1802 debug++;
1803 if (wpa_debug_level > 0)
1804 wpa_debug_level--;
1805 break;
1806 case 'B':
1807 daemonize++;
1808 break;
1809 case 'K':
1810 wpa_debug_show_keys++;
1811 break;
1812 case 'P':
1813 pid_file = optarg;
1814 break;
1815 case 't':
1816 wpa_debug_timestamp++;
1817 break;
1818 case 'v':
1819 show_version();
1820 exit(1);
1821 break;
1823 default:
1824 usage();
1825 break;
1829 if (optind == argc)
1830 usage();
1832 register_drivers(); /* NB: generated by Makefile */
1834 if (eap_server_register_methods()) {
1835 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1836 return -1;
1839 interfaces.count = argc - optind;
1841 interfaces.iface = malloc(interfaces.count *
1842 sizeof(struct hostapd_iface *));
1843 if (interfaces.iface == NULL) {
1844 printf("malloc failed\n");
1845 exit(1);
1848 if (eloop_init(&interfaces)) {
1849 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1850 return -1;
1853 #ifndef CONFIG_NATIVE_WINDOWS
1854 eloop_register_signal(SIGHUP, handle_reload, NULL);
1855 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1856 #endif /* CONFIG_NATIVE_WINDOWS */
1857 eloop_register_signal_terminate(handle_term, NULL);
1859 /* Initialize interfaces */
1860 for (i = 0; i < interfaces.count; i++) {
1861 printf("Configuration file: %s\n", argv[optind + i]);
1862 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1863 if (!interfaces.iface[i])
1864 goto out;
1865 for (k = 0; k < debug; k++) {
1866 if (interfaces.iface[i]->bss[0]->conf->
1867 logger_stdout_level > 0)
1868 interfaces.iface[i]->bss[0]->conf->
1869 logger_stdout_level--;
1870 interfaces.iface[i]->bss[0]->conf->debug++;
1873 ret = hostapd_setup_interface_start(interfaces.iface[i],
1874 setup_interface_done);
1875 if (ret)
1876 goto out;
1879 if (daemonize && os_daemonize(pid_file)) {
1880 perror("daemon");
1881 goto out;
1884 #ifndef CONFIG_NATIVE_WINDOWS
1885 openlog("hostapd", 0, LOG_DAEMON);
1886 #endif /* CONFIG_NATIVE_WINDOWS */
1888 eloop_run();
1890 /* Disconnect associated stations from all interfaces and BSSes */
1891 for (i = 0; i < interfaces.count; i++) {
1892 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1893 struct hostapd_data *hapd =
1894 interfaces.iface[i]->bss[j];
1895 hostapd_free_stas(hapd);
1896 hostapd_flush_old_stations(hapd);
1900 ret = 0;
1902 out:
1903 /* Deinitialize all interfaces */
1904 for (i = 0; i < interfaces.count; i++) {
1905 if (!interfaces.iface[i])
1906 continue;
1907 hostapd_setup_interface_stop(interfaces.iface[i]);
1908 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1909 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1910 struct hostapd_data *hapd =
1911 interfaces.iface[i]->bss[j];
1912 hostapd_cleanup(hapd);
1913 if (j == interfaces.iface[i]->num_bss - 1 &&
1914 hapd->driver)
1915 hostapd_driver_deinit(hapd);
1917 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1918 free(interfaces.iface[i]->bss[j]);
1919 hostapd_cleanup_iface(interfaces.iface[i]);
1921 free(interfaces.iface);
1923 eloop_destroy();
1925 #ifndef CONFIG_NATIVE_WINDOWS
1926 closelog();
1927 #endif /* CONFIG_NATIVE_WINDOWS */
1929 eap_server_unregister_methods();
1931 driver_unregister_all();
1933 os_daemonize_terminate(pid_file);
1935 return ret;