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 (iface
->bss
[i
]->wpa_auth
&&
274 data
->cb(iface
->bss
[i
]->wpa_auth
, data
->cb_ctx
))
281 static int hostapd_wpa_auth_for_each_auth(
282 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
285 struct hostapd_data
*hapd
= ctx
;
286 struct wpa_auth_iface_iter_data data
;
287 if (hapd
->iface
->for_each_interface
== NULL
)
290 data
.cb_ctx
= cb_ctx
;
291 return hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
292 wpa_auth_iface_iter
, &data
);
296 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
297 const u8
*data
, size_t data_len
)
299 struct hostapd_data
*hapd
= ctx
;
301 if (hapd
->driver
&& hapd
->driver
->send_ether
)
302 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
303 hapd
->own_addr
, proto
,
305 if (hapd
->l2
== NULL
)
307 return l2_packet_send(hapd
->l2
, dst
, proto
, data
, data_len
);
311 #ifdef CONFIG_IEEE80211R
313 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
314 const u8
*data
, size_t data_len
)
316 struct hostapd_data
*hapd
= ctx
;
318 struct ieee80211_mgmt
*m
;
320 struct sta_info
*sta
;
322 sta
= ap_get_sta(hapd
, dst
);
323 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
326 m
= os_zalloc(sizeof(*m
) + data_len
);
329 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
330 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
331 WLAN_FC_STYPE_ACTION
);
332 os_memcpy(m
->da
, dst
, ETH_ALEN
);
333 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
334 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
335 os_memcpy(&m
->u
, data
, data_len
);
337 res
= hapd
->drv
.send_mgmt_frame(hapd
, (u8
*) m
, mlen
);
343 static struct wpa_state_machine
*
344 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
346 struct hostapd_data
*hapd
= ctx
;
347 struct sta_info
*sta
;
349 sta
= ap_sta_add(hapd
, sta_addr
);
353 sta
->auth_alg
= WLAN_AUTH_FT
;
357 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
358 if (sta
->wpa_sm
== NULL
) {
359 ap_free_sta(hapd
, sta
);
362 sta
->auth_alg
= WLAN_AUTH_FT
;
368 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
371 struct hostapd_data
*hapd
= ctx
;
372 wpa_ft_rrb_rx(hapd
->wpa_auth
, src_addr
, buf
, len
);
375 #endif /* CONFIG_IEEE80211R */
378 int hostapd_setup_wpa(struct hostapd_data
*hapd
)
380 struct wpa_auth_config _conf
;
381 struct wpa_auth_callbacks cb
;
385 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
386 os_memset(&cb
, 0, sizeof(cb
));
388 cb
.logger
= hostapd_wpa_auth_logger
;
389 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
390 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
391 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
392 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
393 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
394 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
395 cb
.set_key
= hostapd_wpa_auth_set_key
;
396 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
397 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
398 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
399 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
400 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
401 #ifdef CONFIG_IEEE80211R
402 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
403 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
404 #endif /* CONFIG_IEEE80211R */
405 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
406 if (hapd
->wpa_auth
== NULL
) {
407 wpa_printf(MSG_ERROR
, "WPA initialization failed.");
411 if (hostapd_set_privacy(hapd
, 1)) {
412 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
413 "for interface %s", hapd
->conf
->iface
);
417 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
418 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
419 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
420 "the kernel driver.");
424 if (rsn_preauth_iface_init(hapd
)) {
425 wpa_printf(MSG_ERROR
, "Initialization of RSN "
426 "pre-authentication failed.");
430 #ifdef CONFIG_IEEE80211R
431 if (!hostapd_drv_none(hapd
)) {
432 hapd
->l2
= l2_packet_init(hapd
->conf
->bridge
[0] ?
434 hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
435 hostapd_rrb_receive
, hapd
, 0);
436 if (hapd
->l2
== NULL
&&
437 (hapd
->driver
== NULL
||
438 hapd
->driver
->send_ether
== NULL
)) {
439 wpa_printf(MSG_ERROR
, "Failed to open l2_packet "
444 #endif /* CONFIG_IEEE80211R */
451 void hostapd_reconfig_wpa(struct hostapd_data
*hapd
)
453 struct wpa_auth_config wpa_auth_conf
;
454 hostapd_wpa_auth_conf(hapd
->conf
, &wpa_auth_conf
);
455 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
459 void hostapd_deinit_wpa(struct hostapd_data
*hapd
)
461 rsn_preauth_iface_deinit(hapd
);
462 if (hapd
->wpa_auth
) {
463 wpa_deinit(hapd
->wpa_auth
);
464 hapd
->wpa_auth
= NULL
;
466 if (hostapd_set_privacy(hapd
, 0)) {
467 wpa_printf(MSG_DEBUG
, "Could not disable "
468 "PrivacyInvoked for interface %s",
472 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
473 wpa_printf(MSG_DEBUG
, "Could not remove generic "
474 "information element from interface %s",
478 ieee802_1x_deinit(hapd
);
480 #ifdef CONFIG_IEEE80211R
481 l2_packet_deinit(hapd
->l2
);
482 #endif /* CONFIG_IEEE80211R */