2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "eapol_auth/eapol_auth_sm_i.h"
16 #include "eap_server/eap.h"
17 #include "l2_packet/l2_packet.h"
19 #include "ieee802_1x.h"
20 #include "preauth_auth.h"
22 #include "tkip_countermeasures.h"
23 #include "ap_drv_ops.h"
24 #include "ap_config.h"
26 #include "wpa_auth_glue.h"
29 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
30 struct hostapd_config *iconf,
31 struct wpa_auth_config *wconf)
33 os_memset(wconf, 0, sizeof(*wconf));
34 wconf->wpa = conf->wpa;
35 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
36 wconf->wpa_pairwise = conf->wpa_pairwise;
37 wconf->wpa_group = conf->wpa_group;
38 wconf->wpa_group_rekey = conf->wpa_group_rekey;
39 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
40 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
41 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
42 wconf->rsn_pairwise = conf->rsn_pairwise;
43 wconf->rsn_preauth = conf->rsn_preauth;
44 wconf->eapol_version = conf->eapol_version;
45 wconf->peerkey = conf->peerkey;
46 wconf->wmm_enabled = conf->wmm_enabled;
47 wconf->wmm_uapsd = conf->wmm_uapsd;
48 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
49 wconf->okc = conf->okc;
50 #ifdef CONFIG_IEEE80211W
51 wconf->ieee80211w = conf->ieee80211w;
52 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
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 wconf->ft_over_ds = conf->ft_over_ds;
74 #endif /* CONFIG_IEEE80211R */
76 wconf->disable_gtk = conf->disable_dgaf;
78 wconf->disable_gtk = 1;
79 wconf->wpa = WPA_PROTO_OSEN;
80 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
81 wconf->wpa_pairwise = 0;
82 wconf->wpa_group = WPA_CIPHER_CCMP;
83 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
84 wconf->rsn_preauth = 0;
85 wconf->disable_pmksa_caching = 1;
86 #ifdef CONFIG_IEEE80211W
87 wconf->ieee80211w = 1;
88 #endif /* CONFIG_IEEE80211W */
90 #endif /* CONFIG_HS20 */
91 #ifdef CONFIG_TESTING_OPTIONS
92 wconf->corrupt_gtk_rekey_mic_probability =
93 iconf->corrupt_gtk_rekey_mic_probability;
94 #endif /* CONFIG_TESTING_OPTIONS */
96 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
97 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
98 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
99 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
100 #endif /* CONFIG_P2P */
104 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
105 logger_level level, const char *txt)
107 #ifndef CONFIG_NO_HOSTAPD_LOGGER
108 struct hostapd_data *hapd = ctx;
113 hlevel = HOSTAPD_LEVEL_WARNING;
116 hlevel = HOSTAPD_LEVEL_INFO;
120 hlevel = HOSTAPD_LEVEL_DEBUG;
124 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
125 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
129 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
132 struct hostapd_data *hapd = ctx;
133 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
134 "STA " MACSTR " reason %d",
135 __func__, MAC2STR(addr), reason);
136 ap_sta_disconnect(hapd, NULL, addr, reason);
140 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
142 struct hostapd_data *hapd = ctx;
143 return michael_mic_failure(hapd, addr, 0);
147 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
148 wpa_eapol_variable var, int value)
150 struct hostapd_data *hapd = ctx;
151 struct sta_info *sta = ap_get_sta(hapd, addr);
155 case WPA_EAPOL_portEnabled:
156 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
158 case WPA_EAPOL_portValid:
159 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
161 case WPA_EAPOL_authorized:
162 ieee802_1x_set_sta_authorized(hapd, sta, value);
164 case WPA_EAPOL_portControl_Auto:
166 sta->eapol_sm->portControl = Auto;
168 case WPA_EAPOL_keyRun:
170 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
172 case WPA_EAPOL_keyAvailable:
174 sta->eapol_sm->eap_if->eapKeyAvailable =
175 value ? TRUE : FALSE;
177 case WPA_EAPOL_keyDone:
179 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
181 case WPA_EAPOL_inc_EapolFramesTx:
183 sta->eapol_sm->dot1xAuthEapolFramesTx++;
189 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
190 wpa_eapol_variable var)
192 struct hostapd_data *hapd = ctx;
193 struct sta_info *sta = ap_get_sta(hapd, addr);
194 if (sta == NULL || sta->eapol_sm == NULL)
197 case WPA_EAPOL_keyRun:
198 return sta->eapol_sm->keyRun;
199 case WPA_EAPOL_keyAvailable:
200 return sta->eapol_sm->eap_if->eapKeyAvailable;
207 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
208 const u8 *p2p_dev_addr,
211 struct hostapd_data *hapd = ctx;
212 struct sta_info *sta = ap_get_sta(hapd, addr);
216 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
217 if (!sta->sae || prev_psk)
219 return sta->sae->pmk;
221 #endif /* CONFIG_SAE */
223 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
225 * This is about to iterate over all psks, prev_psk gives the last
226 * returned psk which should not be returned again.
227 * logic list (all hostapd_get_psk; all sta->psk)
229 if (sta && sta->psk && !psk) {
230 struct hostapd_sta_wpa_psk_short *pos;
232 for (pos = sta->psk; pos; pos = pos->next) {
233 if (pos->psk == prev_psk) {
234 psk = pos->next ? pos->next->psk : NULL;
243 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
246 struct hostapd_data *hapd = ctx;
249 struct sta_info *sta;
251 sta = ap_get_sta(hapd, addr);
253 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
257 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
259 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
266 os_memcpy(msk, key, keylen);
273 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
274 const u8 *addr, int idx, u8 *key,
277 struct hostapd_data *hapd = ctx;
278 const char *ifname = hapd->conf->iface;
281 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
286 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
291 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
294 struct hostapd_data *hapd = ctx;
295 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
299 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
300 const u8 *data, size_t data_len,
303 struct hostapd_data *hapd = ctx;
304 struct sta_info *sta;
307 #ifdef CONFIG_TESTING_OPTIONS
308 if (hapd->ext_eapol_frame_io) {
309 size_t hex_len = 2 * data_len + 1;
310 char *hex = os_malloc(hex_len);
314 wpa_snprintf_hex(hex, hex_len, data, data_len);
315 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
320 #endif /* CONFIG_TESTING_OPTIONS */
322 sta = ap_get_sta(hapd, addr);
324 flags = hostapd_sta_flags_to_drv(sta->flags);
326 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
331 static int hostapd_wpa_auth_for_each_sta(
332 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
335 struct hostapd_data *hapd = ctx;
336 struct sta_info *sta;
338 for (sta = hapd->sta_list; sta; sta = sta->next) {
339 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
346 struct wpa_auth_iface_iter_data {
347 int (*cb)(struct wpa_authenticator *sm, void *ctx);
351 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
353 struct wpa_auth_iface_iter_data *data = ctx;
355 for (i = 0; i < iface->num_bss; i++) {
356 if (iface->bss[i]->wpa_auth &&
357 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
364 static int hostapd_wpa_auth_for_each_auth(
365 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
368 struct hostapd_data *hapd = ctx;
369 struct wpa_auth_iface_iter_data data;
370 if (hapd->iface->interfaces == NULL ||
371 hapd->iface->interfaces->for_each_interface == NULL)
374 data.cb_ctx = cb_ctx;
375 return hapd->iface->interfaces->for_each_interface(
376 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
380 #ifdef CONFIG_IEEE80211R
382 struct wpa_auth_ft_iface_iter_data {
383 struct hostapd_data *src_hapd;
390 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
392 struct wpa_auth_ft_iface_iter_data *idata = ctx;
393 struct hostapd_data *hapd;
396 for (j = 0; j < iface->num_bss; j++) {
397 hapd = iface->bss[j];
398 if (hapd == idata->src_hapd)
400 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
401 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
402 "locally managed BSS " MACSTR "@%s -> "
404 MAC2STR(idata->src_hapd->own_addr),
405 idata->src_hapd->conf->iface,
406 MAC2STR(hapd->own_addr), hapd->conf->iface);
407 wpa_ft_rrb_rx(hapd->wpa_auth,
408 idata->src_hapd->own_addr,
409 idata->data, idata->data_len);
417 #endif /* CONFIG_IEEE80211R */
420 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
421 const u8 *data, size_t data_len)
423 struct hostapd_data *hapd = ctx;
424 struct l2_ethhdr *buf;
427 #ifdef CONFIG_TESTING_OPTIONS
428 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
429 size_t hex_len = 2 * data_len + 1;
430 char *hex = os_malloc(hex_len);
434 wpa_snprintf_hex(hex, hex_len, data, data_len);
435 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
440 #endif /* CONFIG_TESTING_OPTIONS */
442 #ifdef CONFIG_IEEE80211R
443 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
444 hapd->iface->interfaces->for_each_interface) {
446 struct wpa_auth_ft_iface_iter_data idata;
447 idata.src_hapd = hapd;
450 idata.data_len = data_len;
451 res = hapd->iface->interfaces->for_each_interface(
452 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
457 #endif /* CONFIG_IEEE80211R */
459 if (hapd->driver && hapd->driver->send_ether)
460 return hapd->driver->send_ether(hapd->drv_priv, dst,
461 hapd->own_addr, proto,
463 if (hapd->l2 == NULL)
466 buf = os_malloc(sizeof(*buf) + data_len);
469 os_memcpy(buf->h_dest, dst, ETH_ALEN);
470 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
471 buf->h_proto = host_to_be16(proto);
472 os_memcpy(buf + 1, data, data_len);
473 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
474 sizeof(*buf) + data_len);
480 #ifdef CONFIG_IEEE80211R
482 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
483 const u8 *data, size_t data_len)
485 struct hostapd_data *hapd = ctx;
487 struct ieee80211_mgmt *m;
489 struct sta_info *sta;
491 sta = ap_get_sta(hapd, dst);
492 if (sta == NULL || sta->wpa_sm == NULL)
495 m = os_zalloc(sizeof(*m) + data_len);
498 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
499 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
500 WLAN_FC_STYPE_ACTION);
501 os_memcpy(m->da, dst, ETH_ALEN);
502 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
503 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
504 os_memcpy(&m->u, data, data_len);
506 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
512 static struct wpa_state_machine *
513 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
515 struct hostapd_data *hapd = ctx;
516 struct sta_info *sta;
518 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
521 sta = ap_sta_add(hapd, sta_addr);
525 sta->auth_alg = WLAN_AUTH_FT;
529 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
530 if (sta->wpa_sm == NULL) {
531 ap_free_sta(hapd, sta);
534 sta->auth_alg = WLAN_AUTH_FT;
540 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
543 struct hostapd_data *hapd = ctx;
544 struct l2_ethhdr *ethhdr;
545 if (len < sizeof(*ethhdr))
547 ethhdr = (struct l2_ethhdr *) buf;
548 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
549 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
550 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
551 len - sizeof(*ethhdr));
555 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
556 u8 *tspec_ie, size_t tspec_ielen)
558 struct hostapd_data *hapd = ctx;
559 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
562 #endif /* CONFIG_IEEE80211R */
565 int hostapd_setup_wpa(struct hostapd_data *hapd)
567 struct wpa_auth_config _conf;
568 struct wpa_auth_callbacks cb;
572 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
573 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
575 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
577 os_memset(&cb, 0, sizeof(cb));
579 cb.logger = hostapd_wpa_auth_logger;
580 cb.disconnect = hostapd_wpa_auth_disconnect;
581 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
582 cb.set_eapol = hostapd_wpa_auth_set_eapol;
583 cb.get_eapol = hostapd_wpa_auth_get_eapol;
584 cb.get_psk = hostapd_wpa_auth_get_psk;
585 cb.get_msk = hostapd_wpa_auth_get_msk;
586 cb.set_key = hostapd_wpa_auth_set_key;
587 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
588 cb.send_eapol = hostapd_wpa_auth_send_eapol;
589 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
590 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
591 cb.send_ether = hostapd_wpa_auth_send_ether;
592 #ifdef CONFIG_IEEE80211R
593 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
594 cb.add_sta = hostapd_wpa_auth_add_sta;
595 cb.add_tspec = hostapd_wpa_auth_add_tspec;
596 #endif /* CONFIG_IEEE80211R */
597 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
598 if (hapd->wpa_auth == NULL) {
599 wpa_printf(MSG_ERROR, "WPA initialization failed.");
603 if (hostapd_set_privacy(hapd, 1)) {
604 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
605 "for interface %s", hapd->conf->iface);
609 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
610 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
611 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
612 "the kernel driver.");
616 if (rsn_preauth_iface_init(hapd)) {
617 wpa_printf(MSG_ERROR, "Initialization of RSN "
618 "pre-authentication failed.");
622 #ifdef CONFIG_IEEE80211R
623 if (!hostapd_drv_none(hapd)) {
624 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
626 hapd->conf->iface, NULL, ETH_P_RRB,
627 hostapd_rrb_receive, hapd, 1);
628 if (hapd->l2 == NULL &&
629 (hapd->driver == NULL ||
630 hapd->driver->send_ether == NULL)) {
631 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
636 #endif /* CONFIG_IEEE80211R */
643 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
645 struct wpa_auth_config wpa_auth_conf;
646 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
647 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
651 void hostapd_deinit_wpa(struct hostapd_data *hapd)
653 ieee80211_tkip_countermeasures_deinit(hapd);
654 rsn_preauth_iface_deinit(hapd);
655 if (hapd->wpa_auth) {
656 wpa_deinit(hapd->wpa_auth);
657 hapd->wpa_auth = NULL;
659 if (hostapd_set_privacy(hapd, 0)) {
660 wpa_printf(MSG_DEBUG, "Could not disable "
661 "PrivacyInvoked for interface %s",
665 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
666 wpa_printf(MSG_DEBUG, "Could not remove generic "
667 "information element from interface %s",
671 ieee802_1x_deinit(hapd);
673 #ifdef CONFIG_IEEE80211R
674 l2_packet_deinit(hapd->l2);
676 #endif /* CONFIG_IEEE80211R */