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 "utils/eloop.h"
13#include "utils/list.h"
14#include "common/ieee802_11_defs.h"
15#include "common/sae.h"
16#include "common/wpa_ctrl.h"
17#include "crypto/sha1.h"
18#include "eapol_auth/eapol_auth_sm.h"
19#include "eapol_auth/eapol_auth_sm_i.h"
20#include "eap_server/eap.h"
21#include "l2_packet/l2_packet.h"
22#include "eth_p_oui.h"
23#include "hostapd.h"
24#include "ieee802_1x.h"
25#include "preauth_auth.h"
26#include "sta_info.h"
27#include "tkip_countermeasures.h"
28#include "ap_drv_ops.h"
29#include "ap_config.h"
30#include "pmksa_cache_auth.h"
31#include "wpa_auth.h"
32#include "wpa_auth_glue.h"
33
34
35static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
36				  struct hostapd_config *iconf,
37				  struct wpa_auth_config *wconf)
38{
39	os_memset(wconf, 0, sizeof(*wconf));
40	wconf->wpa = conf->wpa;
41	wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
42	wconf->wpa_pairwise = conf->wpa_pairwise;
43	wconf->wpa_group = conf->wpa_group;
44	wconf->wpa_group_rekey = conf->wpa_group_rekey;
45	wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
46	wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
47	wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
48	wconf->wpa_group_update_count = conf->wpa_group_update_count;
49	wconf->wpa_disable_eapol_key_retries =
50		conf->wpa_disable_eapol_key_retries;
51	wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
52	wconf->rsn_pairwise = conf->rsn_pairwise;
53	wconf->rsn_preauth = conf->rsn_preauth;
54	wconf->eapol_version = conf->eapol_version;
55	wconf->wmm_enabled = conf->wmm_enabled;
56	wconf->wmm_uapsd = conf->wmm_uapsd;
57	wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
58	wconf->okc = conf->okc;
59#ifdef CONFIG_IEEE80211W
60	wconf->ieee80211w = conf->ieee80211w;
61	wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
62#endif /* CONFIG_IEEE80211W */
63#ifdef CONFIG_IEEE80211R_AP
64	wconf->ssid_len = conf->ssid.ssid_len;
65	if (wconf->ssid_len > SSID_MAX_LEN)
66		wconf->ssid_len = SSID_MAX_LEN;
67	os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
68	os_memcpy(wconf->mobility_domain, conf->mobility_domain,
69		  MOBILITY_DOMAIN_ID_LEN);
70	if (conf->nas_identifier &&
71	    os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
72		wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
73		os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
74			  wconf->r0_key_holder_len);
75	}
76	os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
77	wconf->r0_key_lifetime = conf->r0_key_lifetime;
78	wconf->reassociation_deadline = conf->reassociation_deadline;
79	wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
80	wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
81	wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
82	wconf->rkh_pull_retries = conf->rkh_pull_retries;
83	wconf->r0kh_list = &conf->r0kh_list;
84	wconf->r1kh_list = &conf->r1kh_list;
85	wconf->pmk_r1_push = conf->pmk_r1_push;
86	wconf->ft_over_ds = conf->ft_over_ds;
87	wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
88#endif /* CONFIG_IEEE80211R_AP */
89#ifdef CONFIG_HS20
90	wconf->disable_gtk = conf->disable_dgaf;
91	if (conf->osen) {
92		wconf->disable_gtk = 1;
93		wconf->wpa = WPA_PROTO_OSEN;
94		wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
95		wconf->wpa_pairwise = 0;
96		wconf->wpa_group = WPA_CIPHER_CCMP;
97		wconf->rsn_pairwise = WPA_CIPHER_CCMP;
98		wconf->rsn_preauth = 0;
99		wconf->disable_pmksa_caching = 1;
100#ifdef CONFIG_IEEE80211W
101		wconf->ieee80211w = 1;
102#endif /* CONFIG_IEEE80211W */
103	}
104#endif /* CONFIG_HS20 */
105#ifdef CONFIG_TESTING_OPTIONS
106	wconf->corrupt_gtk_rekey_mic_probability =
107		iconf->corrupt_gtk_rekey_mic_probability;
108	if (conf->own_ie_override &&
109	    wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
110		wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
111		os_memcpy(wconf->own_ie_override,
112			  wpabuf_head(conf->own_ie_override),
113			  wconf->own_ie_override_len);
114	}
115#endif /* CONFIG_TESTING_OPTIONS */
116#ifdef CONFIG_P2P
117	os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
118	os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
119	os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
120	os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
121#endif /* CONFIG_P2P */
122#ifdef CONFIG_FILS
123	wconf->fils_cache_id_set = conf->fils_cache_id_set;
124	os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
125		  FILS_CACHE_ID_LEN);
126#endif /* CONFIG_FILS */
127}
128
129
130static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
131				    logger_level level, const char *txt)
132{
133#ifndef CONFIG_NO_HOSTAPD_LOGGER
134	struct hostapd_data *hapd = ctx;
135	int hlevel;
136
137	switch (level) {
138	case LOGGER_WARNING:
139		hlevel = HOSTAPD_LEVEL_WARNING;
140		break;
141	case LOGGER_INFO:
142		hlevel = HOSTAPD_LEVEL_INFO;
143		break;
144	case LOGGER_DEBUG:
145	default:
146		hlevel = HOSTAPD_LEVEL_DEBUG;
147		break;
148	}
149
150	hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
151#endif /* CONFIG_NO_HOSTAPD_LOGGER */
152}
153
154
155static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
156					u16 reason)
157{
158	struct hostapd_data *hapd = ctx;
159	wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
160		   "STA " MACSTR " reason %d",
161		   __func__, MAC2STR(addr), reason);
162	ap_sta_disconnect(hapd, NULL, addr, reason);
163}
164
165
166static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
167{
168	struct hostapd_data *hapd = ctx;
169	return michael_mic_failure(hapd, addr, 0);
170}
171
172
173static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
174{
175	struct hostapd_data *hapd = ctx;
176	wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
177		MAC2STR(addr));
178}
179
180
181static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
182				       wpa_eapol_variable var, int value)
183{
184	struct hostapd_data *hapd = ctx;
185	struct sta_info *sta = ap_get_sta(hapd, addr);
186	if (sta == NULL)
187		return;
188	switch (var) {
189	case WPA_EAPOL_portEnabled:
190		ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
191		break;
192	case WPA_EAPOL_portValid:
193		ieee802_1x_notify_port_valid(sta->eapol_sm, value);
194		break;
195	case WPA_EAPOL_authorized:
196		ieee802_1x_set_sta_authorized(hapd, sta, value);
197		break;
198	case WPA_EAPOL_portControl_Auto:
199		if (sta->eapol_sm)
200			sta->eapol_sm->portControl = Auto;
201		break;
202	case WPA_EAPOL_keyRun:
203		if (sta->eapol_sm)
204			sta->eapol_sm->keyRun = value ? TRUE : FALSE;
205		break;
206	case WPA_EAPOL_keyAvailable:
207		if (sta->eapol_sm)
208			sta->eapol_sm->eap_if->eapKeyAvailable =
209				value ? TRUE : FALSE;
210		break;
211	case WPA_EAPOL_keyDone:
212		if (sta->eapol_sm)
213			sta->eapol_sm->keyDone = value ? TRUE : FALSE;
214		break;
215	case WPA_EAPOL_inc_EapolFramesTx:
216		if (sta->eapol_sm)
217			sta->eapol_sm->dot1xAuthEapolFramesTx++;
218		break;
219	}
220}
221
222
223static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
224				      wpa_eapol_variable var)
225{
226	struct hostapd_data *hapd = ctx;
227	struct sta_info *sta = ap_get_sta(hapd, addr);
228	if (sta == NULL || sta->eapol_sm == NULL)
229		return -1;
230	switch (var) {
231	case WPA_EAPOL_keyRun:
232		return sta->eapol_sm->keyRun;
233	case WPA_EAPOL_keyAvailable:
234		return sta->eapol_sm->eap_if->eapKeyAvailable;
235	default:
236		return -1;
237	}
238}
239
240
241static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
242					   const u8 *p2p_dev_addr,
243					   const u8 *prev_psk, size_t *psk_len)
244{
245	struct hostapd_data *hapd = ctx;
246	struct sta_info *sta = ap_get_sta(hapd, addr);
247	const u8 *psk;
248
249	if (psk_len)
250		*psk_len = PMK_LEN;
251
252#ifdef CONFIG_SAE
253	if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
254		if (!sta->sae || prev_psk)
255			return NULL;
256		return sta->sae->pmk;
257	}
258	if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
259		wpa_printf(MSG_DEBUG,
260			   "No PSK for STA trying to use SAE with PMKSA caching");
261		return NULL;
262	}
263#endif /* CONFIG_SAE */
264
265#ifdef CONFIG_OWE
266	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
267	    sta && sta->owe_pmk) {
268		if (psk_len)
269			*psk_len = sta->owe_pmk_len;
270		return sta->owe_pmk;
271	}
272	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
273		struct rsn_pmksa_cache_entry *sa;
274
275		sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
276		if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
277			if (psk_len)
278				*psk_len = sa->pmk_len;
279			return sa->pmk;
280		}
281	}
282#endif /* CONFIG_OWE */
283
284	psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
285	/*
286	 * This is about to iterate over all psks, prev_psk gives the last
287	 * returned psk which should not be returned again.
288	 * logic list (all hostapd_get_psk; all sta->psk)
289	 */
290	if (sta && sta->psk && !psk) {
291		struct hostapd_sta_wpa_psk_short *pos;
292		psk = sta->psk->psk;
293		for (pos = sta->psk; pos; pos = pos->next) {
294			if (pos->is_passphrase) {
295				pbkdf2_sha1(pos->passphrase,
296					    hapd->conf->ssid.ssid,
297					    hapd->conf->ssid.ssid_len, 4096,
298					    pos->psk, PMK_LEN);
299				pos->is_passphrase = 0;
300			}
301			if (pos->psk == prev_psk) {
302				psk = pos->next ? pos->next->psk : NULL;
303				break;
304			}
305		}
306	}
307	return psk;
308}
309
310
311static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
312				    size_t *len)
313{
314	struct hostapd_data *hapd = ctx;
315	const u8 *key;
316	size_t keylen;
317	struct sta_info *sta;
318
319	sta = ap_get_sta(hapd, addr);
320	if (sta == NULL) {
321		wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
322		return -1;
323	}
324
325	key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
326	if (key == NULL) {
327		wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
328			   sta->eapol_sm);
329		return -1;
330	}
331
332	if (keylen > *len)
333		keylen = *len;
334	os_memcpy(msk, key, keylen);
335	*len = keylen;
336
337	return 0;
338}
339
340
341static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
342				    const u8 *addr, int idx, u8 *key,
343				    size_t key_len)
344{
345	struct hostapd_data *hapd = ctx;
346	const char *ifname = hapd->conf->iface;
347
348	if (vlan_id > 0) {
349		ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
350		if (ifname == NULL)
351			return -1;
352	}
353
354#ifdef CONFIG_TESTING_OPTIONS
355	if (addr && !is_broadcast_ether_addr(addr)) {
356		struct sta_info *sta;
357
358		sta = ap_get_sta(hapd, addr);
359		if (sta) {
360			sta->last_tk_alg = alg;
361			sta->last_tk_key_idx = idx;
362			if (key)
363				os_memcpy(sta->last_tk, key, key_len);
364			sta->last_tk_len = key_len;
365		}
366#ifdef CONFIG_IEEE80211W
367	} else if (alg == WPA_CIPHER_AES_128_CMAC ||
368		   alg == WPA_CIPHER_BIP_GMAC_128 ||
369		   alg == WPA_CIPHER_BIP_GMAC_256 ||
370		   alg == WPA_CIPHER_BIP_CMAC_256) {
371		hapd->last_igtk_alg = alg;
372		hapd->last_igtk_key_idx = idx;
373		if (key)
374			os_memcpy(hapd->last_igtk, key, key_len);
375		hapd->last_igtk_len = key_len;
376#endif /* CONFIG_IEEE80211W */
377	} else {
378		hapd->last_gtk_alg = alg;
379		hapd->last_gtk_key_idx = idx;
380		if (key)
381			os_memcpy(hapd->last_gtk, key, key_len);
382		hapd->last_gtk_len = key_len;
383	}
384#endif /* CONFIG_TESTING_OPTIONS */
385	return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
386				   key, key_len);
387}
388
389
390static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
391				       u8 *seq)
392{
393	struct hostapd_data *hapd = ctx;
394	return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
395}
396
397
398static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
399				       const u8 *data, size_t data_len,
400				       int encrypt)
401{
402	struct hostapd_data *hapd = ctx;
403	struct sta_info *sta;
404	u32 flags = 0;
405
406#ifdef CONFIG_TESTING_OPTIONS
407	if (hapd->ext_eapol_frame_io) {
408		size_t hex_len = 2 * data_len + 1;
409		char *hex = os_malloc(hex_len);
410
411		if (hex == NULL)
412			return -1;
413		wpa_snprintf_hex(hex, hex_len, data, data_len);
414		wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
415			MAC2STR(addr), hex);
416		os_free(hex);
417		return 0;
418	}
419#endif /* CONFIG_TESTING_OPTIONS */
420
421	sta = ap_get_sta(hapd, addr);
422	if (sta)
423		flags = hostapd_sta_flags_to_drv(sta->flags);
424
425	return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
426					   encrypt, flags);
427}
428
429
430static int hostapd_wpa_auth_for_each_sta(
431	void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
432	void *cb_ctx)
433{
434	struct hostapd_data *hapd = ctx;
435	struct sta_info *sta;
436
437	for (sta = hapd->sta_list; sta; sta = sta->next) {
438		if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
439			return 1;
440	}
441	return 0;
442}
443
444
445struct wpa_auth_iface_iter_data {
446	int (*cb)(struct wpa_authenticator *sm, void *ctx);
447	void *cb_ctx;
448};
449
450static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
451{
452	struct wpa_auth_iface_iter_data *data = ctx;
453	size_t i;
454	for (i = 0; i < iface->num_bss; i++) {
455		if (iface->bss[i]->wpa_auth &&
456		    data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
457			return 1;
458	}
459	return 0;
460}
461
462
463static int hostapd_wpa_auth_for_each_auth(
464	void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
465	void *cb_ctx)
466{
467	struct hostapd_data *hapd = ctx;
468	struct wpa_auth_iface_iter_data data;
469	if (hapd->iface->interfaces == NULL ||
470	    hapd->iface->interfaces->for_each_interface == NULL)
471		return -1;
472	data.cb = cb;
473	data.cb_ctx = cb_ctx;
474	return hapd->iface->interfaces->for_each_interface(
475		hapd->iface->interfaces, wpa_auth_iface_iter, &data);
476}
477
478
479#ifdef CONFIG_IEEE80211R_AP
480
481struct wpa_ft_rrb_rx_later_data {
482	struct dl_list list;
483	u8 addr[ETH_ALEN];
484	size_t data_len;
485	/* followed by data_len octets of data */
486};
487
488static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
489{
490	struct hostapd_data *hapd = eloop_ctx;
491	struct wpa_ft_rrb_rx_later_data *data, *n;
492
493	dl_list_for_each_safe(data, n, &hapd->l2_queue,
494			      struct wpa_ft_rrb_rx_later_data, list) {
495		if (hapd->wpa_auth) {
496			wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
497				      (const u8 *) (data + 1),
498				      data->data_len);
499		}
500		dl_list_del(&data->list);
501		os_free(data);
502	}
503}
504
505
506struct wpa_auth_ft_iface_iter_data {
507	struct hostapd_data *src_hapd;
508	const u8 *dst;
509	const u8 *data;
510	size_t data_len;
511};
512
513
514static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
515{
516	struct wpa_auth_ft_iface_iter_data *idata = ctx;
517	struct wpa_ft_rrb_rx_later_data *data;
518	struct hostapd_data *hapd;
519	size_t j;
520
521	for (j = 0; j < iface->num_bss; j++) {
522		hapd = iface->bss[j];
523		if (hapd == idata->src_hapd ||
524		    !hapd->wpa_auth ||
525		    os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
526			continue;
527
528		wpa_printf(MSG_DEBUG,
529			   "FT: Send RRB data directly to locally managed BSS "
530			   MACSTR "@%s -> " MACSTR "@%s",
531			   MAC2STR(idata->src_hapd->own_addr),
532			   idata->src_hapd->conf->iface,
533			   MAC2STR(hapd->own_addr), hapd->conf->iface);
534
535		/* Defer wpa_ft_rrb_rx() until next eloop step as this is
536		 * when it would be triggered when reading from a socket.
537		 * This avoids
538		 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
539		 * that is calling hapd0:recv handler from within
540		 * hapd0:send directly.
541		 */
542		data = os_zalloc(sizeof(*data) + idata->data_len);
543		if (!data)
544			return 1;
545
546		os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
547		os_memcpy(data + 1, idata->data, idata->data_len);
548		data->data_len = idata->data_len;
549
550		dl_list_add(&hapd->l2_queue, &data->list);
551
552		if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
553						 hapd, NULL))
554			eloop_register_timeout(0, 0,
555					       hostapd_wpa_ft_rrb_rx_later,
556					       hapd, NULL);
557
558		return 1;
559	}
560
561	return 0;
562}
563
564#endif /* CONFIG_IEEE80211R_AP */
565
566
567static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
568				       const u8 *data, size_t data_len)
569{
570	struct hostapd_data *hapd = ctx;
571	struct l2_ethhdr *buf;
572	int ret;
573
574#ifdef CONFIG_TESTING_OPTIONS
575	if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
576		size_t hex_len = 2 * data_len + 1;
577		char *hex = os_malloc(hex_len);
578
579		if (hex == NULL)
580			return -1;
581		wpa_snprintf_hex(hex, hex_len, data, data_len);
582		wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
583			MAC2STR(dst), hex);
584		os_free(hex);
585		return 0;
586	}
587#endif /* CONFIG_TESTING_OPTIONS */
588
589#ifdef CONFIG_IEEE80211R_AP
590	if (proto == ETH_P_RRB && hapd->iface->interfaces &&
591	    hapd->iface->interfaces->for_each_interface) {
592		int res;
593		struct wpa_auth_ft_iface_iter_data idata;
594		idata.src_hapd = hapd;
595		idata.dst = dst;
596		idata.data = data;
597		idata.data_len = data_len;
598		res = hapd->iface->interfaces->for_each_interface(
599			hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
600			&idata);
601		if (res == 1)
602			return data_len;
603	}
604#endif /* CONFIG_IEEE80211R_AP */
605
606	if (hapd->driver && hapd->driver->send_ether)
607		return hapd->driver->send_ether(hapd->drv_priv, dst,
608						hapd->own_addr, proto,
609						data, data_len);
610	if (hapd->l2 == NULL)
611		return -1;
612
613	buf = os_malloc(sizeof(*buf) + data_len);
614	if (buf == NULL)
615		return -1;
616	os_memcpy(buf->h_dest, dst, ETH_ALEN);
617	os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
618	buf->h_proto = host_to_be16(proto);
619	os_memcpy(buf + 1, data, data_len);
620	ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
621			     sizeof(*buf) + data_len);
622	os_free(buf);
623	return ret;
624}
625
626
627#ifdef CONFIG_ETH_P_OUI
628static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
629						  u8 oui_suffix)
630{
631	switch (oui_suffix) {
632#ifdef CONFIG_IEEE80211R_AP
633	case FT_PACKET_R0KH_R1KH_PULL:
634		return hapd->oui_pull;
635	case FT_PACKET_R0KH_R1KH_RESP:
636		return hapd->oui_resp;
637	case FT_PACKET_R0KH_R1KH_PUSH:
638		return hapd->oui_push;
639	case FT_PACKET_R0KH_R1KH_SEQ_REQ:
640		return hapd->oui_sreq;
641	case FT_PACKET_R0KH_R1KH_SEQ_RESP:
642		return hapd->oui_sresp;
643#endif /* CONFIG_IEEE80211R_AP */
644	default:
645		return NULL;
646	}
647}
648#endif /* CONFIG_ETH_P_OUI */
649
650
651#ifdef CONFIG_IEEE80211R_AP
652
653struct oui_deliver_later_data {
654	struct dl_list list;
655	u8 src_addr[ETH_ALEN];
656	u8 dst_addr[ETH_ALEN];
657	size_t data_len;
658	u8 oui_suffix;
659	/* followed by data_len octets of data */
660};
661
662static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
663{
664	struct hostapd_data *hapd = eloop_ctx;
665	struct oui_deliver_later_data *data, *n;
666	struct eth_p_oui_ctx *oui_ctx;
667
668	dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
669			      struct oui_deliver_later_data, list) {
670		oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
671		if (hapd->wpa_auth && oui_ctx) {
672			eth_p_oui_deliver(oui_ctx, data->src_addr,
673					  data->dst_addr,
674					  (const u8 *) (data + 1),
675					  data->data_len);
676		}
677		dl_list_del(&data->list);
678		os_free(data);
679	}
680}
681
682
683struct wpa_auth_oui_iface_iter_data {
684	struct hostapd_data *src_hapd;
685	const u8 *dst_addr;
686	const u8 *data;
687	size_t data_len;
688	u8 oui_suffix;
689};
690
691static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
692{
693	struct wpa_auth_oui_iface_iter_data *idata = ctx;
694	struct oui_deliver_later_data *data;
695	struct hostapd_data *hapd;
696	size_t j;
697
698	for (j = 0; j < iface->num_bss; j++) {
699		hapd = iface->bss[j];
700		if (hapd == idata->src_hapd)
701			continue;
702		if (!is_multicast_ether_addr(idata->dst_addr) &&
703		    os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
704			continue;
705
706		/* defer eth_p_oui_deliver until next eloop step as this is
707		 * when it would be triggerd from reading from sock
708		 * This avoids
709		 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
710		 * that is calling hapd0:recv handler from within
711		 * hapd0:send directly.
712		 */
713		data = os_zalloc(sizeof(*data) + idata->data_len);
714		if (!data)
715			return 1;
716
717		os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
718		os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
719		os_memcpy(data + 1, idata->data, idata->data_len);
720		data->data_len = idata->data_len;
721		data->oui_suffix = idata->oui_suffix;
722
723		dl_list_add(&hapd->l2_oui_queue, &data->list);
724
725		if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
726						 hapd, NULL))
727			eloop_register_timeout(0, 0,
728					       hostapd_oui_deliver_later,
729					       hapd, NULL);
730
731		return 1;
732	}
733
734	return 0;
735}
736
737#endif /* CONFIG_IEEE80211R_AP */
738
739
740static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
741				     const u8 *data, size_t data_len)
742{
743#ifdef CONFIG_ETH_P_OUI
744	struct hostapd_data *hapd = ctx;
745	struct eth_p_oui_ctx *oui_ctx;
746
747#ifdef CONFIG_IEEE80211R_AP
748	if (hapd->iface->interfaces &&
749	    hapd->iface->interfaces->for_each_interface) {
750		struct wpa_auth_oui_iface_iter_data idata;
751		int res;
752
753		idata.src_hapd = hapd;
754		idata.dst_addr = dst;
755		idata.data = data;
756		idata.data_len = data_len;
757		idata.oui_suffix = oui_suffix;
758		res = hapd->iface->interfaces->for_each_interface(
759			hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
760			&idata);
761		if (res == 1)
762			return data_len;
763	}
764#endif /* CONFIG_IEEE80211R_AP */
765
766	oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
767	if (!oui_ctx)
768		return -1;
769
770	return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
771#else /* CONFIG_ETH_P_OUI */
772	return -1;
773#endif /* CONFIG_ETH_P_OUI */
774}
775
776
777#ifdef CONFIG_IEEE80211R_AP
778
779static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
780					   const u8 *data, size_t data_len)
781{
782	struct hostapd_data *hapd = ctx;
783	int res;
784	struct ieee80211_mgmt *m;
785	size_t mlen;
786	struct sta_info *sta;
787
788	sta = ap_get_sta(hapd, dst);
789	if (sta == NULL || sta->wpa_sm == NULL)
790		return -1;
791
792	m = os_zalloc(sizeof(*m) + data_len);
793	if (m == NULL)
794		return -1;
795	mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
796	m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
797					WLAN_FC_STYPE_ACTION);
798	os_memcpy(m->da, dst, ETH_ALEN);
799	os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
800	os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
801	os_memcpy(&m->u, data, data_len);
802
803	res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
804	os_free(m);
805	return res;
806}
807
808
809static struct wpa_state_machine *
810hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
811{
812	struct hostapd_data *hapd = ctx;
813	struct sta_info *sta;
814
815	if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
816		return NULL;
817
818	sta = ap_sta_add(hapd, sta_addr);
819	if (sta == NULL)
820		return NULL;
821	if (sta->wpa_sm) {
822		sta->auth_alg = WLAN_AUTH_FT;
823		return sta->wpa_sm;
824	}
825
826	sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
827	if (sta->wpa_sm == NULL) {
828		ap_free_sta(hapd, sta);
829		return NULL;
830	}
831	sta->auth_alg = WLAN_AUTH_FT;
832
833	return sta->wpa_sm;
834}
835
836
837static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
838				size_t len)
839{
840	struct hostapd_data *hapd = ctx;
841	struct l2_ethhdr *ethhdr;
842	if (len < sizeof(*ethhdr))
843		return;
844	ethhdr = (struct l2_ethhdr *) buf;
845	wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
846		   MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
847	if (!is_multicast_ether_addr(ethhdr->h_dest) &&
848	    os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
849		return;
850	wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
851		      len - sizeof(*ethhdr));
852}
853
854
855static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
856				    const u8 *dst_addr, u8 oui_suffix,
857				    const u8 *buf, size_t len)
858{
859	struct hostapd_data *hapd = ctx;
860
861	wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
862		   MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
863	if (!is_multicast_ether_addr(dst_addr) &&
864	    os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
865		return;
866	wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
867			  len);
868}
869
870
871static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
872				      u8 *tspec_ie, size_t tspec_ielen)
873{
874	struct hostapd_data *hapd = ctx;
875	return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
876}
877
878
879
880static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
881				       const char *ft_iface)
882{
883	hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
884					    FT_PACKET_R0KH_R1KH_PULL,
885					    hostapd_rrb_oui_receive, hapd);
886	if (!hapd->oui_pull)
887		return -1;
888
889	hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
890					    FT_PACKET_R0KH_R1KH_RESP,
891					    hostapd_rrb_oui_receive, hapd);
892	if (!hapd->oui_resp)
893		return -1;
894
895	hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
896					    FT_PACKET_R0KH_R1KH_PUSH,
897					    hostapd_rrb_oui_receive, hapd);
898	if (!hapd->oui_push)
899		return -1;
900
901	hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
902					    FT_PACKET_R0KH_R1KH_SEQ_REQ,
903					    hostapd_rrb_oui_receive, hapd);
904	if (!hapd->oui_sreq)
905		return -1;
906
907	hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
908					     FT_PACKET_R0KH_R1KH_SEQ_RESP,
909					     hostapd_rrb_oui_receive, hapd);
910	if (!hapd->oui_sresp)
911		return -1;
912
913	return 0;
914}
915
916
917static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
918{
919	eth_p_oui_unregister(hapd->oui_pull);
920	hapd->oui_pull = NULL;
921	eth_p_oui_unregister(hapd->oui_resp);
922	hapd->oui_resp = NULL;
923	eth_p_oui_unregister(hapd->oui_push);
924	hapd->oui_push = NULL;
925	eth_p_oui_unregister(hapd->oui_sreq);
926	hapd->oui_sreq = NULL;
927	eth_p_oui_unregister(hapd->oui_sresp);
928	hapd->oui_sresp = NULL;
929}
930#endif /* CONFIG_IEEE80211R_AP */
931
932
933int hostapd_setup_wpa(struct hostapd_data *hapd)
934{
935	struct wpa_auth_config _conf;
936	static const struct wpa_auth_callbacks cb = {
937		.logger = hostapd_wpa_auth_logger,
938		.disconnect = hostapd_wpa_auth_disconnect,
939		.mic_failure_report = hostapd_wpa_auth_mic_failure_report,
940		.psk_failure_report = hostapd_wpa_auth_psk_failure_report,
941		.set_eapol = hostapd_wpa_auth_set_eapol,
942		.get_eapol = hostapd_wpa_auth_get_eapol,
943		.get_psk = hostapd_wpa_auth_get_psk,
944		.get_msk = hostapd_wpa_auth_get_msk,
945		.set_key = hostapd_wpa_auth_set_key,
946		.get_seqnum = hostapd_wpa_auth_get_seqnum,
947		.send_eapol = hostapd_wpa_auth_send_eapol,
948		.for_each_sta = hostapd_wpa_auth_for_each_sta,
949		.for_each_auth = hostapd_wpa_auth_for_each_auth,
950		.send_ether = hostapd_wpa_auth_send_ether,
951		.send_oui = hostapd_wpa_auth_send_oui,
952#ifdef CONFIG_IEEE80211R_AP
953		.send_ft_action = hostapd_wpa_auth_send_ft_action,
954		.add_sta = hostapd_wpa_auth_add_sta,
955		.add_tspec = hostapd_wpa_auth_add_tspec,
956#endif /* CONFIG_IEEE80211R_AP */
957	};
958	const u8 *wpa_ie;
959	size_t wpa_ie_len;
960
961	hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
962	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
963		_conf.tx_status = 1;
964	if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
965		_conf.ap_mlme = 1;
966	hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
967	if (hapd->wpa_auth == NULL) {
968		wpa_printf(MSG_ERROR, "WPA initialization failed.");
969		return -1;
970	}
971
972	if (hostapd_set_privacy(hapd, 1)) {
973		wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
974			   "for interface %s", hapd->conf->iface);
975		return -1;
976	}
977
978	wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
979	if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
980		wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
981			   "the kernel driver.");
982		return -1;
983	}
984
985	if (rsn_preauth_iface_init(hapd)) {
986		wpa_printf(MSG_ERROR, "Initialization of RSN "
987			   "pre-authentication failed.");
988		return -1;
989	}
990
991#ifdef CONFIG_IEEE80211R_AP
992	if (!hostapd_drv_none(hapd) &&
993	    wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
994		const char *ft_iface;
995
996		ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
997			   hapd->conf->iface;
998		hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
999					  hostapd_rrb_receive, hapd, 1);
1000		if (hapd->l2 == NULL &&
1001		    (hapd->driver == NULL ||
1002		     hapd->driver->send_ether == NULL)) {
1003			wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1004				   "interface");
1005			return -1;
1006		}
1007
1008		if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1009			wpa_printf(MSG_ERROR,
1010				   "Failed to open ETH_P_OUI interface");
1011			return -1;
1012		}
1013	}
1014#endif /* CONFIG_IEEE80211R_AP */
1015
1016	return 0;
1017
1018}
1019
1020
1021void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1022{
1023	struct wpa_auth_config wpa_auth_conf;
1024	hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1025	wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1026}
1027
1028
1029void hostapd_deinit_wpa(struct hostapd_data *hapd)
1030{
1031	ieee80211_tkip_countermeasures_deinit(hapd);
1032	rsn_preauth_iface_deinit(hapd);
1033	if (hapd->wpa_auth) {
1034		wpa_deinit(hapd->wpa_auth);
1035		hapd->wpa_auth = NULL;
1036
1037		if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1038			wpa_printf(MSG_DEBUG, "Could not disable "
1039				   "PrivacyInvoked for interface %s",
1040				   hapd->conf->iface);
1041		}
1042
1043		if (hapd->drv_priv &&
1044		    hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1045			wpa_printf(MSG_DEBUG, "Could not remove generic "
1046				   "information element from interface %s",
1047				   hapd->conf->iface);
1048		}
1049	}
1050	ieee802_1x_deinit(hapd);
1051
1052#ifdef CONFIG_IEEE80211R_AP
1053	eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1054	hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1055	eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1056	hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1057	l2_packet_deinit(hapd->l2);
1058	hapd->l2 = NULL;
1059	hostapd_wpa_unregister_ft_oui(hapd);
1060#endif /* CONFIG_IEEE80211R_AP */
1061}
1062