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 "utils/eloop.h"
13 #include "utils/list.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/sae.h"
16 #include "common/wpa_ctrl.h"
17 #include "crypto/sha1.h"
18 #include "eapol_auth/eapol_auth_sm.h"
19 #include "eapol_auth/eapol_auth_sm_i.h"
20 #include "eap_server/eap.h"
21 #include "l2_packet/l2_packet.h"
22 #include "eth_p_oui.h"
24 #include "ieee802_1x.h"
25 #include "preauth_auth.h"
27 #include "tkip_countermeasures.h"
28 #include "ap_drv_ops.h"
29 #include "ap_config.h"
30 #include "ieee802_11.h"
31 #include "pmksa_cache_auth.h"
33 #include "wpa_auth_glue.h"
36 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
37 struct hostapd_config *iconf,
38 struct wpa_auth_config *wconf)
40 os_memset(wconf, 0, sizeof(*wconf));
41 wconf->wpa = conf->wpa;
42 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
43 wconf->wpa_pairwise = conf->wpa_pairwise;
44 wconf->wpa_group = conf->wpa_group;
45 wconf->wpa_group_rekey = conf->wpa_group_rekey;
46 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
47 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
48 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
49 wconf->wpa_group_update_count = conf->wpa_group_update_count;
50 wconf->wpa_disable_eapol_key_retries =
51 conf->wpa_disable_eapol_key_retries;
52 wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
53 wconf->rsn_pairwise = conf->rsn_pairwise;
54 wconf->rsn_preauth = conf->rsn_preauth;
55 wconf->eapol_version = conf->eapol_version;
57 if (wconf->eapol_version > 2)
58 wconf->eapol_version = 2;
59 #endif /* CONFIG_MACSEC */
60 wconf->wmm_enabled = conf->wmm_enabled;
61 wconf->wmm_uapsd = conf->wmm_uapsd;
62 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
64 wconf->ocv = conf->ocv;
65 #endif /* CONFIG_OCV */
66 wconf->okc = conf->okc;
67 #ifdef CONFIG_IEEE80211W
68 wconf->ieee80211w = conf->ieee80211w;
69 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
70 wconf->sae_require_mfp = conf->sae_require_mfp;
71 #endif /* CONFIG_IEEE80211W */
72 #ifdef CONFIG_IEEE80211R_AP
73 wconf->ssid_len = conf->ssid.ssid_len;
74 if (wconf->ssid_len > SSID_MAX_LEN)
75 wconf->ssid_len = SSID_MAX_LEN;
76 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
77 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
78 MOBILITY_DOMAIN_ID_LEN);
79 if (conf->nas_identifier &&
80 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
81 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
82 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
83 wconf->r0_key_holder_len);
85 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
86 wconf->r0_key_lifetime = conf->r0_key_lifetime;
87 wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
88 wconf->reassociation_deadline = conf->reassociation_deadline;
89 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
90 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
91 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
92 wconf->rkh_pull_retries = conf->rkh_pull_retries;
93 wconf->r0kh_list = &conf->r0kh_list;
94 wconf->r1kh_list = &conf->r1kh_list;
95 wconf->pmk_r1_push = conf->pmk_r1_push;
96 wconf->ft_over_ds = conf->ft_over_ds;
97 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
98 #endif /* CONFIG_IEEE80211R_AP */
100 wconf->disable_gtk = conf->disable_dgaf;
102 wconf->disable_gtk = 1;
103 wconf->wpa = WPA_PROTO_OSEN;
104 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
105 wconf->wpa_pairwise = 0;
106 wconf->wpa_group = WPA_CIPHER_CCMP;
107 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
108 wconf->rsn_preauth = 0;
109 wconf->disable_pmksa_caching = 1;
110 #ifdef CONFIG_IEEE80211W
111 wconf->ieee80211w = 1;
112 #endif /* CONFIG_IEEE80211W */
114 #endif /* CONFIG_HS20 */
115 #ifdef CONFIG_TESTING_OPTIONS
116 wconf->corrupt_gtk_rekey_mic_probability =
117 iconf->corrupt_gtk_rekey_mic_probability;
118 if (conf->own_ie_override &&
119 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
120 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
121 os_memcpy(wconf->own_ie_override,
122 wpabuf_head(conf->own_ie_override),
123 wconf->own_ie_override_len);
125 #endif /* CONFIG_TESTING_OPTIONS */
127 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
128 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
129 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
130 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
131 #endif /* CONFIG_P2P */
133 wconf->fils_cache_id_set = conf->fils_cache_id_set;
134 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
136 #endif /* CONFIG_FILS */
140 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
141 logger_level level, const char *txt)
143 #ifndef CONFIG_NO_HOSTAPD_LOGGER
144 struct hostapd_data *hapd = ctx;
149 hlevel = HOSTAPD_LEVEL_WARNING;
152 hlevel = HOSTAPD_LEVEL_INFO;
156 hlevel = HOSTAPD_LEVEL_DEBUG;
160 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
161 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
165 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
168 struct hostapd_data *hapd = ctx;
169 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
170 "STA " MACSTR " reason %d",
171 __func__, MAC2STR(addr), reason);
172 ap_sta_disconnect(hapd, NULL, addr, reason);
176 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
178 struct hostapd_data *hapd = ctx;
179 return michael_mic_failure(hapd, addr, 0);
183 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
185 struct hostapd_data *hapd = ctx;
186 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
191 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
192 wpa_eapol_variable var, int value)
194 struct hostapd_data *hapd = ctx;
195 struct sta_info *sta = ap_get_sta(hapd, addr);
199 case WPA_EAPOL_portEnabled:
200 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
202 case WPA_EAPOL_portValid:
203 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
205 case WPA_EAPOL_authorized:
206 ieee802_1x_set_sta_authorized(hapd, sta, value);
208 case WPA_EAPOL_portControl_Auto:
210 sta->eapol_sm->portControl = Auto;
212 case WPA_EAPOL_keyRun:
214 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
216 case WPA_EAPOL_keyAvailable:
218 sta->eapol_sm->eap_if->eapKeyAvailable =
219 value ? TRUE : FALSE;
221 case WPA_EAPOL_keyDone:
223 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
225 case WPA_EAPOL_inc_EapolFramesTx:
227 sta->eapol_sm->dot1xAuthEapolFramesTx++;
233 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
234 wpa_eapol_variable var)
236 struct hostapd_data *hapd = ctx;
237 struct sta_info *sta = ap_get_sta(hapd, addr);
238 if (sta == NULL || sta->eapol_sm == NULL)
241 case WPA_EAPOL_keyRun:
242 return sta->eapol_sm->keyRun;
243 case WPA_EAPOL_keyAvailable:
244 return sta->eapol_sm->eap_if->eapKeyAvailable;
251 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
252 const u8 *p2p_dev_addr,
253 const u8 *prev_psk, size_t *psk_len,
256 struct hostapd_data *hapd = ctx;
257 struct sta_info *sta = ap_get_sta(hapd, addr);
266 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
267 if (!sta->sae || prev_psk)
269 return sta->sae->pmk;
271 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
272 wpa_printf(MSG_DEBUG,
273 "No PSK for STA trying to use SAE with PMKSA caching");
276 #endif /* CONFIG_SAE */
279 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
280 sta && sta->owe_pmk) {
282 *psk_len = sta->owe_pmk_len;
285 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
286 struct rsn_pmksa_cache_entry *sa;
288 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
289 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
291 *psk_len = sa->pmk_len;
295 #endif /* CONFIG_OWE */
297 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk,
300 * This is about to iterate over all psks, prev_psk gives the last
301 * returned psk which should not be returned again.
302 * logic list (all hostapd_get_psk; all sta->psk)
304 if (sta && sta->psk && !psk) {
305 struct hostapd_sta_wpa_psk_short *pos;
310 for (pos = sta->psk; pos; pos = pos->next) {
311 if (pos->is_passphrase) {
312 pbkdf2_sha1(pos->passphrase,
313 hapd->conf->ssid.ssid,
314 hapd->conf->ssid.ssid_len, 4096,
316 pos->is_passphrase = 0;
318 if (pos->psk == prev_psk) {
319 psk = pos->next ? pos->next->psk : NULL;
328 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
331 struct hostapd_data *hapd = ctx;
334 struct sta_info *sta;
336 sta = ap_get_sta(hapd, addr);
338 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
342 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
344 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
351 os_memcpy(msk, key, keylen);
358 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
359 const u8 *addr, int idx, u8 *key,
362 struct hostapd_data *hapd = ctx;
363 const char *ifname = hapd->conf->iface;
366 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
371 #ifdef CONFIG_TESTING_OPTIONS
372 if (addr && !is_broadcast_ether_addr(addr)) {
373 struct sta_info *sta;
375 sta = ap_get_sta(hapd, addr);
377 sta->last_tk_alg = alg;
378 sta->last_tk_key_idx = idx;
380 os_memcpy(sta->last_tk, key, key_len);
381 sta->last_tk_len = key_len;
383 #ifdef CONFIG_IEEE80211W
384 } else if (alg == WPA_ALG_IGTK ||
385 alg == WPA_ALG_BIP_GMAC_128 ||
386 alg == WPA_ALG_BIP_GMAC_256 ||
387 alg == WPA_ALG_BIP_CMAC_256) {
388 hapd->last_igtk_alg = alg;
389 hapd->last_igtk_key_idx = idx;
391 os_memcpy(hapd->last_igtk, key, key_len);
392 hapd->last_igtk_len = key_len;
393 #endif /* CONFIG_IEEE80211W */
395 hapd->last_gtk_alg = alg;
396 hapd->last_gtk_key_idx = idx;
398 os_memcpy(hapd->last_gtk, key, key_len);
399 hapd->last_gtk_len = key_len;
401 #endif /* CONFIG_TESTING_OPTIONS */
402 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
407 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
410 struct hostapd_data *hapd = ctx;
411 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
415 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
416 const u8 *data, size_t data_len,
419 struct hostapd_data *hapd = ctx;
420 struct sta_info *sta;
423 #ifdef CONFIG_TESTING_OPTIONS
424 if (hapd->ext_eapol_frame_io) {
425 size_t hex_len = 2 * data_len + 1;
426 char *hex = os_malloc(hex_len);
430 wpa_snprintf_hex(hex, hex_len, data, data_len);
431 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
436 #endif /* CONFIG_TESTING_OPTIONS */
438 sta = ap_get_sta(hapd, addr);
440 flags = hostapd_sta_flags_to_drv(sta->flags);
442 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
447 static int hostapd_wpa_auth_for_each_sta(
448 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
451 struct hostapd_data *hapd = ctx;
452 struct sta_info *sta;
454 for (sta = hapd->sta_list; sta; sta = sta->next) {
455 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
462 struct wpa_auth_iface_iter_data {
463 int (*cb)(struct wpa_authenticator *sm, void *ctx);
467 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
469 struct wpa_auth_iface_iter_data *data = ctx;
471 for (i = 0; i < iface->num_bss; i++) {
472 if (iface->bss[i]->wpa_auth &&
473 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
480 static int hostapd_wpa_auth_for_each_auth(
481 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
484 struct hostapd_data *hapd = ctx;
485 struct wpa_auth_iface_iter_data data;
486 if (hapd->iface->interfaces == NULL ||
487 hapd->iface->interfaces->for_each_interface == NULL)
490 data.cb_ctx = cb_ctx;
491 return hapd->iface->interfaces->for_each_interface(
492 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
496 #ifdef CONFIG_IEEE80211R_AP
498 struct wpa_ft_rrb_rx_later_data {
502 /* followed by data_len octets of data */
505 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
507 struct hostapd_data *hapd = eloop_ctx;
508 struct wpa_ft_rrb_rx_later_data *data, *n;
510 dl_list_for_each_safe(data, n, &hapd->l2_queue,
511 struct wpa_ft_rrb_rx_later_data, list) {
512 if (hapd->wpa_auth) {
513 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
514 (const u8 *) (data + 1),
517 dl_list_del(&data->list);
523 struct wpa_auth_ft_iface_iter_data {
524 struct hostapd_data *src_hapd;
531 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
533 struct wpa_auth_ft_iface_iter_data *idata = ctx;
534 struct wpa_ft_rrb_rx_later_data *data;
535 struct hostapd_data *hapd;
538 for (j = 0; j < iface->num_bss; j++) {
539 hapd = iface->bss[j];
540 if (hapd == idata->src_hapd ||
542 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
545 wpa_printf(MSG_DEBUG,
546 "FT: Send RRB data directly to locally managed BSS "
547 MACSTR "@%s -> " MACSTR "@%s",
548 MAC2STR(idata->src_hapd->own_addr),
549 idata->src_hapd->conf->iface,
550 MAC2STR(hapd->own_addr), hapd->conf->iface);
552 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
553 * when it would be triggered when reading from a socket.
555 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
556 * that is calling hapd0:recv handler from within
557 * hapd0:send directly.
559 data = os_zalloc(sizeof(*data) + idata->data_len);
563 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
564 os_memcpy(data + 1, idata->data, idata->data_len);
565 data->data_len = idata->data_len;
567 dl_list_add(&hapd->l2_queue, &data->list);
569 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
571 eloop_register_timeout(0, 0,
572 hostapd_wpa_ft_rrb_rx_later,
581 #endif /* CONFIG_IEEE80211R_AP */
584 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
585 const u8 *data, size_t data_len)
587 struct hostapd_data *hapd = ctx;
588 struct l2_ethhdr *buf;
591 #ifdef CONFIG_TESTING_OPTIONS
592 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
593 size_t hex_len = 2 * data_len + 1;
594 char *hex = os_malloc(hex_len);
598 wpa_snprintf_hex(hex, hex_len, data, data_len);
599 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
604 #endif /* CONFIG_TESTING_OPTIONS */
606 #ifdef CONFIG_IEEE80211R_AP
607 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
608 hapd->iface->interfaces->for_each_interface) {
610 struct wpa_auth_ft_iface_iter_data idata;
611 idata.src_hapd = hapd;
614 idata.data_len = data_len;
615 res = hapd->iface->interfaces->for_each_interface(
616 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
621 #endif /* CONFIG_IEEE80211R_AP */
623 if (hapd->driver && hapd->driver->send_ether)
624 return hapd->driver->send_ether(hapd->drv_priv, dst,
625 hapd->own_addr, proto,
627 if (hapd->l2 == NULL)
630 buf = os_malloc(sizeof(*buf) + data_len);
633 os_memcpy(buf->h_dest, dst, ETH_ALEN);
634 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
635 buf->h_proto = host_to_be16(proto);
636 os_memcpy(buf + 1, data, data_len);
637 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
638 sizeof(*buf) + data_len);
644 #ifdef CONFIG_ETH_P_OUI
645 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
648 switch (oui_suffix) {
649 #ifdef CONFIG_IEEE80211R_AP
650 case FT_PACKET_R0KH_R1KH_PULL:
651 return hapd->oui_pull;
652 case FT_PACKET_R0KH_R1KH_RESP:
653 return hapd->oui_resp;
654 case FT_PACKET_R0KH_R1KH_PUSH:
655 return hapd->oui_push;
656 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
657 return hapd->oui_sreq;
658 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
659 return hapd->oui_sresp;
660 #endif /* CONFIG_IEEE80211R_AP */
665 #endif /* CONFIG_ETH_P_OUI */
668 #ifdef CONFIG_IEEE80211R_AP
670 struct oui_deliver_later_data {
672 u8 src_addr[ETH_ALEN];
673 u8 dst_addr[ETH_ALEN];
676 /* followed by data_len octets of data */
679 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
681 struct hostapd_data *hapd = eloop_ctx;
682 struct oui_deliver_later_data *data, *n;
683 struct eth_p_oui_ctx *oui_ctx;
685 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
686 struct oui_deliver_later_data, list) {
687 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
688 if (hapd->wpa_auth && oui_ctx) {
689 eth_p_oui_deliver(oui_ctx, data->src_addr,
691 (const u8 *) (data + 1),
694 dl_list_del(&data->list);
700 struct wpa_auth_oui_iface_iter_data {
701 struct hostapd_data *src_hapd;
708 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
710 struct wpa_auth_oui_iface_iter_data *idata = ctx;
711 struct oui_deliver_later_data *data;
712 struct hostapd_data *hapd;
715 for (j = 0; j < iface->num_bss; j++) {
716 hapd = iface->bss[j];
717 if (hapd == idata->src_hapd)
719 if (!is_multicast_ether_addr(idata->dst_addr) &&
720 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
723 /* defer eth_p_oui_deliver until next eloop step as this is
724 * when it would be triggerd from reading from sock
726 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
727 * that is calling hapd0:recv handler from within
728 * hapd0:send directly.
730 data = os_zalloc(sizeof(*data) + idata->data_len);
734 os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
735 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
736 os_memcpy(data + 1, idata->data, idata->data_len);
737 data->data_len = idata->data_len;
738 data->oui_suffix = idata->oui_suffix;
740 dl_list_add(&hapd->l2_oui_queue, &data->list);
742 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
744 eloop_register_timeout(0, 0,
745 hostapd_oui_deliver_later,
754 #endif /* CONFIG_IEEE80211R_AP */
757 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
758 const u8 *data, size_t data_len)
760 #ifdef CONFIG_ETH_P_OUI
761 struct hostapd_data *hapd = ctx;
762 struct eth_p_oui_ctx *oui_ctx;
764 #ifdef CONFIG_IEEE80211R_AP
765 if (hapd->iface->interfaces &&
766 hapd->iface->interfaces->for_each_interface) {
767 struct wpa_auth_oui_iface_iter_data idata;
770 idata.src_hapd = hapd;
771 idata.dst_addr = dst;
773 idata.data_len = data_len;
774 idata.oui_suffix = oui_suffix;
775 res = hapd->iface->interfaces->for_each_interface(
776 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
781 #endif /* CONFIG_IEEE80211R_AP */
783 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
787 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
788 #else /* CONFIG_ETH_P_OUI */
790 #endif /* CONFIG_ETH_P_OUI */
794 static int hostapd_channel_info(void *ctx, struct wpa_channel_info *ci)
796 struct hostapd_data *hapd = ctx;
798 return hostapd_drv_channel_info(hapd, ci);
802 static int hostapd_wpa_auth_update_vlan(void *ctx, const u8 *addr, int vlan_id)
804 #ifndef CONFIG_NO_VLAN
805 struct hostapd_data *hapd = ctx;
806 struct sta_info *sta;
807 struct vlan_description vlan_desc;
809 sta = ap_get_sta(hapd, addr);
813 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
814 vlan_desc.notempty = 1;
815 vlan_desc.untagged = vlan_id;
816 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
817 wpa_printf(MSG_INFO, "Invalid VLAN ID %d in wpa_psk_file",
822 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0) {
824 "Failed to assign VLAN ID %d from wpa_psk_file to "
825 MACSTR, vlan_id, MAC2STR(sta->addr));
830 "Assigned VLAN ID %d from wpa_psk_file to " MACSTR,
831 vlan_id, MAC2STR(sta->addr));
832 if ((sta->flags & WLAN_STA_ASSOC) &&
833 ap_sta_bind_vlan(hapd, sta) < 0)
835 #endif /* CONFIG_NO_VLAN */
842 static int hostapd_get_sta_tx_params(void *ctx, const u8 *addr,
843 int ap_max_chanwidth, int ap_seg1_idx,
844 int *bandwidth, int *seg1_idx)
846 struct hostapd_data *hapd = ctx;
847 struct sta_info *sta;
849 sta = ap_get_sta(hapd, addr);
851 hostapd_wpa_auth_logger(hapd, addr, LOGGER_INFO,
852 "Failed to get STA info to validate received OCI");
856 return get_tx_parameters(sta, ap_max_chanwidth, ap_seg1_idx, bandwidth,
859 #endif /* CONFIG_OCV */
862 #ifdef CONFIG_IEEE80211R_AP
864 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
865 const u8 *data, size_t data_len)
867 struct hostapd_data *hapd = ctx;
869 struct ieee80211_mgmt *m;
871 struct sta_info *sta;
873 sta = ap_get_sta(hapd, dst);
874 if (sta == NULL || sta->wpa_sm == NULL)
877 m = os_zalloc(sizeof(*m) + data_len);
880 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
881 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
882 WLAN_FC_STYPE_ACTION);
883 os_memcpy(m->da, dst, ETH_ALEN);
884 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
885 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
886 os_memcpy(&m->u, data, data_len);
888 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
894 static struct wpa_state_machine *
895 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
897 struct hostapd_data *hapd = ctx;
898 struct sta_info *sta;
900 wpa_printf(MSG_DEBUG, "Add station entry for " MACSTR
901 " based on WPA authenticator callback",
903 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
906 sta = ap_sta_add(hapd, sta_addr);
909 if (hapd->driver && hapd->driver->add_sta_node)
910 sta->added_unassoc = 1;
913 sta->auth_alg = WLAN_AUTH_FT;
917 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
918 if (sta->wpa_sm == NULL) {
919 ap_free_sta(hapd, sta);
922 sta->auth_alg = WLAN_AUTH_FT;
928 static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
929 struct vlan_description *vlan)
931 struct hostapd_data *hapd = ctx;
932 struct sta_info *sta;
934 sta = ap_get_sta(hapd, sta_addr);
935 if (!sta || !sta->wpa_sm)
938 if (vlan->notempty &&
939 !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
940 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
942 "Invalid VLAN %d%s received from FT",
943 vlan->untagged, vlan->tagged[0] ? "+" : "");
947 if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
949 /* Configure wpa_group for GTK but ignore error due to driver not
950 * knowing this STA. */
951 ap_sta_bind_vlan(hapd, sta);
954 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
955 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
961 static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
962 struct vlan_description *vlan)
964 struct hostapd_data *hapd = ctx;
965 struct sta_info *sta;
967 sta = ap_get_sta(hapd, sta_addr);
972 *vlan = *sta->vlan_desc;
974 os_memset(vlan, 0, sizeof(*vlan));
981 hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
982 const u8 *identity, size_t identity_len)
984 struct hostapd_data *hapd = ctx;
985 struct sta_info *sta;
987 sta = ap_get_sta(hapd, sta_addr);
991 os_free(sta->identity);
992 sta->identity = NULL;
995 os_free(sta->eapol_sm->identity);
996 sta->eapol_sm->identity = NULL;
997 sta->eapol_sm->identity_len = 0;
1003 /* sta->identity is NULL terminated */
1004 sta->identity = os_zalloc(identity_len + 1);
1007 os_memcpy(sta->identity, identity, identity_len);
1009 if (sta->eapol_sm) {
1010 sta->eapol_sm->identity = os_zalloc(identity_len);
1011 if (!sta->eapol_sm->identity)
1013 os_memcpy(sta->eapol_sm->identity, identity, identity_len);
1014 sta->eapol_sm->identity_len = identity_len;
1022 hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
1024 struct hostapd_data *hapd = ctx;
1025 struct sta_info *sta;
1029 sta = ap_get_sta(hapd, sta_addr);
1033 *buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
1037 if (!sta->identity) {
1042 identity = sta->identity;
1043 len = os_strlen(identity);
1044 *buf = (u8 *) identity;
1051 hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
1052 const u8 *radius_cui, size_t radius_cui_len)
1054 struct hostapd_data *hapd = ctx;
1055 struct sta_info *sta;
1057 sta = ap_get_sta(hapd, sta_addr);
1061 os_free(sta->radius_cui);
1062 sta->radius_cui = NULL;
1064 if (sta->eapol_sm) {
1065 wpabuf_free(sta->eapol_sm->radius_cui);
1066 sta->eapol_sm->radius_cui = NULL;
1072 /* sta->radius_cui is NULL terminated */
1073 sta->radius_cui = os_zalloc(radius_cui_len + 1);
1074 if (!sta->radius_cui)
1076 os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
1078 if (sta->eapol_sm) {
1079 sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
1081 if (!sta->eapol_sm->radius_cui)
1090 hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1092 struct hostapd_data *hapd = ctx;
1093 struct sta_info *sta;
1098 sta = ap_get_sta(hapd, sta_addr);
1102 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1104 len = wpabuf_len(b);
1105 *buf = wpabuf_head(b);
1109 if (!sta->radius_cui) {
1114 radius_cui = sta->radius_cui;
1115 len = os_strlen(radius_cui);
1116 *buf = (u8 *) radius_cui;
1122 static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1123 int session_timeout)
1125 struct hostapd_data *hapd = ctx;
1126 struct sta_info *sta;
1128 sta = ap_get_sta(hapd, sta_addr);
1132 if (session_timeout) {
1133 os_get_reltime(&sta->session_timeout);
1134 sta->session_timeout.sec += session_timeout;
1135 sta->session_timeout_set = 1;
1136 ap_sta_session_timeout(hapd, sta, session_timeout);
1138 sta->session_timeout_set = 0;
1139 ap_sta_no_session_timeout(hapd, sta);
1144 static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1146 struct hostapd_data *hapd = ctx;
1147 struct sta_info *sta;
1148 struct os_reltime now, remaining;
1150 sta = ap_get_sta(hapd, sta_addr);
1151 if (!sta || !sta->session_timeout_set)
1154 os_get_reltime(&now);
1155 if (os_reltime_before(&sta->session_timeout, &now)) {
1156 /* already expired, return >0 as timeout was set */
1160 os_reltime_sub(&sta->session_timeout, &now, &remaining);
1162 return (remaining.sec > 0) ? remaining.sec : 1;
1166 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1169 struct hostapd_data *hapd = ctx;
1170 struct l2_ethhdr *ethhdr;
1171 if (len < sizeof(*ethhdr))
1173 ethhdr = (struct l2_ethhdr *) buf;
1174 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1175 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
1176 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1177 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
1179 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1180 len - sizeof(*ethhdr));
1184 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1185 const u8 *dst_addr, u8 oui_suffix,
1186 const u8 *buf, size_t len)
1188 struct hostapd_data *hapd = ctx;
1190 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1191 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1192 if (!is_multicast_ether_addr(dst_addr) &&
1193 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
1195 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1200 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1201 u8 *tspec_ie, size_t tspec_ielen)
1203 struct hostapd_data *hapd = ctx;
1204 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1209 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1210 const char *ft_iface)
1212 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1213 FT_PACKET_R0KH_R1KH_PULL,
1214 hostapd_rrb_oui_receive, hapd);
1215 if (!hapd->oui_pull)
1218 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1219 FT_PACKET_R0KH_R1KH_RESP,
1220 hostapd_rrb_oui_receive, hapd);
1221 if (!hapd->oui_resp)
1224 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1225 FT_PACKET_R0KH_R1KH_PUSH,
1226 hostapd_rrb_oui_receive, hapd);
1227 if (!hapd->oui_push)
1230 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1231 FT_PACKET_R0KH_R1KH_SEQ_REQ,
1232 hostapd_rrb_oui_receive, hapd);
1233 if (!hapd->oui_sreq)
1236 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1237 FT_PACKET_R0KH_R1KH_SEQ_RESP,
1238 hostapd_rrb_oui_receive, hapd);
1239 if (!hapd->oui_sresp)
1246 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1248 eth_p_oui_unregister(hapd->oui_pull);
1249 hapd->oui_pull = NULL;
1250 eth_p_oui_unregister(hapd->oui_resp);
1251 hapd->oui_resp = NULL;
1252 eth_p_oui_unregister(hapd->oui_push);
1253 hapd->oui_push = NULL;
1254 eth_p_oui_unregister(hapd->oui_sreq);
1255 hapd->oui_sreq = NULL;
1256 eth_p_oui_unregister(hapd->oui_sresp);
1257 hapd->oui_sresp = NULL;
1259 #endif /* CONFIG_IEEE80211R_AP */
1262 int hostapd_setup_wpa(struct hostapd_data *hapd)
1264 struct wpa_auth_config _conf;
1265 static const struct wpa_auth_callbacks cb = {
1266 .logger = hostapd_wpa_auth_logger,
1267 .disconnect = hostapd_wpa_auth_disconnect,
1268 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1269 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1270 .set_eapol = hostapd_wpa_auth_set_eapol,
1271 .get_eapol = hostapd_wpa_auth_get_eapol,
1272 .get_psk = hostapd_wpa_auth_get_psk,
1273 .get_msk = hostapd_wpa_auth_get_msk,
1274 .set_key = hostapd_wpa_auth_set_key,
1275 .get_seqnum = hostapd_wpa_auth_get_seqnum,
1276 .send_eapol = hostapd_wpa_auth_send_eapol,
1277 .for_each_sta = hostapd_wpa_auth_for_each_sta,
1278 .for_each_auth = hostapd_wpa_auth_for_each_auth,
1279 .send_ether = hostapd_wpa_auth_send_ether,
1280 .send_oui = hostapd_wpa_auth_send_oui,
1281 .channel_info = hostapd_channel_info,
1282 .update_vlan = hostapd_wpa_auth_update_vlan,
1284 .get_sta_tx_params = hostapd_get_sta_tx_params,
1285 #endif /* CONFIG_OCV */
1286 #ifdef CONFIG_IEEE80211R_AP
1287 .send_ft_action = hostapd_wpa_auth_send_ft_action,
1288 .add_sta = hostapd_wpa_auth_add_sta,
1289 .add_tspec = hostapd_wpa_auth_add_tspec,
1290 .set_vlan = hostapd_wpa_auth_set_vlan,
1291 .get_vlan = hostapd_wpa_auth_get_vlan,
1292 .set_identity = hostapd_wpa_auth_set_identity,
1293 .get_identity = hostapd_wpa_auth_get_identity,
1294 .set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1295 .get_radius_cui = hostapd_wpa_auth_get_radius_cui,
1296 .set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1297 .get_session_timeout = hostapd_wpa_auth_get_session_timeout,
1298 #endif /* CONFIG_IEEE80211R_AP */
1303 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
1304 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1305 _conf.tx_status = 1;
1306 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1308 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
1309 if (hapd->wpa_auth == NULL) {
1310 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1314 if (hostapd_set_privacy(hapd, 1)) {
1315 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1316 "for interface %s", hapd->conf->iface);
1320 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1321 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1322 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1323 "the kernel driver.");
1327 if (rsn_preauth_iface_init(hapd)) {
1328 wpa_printf(MSG_ERROR, "Initialization of RSN "
1329 "pre-authentication failed.");
1333 #ifdef CONFIG_IEEE80211R_AP
1334 if (!hostapd_drv_none(hapd) &&
1335 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
1336 const char *ft_iface;
1338 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1340 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
1341 hostapd_rrb_receive, hapd, 1);
1342 if (hapd->l2 == NULL &&
1343 (hapd->driver == NULL ||
1344 hapd->driver->send_ether == NULL)) {
1345 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1350 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1351 wpa_printf(MSG_ERROR,
1352 "Failed to open ETH_P_OUI interface");
1356 #endif /* CONFIG_IEEE80211R_AP */
1363 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1365 struct wpa_auth_config wpa_auth_conf;
1366 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1367 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1371 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1373 ieee80211_tkip_countermeasures_deinit(hapd);
1374 rsn_preauth_iface_deinit(hapd);
1375 if (hapd->wpa_auth) {
1376 wpa_deinit(hapd->wpa_auth);
1377 hapd->wpa_auth = NULL;
1379 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1380 wpa_printf(MSG_DEBUG, "Could not disable "
1381 "PrivacyInvoked for interface %s",
1385 if (hapd->drv_priv &&
1386 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1387 wpa_printf(MSG_DEBUG, "Could not remove generic "
1388 "information element from interface %s",
1392 ieee802_1x_deinit(hapd);
1394 #ifdef CONFIG_IEEE80211R_AP
1395 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1396 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1397 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1398 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1399 l2_packet_deinit(hapd->l2);
1401 hostapd_wpa_unregister_ft_oui(hapd);
1402 #endif /* CONFIG_IEEE80211R_AP */