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