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