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
->okc
= conf
->okc
;
52 #ifdef CONFIG_IEEE80211W
53 wconf
->ieee80211w
= conf
->ieee80211w
;
54 #endif /* CONFIG_IEEE80211W */
55 #ifdef CONFIG_IEEE80211R
56 wconf
->ssid_len
= conf
->ssid
.ssid_len
;
57 if (wconf
->ssid_len
> SSID_LEN
)
58 wconf
->ssid_len
= SSID_LEN
;
59 os_memcpy(wconf
->ssid
, conf
->ssid
.ssid
, wconf
->ssid_len
);
60 os_memcpy(wconf
->mobility_domain
, conf
->mobility_domain
,
61 MOBILITY_DOMAIN_ID_LEN
);
62 if (conf
->nas_identifier
&&
63 os_strlen(conf
->nas_identifier
) <= FT_R0KH_ID_MAX_LEN
) {
64 wconf
->r0_key_holder_len
= os_strlen(conf
->nas_identifier
);
65 os_memcpy(wconf
->r0_key_holder
, conf
->nas_identifier
,
66 wconf
->r0_key_holder_len
);
68 os_memcpy(wconf
->r1_key_holder
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
69 wconf
->r0_key_lifetime
= conf
->r0_key_lifetime
;
70 wconf
->reassociation_deadline
= conf
->reassociation_deadline
;
71 wconf
->r0kh_list
= conf
->r0kh_list
;
72 wconf
->r1kh_list
= conf
->r1kh_list
;
73 wconf
->pmk_r1_push
= conf
->pmk_r1_push
;
74 wconf
->ft_over_ds
= conf
->ft_over_ds
;
75 #endif /* CONFIG_IEEE80211R */
79 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
80 logger_level level
, const char *txt
)
82 #ifndef CONFIG_NO_HOSTAPD_LOGGER
83 struct hostapd_data
*hapd
= ctx
;
88 hlevel
= HOSTAPD_LEVEL_WARNING
;
91 hlevel
= HOSTAPD_LEVEL_INFO
;
95 hlevel
= HOSTAPD_LEVEL_DEBUG
;
99 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
100 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
104 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
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
);
115 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
117 struct hostapd_data
*hapd
= ctx
;
118 michael_mic_failure(hapd
, addr
, 0);
122 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
123 wpa_eapol_variable var
, int value
)
125 struct hostapd_data
*hapd
= ctx
;
126 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
130 case WPA_EAPOL_portEnabled
:
131 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
133 case WPA_EAPOL_portValid
:
134 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
136 case WPA_EAPOL_authorized
:
137 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
139 case WPA_EAPOL_portControl_Auto
:
141 sta
->eapol_sm
->portControl
= Auto
;
143 case WPA_EAPOL_keyRun
:
145 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
147 case WPA_EAPOL_keyAvailable
:
149 sta
->eapol_sm
->eap_if
->eapKeyAvailable
=
150 value
? TRUE
: FALSE
;
152 case WPA_EAPOL_keyDone
:
154 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
156 case WPA_EAPOL_inc_EapolFramesTx
:
158 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
164 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
165 wpa_eapol_variable var
)
167 struct hostapd_data
*hapd
= ctx
;
168 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
169 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
172 case WPA_EAPOL_keyRun
:
173 return sta
->eapol_sm
->keyRun
;
174 case WPA_EAPOL_keyAvailable
:
175 return sta
->eapol_sm
->eap_if
->eapKeyAvailable
;
182 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
185 struct hostapd_data
*hapd
= ctx
;
186 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
190 static int hostapd_wpa_auth_get_msk(void *ctx
, const u8
*addr
, u8
*msk
,
193 struct hostapd_data
*hapd
= ctx
;
196 struct sta_info
*sta
;
198 sta
= ap_get_sta(hapd
, addr
);
202 key
= ieee802_1x_get_key(sta
->eapol_sm
, &keylen
);
208 os_memcpy(msk
, key
, keylen
);
215 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
216 const u8
*addr
, int idx
, u8
*key
,
219 struct hostapd_data
*hapd
= ctx
;
220 const char *ifname
= hapd
->conf
->iface
;
223 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
228 return hostapd_drv_set_key(ifname
, hapd
, alg
, addr
, idx
, 1, NULL
, 0,
233 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
236 struct hostapd_data
*hapd
= ctx
;
237 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
241 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
242 const u8
*data
, size_t data_len
,
245 struct hostapd_data
*hapd
= ctx
;
246 return hostapd_drv_hapd_send_eapol(hapd
, addr
, data
, data_len
,
251 static int hostapd_wpa_auth_for_each_sta(
252 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
255 struct hostapd_data
*hapd
= ctx
;
256 struct sta_info
*sta
;
258 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
259 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
266 struct wpa_auth_iface_iter_data
{
267 int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
);
271 static int wpa_auth_iface_iter(struct hostapd_iface
*iface
, void *ctx
)
273 struct wpa_auth_iface_iter_data
*data
= ctx
;
275 for (i
= 0; i
< iface
->num_bss
; i
++) {
276 if (iface
->bss
[i
]->wpa_auth
&&
277 data
->cb(iface
->bss
[i
]->wpa_auth
, data
->cb_ctx
))
284 static int hostapd_wpa_auth_for_each_auth(
285 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
288 struct hostapd_data
*hapd
= ctx
;
289 struct wpa_auth_iface_iter_data data
;
290 if (hapd
->iface
->for_each_interface
== NULL
)
293 data
.cb_ctx
= cb_ctx
;
294 return hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
295 wpa_auth_iface_iter
, &data
);
299 #ifdef CONFIG_IEEE80211R
301 struct wpa_auth_ft_iface_iter_data
{
302 struct hostapd_data
*src_hapd
;
309 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface
*iface
, void *ctx
)
311 struct wpa_auth_ft_iface_iter_data
*idata
= ctx
;
312 struct hostapd_data
*hapd
;
315 for (j
= 0; j
< iface
->num_bss
; j
++) {
316 hapd
= iface
->bss
[j
];
317 if (hapd
== idata
->src_hapd
)
319 if (os_memcmp(hapd
->own_addr
, idata
->dst
, ETH_ALEN
) == 0) {
320 wpa_printf(MSG_DEBUG
, "FT: Send RRB data directly to "
321 "locally managed BSS " MACSTR
"@%s -> "
323 MAC2STR(idata
->src_hapd
->own_addr
),
324 idata
->src_hapd
->conf
->iface
,
325 MAC2STR(hapd
->own_addr
), hapd
->conf
->iface
);
326 wpa_ft_rrb_rx(hapd
->wpa_auth
,
327 idata
->src_hapd
->own_addr
,
328 idata
->data
, idata
->data_len
);
336 #endif /* CONFIG_IEEE80211R */
339 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
340 const u8
*data
, size_t data_len
)
342 struct hostapd_data
*hapd
= ctx
;
343 struct l2_ethhdr
*buf
;
346 #ifdef CONFIG_IEEE80211R
347 if (proto
== ETH_P_RRB
&& hapd
->iface
->for_each_interface
) {
349 struct wpa_auth_ft_iface_iter_data idata
;
350 idata
.src_hapd
= hapd
;
353 idata
.data_len
= data_len
;
354 res
= hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
355 hostapd_wpa_auth_ft_iter
,
360 #endif /* CONFIG_IEEE80211R */
362 if (hapd
->driver
&& hapd
->driver
->send_ether
)
363 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
364 hapd
->own_addr
, proto
,
366 if (hapd
->l2
== NULL
)
369 buf
= os_malloc(sizeof(*buf
) + data_len
);
372 os_memcpy(buf
->h_dest
, dst
, ETH_ALEN
);
373 os_memcpy(buf
->h_source
, hapd
->own_addr
, ETH_ALEN
);
374 buf
->h_proto
= host_to_be16(proto
);
375 os_memcpy(buf
+ 1, data
, data_len
);
376 ret
= l2_packet_send(hapd
->l2
, dst
, proto
, (u8
*) buf
,
377 sizeof(*buf
) + data_len
);
383 #ifdef CONFIG_IEEE80211R
385 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
386 const u8
*data
, size_t data_len
)
388 struct hostapd_data
*hapd
= ctx
;
390 struct ieee80211_mgmt
*m
;
392 struct sta_info
*sta
;
394 sta
= ap_get_sta(hapd
, dst
);
395 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
398 m
= os_zalloc(sizeof(*m
) + data_len
);
401 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
402 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
403 WLAN_FC_STYPE_ACTION
);
404 os_memcpy(m
->da
, dst
, ETH_ALEN
);
405 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
406 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
407 os_memcpy(&m
->u
, data
, data_len
);
409 res
= hostapd_drv_send_mlme(hapd
, (u8
*) m
, mlen
);
415 static struct wpa_state_machine
*
416 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
418 struct hostapd_data
*hapd
= ctx
;
419 struct sta_info
*sta
;
421 sta
= ap_sta_add(hapd
, sta_addr
);
425 sta
->auth_alg
= WLAN_AUTH_FT
;
429 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
430 if (sta
->wpa_sm
== NULL
) {
431 ap_free_sta(hapd
, sta
);
434 sta
->auth_alg
= WLAN_AUTH_FT
;
440 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
443 struct hostapd_data
*hapd
= ctx
;
444 struct l2_ethhdr
*ethhdr
;
445 if (len
< sizeof(*ethhdr
))
447 ethhdr
= (struct l2_ethhdr
*) buf
;
448 wpa_printf(MSG_DEBUG
, "FT: RRB received packet " MACSTR
" -> "
449 MACSTR
, MAC2STR(ethhdr
->h_source
), MAC2STR(ethhdr
->h_dest
));
450 wpa_ft_rrb_rx(hapd
->wpa_auth
, ethhdr
->h_source
, buf
+ sizeof(*ethhdr
),
451 len
- sizeof(*ethhdr
));
454 #endif /* CONFIG_IEEE80211R */
457 int hostapd_setup_wpa(struct hostapd_data
*hapd
)
459 struct wpa_auth_config _conf
;
460 struct wpa_auth_callbacks cb
;
464 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
465 if (hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_EAPOL_TX_STATUS
)
467 os_memset(&cb
, 0, sizeof(cb
));
469 cb
.logger
= hostapd_wpa_auth_logger
;
470 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
471 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
472 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
473 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
474 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
475 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
476 cb
.set_key
= hostapd_wpa_auth_set_key
;
477 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
478 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
479 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
480 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
481 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
482 #ifdef CONFIG_IEEE80211R
483 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
484 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
485 #endif /* CONFIG_IEEE80211R */
486 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
487 if (hapd
->wpa_auth
== NULL
) {
488 wpa_printf(MSG_ERROR
, "WPA initialization failed.");
492 if (hostapd_set_privacy(hapd
, 1)) {
493 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
494 "for interface %s", hapd
->conf
->iface
);
498 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
499 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
500 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
501 "the kernel driver.");
505 if (rsn_preauth_iface_init(hapd
)) {
506 wpa_printf(MSG_ERROR
, "Initialization of RSN "
507 "pre-authentication failed.");
511 #ifdef CONFIG_IEEE80211R
512 if (!hostapd_drv_none(hapd
)) {
513 hapd
->l2
= l2_packet_init(hapd
->conf
->bridge
[0] ?
515 hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
516 hostapd_rrb_receive
, hapd
, 1);
517 if (hapd
->l2
== NULL
&&
518 (hapd
->driver
== NULL
||
519 hapd
->driver
->send_ether
== NULL
)) {
520 wpa_printf(MSG_ERROR
, "Failed to open l2_packet "
525 #endif /* CONFIG_IEEE80211R */
532 void hostapd_reconfig_wpa(struct hostapd_data
*hapd
)
534 struct wpa_auth_config wpa_auth_conf
;
535 hostapd_wpa_auth_conf(hapd
->conf
, &wpa_auth_conf
);
536 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
540 void hostapd_deinit_wpa(struct hostapd_data
*hapd
)
542 rsn_preauth_iface_deinit(hapd
);
543 if (hapd
->wpa_auth
) {
544 wpa_deinit(hapd
->wpa_auth
);
545 hapd
->wpa_auth
= NULL
;
547 if (hostapd_set_privacy(hapd
, 0)) {
548 wpa_printf(MSG_DEBUG
, "Could not disable "
549 "PrivacyInvoked for interface %s",
553 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
554 wpa_printf(MSG_DEBUG
, "Could not remove generic "
555 "information element from interface %s",
559 ieee802_1x_deinit(hapd
);
561 #ifdef CONFIG_IEEE80211R
562 l2_packet_deinit(hapd
->l2
);
563 #endif /* CONFIG_IEEE80211R */