wpa_auth_glue.c revision d5e4923d04122f81300fa68fb07d64ede28fd44d
1/* 2 * hostapd / WPA authenticator glue code 3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "utils/includes.h" 10 11#include "utils/common.h" 12#include "common/ieee802_11_defs.h" 13#include "eapol_auth/eapol_auth_sm.h" 14#include "eapol_auth/eapol_auth_sm_i.h" 15#include "eap_server/eap.h" 16#include "l2_packet/l2_packet.h" 17#include "drivers/driver.h" 18#include "hostapd.h" 19#include "ieee802_1x.h" 20#include "preauth_auth.h" 21#include "sta_info.h" 22#include "tkip_countermeasures.h" 23#include "ap_drv_ops.h" 24#include "ap_config.h" 25#include "wpa_auth.h" 26#include "wpa_auth_glue.h" 27 28 29static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf, 30 struct wpa_auth_config *wconf) 31{ 32 os_memset(wconf, 0, sizeof(*wconf)); 33 wconf->wpa = conf->wpa; 34 wconf->wpa_key_mgmt = conf->wpa_key_mgmt; 35 wconf->wpa_pairwise = conf->wpa_pairwise; 36 wconf->wpa_group = conf->wpa_group; 37 wconf->wpa_group_rekey = conf->wpa_group_rekey; 38 wconf->wpa_strict_rekey = conf->wpa_strict_rekey; 39 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey; 40 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey; 41 wconf->rsn_pairwise = conf->rsn_pairwise; 42 wconf->rsn_preauth = conf->rsn_preauth; 43 wconf->eapol_version = conf->eapol_version; 44 wconf->peerkey = conf->peerkey; 45 wconf->wmm_enabled = conf->wmm_enabled; 46 wconf->wmm_uapsd = conf->wmm_uapsd; 47 wconf->disable_pmksa_caching = conf->disable_pmksa_caching; 48 wconf->okc = conf->okc; 49#ifdef CONFIG_IEEE80211W 50 wconf->ieee80211w = conf->ieee80211w; 51#endif /* CONFIG_IEEE80211W */ 52#ifdef CONFIG_IEEE80211R 53 wconf->ssid_len = conf->ssid.ssid_len; 54 if (wconf->ssid_len > SSID_LEN) 55 wconf->ssid_len = SSID_LEN; 56 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len); 57 os_memcpy(wconf->mobility_domain, conf->mobility_domain, 58 MOBILITY_DOMAIN_ID_LEN); 59 if (conf->nas_identifier && 60 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) { 61 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier); 62 os_memcpy(wconf->r0_key_holder, conf->nas_identifier, 63 wconf->r0_key_holder_len); 64 } 65 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN); 66 wconf->r0_key_lifetime = conf->r0_key_lifetime; 67 wconf->reassociation_deadline = conf->reassociation_deadline; 68 wconf->r0kh_list = conf->r0kh_list; 69 wconf->r1kh_list = conf->r1kh_list; 70 wconf->pmk_r1_push = conf->pmk_r1_push; 71 wconf->ft_over_ds = conf->ft_over_ds; 72#endif /* CONFIG_IEEE80211R */ 73#ifdef CONFIG_HS20 74 wconf->disable_gtk = conf->disable_dgaf; 75#endif /* CONFIG_HS20 */ 76} 77 78 79static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr, 80 logger_level level, const char *txt) 81{ 82#ifndef CONFIG_NO_HOSTAPD_LOGGER 83 struct hostapd_data *hapd = ctx; 84 int hlevel; 85 86 switch (level) { 87 case LOGGER_WARNING: 88 hlevel = HOSTAPD_LEVEL_WARNING; 89 break; 90 case LOGGER_INFO: 91 hlevel = HOSTAPD_LEVEL_INFO; 92 break; 93 case LOGGER_DEBUG: 94 default: 95 hlevel = HOSTAPD_LEVEL_DEBUG; 96 break; 97 } 98 99 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt); 100#endif /* CONFIG_NO_HOSTAPD_LOGGER */ 101} 102 103 104static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr, 105 u16 reason) 106{ 107 struct hostapd_data *hapd = ctx; 108 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: " 109 "STA " MACSTR " reason %d", 110 __func__, MAC2STR(addr), reason); 111 ap_sta_disconnect(hapd, NULL, addr, reason); 112} 113 114 115static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr) 116{ 117 struct hostapd_data *hapd = ctx; 118 return michael_mic_failure(hapd, addr, 0); 119} 120 121 122static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr, 123 wpa_eapol_variable var, int value) 124{ 125 struct hostapd_data *hapd = ctx; 126 struct sta_info *sta = ap_get_sta(hapd, addr); 127 if (sta == NULL) 128 return; 129 switch (var) { 130 case WPA_EAPOL_portEnabled: 131 ieee802_1x_notify_port_enabled(sta->eapol_sm, value); 132 break; 133 case WPA_EAPOL_portValid: 134 ieee802_1x_notify_port_valid(sta->eapol_sm, value); 135 break; 136 case WPA_EAPOL_authorized: 137 ieee802_1x_set_sta_authorized(hapd, sta, value); 138 break; 139 case WPA_EAPOL_portControl_Auto: 140 if (sta->eapol_sm) 141 sta->eapol_sm->portControl = Auto; 142 break; 143 case WPA_EAPOL_keyRun: 144 if (sta->eapol_sm) 145 sta->eapol_sm->keyRun = value ? TRUE : FALSE; 146 break; 147 case WPA_EAPOL_keyAvailable: 148 if (sta->eapol_sm) 149 sta->eapol_sm->eap_if->eapKeyAvailable = 150 value ? TRUE : FALSE; 151 break; 152 case WPA_EAPOL_keyDone: 153 if (sta->eapol_sm) 154 sta->eapol_sm->keyDone = value ? TRUE : FALSE; 155 break; 156 case WPA_EAPOL_inc_EapolFramesTx: 157 if (sta->eapol_sm) 158 sta->eapol_sm->dot1xAuthEapolFramesTx++; 159 break; 160 } 161} 162 163 164static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr, 165 wpa_eapol_variable var) 166{ 167 struct hostapd_data *hapd = ctx; 168 struct sta_info *sta = ap_get_sta(hapd, addr); 169 if (sta == NULL || sta->eapol_sm == NULL) 170 return -1; 171 switch (var) { 172 case WPA_EAPOL_keyRun: 173 return sta->eapol_sm->keyRun; 174 case WPA_EAPOL_keyAvailable: 175 return sta->eapol_sm->eap_if->eapKeyAvailable; 176 default: 177 return -1; 178 } 179} 180 181 182static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr, 183 const u8 *prev_psk) 184{ 185 struct hostapd_data *hapd = ctx; 186 struct sta_info *sta = ap_get_sta(hapd, addr); 187 const u8 *psk = hostapd_get_psk(hapd->conf, addr, prev_psk); 188 /* 189 * This is about to iterate over all psks, prev_psk gives the last 190 * returned psk which should not be returned again. 191 * logic list (all hostapd_get_psk; all sta->psk) 192 */ 193 if (sta && sta->psk && !psk) { 194 struct hostapd_sta_wpa_psk_short *pos; 195 psk = sta->psk->psk; 196 for (pos = sta->psk; pos; pos = pos->next) { 197 if (pos->psk == prev_psk) { 198 psk = pos->next ? pos->next->psk : NULL; 199 break; 200 } 201 } 202 } 203 return psk; 204} 205 206 207static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk, 208 size_t *len) 209{ 210 struct hostapd_data *hapd = ctx; 211 const u8 *key; 212 size_t keylen; 213 struct sta_info *sta; 214 215 sta = ap_get_sta(hapd, addr); 216 if (sta == NULL) 217 return -1; 218 219 key = ieee802_1x_get_key(sta->eapol_sm, &keylen); 220 if (key == NULL) 221 return -1; 222 223 if (keylen > *len) 224 keylen = *len; 225 os_memcpy(msk, key, keylen); 226 *len = keylen; 227 228 return 0; 229} 230 231 232static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg, 233 const u8 *addr, int idx, u8 *key, 234 size_t key_len) 235{ 236 struct hostapd_data *hapd = ctx; 237 const char *ifname = hapd->conf->iface; 238 239 if (vlan_id > 0) { 240 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id); 241 if (ifname == NULL) 242 return -1; 243 } 244 245 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0, 246 key, key_len); 247} 248 249 250static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx, 251 u8 *seq) 252{ 253 struct hostapd_data *hapd = ctx; 254 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq); 255} 256 257 258static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr, 259 const u8 *data, size_t data_len, 260 int encrypt) 261{ 262 struct hostapd_data *hapd = ctx; 263 struct sta_info *sta; 264 u32 flags = 0; 265 266 sta = ap_get_sta(hapd, addr); 267 if (sta) 268 flags = hostapd_sta_flags_to_drv(sta->flags); 269 270 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len, 271 encrypt, flags); 272} 273 274 275static int hostapd_wpa_auth_for_each_sta( 276 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx), 277 void *cb_ctx) 278{ 279 struct hostapd_data *hapd = ctx; 280 struct sta_info *sta; 281 282 for (sta = hapd->sta_list; sta; sta = sta->next) { 283 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx)) 284 return 1; 285 } 286 return 0; 287} 288 289 290struct wpa_auth_iface_iter_data { 291 int (*cb)(struct wpa_authenticator *sm, void *ctx); 292 void *cb_ctx; 293}; 294 295static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx) 296{ 297 struct wpa_auth_iface_iter_data *data = ctx; 298 size_t i; 299 for (i = 0; i < iface->num_bss; i++) { 300 if (iface->bss[i]->wpa_auth && 301 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx)) 302 return 1; 303 } 304 return 0; 305} 306 307 308static int hostapd_wpa_auth_for_each_auth( 309 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx), 310 void *cb_ctx) 311{ 312 struct hostapd_data *hapd = ctx; 313 struct wpa_auth_iface_iter_data data; 314 if (hapd->iface->interfaces == NULL || 315 hapd->iface->interfaces->for_each_interface == NULL) 316 return -1; 317 data.cb = cb; 318 data.cb_ctx = cb_ctx; 319 return hapd->iface->interfaces->for_each_interface( 320 hapd->iface->interfaces, wpa_auth_iface_iter, &data); 321} 322 323 324#ifdef CONFIG_IEEE80211R 325 326struct wpa_auth_ft_iface_iter_data { 327 struct hostapd_data *src_hapd; 328 const u8 *dst; 329 const u8 *data; 330 size_t data_len; 331}; 332 333 334static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx) 335{ 336 struct wpa_auth_ft_iface_iter_data *idata = ctx; 337 struct hostapd_data *hapd; 338 size_t j; 339 340 for (j = 0; j < iface->num_bss; j++) { 341 hapd = iface->bss[j]; 342 if (hapd == idata->src_hapd) 343 continue; 344 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) { 345 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to " 346 "locally managed BSS " MACSTR "@%s -> " 347 MACSTR "@%s", 348 MAC2STR(idata->src_hapd->own_addr), 349 idata->src_hapd->conf->iface, 350 MAC2STR(hapd->own_addr), hapd->conf->iface); 351 wpa_ft_rrb_rx(hapd->wpa_auth, 352 idata->src_hapd->own_addr, 353 idata->data, idata->data_len); 354 return 1; 355 } 356 } 357 358 return 0; 359} 360 361#endif /* CONFIG_IEEE80211R */ 362 363 364static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto, 365 const u8 *data, size_t data_len) 366{ 367 struct hostapd_data *hapd = ctx; 368 struct l2_ethhdr *buf; 369 int ret; 370 371#ifdef CONFIG_IEEE80211R 372 if (proto == ETH_P_RRB && hapd->iface->interfaces && 373 hapd->iface->interfaces->for_each_interface) { 374 int res; 375 struct wpa_auth_ft_iface_iter_data idata; 376 idata.src_hapd = hapd; 377 idata.dst = dst; 378 idata.data = data; 379 idata.data_len = data_len; 380 res = hapd->iface->interfaces->for_each_interface( 381 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter, 382 &idata); 383 if (res == 1) 384 return data_len; 385 } 386#endif /* CONFIG_IEEE80211R */ 387 388 if (hapd->driver && hapd->driver->send_ether) 389 return hapd->driver->send_ether(hapd->drv_priv, dst, 390 hapd->own_addr, proto, 391 data, data_len); 392 if (hapd->l2 == NULL) 393 return -1; 394 395 buf = os_malloc(sizeof(*buf) + data_len); 396 if (buf == NULL) 397 return -1; 398 os_memcpy(buf->h_dest, dst, ETH_ALEN); 399 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN); 400 buf->h_proto = host_to_be16(proto); 401 os_memcpy(buf + 1, data, data_len); 402 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf, 403 sizeof(*buf) + data_len); 404 os_free(buf); 405 return ret; 406} 407 408 409#ifdef CONFIG_IEEE80211R 410 411static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst, 412 const u8 *data, size_t data_len) 413{ 414 struct hostapd_data *hapd = ctx; 415 int res; 416 struct ieee80211_mgmt *m; 417 size_t mlen; 418 struct sta_info *sta; 419 420 sta = ap_get_sta(hapd, dst); 421 if (sta == NULL || sta->wpa_sm == NULL) 422 return -1; 423 424 m = os_zalloc(sizeof(*m) + data_len); 425 if (m == NULL) 426 return -1; 427 mlen = ((u8 *) &m->u - (u8 *) m) + data_len; 428 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 429 WLAN_FC_STYPE_ACTION); 430 os_memcpy(m->da, dst, ETH_ALEN); 431 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN); 432 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN); 433 os_memcpy(&m->u, data, data_len); 434 435 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0); 436 os_free(m); 437 return res; 438} 439 440 441static struct wpa_state_machine * 442hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr) 443{ 444 struct hostapd_data *hapd = ctx; 445 struct sta_info *sta; 446 447 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0) 448 return NULL; 449 450 sta = ap_sta_add(hapd, sta_addr); 451 if (sta == NULL) 452 return NULL; 453 if (sta->wpa_sm) { 454 sta->auth_alg = WLAN_AUTH_FT; 455 return sta->wpa_sm; 456 } 457 458 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr); 459 if (sta->wpa_sm == NULL) { 460 ap_free_sta(hapd, sta); 461 return NULL; 462 } 463 sta->auth_alg = WLAN_AUTH_FT; 464 465 return sta->wpa_sm; 466} 467 468 469static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf, 470 size_t len) 471{ 472 struct hostapd_data *hapd = ctx; 473 struct l2_ethhdr *ethhdr; 474 if (len < sizeof(*ethhdr)) 475 return; 476 ethhdr = (struct l2_ethhdr *) buf; 477 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> " 478 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest)); 479 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr), 480 len - sizeof(*ethhdr)); 481} 482 483 484static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr, 485 u8 *tspec_ie, size_t tspec_ielen) 486{ 487 struct hostapd_data *hapd = ctx; 488 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen); 489} 490 491#endif /* CONFIG_IEEE80211R */ 492 493 494int hostapd_setup_wpa(struct hostapd_data *hapd) 495{ 496 struct wpa_auth_config _conf; 497 struct wpa_auth_callbacks cb; 498 const u8 *wpa_ie; 499 size_t wpa_ie_len; 500 501 hostapd_wpa_auth_conf(hapd->conf, &_conf); 502 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS) 503 _conf.tx_status = 1; 504 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME) 505 _conf.ap_mlme = 1; 506 os_memset(&cb, 0, sizeof(cb)); 507 cb.ctx = hapd; 508 cb.logger = hostapd_wpa_auth_logger; 509 cb.disconnect = hostapd_wpa_auth_disconnect; 510 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report; 511 cb.set_eapol = hostapd_wpa_auth_set_eapol; 512 cb.get_eapol = hostapd_wpa_auth_get_eapol; 513 cb.get_psk = hostapd_wpa_auth_get_psk; 514 cb.get_msk = hostapd_wpa_auth_get_msk; 515 cb.set_key = hostapd_wpa_auth_set_key; 516 cb.get_seqnum = hostapd_wpa_auth_get_seqnum; 517 cb.send_eapol = hostapd_wpa_auth_send_eapol; 518 cb.for_each_sta = hostapd_wpa_auth_for_each_sta; 519 cb.for_each_auth = hostapd_wpa_auth_for_each_auth; 520 cb.send_ether = hostapd_wpa_auth_send_ether; 521#ifdef CONFIG_IEEE80211R 522 cb.send_ft_action = hostapd_wpa_auth_send_ft_action; 523 cb.add_sta = hostapd_wpa_auth_add_sta; 524 cb.add_tspec = hostapd_wpa_auth_add_tspec; 525#endif /* CONFIG_IEEE80211R */ 526 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb); 527 if (hapd->wpa_auth == NULL) { 528 wpa_printf(MSG_ERROR, "WPA initialization failed."); 529 return -1; 530 } 531 532 if (hostapd_set_privacy(hapd, 1)) { 533 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked " 534 "for interface %s", hapd->conf->iface); 535 return -1; 536 } 537 538 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len); 539 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) { 540 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for " 541 "the kernel driver."); 542 return -1; 543 } 544 545 if (rsn_preauth_iface_init(hapd)) { 546 wpa_printf(MSG_ERROR, "Initialization of RSN " 547 "pre-authentication failed."); 548 return -1; 549 } 550 551#ifdef CONFIG_IEEE80211R 552 if (!hostapd_drv_none(hapd)) { 553 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ? 554 hapd->conf->bridge : 555 hapd->conf->iface, NULL, ETH_P_RRB, 556 hostapd_rrb_receive, hapd, 1); 557 if (hapd->l2 == NULL && 558 (hapd->driver == NULL || 559 hapd->driver->send_ether == NULL)) { 560 wpa_printf(MSG_ERROR, "Failed to open l2_packet " 561 "interface"); 562 return -1; 563 } 564 } 565#endif /* CONFIG_IEEE80211R */ 566 567 return 0; 568 569} 570 571 572void hostapd_reconfig_wpa(struct hostapd_data *hapd) 573{ 574 struct wpa_auth_config wpa_auth_conf; 575 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf); 576 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf); 577} 578 579 580void hostapd_deinit_wpa(struct hostapd_data *hapd) 581{ 582 ieee80211_tkip_countermeasures_deinit(hapd); 583 rsn_preauth_iface_deinit(hapd); 584 if (hapd->wpa_auth) { 585 wpa_deinit(hapd->wpa_auth); 586 hapd->wpa_auth = NULL; 587 588 if (hostapd_set_privacy(hapd, 0)) { 589 wpa_printf(MSG_DEBUG, "Could not disable " 590 "PrivacyInvoked for interface %s", 591 hapd->conf->iface); 592 } 593 594 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) { 595 wpa_printf(MSG_DEBUG, "Could not remove generic " 596 "information element from interface %s", 597 hapd->conf->iface); 598 } 599 } 600 ieee802_1x_deinit(hapd); 601 602#ifdef CONFIG_IEEE80211R 603 l2_packet_deinit(hapd->l2); 604#endif /* CONFIG_IEEE80211R */ 605} 606