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 os_memset(wconf
, 0, sizeof(*wconf
));
38 wconf
->wpa
= conf
->wpa
;
39 wconf
->wpa_key_mgmt
= conf
->wpa_key_mgmt
;
40 wconf
->wpa_pairwise
= conf
->wpa_pairwise
;
41 wconf
->wpa_group
= conf
->wpa_group
;
42 wconf
->wpa_group_rekey
= conf
->wpa_group_rekey
;
43 wconf
->wpa_strict_rekey
= conf
->wpa_strict_rekey
;
44 wconf
->wpa_gmk_rekey
= conf
->wpa_gmk_rekey
;
45 wconf
->wpa_ptk_rekey
= conf
->wpa_ptk_rekey
;
46 wconf
->rsn_pairwise
= conf
->rsn_pairwise
;
47 wconf
->rsn_preauth
= conf
->rsn_preauth
;
48 wconf
->eapol_version
= conf
->eapol_version
;
49 wconf
->peerkey
= conf
->peerkey
;
50 wconf
->wmm_enabled
= conf
->wmm_enabled
;
51 wconf
->wmm_uapsd
= conf
->wmm_uapsd
;
52 wconf
->disable_pmksa_caching
= conf
->disable_pmksa_caching
;
53 wconf
->okc
= conf
->okc
;
54 #ifdef CONFIG_IEEE80211W
55 wconf
->ieee80211w
= conf
->ieee80211w
;
56 #endif /* CONFIG_IEEE80211W */
57 #ifdef CONFIG_IEEE80211R
58 wconf
->ssid_len
= conf
->ssid
.ssid_len
;
59 if (wconf
->ssid_len
> SSID_LEN
)
60 wconf
->ssid_len
= SSID_LEN
;
61 os_memcpy(wconf
->ssid
, conf
->ssid
.ssid
, wconf
->ssid_len
);
62 os_memcpy(wconf
->mobility_domain
, conf
->mobility_domain
,
63 MOBILITY_DOMAIN_ID_LEN
);
64 if (conf
->nas_identifier
&&
65 os_strlen(conf
->nas_identifier
) <= FT_R0KH_ID_MAX_LEN
) {
66 wconf
->r0_key_holder_len
= os_strlen(conf
->nas_identifier
);
67 os_memcpy(wconf
->r0_key_holder
, conf
->nas_identifier
,
68 wconf
->r0_key_holder_len
);
70 os_memcpy(wconf
->r1_key_holder
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
71 wconf
->r0_key_lifetime
= conf
->r0_key_lifetime
;
72 wconf
->reassociation_deadline
= conf
->reassociation_deadline
;
73 wconf
->r0kh_list
= conf
->r0kh_list
;
74 wconf
->r1kh_list
= conf
->r1kh_list
;
75 wconf
->pmk_r1_push
= conf
->pmk_r1_push
;
76 wconf
->ft_over_ds
= conf
->ft_over_ds
;
77 #endif /* CONFIG_IEEE80211R */
81 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
82 logger_level level
, const char *txt
)
84 #ifndef CONFIG_NO_HOSTAPD_LOGGER
85 struct hostapd_data
*hapd
= ctx
;
90 hlevel
= HOSTAPD_LEVEL_WARNING
;
93 hlevel
= HOSTAPD_LEVEL_INFO
;
97 hlevel
= HOSTAPD_LEVEL_DEBUG
;
101 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
102 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
106 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
109 struct hostapd_data
*hapd
= ctx
;
110 wpa_printf(MSG_DEBUG
, "%s: WPA authenticator requests disconnect: "
111 "STA " MACSTR
" reason %d",
112 __func__
, MAC2STR(addr
), reason
);
113 ap_sta_disconnect(hapd
, NULL
, addr
, reason
);
117 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
119 struct hostapd_data
*hapd
= ctx
;
120 michael_mic_failure(hapd
, addr
, 0);
124 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
125 wpa_eapol_variable var
, int value
)
127 struct hostapd_data
*hapd
= ctx
;
128 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
132 case WPA_EAPOL_portEnabled
:
133 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
135 case WPA_EAPOL_portValid
:
136 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
138 case WPA_EAPOL_authorized
:
139 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
141 case WPA_EAPOL_portControl_Auto
:
143 sta
->eapol_sm
->portControl
= Auto
;
145 case WPA_EAPOL_keyRun
:
147 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
149 case WPA_EAPOL_keyAvailable
:
151 sta
->eapol_sm
->eap_if
->eapKeyAvailable
=
152 value
? TRUE
: FALSE
;
154 case WPA_EAPOL_keyDone
:
156 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
158 case WPA_EAPOL_inc_EapolFramesTx
:
160 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
166 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
167 wpa_eapol_variable var
)
169 struct hostapd_data
*hapd
= ctx
;
170 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
171 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
174 case WPA_EAPOL_keyRun
:
175 return sta
->eapol_sm
->keyRun
;
176 case WPA_EAPOL_keyAvailable
:
177 return sta
->eapol_sm
->eap_if
->eapKeyAvailable
;
184 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
187 struct hostapd_data
*hapd
= ctx
;
188 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
192 static int hostapd_wpa_auth_get_msk(void *ctx
, const u8
*addr
, u8
*msk
,
195 struct hostapd_data
*hapd
= ctx
;
198 struct sta_info
*sta
;
200 sta
= ap_get_sta(hapd
, addr
);
204 key
= ieee802_1x_get_key(sta
->eapol_sm
, &keylen
);
210 os_memcpy(msk
, key
, keylen
);
217 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, enum wpa_alg alg
,
218 const u8
*addr
, int idx
, u8
*key
,
221 struct hostapd_data
*hapd
= ctx
;
222 const char *ifname
= hapd
->conf
->iface
;
225 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
230 return hostapd_drv_set_key(ifname
, hapd
, alg
, addr
, idx
, 1, NULL
, 0,
235 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
238 struct hostapd_data
*hapd
= ctx
;
239 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
243 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
244 const u8
*data
, size_t data_len
,
247 struct hostapd_data
*hapd
= ctx
;
248 struct sta_info
*sta
;
251 sta
= ap_get_sta(hapd
, addr
);
253 flags
= hostapd_sta_flags_to_drv(sta
->flags
);
255 return hostapd_drv_hapd_send_eapol(hapd
, addr
, data
, data_len
,
260 static int hostapd_wpa_auth_for_each_sta(
261 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
264 struct hostapd_data
*hapd
= ctx
;
265 struct sta_info
*sta
;
267 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
268 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
275 struct wpa_auth_iface_iter_data
{
276 int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
);
280 static int wpa_auth_iface_iter(struct hostapd_iface
*iface
, void *ctx
)
282 struct wpa_auth_iface_iter_data
*data
= ctx
;
284 for (i
= 0; i
< iface
->num_bss
; i
++) {
285 if (iface
->bss
[i
]->wpa_auth
&&
286 data
->cb(iface
->bss
[i
]->wpa_auth
, data
->cb_ctx
))
293 static int hostapd_wpa_auth_for_each_auth(
294 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
297 struct hostapd_data
*hapd
= ctx
;
298 struct wpa_auth_iface_iter_data data
;
299 if (hapd
->iface
->for_each_interface
== NULL
)
302 data
.cb_ctx
= cb_ctx
;
303 return hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
304 wpa_auth_iface_iter
, &data
);
308 #ifdef CONFIG_IEEE80211R
310 struct wpa_auth_ft_iface_iter_data
{
311 struct hostapd_data
*src_hapd
;
318 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface
*iface
, void *ctx
)
320 struct wpa_auth_ft_iface_iter_data
*idata
= ctx
;
321 struct hostapd_data
*hapd
;
324 for (j
= 0; j
< iface
->num_bss
; j
++) {
325 hapd
= iface
->bss
[j
];
326 if (hapd
== idata
->src_hapd
)
328 if (os_memcmp(hapd
->own_addr
, idata
->dst
, ETH_ALEN
) == 0) {
329 wpa_printf(MSG_DEBUG
, "FT: Send RRB data directly to "
330 "locally managed BSS " MACSTR
"@%s -> "
332 MAC2STR(idata
->src_hapd
->own_addr
),
333 idata
->src_hapd
->conf
->iface
,
334 MAC2STR(hapd
->own_addr
), hapd
->conf
->iface
);
335 wpa_ft_rrb_rx(hapd
->wpa_auth
,
336 idata
->src_hapd
->own_addr
,
337 idata
->data
, idata
->data_len
);
345 #endif /* CONFIG_IEEE80211R */
348 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
349 const u8
*data
, size_t data_len
)
351 struct hostapd_data
*hapd
= ctx
;
352 struct l2_ethhdr
*buf
;
355 #ifdef CONFIG_IEEE80211R
356 if (proto
== ETH_P_RRB
&& hapd
->iface
->for_each_interface
) {
358 struct wpa_auth_ft_iface_iter_data idata
;
359 idata
.src_hapd
= hapd
;
362 idata
.data_len
= data_len
;
363 res
= hapd
->iface
->for_each_interface(hapd
->iface
->interfaces
,
364 hostapd_wpa_auth_ft_iter
,
369 #endif /* CONFIG_IEEE80211R */
371 if (hapd
->driver
&& hapd
->driver
->send_ether
)
372 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
373 hapd
->own_addr
, proto
,
375 if (hapd
->l2
== NULL
)
378 buf
= os_malloc(sizeof(*buf
) + data_len
);
381 os_memcpy(buf
->h_dest
, dst
, ETH_ALEN
);
382 os_memcpy(buf
->h_source
, hapd
->own_addr
, ETH_ALEN
);
383 buf
->h_proto
= host_to_be16(proto
);
384 os_memcpy(buf
+ 1, data
, data_len
);
385 ret
= l2_packet_send(hapd
->l2
, dst
, proto
, (u8
*) buf
,
386 sizeof(*buf
) + data_len
);
392 #ifdef CONFIG_IEEE80211R
394 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
395 const u8
*data
, size_t data_len
)
397 struct hostapd_data
*hapd
= ctx
;
399 struct ieee80211_mgmt
*m
;
401 struct sta_info
*sta
;
403 sta
= ap_get_sta(hapd
, dst
);
404 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
407 m
= os_zalloc(sizeof(*m
) + data_len
);
410 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
411 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
412 WLAN_FC_STYPE_ACTION
);
413 os_memcpy(m
->da
, dst
, ETH_ALEN
);
414 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
415 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
416 os_memcpy(&m
->u
, data
, data_len
);
418 res
= hostapd_drv_send_mlme(hapd
, (u8
*) m
, mlen
);
424 static struct wpa_state_machine
*
425 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
427 struct hostapd_data
*hapd
= ctx
;
428 struct sta_info
*sta
;
430 sta
= ap_sta_add(hapd
, sta_addr
);
434 sta
->auth_alg
= WLAN_AUTH_FT
;
438 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
439 if (sta
->wpa_sm
== NULL
) {
440 ap_free_sta(hapd
, sta
);
443 sta
->auth_alg
= WLAN_AUTH_FT
;
449 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
452 struct hostapd_data
*hapd
= ctx
;
453 struct l2_ethhdr
*ethhdr
;
454 if (len
< sizeof(*ethhdr
))
456 ethhdr
= (struct l2_ethhdr
*) buf
;
457 wpa_printf(MSG_DEBUG
, "FT: RRB received packet " MACSTR
" -> "
458 MACSTR
, MAC2STR(ethhdr
->h_source
), MAC2STR(ethhdr
->h_dest
));
459 wpa_ft_rrb_rx(hapd
->wpa_auth
, ethhdr
->h_source
, buf
+ sizeof(*ethhdr
),
460 len
- sizeof(*ethhdr
));
463 #endif /* CONFIG_IEEE80211R */
466 int hostapd_setup_wpa(struct hostapd_data
*hapd
)
468 struct wpa_auth_config _conf
;
469 struct wpa_auth_callbacks cb
;
473 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
474 if (hapd
->iface
->drv_flags
& WPA_DRIVER_FLAGS_EAPOL_TX_STATUS
)
476 os_memset(&cb
, 0, sizeof(cb
));
478 cb
.logger
= hostapd_wpa_auth_logger
;
479 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
480 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
481 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
482 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
483 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
484 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
485 cb
.set_key
= hostapd_wpa_auth_set_key
;
486 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
487 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
488 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
489 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
490 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
491 #ifdef CONFIG_IEEE80211R
492 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
493 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
494 #endif /* CONFIG_IEEE80211R */
495 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
496 if (hapd
->wpa_auth
== NULL
) {
497 wpa_printf(MSG_ERROR
, "WPA initialization failed.");
501 if (hostapd_set_privacy(hapd
, 1)) {
502 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
503 "for interface %s", hapd
->conf
->iface
);
507 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
508 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
509 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
510 "the kernel driver.");
514 if (rsn_preauth_iface_init(hapd
)) {
515 wpa_printf(MSG_ERROR
, "Initialization of RSN "
516 "pre-authentication failed.");
520 #ifdef CONFIG_IEEE80211R
521 if (!hostapd_drv_none(hapd
)) {
522 hapd
->l2
= l2_packet_init(hapd
->conf
->bridge
[0] ?
524 hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
525 hostapd_rrb_receive
, hapd
, 1);
526 if (hapd
->l2
== NULL
&&
527 (hapd
->driver
== NULL
||
528 hapd
->driver
->send_ether
== NULL
)) {
529 wpa_printf(MSG_ERROR
, "Failed to open l2_packet "
534 #endif /* CONFIG_IEEE80211R */
541 void hostapd_reconfig_wpa(struct hostapd_data
*hapd
)
543 struct wpa_auth_config wpa_auth_conf
;
544 hostapd_wpa_auth_conf(hapd
->conf
, &wpa_auth_conf
);
545 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
549 void hostapd_deinit_wpa(struct hostapd_data
*hapd
)
551 ieee80211_tkip_countermeasures_deinit(hapd
);
552 rsn_preauth_iface_deinit(hapd
);
553 if (hapd
->wpa_auth
) {
554 wpa_deinit(hapd
->wpa_auth
);
555 hapd
->wpa_auth
= NULL
;
557 if (hostapd_set_privacy(hapd
, 0)) {
558 wpa_printf(MSG_DEBUG
, "Could not disable "
559 "PrivacyInvoked for interface %s",
563 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
564 wpa_printf(MSG_DEBUG
, "Could not remove generic "
565 "information element from interface %s",
569 ieee802_1x_deinit(hapd
);
571 #ifdef CONFIG_IEEE80211R
572 l2_packet_deinit(hapd
->l2
);
573 #endif /* CONFIG_IEEE80211R */