1214501Srpaulo/*
2214501Srpaulo * hostapd / WPA authenticator glue code
3252726Srpaulo * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
4214501Srpaulo *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7214501Srpaulo */
8214501Srpaulo
9214501Srpaulo#include "utils/includes.h"
10214501Srpaulo
11214501Srpaulo#include "utils/common.h"
12214501Srpaulo#include "common/ieee802_11_defs.h"
13214501Srpaulo#include "eapol_auth/eapol_auth_sm.h"
14214501Srpaulo#include "eapol_auth/eapol_auth_sm_i.h"
15214501Srpaulo#include "eap_server/eap.h"
16214501Srpaulo#include "l2_packet/l2_packet.h"
17214501Srpaulo#include "drivers/driver.h"
18214501Srpaulo#include "hostapd.h"
19214501Srpaulo#include "ieee802_1x.h"
20214501Srpaulo#include "preauth_auth.h"
21214501Srpaulo#include "sta_info.h"
22214501Srpaulo#include "tkip_countermeasures.h"
23214501Srpaulo#include "ap_drv_ops.h"
24214501Srpaulo#include "ap_config.h"
25214501Srpaulo#include "wpa_auth.h"
26252726Srpaulo#include "wpa_auth_glue.h"
27214501Srpaulo
28214501Srpaulo
29214501Srpaulostatic void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
30214501Srpaulo				  struct wpa_auth_config *wconf)
31214501Srpaulo{
32252726Srpaulo	os_memset(wconf, 0, sizeof(*wconf));
33214501Srpaulo	wconf->wpa = conf->wpa;
34214501Srpaulo	wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
35214501Srpaulo	wconf->wpa_pairwise = conf->wpa_pairwise;
36214501Srpaulo	wconf->wpa_group = conf->wpa_group;
37214501Srpaulo	wconf->wpa_group_rekey = conf->wpa_group_rekey;
38214501Srpaulo	wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
39214501Srpaulo	wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
40214501Srpaulo	wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
41214501Srpaulo	wconf->rsn_pairwise = conf->rsn_pairwise;
42214501Srpaulo	wconf->rsn_preauth = conf->rsn_preauth;
43214501Srpaulo	wconf->eapol_version = conf->eapol_version;
44214501Srpaulo	wconf->peerkey = conf->peerkey;
45214501Srpaulo	wconf->wmm_enabled = conf->wmm_enabled;
46214501Srpaulo	wconf->wmm_uapsd = conf->wmm_uapsd;
47252726Srpaulo	wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
48214501Srpaulo	wconf->okc = conf->okc;
49214501Srpaulo#ifdef CONFIG_IEEE80211W
50214501Srpaulo	wconf->ieee80211w = conf->ieee80211w;
51214501Srpaulo#endif /* CONFIG_IEEE80211W */
52214501Srpaulo#ifdef CONFIG_IEEE80211R
53214501Srpaulo	wconf->ssid_len = conf->ssid.ssid_len;
54214501Srpaulo	if (wconf->ssid_len > SSID_LEN)
55214501Srpaulo		wconf->ssid_len = SSID_LEN;
56214501Srpaulo	os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
57214501Srpaulo	os_memcpy(wconf->mobility_domain, conf->mobility_domain,
58214501Srpaulo		  MOBILITY_DOMAIN_ID_LEN);
59214501Srpaulo	if (conf->nas_identifier &&
60214501Srpaulo	    os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
61214501Srpaulo		wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
62214501Srpaulo		os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
63214501Srpaulo			  wconf->r0_key_holder_len);
64214501Srpaulo	}
65214501Srpaulo	os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
66214501Srpaulo	wconf->r0_key_lifetime = conf->r0_key_lifetime;
67214501Srpaulo	wconf->reassociation_deadline = conf->reassociation_deadline;
68214501Srpaulo	wconf->r0kh_list = conf->r0kh_list;
69214501Srpaulo	wconf->r1kh_list = conf->r1kh_list;
70214501Srpaulo	wconf->pmk_r1_push = conf->pmk_r1_push;
71252726Srpaulo	wconf->ft_over_ds = conf->ft_over_ds;
72214501Srpaulo#endif /* CONFIG_IEEE80211R */
73252726Srpaulo#ifdef CONFIG_HS20
74252726Srpaulo	wconf->disable_gtk = conf->disable_dgaf;
75252726Srpaulo#endif /* CONFIG_HS20 */
76214501Srpaulo}
77214501Srpaulo
78214501Srpaulo
79214501Srpaulostatic void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
80214501Srpaulo				    logger_level level, const char *txt)
81214501Srpaulo{
82214501Srpaulo#ifndef CONFIG_NO_HOSTAPD_LOGGER
83214501Srpaulo	struct hostapd_data *hapd = ctx;
84214501Srpaulo	int hlevel;
85214501Srpaulo
86214501Srpaulo	switch (level) {
87214501Srpaulo	case LOGGER_WARNING:
88214501Srpaulo		hlevel = HOSTAPD_LEVEL_WARNING;
89214501Srpaulo		break;
90214501Srpaulo	case LOGGER_INFO:
91214501Srpaulo		hlevel = HOSTAPD_LEVEL_INFO;
92214501Srpaulo		break;
93214501Srpaulo	case LOGGER_DEBUG:
94214501Srpaulo	default:
95214501Srpaulo		hlevel = HOSTAPD_LEVEL_DEBUG;
96214501Srpaulo		break;
97214501Srpaulo	}
98214501Srpaulo
99214501Srpaulo	hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
100214501Srpaulo#endif /* CONFIG_NO_HOSTAPD_LOGGER */
101214501Srpaulo}
102214501Srpaulo
103214501Srpaulo
104214501Srpaulostatic void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
105214501Srpaulo					u16 reason)
106214501Srpaulo{
107214501Srpaulo	struct hostapd_data *hapd = ctx;
108214501Srpaulo	wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
109214501Srpaulo		   "STA " MACSTR " reason %d",
110214501Srpaulo		   __func__, MAC2STR(addr), reason);
111214501Srpaulo	ap_sta_disconnect(hapd, NULL, addr, reason);
112214501Srpaulo}
113214501Srpaulo
114214501Srpaulo
115252726Srpaulostatic int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
116214501Srpaulo{
117214501Srpaulo	struct hostapd_data *hapd = ctx;
118252726Srpaulo	return michael_mic_failure(hapd, addr, 0);
119214501Srpaulo}
120214501Srpaulo
121214501Srpaulo
122214501Srpaulostatic void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
123214501Srpaulo				       wpa_eapol_variable var, int value)
124214501Srpaulo{
125214501Srpaulo	struct hostapd_data *hapd = ctx;
126214501Srpaulo	struct sta_info *sta = ap_get_sta(hapd, addr);
127214501Srpaulo	if (sta == NULL)
128214501Srpaulo		return;
129214501Srpaulo	switch (var) {
130214501Srpaulo	case WPA_EAPOL_portEnabled:
131214501Srpaulo		ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
132214501Srpaulo		break;
133214501Srpaulo	case WPA_EAPOL_portValid:
134214501Srpaulo		ieee802_1x_notify_port_valid(sta->eapol_sm, value);
135214501Srpaulo		break;
136214501Srpaulo	case WPA_EAPOL_authorized:
137214501Srpaulo		ieee802_1x_set_sta_authorized(hapd, sta, value);
138214501Srpaulo		break;
139214501Srpaulo	case WPA_EAPOL_portControl_Auto:
140214501Srpaulo		if (sta->eapol_sm)
141214501Srpaulo			sta->eapol_sm->portControl = Auto;
142214501Srpaulo		break;
143214501Srpaulo	case WPA_EAPOL_keyRun:
144214501Srpaulo		if (sta->eapol_sm)
145214501Srpaulo			sta->eapol_sm->keyRun = value ? TRUE : FALSE;
146214501Srpaulo		break;
147214501Srpaulo	case WPA_EAPOL_keyAvailable:
148214501Srpaulo		if (sta->eapol_sm)
149214501Srpaulo			sta->eapol_sm->eap_if->eapKeyAvailable =
150214501Srpaulo				value ? TRUE : FALSE;
151214501Srpaulo		break;
152214501Srpaulo	case WPA_EAPOL_keyDone:
153214501Srpaulo		if (sta->eapol_sm)
154214501Srpaulo			sta->eapol_sm->keyDone = value ? TRUE : FALSE;
155214501Srpaulo		break;
156214501Srpaulo	case WPA_EAPOL_inc_EapolFramesTx:
157214501Srpaulo		if (sta->eapol_sm)
158214501Srpaulo			sta->eapol_sm->dot1xAuthEapolFramesTx++;
159214501Srpaulo		break;
160214501Srpaulo	}
161214501Srpaulo}
162214501Srpaulo
163214501Srpaulo
164214501Srpaulostatic int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
165214501Srpaulo				      wpa_eapol_variable var)
166214501Srpaulo{
167214501Srpaulo	struct hostapd_data *hapd = ctx;
168214501Srpaulo	struct sta_info *sta = ap_get_sta(hapd, addr);
169214501Srpaulo	if (sta == NULL || sta->eapol_sm == NULL)
170214501Srpaulo		return -1;
171214501Srpaulo	switch (var) {
172214501Srpaulo	case WPA_EAPOL_keyRun:
173214501Srpaulo		return sta->eapol_sm->keyRun;
174214501Srpaulo	case WPA_EAPOL_keyAvailable:
175214501Srpaulo		return sta->eapol_sm->eap_if->eapKeyAvailable;
176214501Srpaulo	default:
177214501Srpaulo		return -1;
178214501Srpaulo	}
179214501Srpaulo}
180214501Srpaulo
181214501Srpaulo
182214501Srpaulostatic const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
183214501Srpaulo					   const u8 *prev_psk)
184214501Srpaulo{
185214501Srpaulo	struct hostapd_data *hapd = ctx;
186252726Srpaulo	struct sta_info *sta = ap_get_sta(hapd, addr);
187252726Srpaulo	const u8 *psk = hostapd_get_psk(hapd->conf, addr, prev_psk);
188252726Srpaulo	/*
189252726Srpaulo	 * This is about to iterate over all psks, prev_psk gives the last
190252726Srpaulo	 * returned psk which should not be returned again.
191252726Srpaulo	 * logic list (all hostapd_get_psk; all sta->psk)
192252726Srpaulo	 */
193252726Srpaulo	if (sta && sta->psk && !psk) {
194252726Srpaulo		struct hostapd_sta_wpa_psk_short *pos;
195252726Srpaulo		psk = sta->psk->psk;
196252726Srpaulo		for (pos = sta->psk; pos; pos = pos->next) {
197252726Srpaulo			if (pos->psk == prev_psk) {
198252726Srpaulo				psk = pos->next ? pos->next->psk : NULL;
199252726Srpaulo				break;
200252726Srpaulo			}
201252726Srpaulo		}
202252726Srpaulo	}
203252726Srpaulo	return psk;
204214501Srpaulo}
205214501Srpaulo
206214501Srpaulo
207214501Srpaulostatic int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
208214501Srpaulo				    size_t *len)
209214501Srpaulo{
210214501Srpaulo	struct hostapd_data *hapd = ctx;
211214501Srpaulo	const u8 *key;
212214501Srpaulo	size_t keylen;
213214501Srpaulo	struct sta_info *sta;
214214501Srpaulo
215214501Srpaulo	sta = ap_get_sta(hapd, addr);
216214501Srpaulo	if (sta == NULL)
217214501Srpaulo		return -1;
218214501Srpaulo
219214501Srpaulo	key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
220214501Srpaulo	if (key == NULL)
221214501Srpaulo		return -1;
222214501Srpaulo
223214501Srpaulo	if (keylen > *len)
224214501Srpaulo		keylen = *len;
225214501Srpaulo	os_memcpy(msk, key, keylen);
226214501Srpaulo	*len = keylen;
227214501Srpaulo
228214501Srpaulo	return 0;
229214501Srpaulo}
230214501Srpaulo
231214501Srpaulo
232214501Srpaulostatic int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
233214501Srpaulo				    const u8 *addr, int idx, u8 *key,
234214501Srpaulo				    size_t key_len)
235214501Srpaulo{
236214501Srpaulo	struct hostapd_data *hapd = ctx;
237214501Srpaulo	const char *ifname = hapd->conf->iface;
238214501Srpaulo
239214501Srpaulo	if (vlan_id > 0) {
240214501Srpaulo		ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
241214501Srpaulo		if (ifname == NULL)
242214501Srpaulo			return -1;
243214501Srpaulo	}
244214501Srpaulo
245252726Srpaulo	return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
246252726Srpaulo				   key, key_len);
247214501Srpaulo}
248214501Srpaulo
249214501Srpaulo
250214501Srpaulostatic int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
251214501Srpaulo				       u8 *seq)
252214501Srpaulo{
253214501Srpaulo	struct hostapd_data *hapd = ctx;
254214501Srpaulo	return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
255214501Srpaulo}
256214501Srpaulo
257214501Srpaulo
258214501Srpaulostatic int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
259214501Srpaulo				       const u8 *data, size_t data_len,
260214501Srpaulo				       int encrypt)
261214501Srpaulo{
262214501Srpaulo	struct hostapd_data *hapd = ctx;
263252726Srpaulo	struct sta_info *sta;
264252726Srpaulo	u32 flags = 0;
265252726Srpaulo
266252726Srpaulo	sta = ap_get_sta(hapd, addr);
267252726Srpaulo	if (sta)
268252726Srpaulo		flags = hostapd_sta_flags_to_drv(sta->flags);
269252726Srpaulo
270252726Srpaulo	return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
271252726Srpaulo					   encrypt, flags);
272214501Srpaulo}
273214501Srpaulo
274214501Srpaulo
275214501Srpaulostatic int hostapd_wpa_auth_for_each_sta(
276214501Srpaulo	void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
277214501Srpaulo	void *cb_ctx)
278214501Srpaulo{
279214501Srpaulo	struct hostapd_data *hapd = ctx;
280214501Srpaulo	struct sta_info *sta;
281214501Srpaulo
282214501Srpaulo	for (sta = hapd->sta_list; sta; sta = sta->next) {
283214501Srpaulo		if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
284214501Srpaulo			return 1;
285214501Srpaulo	}
286214501Srpaulo	return 0;
287214501Srpaulo}
288214501Srpaulo
289214501Srpaulo
290214501Srpaulostruct wpa_auth_iface_iter_data {
291214501Srpaulo	int (*cb)(struct wpa_authenticator *sm, void *ctx);
292214501Srpaulo	void *cb_ctx;
293214501Srpaulo};
294214501Srpaulo
295214501Srpaulostatic int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
296214501Srpaulo{
297214501Srpaulo	struct wpa_auth_iface_iter_data *data = ctx;
298214501Srpaulo	size_t i;
299214501Srpaulo	for (i = 0; i < iface->num_bss; i++) {
300214501Srpaulo		if (iface->bss[i]->wpa_auth &&
301214501Srpaulo		    data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
302214501Srpaulo			return 1;
303214501Srpaulo	}
304214501Srpaulo	return 0;
305214501Srpaulo}
306214501Srpaulo
307214501Srpaulo
308214501Srpaulostatic int hostapd_wpa_auth_for_each_auth(
309214501Srpaulo	void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
310214501Srpaulo	void *cb_ctx)
311214501Srpaulo{
312214501Srpaulo	struct hostapd_data *hapd = ctx;
313214501Srpaulo	struct wpa_auth_iface_iter_data data;
314252726Srpaulo	if (hapd->iface->interfaces == NULL ||
315252726Srpaulo	    hapd->iface->interfaces->for_each_interface == NULL)
316214501Srpaulo		return -1;
317214501Srpaulo	data.cb = cb;
318214501Srpaulo	data.cb_ctx = cb_ctx;
319252726Srpaulo	return hapd->iface->interfaces->for_each_interface(
320252726Srpaulo		hapd->iface->interfaces, wpa_auth_iface_iter, &data);
321214501Srpaulo}
322214501Srpaulo
323214501Srpaulo
324214501Srpaulo#ifdef CONFIG_IEEE80211R
325214501Srpaulo
326214501Srpaulostruct wpa_auth_ft_iface_iter_data {
327214501Srpaulo	struct hostapd_data *src_hapd;
328214501Srpaulo	const u8 *dst;
329214501Srpaulo	const u8 *data;
330214501Srpaulo	size_t data_len;
331214501Srpaulo};
332214501Srpaulo
333214501Srpaulo
334214501Srpaulostatic int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
335214501Srpaulo{
336214501Srpaulo	struct wpa_auth_ft_iface_iter_data *idata = ctx;
337214501Srpaulo	struct hostapd_data *hapd;
338214501Srpaulo	size_t j;
339214501Srpaulo
340214501Srpaulo	for (j = 0; j < iface->num_bss; j++) {
341214501Srpaulo		hapd = iface->bss[j];
342214501Srpaulo		if (hapd == idata->src_hapd)
343214501Srpaulo			continue;
344214501Srpaulo		if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
345214501Srpaulo			wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
346214501Srpaulo				   "locally managed BSS " MACSTR "@%s -> "
347214501Srpaulo				   MACSTR "@%s",
348214501Srpaulo				   MAC2STR(idata->src_hapd->own_addr),
349214501Srpaulo				   idata->src_hapd->conf->iface,
350214501Srpaulo				   MAC2STR(hapd->own_addr), hapd->conf->iface);
351252726Srpaulo			wpa_ft_rrb_rx(hapd->wpa_auth,
352252726Srpaulo				      idata->src_hapd->own_addr,
353252726Srpaulo				      idata->data, idata->data_len);
354214501Srpaulo			return 1;
355214501Srpaulo		}
356214501Srpaulo	}
357214501Srpaulo
358214501Srpaulo	return 0;
359214501Srpaulo}
360214501Srpaulo
361214501Srpaulo#endif /* CONFIG_IEEE80211R */
362214501Srpaulo
363214501Srpaulo
364214501Srpaulostatic int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
365214501Srpaulo				       const u8 *data, size_t data_len)
366214501Srpaulo{
367214501Srpaulo	struct hostapd_data *hapd = ctx;
368252726Srpaulo	struct l2_ethhdr *buf;
369252726Srpaulo	int ret;
370214501Srpaulo
371214501Srpaulo#ifdef CONFIG_IEEE80211R
372252726Srpaulo	if (proto == ETH_P_RRB && hapd->iface->interfaces &&
373252726Srpaulo	    hapd->iface->interfaces->for_each_interface) {
374214501Srpaulo		int res;
375214501Srpaulo		struct wpa_auth_ft_iface_iter_data idata;
376214501Srpaulo		idata.src_hapd = hapd;
377214501Srpaulo		idata.dst = dst;
378214501Srpaulo		idata.data = data;
379214501Srpaulo		idata.data_len = data_len;
380252726Srpaulo		res = hapd->iface->interfaces->for_each_interface(
381252726Srpaulo			hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
382252726Srpaulo			&idata);
383214501Srpaulo		if (res == 1)
384214501Srpaulo			return data_len;
385214501Srpaulo	}
386214501Srpaulo#endif /* CONFIG_IEEE80211R */
387214501Srpaulo
388214501Srpaulo	if (hapd->driver && hapd->driver->send_ether)
389214501Srpaulo		return hapd->driver->send_ether(hapd->drv_priv, dst,
390214501Srpaulo						hapd->own_addr, proto,
391214501Srpaulo						data, data_len);
392214501Srpaulo	if (hapd->l2 == NULL)
393214501Srpaulo		return -1;
394252726Srpaulo
395252726Srpaulo	buf = os_malloc(sizeof(*buf) + data_len);
396252726Srpaulo	if (buf == NULL)
397252726Srpaulo		return -1;
398252726Srpaulo	os_memcpy(buf->h_dest, dst, ETH_ALEN);
399252726Srpaulo	os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
400252726Srpaulo	buf->h_proto = host_to_be16(proto);
401252726Srpaulo	os_memcpy(buf + 1, data, data_len);
402252726Srpaulo	ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
403252726Srpaulo			     sizeof(*buf) + data_len);
404252726Srpaulo	os_free(buf);
405252726Srpaulo	return ret;
406214501Srpaulo}
407214501Srpaulo
408214501Srpaulo
409214501Srpaulo#ifdef CONFIG_IEEE80211R
410214501Srpaulo
411214501Srpaulostatic int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
412214501Srpaulo					   const u8 *data, size_t data_len)
413214501Srpaulo{
414214501Srpaulo	struct hostapd_data *hapd = ctx;
415214501Srpaulo	int res;
416214501Srpaulo	struct ieee80211_mgmt *m;
417214501Srpaulo	size_t mlen;
418214501Srpaulo	struct sta_info *sta;
419214501Srpaulo
420214501Srpaulo	sta = ap_get_sta(hapd, dst);
421214501Srpaulo	if (sta == NULL || sta->wpa_sm == NULL)
422214501Srpaulo		return -1;
423214501Srpaulo
424214501Srpaulo	m = os_zalloc(sizeof(*m) + data_len);
425214501Srpaulo	if (m == NULL)
426214501Srpaulo		return -1;
427214501Srpaulo	mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
428214501Srpaulo	m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
429214501Srpaulo					WLAN_FC_STYPE_ACTION);
430214501Srpaulo	os_memcpy(m->da, dst, ETH_ALEN);
431214501Srpaulo	os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
432214501Srpaulo	os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
433214501Srpaulo	os_memcpy(&m->u, data, data_len);
434214501Srpaulo
435252726Srpaulo	res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
436214501Srpaulo	os_free(m);
437214501Srpaulo	return res;
438214501Srpaulo}
439214501Srpaulo
440214501Srpaulo
441214501Srpaulostatic struct wpa_state_machine *
442214501Srpaulohostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
443214501Srpaulo{
444214501Srpaulo	struct hostapd_data *hapd = ctx;
445214501Srpaulo	struct sta_info *sta;
446214501Srpaulo
447252726Srpaulo	if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
448252726Srpaulo		return NULL;
449252726Srpaulo
450214501Srpaulo	sta = ap_sta_add(hapd, sta_addr);
451214501Srpaulo	if (sta == NULL)
452214501Srpaulo		return NULL;
453214501Srpaulo	if (sta->wpa_sm) {
454214501Srpaulo		sta->auth_alg = WLAN_AUTH_FT;
455214501Srpaulo		return sta->wpa_sm;
456214501Srpaulo	}
457214501Srpaulo
458214501Srpaulo	sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
459214501Srpaulo	if (sta->wpa_sm == NULL) {
460214501Srpaulo		ap_free_sta(hapd, sta);
461214501Srpaulo		return NULL;
462214501Srpaulo	}
463214501Srpaulo	sta->auth_alg = WLAN_AUTH_FT;
464214501Srpaulo
465214501Srpaulo	return sta->wpa_sm;
466214501Srpaulo}
467214501Srpaulo
468214501Srpaulo
469214501Srpaulostatic void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
470214501Srpaulo				size_t len)
471214501Srpaulo{
472214501Srpaulo	struct hostapd_data *hapd = ctx;
473252726Srpaulo	struct l2_ethhdr *ethhdr;
474252726Srpaulo	if (len < sizeof(*ethhdr))
475252726Srpaulo		return;
476252726Srpaulo	ethhdr = (struct l2_ethhdr *) buf;
477252726Srpaulo	wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
478252726Srpaulo		   MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
479252726Srpaulo	wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
480252726Srpaulo		      len - sizeof(*ethhdr));
481214501Srpaulo}
482214501Srpaulo
483252726Srpaulo
484252726Srpaulostatic int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
485252726Srpaulo				      u8 *tspec_ie, size_t tspec_ielen)
486252726Srpaulo{
487252726Srpaulo	struct hostapd_data *hapd = ctx;
488252726Srpaulo	return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
489252726Srpaulo}
490252726Srpaulo
491214501Srpaulo#endif /* CONFIG_IEEE80211R */
492214501Srpaulo
493214501Srpaulo
494214501Srpauloint hostapd_setup_wpa(struct hostapd_data *hapd)
495214501Srpaulo{
496214501Srpaulo	struct wpa_auth_config _conf;
497214501Srpaulo	struct wpa_auth_callbacks cb;
498214501Srpaulo	const u8 *wpa_ie;
499214501Srpaulo	size_t wpa_ie_len;
500214501Srpaulo
501214501Srpaulo	hostapd_wpa_auth_conf(hapd->conf, &_conf);
502252726Srpaulo	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
503252726Srpaulo		_conf.tx_status = 1;
504252726Srpaulo	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
505252726Srpaulo		_conf.ap_mlme = 1;
506214501Srpaulo	os_memset(&cb, 0, sizeof(cb));
507214501Srpaulo	cb.ctx = hapd;
508214501Srpaulo	cb.logger = hostapd_wpa_auth_logger;
509214501Srpaulo	cb.disconnect = hostapd_wpa_auth_disconnect;
510214501Srpaulo	cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
511214501Srpaulo	cb.set_eapol = hostapd_wpa_auth_set_eapol;
512214501Srpaulo	cb.get_eapol = hostapd_wpa_auth_get_eapol;
513214501Srpaulo	cb.get_psk = hostapd_wpa_auth_get_psk;
514214501Srpaulo	cb.get_msk = hostapd_wpa_auth_get_msk;
515214501Srpaulo	cb.set_key = hostapd_wpa_auth_set_key;
516214501Srpaulo	cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
517214501Srpaulo	cb.send_eapol = hostapd_wpa_auth_send_eapol;
518214501Srpaulo	cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
519214501Srpaulo	cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
520214501Srpaulo	cb.send_ether = hostapd_wpa_auth_send_ether;
521214501Srpaulo#ifdef CONFIG_IEEE80211R
522214501Srpaulo	cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
523214501Srpaulo	cb.add_sta = hostapd_wpa_auth_add_sta;
524252726Srpaulo	cb.add_tspec = hostapd_wpa_auth_add_tspec;
525214501Srpaulo#endif /* CONFIG_IEEE80211R */
526214501Srpaulo	hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
527214501Srpaulo	if (hapd->wpa_auth == NULL) {
528214501Srpaulo		wpa_printf(MSG_ERROR, "WPA initialization failed.");
529214501Srpaulo		return -1;
530214501Srpaulo	}
531214501Srpaulo
532214501Srpaulo	if (hostapd_set_privacy(hapd, 1)) {
533214501Srpaulo		wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
534214501Srpaulo			   "for interface %s", hapd->conf->iface);
535214501Srpaulo		return -1;
536214501Srpaulo	}
537214501Srpaulo
538214501Srpaulo	wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
539214501Srpaulo	if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
540214501Srpaulo		wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
541214501Srpaulo			   "the kernel driver.");
542214501Srpaulo		return -1;
543214501Srpaulo	}
544214501Srpaulo
545214501Srpaulo	if (rsn_preauth_iface_init(hapd)) {
546214501Srpaulo		wpa_printf(MSG_ERROR, "Initialization of RSN "
547214501Srpaulo			   "pre-authentication failed.");
548214501Srpaulo		return -1;
549214501Srpaulo	}
550214501Srpaulo
551214501Srpaulo#ifdef CONFIG_IEEE80211R
552214501Srpaulo	if (!hostapd_drv_none(hapd)) {
553214501Srpaulo		hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
554214501Srpaulo					  hapd->conf->bridge :
555214501Srpaulo					  hapd->conf->iface, NULL, ETH_P_RRB,
556252726Srpaulo					  hostapd_rrb_receive, hapd, 1);
557214501Srpaulo		if (hapd->l2 == NULL &&
558214501Srpaulo		    (hapd->driver == NULL ||
559214501Srpaulo		     hapd->driver->send_ether == NULL)) {
560214501Srpaulo			wpa_printf(MSG_ERROR, "Failed to open l2_packet "
561214501Srpaulo				   "interface");
562214501Srpaulo			return -1;
563214501Srpaulo		}
564214501Srpaulo	}
565214501Srpaulo#endif /* CONFIG_IEEE80211R */
566214501Srpaulo
567214501Srpaulo	return 0;
568214501Srpaulo
569214501Srpaulo}
570214501Srpaulo
571214501Srpaulo
572214501Srpaulovoid hostapd_reconfig_wpa(struct hostapd_data *hapd)
573214501Srpaulo{
574214501Srpaulo	struct wpa_auth_config wpa_auth_conf;
575214501Srpaulo	hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
576214501Srpaulo	wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
577214501Srpaulo}
578214501Srpaulo
579214501Srpaulo
580214501Srpaulovoid hostapd_deinit_wpa(struct hostapd_data *hapd)
581214501Srpaulo{
582252726Srpaulo	ieee80211_tkip_countermeasures_deinit(hapd);
583214501Srpaulo	rsn_preauth_iface_deinit(hapd);
584214501Srpaulo	if (hapd->wpa_auth) {
585214501Srpaulo		wpa_deinit(hapd->wpa_auth);
586214501Srpaulo		hapd->wpa_auth = NULL;
587214501Srpaulo
588214501Srpaulo		if (hostapd_set_privacy(hapd, 0)) {
589214501Srpaulo			wpa_printf(MSG_DEBUG, "Could not disable "
590214501Srpaulo				   "PrivacyInvoked for interface %s",
591214501Srpaulo				   hapd->conf->iface);
592214501Srpaulo		}
593214501Srpaulo
594214501Srpaulo		if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
595214501Srpaulo			wpa_printf(MSG_DEBUG, "Could not remove generic "
596214501Srpaulo				   "information element from interface %s",
597214501Srpaulo				   hapd->conf->iface);
598214501Srpaulo		}
599214501Srpaulo	}
600214501Srpaulo	ieee802_1x_deinit(hapd);
601214501Srpaulo
602214501Srpaulo#ifdef CONFIG_IEEE80211R
603214501Srpaulo	l2_packet_deinit(hapd->l2);
604214501Srpaulo#endif /* CONFIG_IEEE80211R */
605214501Srpaulo}
606