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