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