hostapd.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "eloop.h"
00019 #include "hostapd.h"
00020 #include "ieee802_1x.h"
00021 #include "beacon.h"
00022 #include "hw_features.h"
00023 #include "accounting.h"
00024 #include "eapol_sm.h"
00025 #include "iapp.h"
00026 #include "ieee802_11_defs.h"
00027 #include "ieee802_11_auth.h"
00028 #include "sta_info.h"
00029 #include "ap_list.h"
00030 #include "driver_i.h"
00031 #include "radius/radius_client.h"
00032 #include "radius/radius_server.h"
00033 #include "wpa.h"
00034 #include "preauth.h"
00035 #include "vlan_init.h"
00036 #include "ctrl_iface.h"
00037 #include "tls.h"
00038 #include "eap_server/eap_sim_db.h"
00039 #include "eap_server/eap.h"
00040 #include "eap_server/tncs.h"
00041 #include "version.h"
00042 #include "l2_packet/l2_packet.h"
00043 #include "wps_hostapd.h"
00044 #include "tkip_countermeasures.h"
00045 
00046 
00047 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
00048                                        size_t identity_len, int phase2,
00049                                        struct eap_user *user);
00050 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
00051 static int hostapd_setup_wpa(struct hostapd_data *hapd);
00052 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
00053 
00054 extern int wpa_debug_level;
00055 
00056 
00057 #ifdef EAP_SERVER
00058 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
00059                                  struct sta_info *sta, void *ctx)
00060 {
00061         if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
00062                 return 1;
00063         return 0;
00064 }
00065 
00066 
00067 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
00068 {
00069         struct hostapd_data *hapd = ctx;
00070         if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
00071                 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
00072 }
00073 #endif /* EAP_SERVER */
00074 
00075 
00076 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
00077                                   struct wpa_auth_config *wconf)
00078 {
00079         wconf->wpa = conf->wpa;
00080         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
00081         wconf->wpa_pairwise = conf->wpa_pairwise;
00082         wconf->wpa_group = conf->wpa_group;
00083         wconf->wpa_group_rekey = conf->wpa_group_rekey;
00084         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
00085         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
00086         wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
00087         wconf->rsn_pairwise = conf->rsn_pairwise;
00088         wconf->rsn_preauth = conf->rsn_preauth;
00089         wconf->eapol_version = conf->eapol_version;
00090         wconf->peerkey = conf->peerkey;
00091         wconf->wmm_enabled = conf->wmm_enabled;
00092         wconf->okc = conf->okc;
00093 #ifdef CONFIG_IEEE80211W
00094         wconf->ieee80211w = conf->ieee80211w;
00095 #endif /* CONFIG_IEEE80211W */
00096 #ifdef CONFIG_IEEE80211R
00097         wconf->ssid_len = conf->ssid.ssid_len;
00098         if (wconf->ssid_len > SSID_LEN)
00099                 wconf->ssid_len = SSID_LEN;
00100         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
00101         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
00102                   MOBILITY_DOMAIN_ID_LEN);
00103         if (conf->nas_identifier &&
00104             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
00105                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
00106                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
00107                           wconf->r0_key_holder_len);
00108         }
00109         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
00110         wconf->r0_key_lifetime = conf->r0_key_lifetime;
00111         wconf->reassociation_deadline = conf->reassociation_deadline;
00112         wconf->r0kh_list = conf->r0kh_list;
00113         wconf->r1kh_list = conf->r1kh_list;
00114         wconf->pmk_r1_push = conf->pmk_r1_push;
00115 #endif /* CONFIG_IEEE80211R */
00116 }
00117 
00118 
00119 int hostapd_reload_config(struct hostapd_iface *iface)
00120 {
00121         struct hostapd_data *hapd = iface->bss[0];
00122         struct hostapd_config *newconf, *oldconf;
00123         struct wpa_auth_config wpa_auth_conf;
00124         size_t j;
00125 
00126         newconf = hostapd_config_read(iface->config_fname);
00127         if (newconf == NULL)
00128                 return -1;
00129 
00130         /*
00131          * Deauthenticate all stations since the new configuration may not
00132          * allow them to use the BSS anymore.
00133          */
00134         for (j = 0; j < iface->num_bss; j++)
00135                 hostapd_flush_old_stations(iface->bss[j]);
00136 
00137         /* TODO: update dynamic data based on changed configuration
00138          * items (e.g., open/close sockets, etc.) */
00139         radius_client_flush(hapd->radius, 0);
00140 
00141         oldconf = hapd->iconf;
00142         hapd->iconf = newconf;
00143         hapd->conf = &newconf->bss[0];
00144         iface->conf = newconf;
00145 
00146         if (hostapd_setup_wpa_psk(hapd->conf)) {
00147                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
00148                            "after reloading configuration");
00149         }
00150 
00151         if (hapd->conf->wpa && hapd->wpa_auth == NULL)
00152                 hostapd_setup_wpa(hapd);
00153         else if (hapd->conf->wpa) {
00154                 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
00155                 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
00156         } else if (hapd->wpa_auth) {
00157                 wpa_deinit(hapd->wpa_auth);
00158                 hapd->wpa_auth = NULL;
00159                 hostapd_set_privacy(hapd, 0);
00160                 hostapd_setup_encryption(hapd->conf->iface, hapd);
00161         }
00162 
00163         ieee802_11_set_beacon(hapd);
00164 
00165         if (hapd->conf->ssid.ssid_set &&
00166             hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
00167                              hapd->conf->ssid.ssid_len)) {
00168                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
00169                 /* try to continue */
00170         }
00171 
00172         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
00173                 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1);
00174 
00175         hostapd_config_free(oldconf);
00176 
00177         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
00178 
00179         return 0;
00180 }
00181 
00182 
00183 int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
00184 {
00185         if (hostapd_reload_config(iface) < 0) {
00186                 wpa_printf(MSG_WARNING, "Failed to read new configuration "
00187                            "file - continuing with old.");
00188         }
00189         return 0;
00190 }
00191 
00192 
00193 #ifdef HOSTAPD_DUMP_STATE
00194 
00198 static void hostapd_dump_state(struct hostapd_data *hapd)
00199 {
00200         FILE *f;
00201         time_t now;
00202         struct sta_info *sta;
00203         int i;
00204         char *buf;
00205 
00206         if (!hapd->conf->dump_log_name) {
00207                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
00208                            "request");
00209                 return;
00210         }
00211 
00212         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
00213                    hapd->conf->dump_log_name);
00214         f = fopen(hapd->conf->dump_log_name, "w");
00215         if (f == NULL) {
00216                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
00217                            "writing.", hapd->conf->dump_log_name);
00218                 return;
00219         }
00220 
00221         time(&now);
00222         fprintf(f, "hostapd state dump - %s", ctime(&now));
00223         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
00224                 "num_sta_no_short_slot_time=%d\n"
00225                 "num_sta_no_short_preamble=%d\n",
00226                 hapd->num_sta, hapd->iface->num_sta_non_erp,
00227                 hapd->iface->num_sta_no_short_slot_time,
00228                 hapd->iface->num_sta_no_short_preamble);
00229 
00230         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
00231                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
00232 
00233                 fprintf(f,
00234                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
00235                         "  capability=0x%x listen_interval=%d\n",
00236                         sta->aid,
00237                         sta->flags,
00238                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
00239                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
00240                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
00241                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
00242                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
00243                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
00244                          ""),
00245                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
00246                          ""),
00247                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
00248                          "[SHORT_PREAMBLE]" : ""),
00249                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
00250                         (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
00251                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
00252                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
00253                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
00254                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
00255                         sta->capability,
00256                         sta->listen_interval);
00257 
00258                 fprintf(f, "  supported_rates=");
00259                 for (i = 0; i < sta->supported_rates_len; i++)
00260                         fprintf(f, "%02x ", sta->supported_rates[i]);
00261                 fprintf(f, "\n");
00262 
00263                 fprintf(f,
00264                         "  timeout_next=%s\n",
00265                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
00266                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
00267                           "DEAUTH")));
00268 
00269                 ieee802_1x_dump_state(f, "  ", sta);
00270         }
00271 
00272         buf = os_malloc(4096);
00273         if (buf) {
00274                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
00275                 if (count < 0)
00276                         count = 0;
00277                 else if (count > 4095)
00278                         count = 4095;
00279                 buf[count] = '\0';
00280                 fprintf(f, "%s", buf);
00281 
00282                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
00283                 if (count < 0)
00284                         count = 0;
00285                 else if (count > 4095)
00286                         count = 4095;
00287                 buf[count] = '\0';
00288                 fprintf(f, "%s", buf);
00289                 os_free(buf);
00290         }
00291         fclose(f);
00292 }
00293 
00294 
00295 int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
00296 {
00297         size_t i;
00298 
00299         for (i = 0; i < iface->num_bss; i++)
00300                 hostapd_dump_state(iface->bss[i]);
00301 
00302         return 0;
00303 }
00304 #endif /* HOSTAPD_DUMP_STATE */
00305 
00306 
00307 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
00308                                               char *ifname)
00309 {
00310         int i;
00311 
00312         for (i = 0; i < NUM_WEP_KEYS; i++) {
00313                 if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
00314                                     i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
00315                         wpa_printf(MSG_DEBUG, "Failed to clear default "
00316                                    "encryption keys (ifname=%s keyidx=%d)",
00317                                    ifname, i);
00318                 }
00319         }
00320 #ifdef CONFIG_IEEE80211W
00321         if (hapd->conf->ieee80211w) {
00322                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
00323                         if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL,
00324                                             i, i == 0 ? 1 : 0, NULL, 0,
00325                                             NULL, 0)) {
00326                                 wpa_printf(MSG_DEBUG, "Failed to clear "
00327                                            "default mgmt encryption keys "
00328                                            "(ifname=%s keyidx=%d)", ifname, i);
00329                         }
00330                 }
00331         }
00332 #endif /* CONFIG_IEEE80211W */
00333 }
00334 
00335 
00336 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
00337 {
00338         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
00339         return 0;
00340 }
00341 
00342 
00343 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
00344 {
00345         int errors = 0, idx;
00346         struct hostapd_ssid *ssid = &hapd->conf->ssid;
00347 
00348         idx = ssid->wep.idx;
00349         if (ssid->wep.default_len &&
00350             hostapd_set_key(hapd->conf->iface,
00351                             hapd, WPA_ALG_WEP, NULL, idx, idx == ssid->wep.idx,
00352                             NULL, 0, ssid->wep.key[idx], ssid->wep.len[idx])) {
00353                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
00354                 errors++;
00355         }
00356 
00357         if (ssid->dyn_vlan_keys) {
00358                 size_t i;
00359                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
00360                         const char *ifname;
00361                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
00362                         if (key == NULL)
00363                                 continue;
00364                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
00365                                                             i);
00366                         if (ifname == NULL)
00367                                 continue;
00368 
00369                         idx = key->idx;
00370                         if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL,
00371                                             idx, idx == key->idx, NULL, 0,
00372                                             key->key[idx], key->len[idx])) {
00373                                 wpa_printf(MSG_WARNING, "Could not set "
00374                                            "dynamic VLAN WEP encryption.");
00375                                 errors++;
00376                         }
00377                 }
00378         }
00379 
00380         return errors;
00381 }
00382 
00394 static void hostapd_cleanup(struct hostapd_data *hapd)
00395 {
00396         hostapd_ctrl_iface_deinit(hapd);
00397 
00398         iapp_deinit(hapd->iapp);
00399         hapd->iapp = NULL;
00400         accounting_deinit(hapd);
00401         rsn_preauth_iface_deinit(hapd);
00402         if (hapd->wpa_auth) {
00403                 wpa_deinit(hapd->wpa_auth);
00404                 hapd->wpa_auth = NULL;
00405 
00406                 if (hostapd_set_privacy(hapd, 0)) {
00407                         wpa_printf(MSG_DEBUG, "Could not disable "
00408                                    "PrivacyInvoked for interface %s",
00409                                    hapd->conf->iface);
00410                 }
00411 
00412                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
00413                         wpa_printf(MSG_DEBUG, "Could not remove generic "
00414                                    "information element from interface %s",
00415                                    hapd->conf->iface);
00416                 }
00417         }
00418         ieee802_1x_deinit(hapd);
00419         vlan_deinit(hapd);
00420         hostapd_acl_deinit(hapd);
00421         radius_client_deinit(hapd->radius);
00422         hapd->radius = NULL;
00423         radius_server_deinit(hapd->radius_srv);
00424         hapd->radius_srv = NULL;
00425 
00426 #ifdef CONFIG_IEEE80211R
00427         l2_packet_deinit(hapd->l2);
00428 #endif /* CONFIG_IEEE80211R */
00429 
00430         hostapd_deinit_wps(hapd);
00431 
00432 #ifdef EAP_TLS_FUNCS
00433         if (hapd->ssl_ctx) {
00434                 tls_deinit(hapd->ssl_ctx);
00435                 hapd->ssl_ctx = NULL;
00436         }
00437 #endif /* EAP_TLS_FUNCS */
00438 
00439 #ifdef EAP_SERVER
00440         if (hapd->eap_sim_db_priv) {
00441                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
00442                 hapd->eap_sim_db_priv = NULL;
00443         }
00444 #endif /* EAP_SERVER */
00445 
00446         if (hapd->interface_added &&
00447             hostapd_bss_remove(hapd, hapd->conf->iface)) {
00448                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
00449                            hapd->conf->iface);
00450         }
00451 
00452         os_free(hapd->probereq_cb);
00453         hapd->probereq_cb = NULL;
00454 }
00455 
00456 
00465 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
00466 {
00467 }
00468 
00469 
00478 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
00479 {
00480         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
00481         iface->hw_features = NULL;
00482         os_free(iface->current_rates);
00483         iface->current_rates = NULL;
00484         ap_list_deinit(iface);
00485         hostapd_config_free(iface->conf);
00486         iface->conf = NULL;
00487 
00488         os_free(iface->config_fname);
00489         os_free(iface->bss);
00490         os_free(iface);
00491 }
00492 
00493 
00494 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
00495 {
00496         int i;
00497 
00498         hostapd_broadcast_wep_set(hapd);
00499 
00500         if (hapd->conf->ssid.wep.default_len)
00501                 return 0;
00502 
00503         for (i = 0; i < 4; i++) {
00504                 if (hapd->conf->ssid.wep.key[i] &&
00505                     hostapd_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
00506                                     i == hapd->conf->ssid.wep.idx, NULL, 0,
00507                                     hapd->conf->ssid.wep.key[i],
00508                                     hapd->conf->ssid.wep.len[i])) {
00509                         wpa_printf(MSG_WARNING, "Could not set WEP "
00510                                    "encryption.");
00511                         return -1;
00512                 }
00513                 if (hapd->conf->ssid.wep.key[i] &&
00514                     i == hapd->conf->ssid.wep.idx)
00515                         hostapd_set_privacy(hapd, 1);
00516         }
00517 
00518         return 0;
00519 }
00520 
00521 
00522 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
00523 {
00524         int ret = 0;
00525 
00526         if (hostapd_drv_none(hapd))
00527                 return 0;
00528 
00529         wpa_printf(MSG_DEBUG, "Flushing old station entries");
00530         if (hostapd_flush(hapd)) {
00531                 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
00532                 ret = -1;
00533         }
00534         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
00535 
00536         /* New Prism2.5/3 STA firmware versions seem to have issues with this
00537          * broadcast deauth frame. This gets the firmware in odd state where
00538          * nothing works correctly, so let's skip sending this for the hostap
00539          * driver. */
00540         if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
00541                 u8 addr[ETH_ALEN];
00542                 os_memset(addr, 0xff, ETH_ALEN);
00543                 hostapd_sta_deauth(hapd, addr,
00544                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
00545         }
00546 
00547         return ret;
00548 }
00549 
00550 
00551 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
00552                                     logger_level level, const char *txt)
00553 {
00554 #ifndef CONFIG_NO_HOSTAPD_LOGGER
00555         struct hostapd_data *hapd = ctx;
00556         int hlevel;
00557 
00558         switch (level) {
00559         case LOGGER_WARNING:
00560                 hlevel = HOSTAPD_LEVEL_WARNING;
00561                 break;
00562         case LOGGER_INFO:
00563                 hlevel = HOSTAPD_LEVEL_INFO;
00564                 break;
00565         case LOGGER_DEBUG:
00566         default:
00567                 hlevel = HOSTAPD_LEVEL_DEBUG;
00568                 break;
00569         }
00570 
00571         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
00572 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
00573 }
00574 
00575 
00576 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
00577                                         u16 reason)
00578 {
00579         struct hostapd_data *hapd = ctx;
00580         struct sta_info *sta;
00581 
00582         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
00583                    "STA " MACSTR " reason %d",
00584                    __func__, MAC2STR(addr), reason);
00585 
00586         sta = ap_get_sta(hapd, addr);
00587         hostapd_sta_deauth(hapd, addr, reason);
00588         if (sta == NULL)
00589                 return;
00590         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
00591         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
00592         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
00593         sta->timeout_next = STA_REMOVE;
00594 }
00595 
00596 
00597 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
00598 {
00599         struct hostapd_data *hapd = ctx;
00600         michael_mic_failure(hapd, addr, 0);
00601 }
00602 
00603 
00604 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
00605                                        wpa_eapol_variable var, int value)
00606 {
00607         struct hostapd_data *hapd = ctx;
00608         struct sta_info *sta = ap_get_sta(hapd, addr);
00609         if (sta == NULL)
00610                 return;
00611         switch (var) {
00612         case WPA_EAPOL_portEnabled:
00613                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
00614                 break;
00615         case WPA_EAPOL_portValid:
00616                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
00617                 break;
00618         case WPA_EAPOL_authorized:
00619                 ieee802_1x_set_sta_authorized(hapd, sta, value);
00620                 break;
00621         case WPA_EAPOL_portControl_Auto:
00622                 if (sta->eapol_sm)
00623                         sta->eapol_sm->portControl = Auto;
00624                 break;
00625         case WPA_EAPOL_keyRun:
00626                 if (sta->eapol_sm)
00627                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
00628                 break;
00629         case WPA_EAPOL_keyAvailable:
00630                 if (sta->eapol_sm)
00631                         sta->eapol_sm->eap_if->eapKeyAvailable =
00632                                 value ? TRUE : FALSE;
00633                 break;
00634         case WPA_EAPOL_keyDone:
00635                 if (sta->eapol_sm)
00636                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
00637                 break;
00638         case WPA_EAPOL_inc_EapolFramesTx:
00639                 if (sta->eapol_sm)
00640                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
00641                 break;
00642         }
00643 }
00644 
00645 
00646 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
00647                                       wpa_eapol_variable var)
00648 {
00649         struct hostapd_data *hapd = ctx;
00650         struct sta_info *sta = ap_get_sta(hapd, addr);
00651         if (sta == NULL || sta->eapol_sm == NULL)
00652                 return -1;
00653         switch (var) {
00654         case WPA_EAPOL_keyRun:
00655                 return sta->eapol_sm->keyRun;
00656         case WPA_EAPOL_keyAvailable:
00657                 return sta->eapol_sm->eap_if->eapKeyAvailable;
00658         default:
00659                 return -1;
00660         }
00661 }
00662 
00663 
00664 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
00665                                            const u8 *prev_psk)
00666 {
00667         struct hostapd_data *hapd = ctx;
00668         return hostapd_get_psk(hapd->conf, addr, prev_psk);
00669 }
00670 
00671 
00672 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
00673                                     size_t *len)
00674 {
00675         struct hostapd_data *hapd = ctx;
00676         const u8 *key;
00677         size_t keylen;
00678         struct sta_info *sta;
00679 
00680         sta = ap_get_sta(hapd, addr);
00681         if (sta == NULL)
00682                 return -1;
00683 
00684         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
00685         if (key == NULL)
00686                 return -1;
00687 
00688         if (keylen > *len)
00689                 keylen = *len;
00690         os_memcpy(msk, key, keylen);
00691         *len = keylen;
00692 
00693         return 0;
00694 }
00695 
00696 
00697 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
00698                                     const u8 *addr, int idx, u8 *key,
00699                                     size_t key_len)
00700 {
00701         struct hostapd_data *hapd = ctx;
00702         const char *ifname = hapd->conf->iface;
00703 
00704         if (vlan_id > 0) {
00705                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
00706                 if (ifname == NULL)
00707                         return -1;
00708         }
00709 
00710         return hostapd_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
00711                                key, key_len);
00712 }
00713 
00714 
00715 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
00716                                        u8 *seq)
00717 {
00718         struct hostapd_data *hapd = ctx;
00719         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
00720 }
00721 
00722 
00723 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
00724                                             u8 *seq)
00725 {
00726         struct hostapd_data *hapd = ctx;
00727         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
00728                                        seq);
00729 }
00730 
00731 
00732 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
00733                                        const u8 *data, size_t data_len,
00734                                        int encrypt)
00735 {
00736         struct hostapd_data *hapd = ctx;
00737         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
00738 }
00739 
00740 
00741 static int hostapd_wpa_auth_for_each_sta(
00742         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
00743         void *cb_ctx)
00744 {
00745         struct hostapd_data *hapd = ctx;
00746         struct sta_info *sta;
00747 
00748         for (sta = hapd->sta_list; sta; sta = sta->next) {
00749                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
00750                         return 1;
00751         }
00752         return 0;
00753 }
00754 
00755 
00756 struct wpa_auth_iface_iter_data {
00757         int (*cb)(struct wpa_authenticator *sm, void *ctx);
00758         void *cb_ctx;
00759 };
00760 
00761 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
00762 {
00763         struct wpa_auth_iface_iter_data *data = ctx;
00764         size_t i;
00765         for (i = 0; i < iface->num_bss; i++) {
00766                 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
00767                         return 1;
00768         }
00769         return 0;
00770 }
00771 
00772 
00773 static int hostapd_wpa_auth_for_each_auth(
00774         void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
00775         void *cb_ctx)
00776 {
00777         struct wpa_auth_iface_iter_data data;
00778         data.cb = cb;
00779         data.cb_ctx = cb_ctx;
00780         return hostapd_for_each_interface(wpa_auth_iface_iter, &data);
00781 }
00782 
00783 
00784 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
00785                                        const u8 *data, size_t data_len)
00786 {
00787         struct hostapd_data *hapd = ctx;
00788 
00789         if (hapd->driver && hapd->driver->send_ether)
00790                 return hapd->driver->send_ether(hapd->drv_priv, dst,
00791                                                 hapd->own_addr, proto,
00792                                                 data, data_len);
00793         if (hapd->l2 == NULL)
00794                 return -1;
00795         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
00796 }
00797 
00798 
00799 #ifdef CONFIG_IEEE80211R
00800 
00801 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
00802                                            const u8 *data, size_t data_len)
00803 {
00804         struct hostapd_data *hapd = ctx;
00805         int res;
00806         struct ieee80211_mgmt *m;
00807         size_t mlen;
00808         struct sta_info *sta;
00809 
00810         sta = ap_get_sta(hapd, dst);
00811         if (sta == NULL || sta->wpa_sm == NULL)
00812                 return -1;
00813 
00814         m = os_zalloc(sizeof(*m) + data_len);
00815         if (m == NULL)
00816                 return -1;
00817         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
00818         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00819                                         WLAN_FC_STYPE_ACTION);
00820         os_memcpy(m->da, dst, ETH_ALEN);
00821         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
00822         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
00823         os_memcpy(&m->u, data, data_len);
00824 
00825         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen);
00826         os_free(m);
00827         return res;
00828 }
00829 
00830 
00831 static struct wpa_state_machine *
00832 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
00833 {
00834         struct hostapd_data *hapd = ctx;
00835         struct sta_info *sta;
00836 
00837         sta = ap_sta_add(hapd, sta_addr);
00838         if (sta == NULL)
00839                 return NULL;
00840         if (sta->wpa_sm)
00841                 return sta->wpa_sm;
00842 
00843         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
00844         if (sta->wpa_sm == NULL) {
00845                 ap_free_sta(hapd, sta);
00846                 return NULL;
00847         }
00848         sta->auth_alg = WLAN_AUTH_FT;
00849 
00850         return sta->wpa_sm;
00851 }
00852 
00853 
00854 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
00855                                 size_t len)
00856 {
00857         struct hostapd_data *hapd = ctx;
00858         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
00859 }
00860 
00861 #endif /* CONFIG_IEEE80211R */
00862 
00863 
00872 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
00873 {
00874         u8 mask[ETH_ALEN] = { 0 };
00875         struct hostapd_data *hapd = iface->bss[0];
00876         unsigned int i = iface->conf->num_bss, bits = 0, j;
00877         int res;
00878         int auto_addr = 0;
00879 
00880         if (hostapd_drv_none(hapd))
00881                 return 0;
00882 
00883         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
00884 
00885         /* Determine the bits necessary to cover the number of BSSIDs. */
00886         for (i--; i; i >>= 1)
00887                 bits++;
00888 
00889         /* Determine the bits necessary to any configured BSSIDs,
00890            if they are higher than the number of BSSIDs. */
00891         for (j = 0; j < iface->conf->num_bss; j++) {
00892                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
00893                         if (j)
00894                                 auto_addr++;
00895                         continue;
00896                 }
00897 
00898                 for (i = 0; i < ETH_ALEN; i++) {
00899                         mask[i] |=
00900                                 iface->conf->bss[j].bssid[i] ^
00901                                 hapd->own_addr[i];
00902                 }
00903         }
00904 
00905         if (!auto_addr)
00906                 goto skip_mask_ext;
00907 
00908         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
00909                 ;
00910         j = 0;
00911         if (i < ETH_ALEN) {
00912                 j = (5 - i) * 8;
00913 
00914                 while (mask[i] != 0) {
00915                         mask[i] >>= 1;
00916                         j++;
00917                 }
00918         }
00919 
00920         if (bits < j)
00921                 bits = j;
00922 
00923         if (bits > 40) {
00924                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
00925                            bits);
00926                 return -1;
00927         }
00928 
00929         os_memset(mask, 0xff, ETH_ALEN);
00930         j = bits / 8;
00931         for (i = 5; i > 5 - j; i--)
00932                 mask[i] = 0;
00933         j = bits % 8;
00934         while (j--)
00935                 mask[i] <<= 1;
00936 
00937 skip_mask_ext:
00938         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
00939                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
00940 
00941         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
00942         if (res == 0)
00943                 return 0;
00944 
00945         if (res < 0) {
00946                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
00947                            MACSTR " for start address " MACSTR ".",
00948                            MAC2STR(mask), MAC2STR(hapd->own_addr));
00949                 return -1;
00950         }
00951 
00952         if (!auto_addr)
00953                 return 0;
00954 
00955         for (i = 0; i < ETH_ALEN; i++) {
00956                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
00957                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
00958                                    " for start address " MACSTR ".",
00959                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
00960                         wpa_printf(MSG_ERROR, "Start address must be the "
00961                                    "first address in the block (i.e., addr "
00962                                    "AND mask == addr).");
00963                         return -1;
00964                 }
00965         }
00966 
00967         return 0;
00968 }
00969 
00970 
00971 static int mac_in_conf(struct hostapd_config *conf, const void *a)
00972 {
00973         size_t i;
00974 
00975         for (i = 0; i < conf->num_bss; i++) {
00976                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
00977                         return 1;
00978                 }
00979         }
00980 
00981         return 0;
00982 }
00983 
00984 
00985 static int hostapd_setup_wpa(struct hostapd_data *hapd)
00986 {
00987         struct wpa_auth_config _conf;
00988         struct wpa_auth_callbacks cb;
00989         const u8 *wpa_ie;
00990         size_t wpa_ie_len;
00991 
00992         hostapd_wpa_auth_conf(hapd->conf, &_conf);
00993         os_memset(&cb, 0, sizeof(cb));
00994         cb.ctx = hapd;
00995         cb.logger = hostapd_wpa_auth_logger;
00996         cb.disconnect = hostapd_wpa_auth_disconnect;
00997         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
00998         cb.set_eapol = hostapd_wpa_auth_set_eapol;
00999         cb.get_eapol = hostapd_wpa_auth_get_eapol;
01000         cb.get_psk = hostapd_wpa_auth_get_psk;
01001         cb.get_msk = hostapd_wpa_auth_get_msk;
01002         cb.set_key = hostapd_wpa_auth_set_key;
01003         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
01004         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
01005         cb.send_eapol = hostapd_wpa_auth_send_eapol;
01006         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
01007         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
01008         cb.send_ether = hostapd_wpa_auth_send_ether;
01009 #ifdef CONFIG_IEEE80211R
01010         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
01011         cb.add_sta = hostapd_wpa_auth_add_sta;
01012 #endif /* CONFIG_IEEE80211R */
01013         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
01014         if (hapd->wpa_auth == NULL) {
01015                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
01016                 return -1;
01017         }
01018 
01019         if (hostapd_set_privacy(hapd, 1)) {
01020                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
01021                            "for interface %s", hapd->conf->iface);
01022                 return -1;
01023         }
01024 
01025         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
01026         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
01027                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
01028                            "the kernel driver.");
01029                 return -1;
01030         }
01031 
01032         if (rsn_preauth_iface_init(hapd)) {
01033                 wpa_printf(MSG_ERROR, "Initialization of RSN "
01034                            "pre-authentication failed.");
01035                 return -1;
01036         }
01037 
01038         return 0;
01039 
01040 }
01041 
01042 
01043 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
01044                                     struct hostapd_bss_config *conf)
01045 {
01046         struct radius_server_conf srv;
01047         os_memset(&srv, 0, sizeof(srv));
01048         srv.client_file = conf->radius_server_clients;
01049         srv.auth_port = conf->radius_server_auth_port;
01050         srv.conf_ctx = conf;
01051         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
01052         srv.ssl_ctx = hapd->ssl_ctx;
01053         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
01054         srv.eap_fast_a_id = conf->eap_fast_a_id;
01055         srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
01056         srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
01057         srv.eap_fast_prov = conf->eap_fast_prov;
01058         srv.pac_key_lifetime = conf->pac_key_lifetime;
01059         srv.pac_key_refresh_time = conf->pac_key_refresh_time;
01060         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
01061         srv.tnc = conf->tnc;
01062         srv.wps = hapd->wps;
01063         srv.ipv6 = conf->radius_server_ipv6;
01064         srv.get_eap_user = hostapd_radius_get_eap_user;
01065         srv.eap_req_id_text = conf->eap_req_id_text;
01066         srv.eap_req_id_text_len = conf->eap_req_id_text_len;
01067 
01068         hapd->radius_srv = radius_server_init(&srv);
01069         if (hapd->radius_srv == NULL) {
01070                 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
01071                 return -1;
01072         }
01073 
01074         return 0;
01075 }
01076 
01077 
01089 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
01090 {
01091         struct hostapd_bss_config *conf = hapd->conf;
01092         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
01093         int ssid_len, set_ssid;
01094 
01095         if (!first) {
01096                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
01097                         /* Allocate the next available BSSID. */
01098                         do {
01099                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
01100                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
01101                 } else {
01102                         /* Allocate the configured BSSID. */
01103                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
01104 
01105                         if (hostapd_mac_comp(hapd->own_addr,
01106                                              hapd->iface->bss[0]->own_addr) ==
01107                             0) {
01108                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
01109                                            "BSSID set to the MAC address of "
01110                                            "the radio", hapd->conf->iface);
01111                                 return -1;
01112                         }
01113                 }
01114 
01115                 hapd->interface_added = 1;
01116                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
01117                                     hapd->own_addr)) {
01118                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
01119                                    MACSTR ")", MAC2STR(hapd->own_addr));
01120                         return -1;
01121                 }
01122         }
01123 
01124         hostapd_flush_old_stations(hapd);
01125         hostapd_set_privacy(hapd, 0);
01126 
01127         hostapd_broadcast_wep_clear(hapd);
01128         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
01129                 return -1;
01130 
01131         /*
01132          * Fetch the SSID from the system and use it or,
01133          * if one was specified in the config file, verify they
01134          * match.
01135          */
01136         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
01137         if (ssid_len < 0) {
01138                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
01139                 return -1;
01140         }
01141         if (conf->ssid.ssid_set) {
01142                 /*
01143                  * If SSID is specified in the config file and it differs
01144                  * from what is being used then force installation of the
01145                  * new SSID.
01146                  */
01147                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
01148                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
01149         } else {
01150                 /*
01151                  * No SSID in the config file; just use the one we got
01152                  * from the system.
01153                  */
01154                 set_ssid = 0;
01155                 conf->ssid.ssid_len = ssid_len;
01156                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
01157                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
01158         }
01159 
01160         if (!hostapd_drv_none(hapd)) {
01161                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
01162                            " and ssid '%s'",
01163                            hapd->conf->iface, MAC2STR(hapd->own_addr),
01164                            hapd->conf->ssid.ssid);
01165         }
01166 
01167         if (hostapd_setup_wpa_psk(conf)) {
01168                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
01169                 return -1;
01170         }
01171 
01172         /* Set SSID for the kernel driver (to be used in beacon and probe
01173          * response frames) */
01174         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
01175                                          conf->ssid.ssid_len)) {
01176                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
01177                 return -1;
01178         }
01179 
01180         if (wpa_debug_level == MSG_MSGDUMP)
01181                 conf->radius->msg_dumps = 1;
01182         hapd->radius = radius_client_init(hapd, conf->radius);
01183         if (hapd->radius == NULL) {
01184                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
01185                 return -1;
01186         }
01187 
01188         if (hostapd_acl_init(hapd)) {
01189                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
01190                 return -1;
01191         }
01192         if (hostapd_init_wps(hapd, conf))
01193                 return -1;
01194 
01195         if (ieee802_1x_init(hapd)) {
01196                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
01197                 return -1;
01198         }
01199 
01200         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
01201                 return -1;
01202 
01203         if (accounting_init(hapd)) {
01204                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
01205                 return -1;
01206         }
01207 
01208         if (hapd->conf->ieee802_11f &&
01209             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
01210                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
01211                            "failed.");
01212                 return -1;
01213         }
01214 
01215         if (hostapd_ctrl_iface_init(hapd)) {
01216                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
01217                 return -1;
01218         }
01219 
01220         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
01221                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
01222                 return -1;
01223         }
01224 
01225 #ifdef CONFIG_IEEE80211R
01226         if (!hostapd_drv_none(hapd)) {
01227                 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
01228                                           hostapd_rrb_receive, hapd, 0);
01229                 if (hapd->l2 == NULL &&
01230                     (hapd->driver == NULL ||
01231                      hapd->driver->send_ether == NULL)) {
01232                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
01233                                    "interface");
01234                         return -1;
01235                 }
01236         }
01237 #endif /* CONFIG_IEEE80211R */
01238 
01239         ieee802_11_set_beacon(hapd);
01240 
01241         if (conf->radius_server_clients &&
01242             hostapd_setup_radius_srv(hapd, conf))
01243                 return -1;
01244 
01245         return 0;
01246 }
01247 
01248 
01249 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
01250 {
01251         struct hostapd_data *hapd = iface->bss[0];
01252         int i;
01253         struct hostapd_tx_queue_params *p;
01254 
01255         for (i = 0; i < NUM_TX_QUEUES; i++) {
01256                 p = &iface->conf->tx_queue[i];
01257 
01258                 if (!p->configured)
01259                         continue;
01260 
01261                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
01262                                                 p->cwmax, p->burst)) {
01263                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
01264                                    "parameters for queue %d.", i);
01265                         /* Continue anyway */
01266                 }
01267         }
01268 }
01269 
01270 
01271 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
01272                                        size_t identity_len, int phase2,
01273                                        struct eap_user *user)
01274 {
01275         const struct hostapd_eap_user *eap_user;
01276         int i, count;
01277 
01278         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
01279         if (eap_user == NULL)
01280                 return -1;
01281 
01282         if (user == NULL)
01283                 return 0;
01284 
01285         os_memset(user, 0, sizeof(*user));
01286         count = EAP_USER_MAX_METHODS;
01287         if (count > EAP_MAX_METHODS)
01288                 count = EAP_MAX_METHODS;
01289         for (i = 0; i < count; i++) {
01290                 user->methods[i].vendor = eap_user->methods[i].vendor;
01291                 user->methods[i].method = eap_user->methods[i].method;
01292         }
01293 
01294         if (eap_user->password) {
01295                 user->password = os_malloc(eap_user->password_len);
01296                 if (user->password == NULL)
01297                         return -1;
01298                 os_memcpy(user->password, eap_user->password,
01299                           eap_user->password_len);
01300                 user->password_len = eap_user->password_len;
01301                 user->password_hash = eap_user->password_hash;
01302         }
01303         user->force_version = eap_user->force_version;
01304         user->ttls_auth = eap_user->ttls_auth;
01305 
01306         return 0;
01307 }
01308 
01309 
01310 static int setup_interface(struct hostapd_iface *iface)
01311 {
01312         struct hostapd_data *hapd = iface->bss[0];
01313         struct hostapd_bss_config *conf = hapd->conf;
01314         size_t i;
01315         char country[4];
01316         u8 *b = conf->bssid;
01317 
01318         /*
01319          * Initialize the driver interface and make sure that all BSSes get
01320          * configured with a pointer to this driver interface.
01321          */
01322         if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
01323                 b = NULL;
01324         hapd->drv_priv = hostapd_driver_init(hapd, b);
01325 
01326         if (hapd->drv_priv == NULL) {
01327                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
01328                            hapd->driver ? hapd->driver->name : "Unknown");
01329                 hapd->driver = NULL;
01330                 return -1;
01331         }
01332         for (i = 0; i < iface->num_bss; i++) {
01333                 iface->bss[i]->driver = hapd->driver;
01334                 iface->bss[i]->drv_priv = hapd->drv_priv;
01335         }
01336 
01337         if (hostapd_driver_set_mode(hapd, IEEE80211_MODE_AP)) {
01338                 wpa_printf(MSG_ERROR, "Failed to set driver in AP mode");
01339                 return -1;
01340         }
01341 
01342         if (hostapd_validate_bssid_configuration(iface))
01343                 return -1;
01344 
01345 #ifdef CONFIG_IEEE80211N
01346         SET_2BIT_LE16(&iface->ht_op_mode,
01347                       HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
01348                       OP_MODE_PURE);
01349 #endif /* CONFIG_IEEE80211N */
01350 
01351         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
01352                 os_memcpy(country, hapd->iconf->country, 3);
01353                 country[3] = '\0';
01354                 if (hostapd_set_country(hapd, country) < 0) {
01355                         wpa_printf(MSG_ERROR, "Failed to set country code");
01356                         return -1;
01357                 }
01358         }
01359 
01360         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
01361             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
01362                 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
01363                            "kernel driver");
01364                 return -1;
01365         }
01366 
01367         if (hostapd_get_hw_features(iface)) {
01368                 /* Not all drivers support this yet, so continue without hw
01369                  * feature data. */
01370         } else {
01371                 int ret = hostapd_select_hw_mode(iface);
01372                 if (ret < 0) {
01373                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
01374                                    "channel. (%d)", ret);
01375                         return -1;
01376                 }
01377                 ret = hostapd_check_ht_capab(iface);
01378                 if (ret < 0)
01379                         return -1;
01380                 if (ret == 1) {
01381                         wpa_printf(MSG_DEBUG, "Interface initialization will "
01382                                    "be completed in a callback");
01383                         return 0;
01384                 }
01385         }
01386         return hostapd_setup_interface_complete(iface, 0);
01387 }
01388 
01389 
01390 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
01391 {
01392         struct hostapd_data *hapd = iface->bss[0];
01393         int freq;
01394         size_t j;
01395         u8 *prev_addr;
01396 
01397         if (err) {
01398                 wpa_printf(MSG_ERROR, "Interface initialization failed");
01399                 eloop_terminate();
01400                 return -1;
01401         }
01402 
01403         wpa_printf(MSG_DEBUG, "Completing interface initialization");
01404         if (hapd->iconf->channel) {
01405                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
01406                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
01407                            "Frequency: %d MHz",
01408                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
01409                            hapd->iconf->channel, freq);
01410 
01411                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
01412                                      hapd->iconf->channel,
01413                                      hapd->iconf->ieee80211n,
01414                                      hapd->iconf->secondary_channel)) {
01415                         wpa_printf(MSG_ERROR, "Could not set channel for "
01416                                    "kernel driver");
01417                         return -1;
01418                 }
01419         }
01420 
01421         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
01422 
01423         if (hapd->iconf->rts_threshold > -1 &&
01424             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
01425                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
01426                            "kernel driver");
01427                 return -1;
01428         }
01429 
01430         if (hapd->iconf->fragm_threshold > -1 &&
01431             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
01432                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
01433                            "for kernel driver");
01434                 return -1;
01435         }
01436 
01437         prev_addr = hapd->own_addr;
01438 
01439         for (j = 0; j < iface->num_bss; j++) {
01440                 hapd = iface->bss[j];
01441                 if (j)
01442                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
01443                 if (hostapd_setup_bss(hapd, j == 0))
01444                         return -1;
01445                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
01446                         prev_addr = hapd->own_addr;
01447         }
01448 
01449         hostapd_tx_queue_params(iface);
01450 
01451         ap_list_init(iface);
01452 
01453         if (hostapd_driver_commit(hapd) < 0) {
01454                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
01455                            "configuration", __func__);
01456                 return -1;
01457         }
01458 
01459         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
01460                    iface->bss[0]->conf->iface);
01461 
01462         return 0;
01463 }
01464 
01465 
01477 int hostapd_setup_interface(struct hostapd_iface *iface)
01478 {
01479         int ret;
01480 
01481         ret = setup_interface(iface);
01482         if (ret) {
01483                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
01484                            iface->bss[0]->conf->iface);
01485                 eloop_terminate();
01486                 return -1;
01487         }
01488 
01489         return 0;
01490 }
01491 
01492 
01505 struct hostapd_data *
01506 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
01507                        struct hostapd_config *conf,
01508                        struct hostapd_bss_config *bss)
01509 {
01510         struct hostapd_data *hapd;
01511 
01512         hapd = os_zalloc(sizeof(*hapd));
01513         if (hapd == NULL)
01514                 return NULL;
01515 
01516         hapd->iconf = conf;
01517         hapd->conf = bss;
01518         hapd->iface = hapd_iface;
01519 
01520 #ifdef EAP_TLS_FUNCS
01521         if (hapd->conf->eap_server &&
01522             (hapd->conf->ca_cert || hapd->conf->server_cert ||
01523              hapd->conf->dh_file)) {
01524                 struct tls_connection_params params;
01525 
01526                 hapd->ssl_ctx = tls_init(NULL);
01527                 if (hapd->ssl_ctx == NULL) {
01528                         wpa_printf(MSG_ERROR, "Failed to initialize TLS");
01529                         goto fail;
01530                 }
01531 
01532                 os_memset(&params, 0, sizeof(params));
01533                 params.ca_cert = hapd->conf->ca_cert;
01534                 params.client_cert = hapd->conf->server_cert;
01535                 params.private_key = hapd->conf->private_key;
01536                 params.private_key_passwd = hapd->conf->private_key_passwd;
01537                 params.dh_file = hapd->conf->dh_file;
01538 
01539                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
01540                         wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
01541                         goto fail;
01542                 }
01543 
01544                 if (tls_global_set_verify(hapd->ssl_ctx,
01545                                           hapd->conf->check_crl)) {
01546                         wpa_printf(MSG_ERROR, "Failed to enable check_crl");
01547                         goto fail;
01548                 }
01549         }
01550 #endif /* EAP_TLS_FUNCS */
01551 
01552 #ifdef EAP_SERVER
01553         if (hapd->conf->eap_sim_db) {
01554                 hapd->eap_sim_db_priv =
01555                         eap_sim_db_init(hapd->conf->eap_sim_db,
01556                                         hostapd_sim_db_cb, hapd);
01557                 if (hapd->eap_sim_db_priv == NULL) {
01558                         wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
01559                                    "database interface");
01560                         goto fail;
01561                 }
01562         }
01563 #endif /* EAP_SERVER */
01564 
01565         hapd->driver = hapd->iconf->driver;
01566 
01567         return hapd;
01568 
01569 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
01570 fail:
01571 #endif
01572         /* TODO: cleanup allocated resources(?) */
01573         os_free(hapd);
01574         return NULL;
01575 }
01576 
01577 
01578 void hostapd_interface_deinit(struct hostapd_iface *iface)
01579 {
01580         size_t j;
01581 
01582         if (iface == NULL)
01583                 return;
01584 
01585         hostapd_cleanup_iface_pre(iface);
01586         for (j = 0; j < iface->num_bss; j++) {
01587                 struct hostapd_data *hapd = iface->bss[j];
01588                 hostapd_free_stas(hapd);
01589                 hostapd_flush_old_stations(hapd);
01590                 hostapd_cleanup(hapd);
01591                 if (j == iface->num_bss - 1 && hapd->driver)
01592                         hostapd_driver_deinit(hapd);
01593         }
01594         for (j = 0; j < iface->num_bss; j++)
01595                 os_free(iface->bss[j]);
01596         hostapd_cleanup_iface(iface);
01597 }
01598 
01599 
01600 int hostapd_register_probereq_cb(struct hostapd_data *hapd,
01601                                  void (*cb)(void *ctx, const u8 *sa,
01602                                             const u8 *ie, size_t ie_len),
01603                                  void *ctx)
01604 {
01605         struct hostapd_probereq_cb *n;
01606 
01607         n = os_realloc(hapd->probereq_cb, (hapd->num_probereq_cb + 1) *
01608                        sizeof(struct hostapd_probereq_cb));
01609         if (n == NULL)
01610                 return -1;
01611 
01612         hapd->probereq_cb = n;
01613         n = &hapd->probereq_cb[hapd->num_probereq_cb];
01614         hapd->num_probereq_cb++;
01615 
01616         n->cb = cb;
01617         n->ctx = ctx;
01618 
01619         return 0;
01620 }
01621 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:47 2009 for hostapd by  doxygen 1.6.1