2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2011, 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
->wmm_uapsd
= conf
->wmm_uapsd
;
51 wconf
->disable_pmksa_caching
= conf
->disable_pmksa_caching
;
52 wconf
->okc
= conf
->okc
;
53 #ifdef CONFIG_IEEE80211W
54 wconf
->ieee80211w
= conf
->ieee80211w
;
55 #endif /* CONFIG_IEEE80211W */
56 #ifdef CONFIG_IEEE80211R
57 wconf
->ssid_len
= conf
->ssid
.ssid_len
;
58 if (wconf
->ssid_len
> SSID_LEN
)
59 wconf
->ssid_len
= SSID_LEN
;
60 os_memcpy(wconf
->ssid
, conf
->ssid
.ssid
, wconf
->ssid_len
);
61 os_memcpy(wconf
->mobility_domain
, conf
->mobility_domain
,
62 MOBILITY_DOMAIN_ID_LEN
);
63 if (conf
->nas_identifier
&&
64 os_strlen(conf
->nas_identifier
) <= FT_R0KH_ID_MAX_LEN
) {
65 wconf
->r0_key_holder_len
= os_strlen(conf
->nas_identifier
);
66 os_memcpy(wconf
->r0_key_holder
, conf
->nas_identifier
,
67 wconf
->r0_key_holder_len
);
69 os_memcpy(wconf
->r1_key_holder
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
70 wconf
->r0_key_lifetime
= conf
->r0_key_lifetime
;
71 wconf
->reassociation_deadline
= conf
->reassociation_deadline
;
72 wconf
->r0kh_list
= conf
->r0kh_list
;
73 wconf
->r1kh_list
= conf
->r1kh_list
;
74 wconf
->pmk_r1_push
= conf
->pmk_r1_push
;
75 wconf
->ft_over_ds
= conf
->ft_over_ds
;
76 #endif /* CONFIG_IEEE80211R */
80 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
81 logger_level level
, const char *txt
)
83 #ifndef CONFIG_NO_HOSTAPD_LOGGER
84 struct hostapd_data
*hapd
= ctx
;
89 hlevel
= HOSTAPD_LEVEL_WARNING
;
92 hlevel
= HOSTAPD_LEVEL_INFO
;
96 hlevel
= HOSTAPD_LEVEL_DEBUG
;
100 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
101 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
105 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
108 struct hostapd_data
*hapd
= ctx
;
109 wpa_printf(MSG_DEBUG
, "%s: WPA authenticator requests disconnect: "
110 "STA " MACSTR
" reason %d",
111 __func__
, MAC2STR(addr
), reason
);
112 ap_sta_disconnect(hapd
, NULL
, addr
, reason
);
116 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
118 struct hostapd_data
*hapd
= ctx
;
119 michael_mic_failure(hapd
, addr
, 0);
123 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
124 wpa_eapol_variable var
, int value
)
126 struct hostapd_data
*hapd
= ctx
;
127 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
131 case WPA_EAPOL_portEnabled
:
132 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
134 case WPA_EAPOL_portValid
:
135 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
137 case WPA_EAPOL_authorized
:
138 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
140 case WPA_EAPOL_portControl_Auto
:
142 sta
->eapol_sm
->portControl
= Auto
;
144 case WPA_EAPOL_keyRun
:
146 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
148 case WPA_EAPOL_keyAvailable
:
150 sta
->eapol_sm
->eap_if
->eapKeyAvailable
=
151 value
? TRUE
: FALSE
;
153 case WPA_EAPOL_keyDone
:
155 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
157 case WPA_EAPOL_inc_EapolFramesTx
:
159 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
165 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
166 wpa_eapol_variable var
)
168 struct hostapd_data
*hapd
= ctx
;
169 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
170 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
173 case WPA_EAPOL_keyRun
:
174 return sta
->eapol_sm
->keyRun
;
175 case WPA_EAPOL_keyAvailable
:
176 return sta
->eapol_sm
->eap_if
->eapKeyAvailable
;
183 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
186 struct hostapd_data
*hapd
= ctx
;
187 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
191 static int hostapd_wpa_auth_get_msk(void *ctx
, const u8
*addr
, u8
*msk
,
194 struct hostapd_data
*hapd
= ctx
;
197 struct sta_info
*sta
;
199 sta
= ap_get_sta(hapd
, addr
);
203 key
= ieee802_1x_get_key(sta
->eapol_sm
, &keylen
);
209 os_memcpy(msk
, key
, keylen
);
216 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
217 const u8
*addr
, int idx
, u8
*key
,
220 struct hostapd_data
*hapd
= ctx
;
221 const char *ifname
= hapd
->conf
->iface
;
224 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
229 return hostapd_drv_set_key(ifname
, hapd
, alg
, addr
, idx
, 1, NULL
, 0,
234 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
237 struct hostapd_data
*hapd
= ctx
;
238 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
242 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
243 const u8
*data
, size_t data_len
,
246 struct hostapd_data
*hapd
= ctx
;
247 struct sta_info
*sta
;
250 sta
= ap_get_sta(hapd
, addr
);
252 flags
= hostapd_sta_flags_to_drv(sta
->flags
);
254 return hostapd_drv_hapd_send_eapol(hapd
, addr
, data
, data_len
,
259 static int hostapd_wpa_auth_for_each_sta(
260 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
263 struct hostapd_data
*hapd
= ctx
;
264 struct sta_info
*sta
;
266 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
267 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
274 struct wpa_auth_iface_iter_data
{
275 int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
);
279 static int wpa_auth_iface_iter(struct hostapd_iface
*iface
, void *ctx
)
281 struct wpa_auth_iface_iter_data
*data
= ctx
;
283 for (i
= 0; i
< iface
->num_bss
; i
++) {
284 if (iface
->bss
[i
]->wpa_auth
&&
285 data
->cb(iface
->bss
[i
]->wpa_auth
, data
->cb_ctx
))
292 static int hostapd_wpa_auth_for_each_auth(
293 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
296 struct hostapd_data
*hapd
= ctx
;
297 struct wpa_auth_iface_iter_data data
;
298 if (hapd
->iface
->for_each_interface
== NULL
)
301 data
.cb_ctx
= cb_ctx
;
302 return hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
303 wpa_auth_iface_iter
, &data
);
307 #ifdef CONFIG_IEEE80211R
309 struct wpa_auth_ft_iface_iter_data
{
310 struct hostapd_data
*src_hapd
;
317 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface
*iface
, void *ctx
)
319 struct wpa_auth_ft_iface_iter_data
*idata
= ctx
;
320 struct hostapd_data
*hapd
;
323 for (j
= 0; j
< iface
->num_bss
; j
++) {
324 hapd
= iface
->bss
[j
];
325 if (hapd
== idata
->src_hapd
)
327 if (os_memcmp(hapd
->own_addr
, idata
->dst
, ETH_ALEN
) == 0) {
328 wpa_printf(MSG_DEBUG
, "FT: Send RRB data directly to "
329 "locally managed BSS " MACSTR
"@%s -> "
331 MAC2STR(idata
->src_hapd
->own_addr
),
332 idata
->src_hapd
->conf
->iface
,
333 MAC2STR(hapd
->own_addr
), hapd
->conf
->iface
);
334 wpa_ft_rrb_rx(hapd
->wpa_auth
,
335 idata
->src_hapd
->own_addr
,
336 idata
->data
, idata
->data_len
);
344 #endif /* CONFIG_IEEE80211R */
347 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
348 const u8
*data
, size_t data_len
)
350 struct hostapd_data
*hapd
= ctx
;
351 struct l2_ethhdr
*buf
;
354 #ifdef CONFIG_IEEE80211R
355 if (proto
== ETH_P_RRB
&& hapd
->iface
->for_each_interface
) {
357 struct wpa_auth_ft_iface_iter_data idata
;
358 idata
.src_hapd
= hapd
;
361 idata
.data_len
= data_len
;
362 res
= hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
363 hostapd_wpa_auth_ft_iter
,
368 #endif /* CONFIG_IEEE80211R */
370 if (hapd
->driver
&& hapd
->driver
->send_ether
)
371 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
372 hapd
->own_addr
, proto
,
374 if (hapd
->l2
== NULL
)
377 buf
= os_malloc(sizeof(*buf
) + data_len
);
380 os_memcpy(buf
->h_dest
, dst
, ETH_ALEN
);
381 os_memcpy(buf
->h_source
, hapd
->own_addr
, ETH_ALEN
);
382 buf
->h_proto
= host_to_be16(proto
);
383 os_memcpy(buf
+ 1, data
, data_len
);
384 ret
= l2_packet_send(hapd
->l2
, dst
, proto
, (u8
*) buf
,
385 sizeof(*buf
) + data_len
);
391 #ifdef CONFIG_IEEE80211R
393 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
394 const u8
*data
, size_t data_len
)
396 struct hostapd_data
*hapd
= ctx
;
398 struct ieee80211_mgmt
*m
;
400 struct sta_info
*sta
;
402 sta
= ap_get_sta(hapd
, dst
);
403 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
406 m
= os_zalloc(sizeof(*m
) + data_len
);
409 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
410 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
411 WLAN_FC_STYPE_ACTION
);
412 os_memcpy(m
->da
, dst
, ETH_ALEN
);
413 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
414 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
415 os_memcpy(&m
->u
, data
, data_len
);
417 res
= hostapd_drv_send_mlme(hapd
, (u8
*) m
, mlen
);
423 static struct wpa_state_machine
*
424 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
426 struct hostapd_data
*hapd
= ctx
;
427 struct sta_info
*sta
;
429 sta
= ap_sta_add(hapd
, sta_addr
);
433 sta
->auth_alg
= WLAN_AUTH_FT
;
437 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
438 if (sta
->wpa_sm
== NULL
) {
439 ap_free_sta(hapd
, sta
);
442 sta
->auth_alg
= WLAN_AUTH_FT
;
448 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
451 struct hostapd_data
*hapd
= ctx
;
452 struct l2_ethhdr
*ethhdr
;
453 if (len
< sizeof(*ethhdr
))
455 ethhdr
= (struct l2_ethhdr
*) buf
;
456 wpa_printf(MSG_DEBUG
, "FT: RRB received packet " MACSTR
" -> "
457 MACSTR
, MAC2STR(ethhdr
->h_source
), MAC2STR(ethhdr
->h_dest
));
458 wpa_ft_rrb_rx(hapd
->wpa_auth
, ethhdr
->h_source
, buf
+ sizeof(*ethhdr
),
459 len
- sizeof(*ethhdr
));
462 #endif /* CONFIG_IEEE80211R */
465 int hostapd_setup_wpa(struct hostapd_data
*hapd
)
467 struct wpa_auth_config _conf
;
468 struct wpa_auth_callbacks cb
;
472 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
473 if (hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_EAPOL_TX_STATUS
)
475 os_memset(&cb
, 0, sizeof(cb
));
477 cb
.logger
= hostapd_wpa_auth_logger
;
478 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
479 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
480 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
481 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
482 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
483 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
484 cb
.set_key
= hostapd_wpa_auth_set_key
;
485 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
486 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
487 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
488 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
489 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
490 #ifdef CONFIG_IEEE80211R
491 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
492 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
493 #endif /* CONFIG_IEEE80211R */
494 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
495 if (hapd
->wpa_auth
== NULL
) {
496 wpa_printf(MSG_ERROR
, "WPA initialization failed.");
500 if (hostapd_set_privacy(hapd
, 1)) {
501 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
502 "for interface %s", hapd
->conf
->iface
);
506 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
507 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
508 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
509 "the kernel driver.");
513 if (rsn_preauth_iface_init(hapd
)) {
514 wpa_printf(MSG_ERROR
, "Initialization of RSN "
515 "pre-authentication failed.");
519 #ifdef CONFIG_IEEE80211R
520 if (!hostapd_drv_none(hapd
)) {
521 hapd
->l2
= l2_packet_init(hapd
->conf
->bridge
[0] ?
523 hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
524 hostapd_rrb_receive
, hapd
, 1);
525 if (hapd
->l2
== NULL
&&
526 (hapd
->driver
== NULL
||
527 hapd
->driver
->send_ether
== NULL
)) {
528 wpa_printf(MSG_ERROR
, "Failed to open l2_packet "
533 #endif /* CONFIG_IEEE80211R */
540 void hostapd_reconfig_wpa(struct hostapd_data
*hapd
)
542 struct wpa_auth_config wpa_auth_conf
;
543 hostapd_wpa_auth_conf(hapd
->conf
, &wpa_auth_conf
);
544 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
548 void hostapd_deinit_wpa(struct hostapd_data
*hapd
)
550 rsn_preauth_iface_deinit(hapd
);
551 if (hapd
->wpa_auth
) {
552 wpa_deinit(hapd
->wpa_auth
);
553 hapd
->wpa_auth
= NULL
;
555 if (hostapd_set_privacy(hapd
, 0)) {
556 wpa_printf(MSG_DEBUG
, "Could not disable "
557 "PrivacyInvoked for interface %s",
561 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
562 wpa_printf(MSG_DEBUG
, "Could not remove generic "
563 "information element from interface %s",
567 ieee802_1x_deinit(hapd
);
569 #ifdef CONFIG_IEEE80211R
570 l2_packet_deinit(hapd
->l2
);
571 #endif /* CONFIG_IEEE80211R */