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