2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
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"
25 #include "ieee802_1x.h"
26 #include "preauth_auth.h"
28 #include "tkip_countermeasures.h"
29 #include "ap_drv_ops.h"
30 #include "ap_config.h"
34 static void hostapd_wpa_auth_conf(struct hostapd_bss_config
*conf
,
35 struct wpa_auth_config
*wconf
)
37 wconf
->wpa
= conf
->wpa
;
38 wconf
->wpa_key_mgmt
= conf
->wpa_key_mgmt
;
39 wconf
->wpa_pairwise
= conf
->wpa_pairwise
;
40 wconf
->wpa_group
= conf
->wpa_group
;
41 wconf
->wpa_group_rekey
= conf
->wpa_group_rekey
;
42 wconf
->wpa_strict_rekey
= conf
->wpa_strict_rekey
;
43 wconf
->wpa_gmk_rekey
= conf
->wpa_gmk_rekey
;
44 wconf
->wpa_ptk_rekey
= conf
->wpa_ptk_rekey
;
45 wconf
->rsn_pairwise
= conf
->rsn_pairwise
;
46 wconf
->rsn_preauth
= conf
->rsn_preauth
;
47 wconf
->eapol_version
= conf
->eapol_version
;
48 wconf
->peerkey
= conf
->peerkey
;
49 wconf
->wmm_enabled
= conf
->wmm_enabled
;
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
);
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 #endif /* CONFIG_IEEE80211R */
77 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
78 logger_level level
, const char *txt
)
80 #ifndef CONFIG_NO_HOSTAPD_LOGGER
81 struct hostapd_data
*hapd
= ctx
;
86 hlevel
= HOSTAPD_LEVEL_WARNING
;
89 hlevel
= HOSTAPD_LEVEL_INFO
;
93 hlevel
= HOSTAPD_LEVEL_DEBUG
;
97 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
98 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
102 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
105 struct hostapd_data
*hapd
= ctx
;
106 wpa_printf(MSG_DEBUG
, "%s: WPA authenticator requests disconnect: "
107 "STA " MACSTR
" reason %d",
108 __func__
, MAC2STR(addr
), reason
);
109 ap_sta_disconnect(hapd
, NULL
, addr
, reason
);
113 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
115 struct hostapd_data
*hapd
= ctx
;
116 michael_mic_failure(hapd
, addr
, 0);
120 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
121 wpa_eapol_variable var
, int value
)
123 struct hostapd_data
*hapd
= ctx
;
124 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
128 case WPA_EAPOL_portEnabled
:
129 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
131 case WPA_EAPOL_portValid
:
132 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
134 case WPA_EAPOL_authorized
:
135 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
137 case WPA_EAPOL_portControl_Auto
:
139 sta
->eapol_sm
->portControl
= Auto
;
141 case WPA_EAPOL_keyRun
:
143 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
145 case WPA_EAPOL_keyAvailable
:
147 sta
->eapol_sm
->eap_if
->eapKeyAvailable
=
148 value
? TRUE
: FALSE
;
150 case WPA_EAPOL_keyDone
:
152 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
154 case WPA_EAPOL_inc_EapolFramesTx
:
156 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
162 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
163 wpa_eapol_variable var
)
165 struct hostapd_data
*hapd
= ctx
;
166 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
167 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
170 case WPA_EAPOL_keyRun
:
171 return sta
->eapol_sm
->keyRun
;
172 case WPA_EAPOL_keyAvailable
:
173 return sta
->eapol_sm
->eap_if
->eapKeyAvailable
;
180 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
183 struct hostapd_data
*hapd
= ctx
;
184 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
188 static int hostapd_wpa_auth_get_msk(void *ctx
, const u8
*addr
, u8
*msk
,
191 struct hostapd_data
*hapd
= ctx
;
194 struct sta_info
*sta
;
196 sta
= ap_get_sta(hapd
, addr
);
200 key
= ieee802_1x_get_key(sta
->eapol_sm
, &keylen
);
206 os_memcpy(msk
, key
, keylen
);
213 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
214 const u8
*addr
, int idx
, u8
*key
,
217 struct hostapd_data
*hapd
= ctx
;
218 const char *ifname
= hapd
->conf
->iface
;
221 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
226 return hapd
->drv
.set_key(ifname
, hapd
, alg
, addr
, idx
, 1, NULL
, 0,
231 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
234 struct hostapd_data
*hapd
= ctx
;
235 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
239 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
240 const u8
*data
, size_t data_len
,
243 struct hostapd_data
*hapd
= ctx
;
244 return hapd
->drv
.send_eapol(hapd
, addr
, data
, data_len
, encrypt
);
248 static int hostapd_wpa_auth_for_each_sta(
249 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
252 struct hostapd_data
*hapd
= ctx
;
253 struct sta_info
*sta
;
255 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
256 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
263 struct wpa_auth_iface_iter_data
{
264 int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
);
268 static int wpa_auth_iface_iter(struct hostapd_iface
*iface
, void *ctx
)
270 struct wpa_auth_iface_iter_data
*data
= ctx
;
272 for (i
= 0; i
< iface
->num_bss
; i
++) {
273 if (data
->cb(iface
->bss
[i
]->wpa_auth
, data
->cb_ctx
))
280 static int hostapd_wpa_auth_for_each_auth(
281 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
284 struct hostapd_data
*hapd
= ctx
;
285 struct wpa_auth_iface_iter_data data
;
286 if (hapd
->iface
->for_each_interface
== NULL
)
289 data
.cb_ctx
= cb_ctx
;
290 return hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
291 wpa_auth_iface_iter
, &data
);
295 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
296 const u8
*data
, size_t data_len
)
298 struct hostapd_data
*hapd
= ctx
;
300 if (hapd
->driver
&& hapd
->driver
->send_ether
)
301 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
302 hapd
->own_addr
, proto
,
304 if (hapd
->l2
== NULL
)
306 return l2_packet_send(hapd
->l2
, dst
, proto
, data
, data_len
);
310 #ifdef CONFIG_IEEE80211R
312 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
313 const u8
*data
, size_t data_len
)
315 struct hostapd_data
*hapd
= ctx
;
317 struct ieee80211_mgmt
*m
;
319 struct sta_info
*sta
;
321 sta
= ap_get_sta(hapd
, dst
);
322 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
325 m
= os_zalloc(sizeof(*m
) + data_len
);
328 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
329 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
330 WLAN_FC_STYPE_ACTION
);
331 os_memcpy(m
->da
, dst
, ETH_ALEN
);
332 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
333 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
334 os_memcpy(&m
->u
, data
, data_len
);
336 res
= hapd
->drv
.send_mgmt_frame(hapd
, (u8
*) m
, mlen
);
342 static struct wpa_state_machine
*
343 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
345 struct hostapd_data
*hapd
= ctx
;
346 struct sta_info
*sta
;
348 sta
= ap_sta_add(hapd
, sta_addr
);
354 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
355 if (sta
->wpa_sm
== NULL
) {
356 ap_free_sta(hapd
, sta
);
359 sta
->auth_alg
= WLAN_AUTH_FT
;
365 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
368 struct hostapd_data
*hapd
= ctx
;
369 wpa_ft_rrb_rx(hapd
->wpa_auth
, src_addr
, buf
, len
);
372 #endif /* CONFIG_IEEE80211R */
375 int hostapd_setup_wpa(struct hostapd_data
*hapd
)
377 struct wpa_auth_config _conf
;
378 struct wpa_auth_callbacks cb
;
382 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
383 os_memset(&cb
, 0, sizeof(cb
));
385 cb
.logger
= hostapd_wpa_auth_logger
;
386 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
387 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
388 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
389 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
390 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
391 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
392 cb
.set_key
= hostapd_wpa_auth_set_key
;
393 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
394 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
395 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
396 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
397 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
398 #ifdef CONFIG_IEEE80211R
399 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
400 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
401 #endif /* CONFIG_IEEE80211R */
402 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
403 if (hapd
->wpa_auth
== NULL
) {
404 wpa_printf(MSG_ERROR
, "WPA initialization failed.");
408 if (hostapd_set_privacy(hapd
, 1)) {
409 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
410 "for interface %s", hapd
->conf
->iface
);
414 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
415 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
416 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
417 "the kernel driver.");
421 if (rsn_preauth_iface_init(hapd
)) {
422 wpa_printf(MSG_ERROR
, "Initialization of RSN "
423 "pre-authentication failed.");
427 #ifdef CONFIG_IEEE80211R
428 if (!hostapd_drv_none(hapd
)) {
429 hapd
->l2
= l2_packet_init(hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
430 hostapd_rrb_receive
, hapd
, 0);
431 if (hapd
->l2
== NULL
&&
432 (hapd
->driver
== NULL
||
433 hapd
->driver
->send_ether
== NULL
)) {
434 wpa_printf(MSG_ERROR
, "Failed to open l2_packet "
439 #endif /* CONFIG_IEEE80211R */
446 void hostapd_reconfig_wpa(struct hostapd_data
*hapd
)
448 struct wpa_auth_config wpa_auth_conf
;
449 hostapd_wpa_auth_conf(hapd
->conf
, &wpa_auth_conf
);
450 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
454 void hostapd_deinit_wpa(struct hostapd_data
*hapd
)
456 rsn_preauth_iface_deinit(hapd
);
457 if (hapd
->wpa_auth
) {
458 wpa_deinit(hapd
->wpa_auth
);
459 hapd
->wpa_auth
= NULL
;
461 if (hostapd_set_privacy(hapd
, 0)) {
462 wpa_printf(MSG_DEBUG
, "Could not disable "
463 "PrivacyInvoked for interface %s",
467 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
468 wpa_printf(MSG_DEBUG
, "Could not remove generic "
469 "information element from interface %s",
473 ieee802_1x_deinit(hapd
);
475 #ifdef CONFIG_IEEE80211R
476 l2_packet_deinit(hapd
->l2
);
477 #endif /* CONFIG_IEEE80211R */