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;
56 wconf->wmm_enabled = conf->wmm_enabled;
57 wconf->wmm_uapsd = conf->wmm_uapsd;
58 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
60 wconf->ocv = conf->ocv;
61 #endif /* CONFIG_OCV */
62 wconf->okc = conf->okc;
63 #ifdef CONFIG_IEEE80211W
64 wconf->ieee80211w = conf->ieee80211w;
65 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
66 wconf->sae_require_mfp = conf->sae_require_mfp;
67 #endif /* CONFIG_IEEE80211W */
68 #ifdef CONFIG_IEEE80211R_AP
69 wconf->ssid_len = conf->ssid.ssid_len;
70 if (wconf->ssid_len > SSID_MAX_LEN)
71 wconf->ssid_len = SSID_MAX_LEN;
72 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
73 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
74 MOBILITY_DOMAIN_ID_LEN);
75 if (conf->nas_identifier &&
76 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
77 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
78 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
79 wconf->r0_key_holder_len);
81 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
82 wconf->r0_key_lifetime = conf->r0_key_lifetime;
83 wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
84 wconf->reassociation_deadline = conf->reassociation_deadline;
85 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
86 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
87 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
88 wconf->rkh_pull_retries = conf->rkh_pull_retries;
89 wconf->r0kh_list = &conf->r0kh_list;
90 wconf->r1kh_list = &conf->r1kh_list;
91 wconf->pmk_r1_push = conf->pmk_r1_push;
92 wconf->ft_over_ds = conf->ft_over_ds;
93 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
94 #endif /* CONFIG_IEEE80211R_AP */
96 wconf->disable_gtk = conf->disable_dgaf;
98 wconf->disable_gtk = 1;
99 wconf->wpa = WPA_PROTO_OSEN;
100 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
101 wconf->wpa_pairwise = 0;
102 wconf->wpa_group = WPA_CIPHER_CCMP;
103 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
104 wconf->rsn_preauth = 0;
105 wconf->disable_pmksa_caching = 1;
106 #ifdef CONFIG_IEEE80211W
107 wconf->ieee80211w = 1;
108 #endif /* CONFIG_IEEE80211W */
110 #endif /* CONFIG_HS20 */
111 #ifdef CONFIG_TESTING_OPTIONS
112 wconf->corrupt_gtk_rekey_mic_probability =
113 iconf->corrupt_gtk_rekey_mic_probability;
114 if (conf->own_ie_override &&
115 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
116 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
117 os_memcpy(wconf->own_ie_override,
118 wpabuf_head(conf->own_ie_override),
119 wconf->own_ie_override_len);
121 #endif /* CONFIG_TESTING_OPTIONS */
123 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
124 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
125 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
126 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
127 #endif /* CONFIG_P2P */
129 wconf->fils_cache_id_set = conf->fils_cache_id_set;
130 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
132 #endif /* CONFIG_FILS */
136 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
137 logger_level level, const char *txt)
139 #ifndef CONFIG_NO_HOSTAPD_LOGGER
140 struct hostapd_data *hapd = ctx;
145 hlevel = HOSTAPD_LEVEL_WARNING;
148 hlevel = HOSTAPD_LEVEL_INFO;
152 hlevel = HOSTAPD_LEVEL_DEBUG;
156 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
157 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
161 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
164 struct hostapd_data *hapd = ctx;
165 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
166 "STA " MACSTR " reason %d",
167 __func__, MAC2STR(addr), reason);
168 ap_sta_disconnect(hapd, NULL, addr, reason);
172 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
174 struct hostapd_data *hapd = ctx;
175 return michael_mic_failure(hapd, addr, 0);
179 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
181 struct hostapd_data *hapd = ctx;
182 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
187 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
188 wpa_eapol_variable var, int value)
190 struct hostapd_data *hapd = ctx;
191 struct sta_info *sta = ap_get_sta(hapd, addr);
195 case WPA_EAPOL_portEnabled:
196 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
198 case WPA_EAPOL_portValid:
199 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
201 case WPA_EAPOL_authorized:
202 ieee802_1x_set_sta_authorized(hapd, sta, value);
204 case WPA_EAPOL_portControl_Auto:
206 sta->eapol_sm->portControl = Auto;
208 case WPA_EAPOL_keyRun:
210 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
212 case WPA_EAPOL_keyAvailable:
214 sta->eapol_sm->eap_if->eapKeyAvailable =
215 value ? TRUE : FALSE;
217 case WPA_EAPOL_keyDone:
219 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
221 case WPA_EAPOL_inc_EapolFramesTx:
223 sta->eapol_sm->dot1xAuthEapolFramesTx++;
229 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
230 wpa_eapol_variable var)
232 struct hostapd_data *hapd = ctx;
233 struct sta_info *sta = ap_get_sta(hapd, addr);
234 if (sta == NULL || sta->eapol_sm == NULL)
237 case WPA_EAPOL_keyRun:
238 return sta->eapol_sm->keyRun;
239 case WPA_EAPOL_keyAvailable:
240 return sta->eapol_sm->eap_if->eapKeyAvailable;
247 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
248 const u8 *p2p_dev_addr,
249 const u8 *prev_psk, size_t *psk_len,
252 struct hostapd_data *hapd = ctx;
253 struct sta_info *sta = ap_get_sta(hapd, addr);
262 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
263 if (!sta->sae || prev_psk)
265 return sta->sae->pmk;
267 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
268 wpa_printf(MSG_DEBUG,
269 "No PSK for STA trying to use SAE with PMKSA caching");
272 #endif /* CONFIG_SAE */
275 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
276 sta && sta->owe_pmk) {
278 *psk_len = sta->owe_pmk_len;
281 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
282 struct rsn_pmksa_cache_entry *sa;
284 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
285 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
287 *psk_len = sa->pmk_len;
291 #endif /* CONFIG_OWE */
293 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk,
296 * This is about to iterate over all psks, prev_psk gives the last
297 * returned psk which should not be returned again.
298 * logic list (all hostapd_get_psk; all sta->psk)
300 if (sta && sta->psk && !psk) {
301 struct hostapd_sta_wpa_psk_short *pos;
306 for (pos = sta->psk; pos; pos = pos->next) {
307 if (pos->is_passphrase) {
308 pbkdf2_sha1(pos->passphrase,
309 hapd->conf->ssid.ssid,
310 hapd->conf->ssid.ssid_len, 4096,
312 pos->is_passphrase = 0;
314 if (pos->psk == prev_psk) {
315 psk = pos->next ? pos->next->psk : NULL;
324 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
327 struct hostapd_data *hapd = ctx;
330 struct sta_info *sta;
332 sta = ap_get_sta(hapd, addr);
334 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
338 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
340 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
347 os_memcpy(msk, key, keylen);
354 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
355 const u8 *addr, int idx, u8 *key,
358 struct hostapd_data *hapd = ctx;
359 const char *ifname = hapd->conf->iface;
362 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
367 #ifdef CONFIG_TESTING_OPTIONS
368 if (addr && !is_broadcast_ether_addr(addr)) {
369 struct sta_info *sta;
371 sta = ap_get_sta(hapd, addr);
373 sta->last_tk_alg = alg;
374 sta->last_tk_key_idx = idx;
376 os_memcpy(sta->last_tk, key, key_len);
377 sta->last_tk_len = key_len;
379 #ifdef CONFIG_IEEE80211W
380 } else if (alg == WPA_ALG_IGTK ||
381 alg == WPA_ALG_BIP_GMAC_128 ||
382 alg == WPA_ALG_BIP_GMAC_256 ||
383 alg == WPA_ALG_BIP_CMAC_256) {
384 hapd->last_igtk_alg = alg;
385 hapd->last_igtk_key_idx = idx;
387 os_memcpy(hapd->last_igtk, key, key_len);
388 hapd->last_igtk_len = key_len;
389 #endif /* CONFIG_IEEE80211W */
391 hapd->last_gtk_alg = alg;
392 hapd->last_gtk_key_idx = idx;
394 os_memcpy(hapd->last_gtk, key, key_len);
395 hapd->last_gtk_len = key_len;
397 #endif /* CONFIG_TESTING_OPTIONS */
398 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
403 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
406 struct hostapd_data *hapd = ctx;
407 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
411 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
412 const u8 *data, size_t data_len,
415 struct hostapd_data *hapd = ctx;
416 struct sta_info *sta;
419 #ifdef CONFIG_TESTING_OPTIONS
420 if (hapd->ext_eapol_frame_io) {
421 size_t hex_len = 2 * data_len + 1;
422 char *hex = os_malloc(hex_len);
426 wpa_snprintf_hex(hex, hex_len, data, data_len);
427 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
432 #endif /* CONFIG_TESTING_OPTIONS */
434 sta = ap_get_sta(hapd, addr);
436 flags = hostapd_sta_flags_to_drv(sta->flags);
438 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
443 static int hostapd_wpa_auth_for_each_sta(
444 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
447 struct hostapd_data *hapd = ctx;
448 struct sta_info *sta;
450 for (sta = hapd->sta_list; sta; sta = sta->next) {
451 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
458 struct wpa_auth_iface_iter_data {
459 int (*cb)(struct wpa_authenticator *sm, void *ctx);
463 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
465 struct wpa_auth_iface_iter_data *data = ctx;
467 for (i = 0; i < iface->num_bss; i++) {
468 if (iface->bss[i]->wpa_auth &&
469 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
476 static int hostapd_wpa_auth_for_each_auth(
477 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
480 struct hostapd_data *hapd = ctx;
481 struct wpa_auth_iface_iter_data data;
482 if (hapd->iface->interfaces == NULL ||
483 hapd->iface->interfaces->for_each_interface == NULL)
486 data.cb_ctx = cb_ctx;
487 return hapd->iface->interfaces->for_each_interface(
488 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
492 #ifdef CONFIG_IEEE80211R_AP
494 struct wpa_ft_rrb_rx_later_data {
498 /* followed by data_len octets of data */
501 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
503 struct hostapd_data *hapd = eloop_ctx;
504 struct wpa_ft_rrb_rx_later_data *data, *n;
506 dl_list_for_each_safe(data, n, &hapd->l2_queue,
507 struct wpa_ft_rrb_rx_later_data, list) {
508 if (hapd->wpa_auth) {
509 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
510 (const u8 *) (data + 1),
513 dl_list_del(&data->list);
519 struct wpa_auth_ft_iface_iter_data {
520 struct hostapd_data *src_hapd;
527 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
529 struct wpa_auth_ft_iface_iter_data *idata = ctx;
530 struct wpa_ft_rrb_rx_later_data *data;
531 struct hostapd_data *hapd;
534 for (j = 0; j < iface->num_bss; j++) {
535 hapd = iface->bss[j];
536 if (hapd == idata->src_hapd ||
538 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
541 wpa_printf(MSG_DEBUG,
542 "FT: Send RRB data directly to locally managed BSS "
543 MACSTR "@%s -> " MACSTR "@%s",
544 MAC2STR(idata->src_hapd->own_addr),
545 idata->src_hapd->conf->iface,
546 MAC2STR(hapd->own_addr), hapd->conf->iface);
548 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
549 * when it would be triggered when reading from a socket.
551 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
552 * that is calling hapd0:recv handler from within
553 * hapd0:send directly.
555 data = os_zalloc(sizeof(*data) + idata->data_len);
559 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
560 os_memcpy(data + 1, idata->data, idata->data_len);
561 data->data_len = idata->data_len;
563 dl_list_add(&hapd->l2_queue, &data->list);
565 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
567 eloop_register_timeout(0, 0,
568 hostapd_wpa_ft_rrb_rx_later,
577 #endif /* CONFIG_IEEE80211R_AP */
580 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
581 const u8 *data, size_t data_len)
583 struct hostapd_data *hapd = ctx;
584 struct l2_ethhdr *buf;
587 #ifdef CONFIG_TESTING_OPTIONS
588 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
589 size_t hex_len = 2 * data_len + 1;
590 char *hex = os_malloc(hex_len);
594 wpa_snprintf_hex(hex, hex_len, data, data_len);
595 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
600 #endif /* CONFIG_TESTING_OPTIONS */
602 #ifdef CONFIG_IEEE80211R_AP
603 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
604 hapd->iface->interfaces->for_each_interface) {
606 struct wpa_auth_ft_iface_iter_data idata;
607 idata.src_hapd = hapd;
610 idata.data_len = data_len;
611 res = hapd->iface->interfaces->for_each_interface(
612 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
617 #endif /* CONFIG_IEEE80211R_AP */
619 if (hapd->driver && hapd->driver->send_ether)
620 return hapd->driver->send_ether(hapd->drv_priv, dst,
621 hapd->own_addr, proto,
623 if (hapd->l2 == NULL)
626 buf = os_malloc(sizeof(*buf) + data_len);
629 os_memcpy(buf->h_dest, dst, ETH_ALEN);
630 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
631 buf->h_proto = host_to_be16(proto);
632 os_memcpy(buf + 1, data, data_len);
633 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
634 sizeof(*buf) + data_len);
640 #ifdef CONFIG_ETH_P_OUI
641 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
644 switch (oui_suffix) {
645 #ifdef CONFIG_IEEE80211R_AP
646 case FT_PACKET_R0KH_R1KH_PULL:
647 return hapd->oui_pull;
648 case FT_PACKET_R0KH_R1KH_RESP:
649 return hapd->oui_resp;
650 case FT_PACKET_R0KH_R1KH_PUSH:
651 return hapd->oui_push;
652 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
653 return hapd->oui_sreq;
654 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
655 return hapd->oui_sresp;
656 #endif /* CONFIG_IEEE80211R_AP */
661 #endif /* CONFIG_ETH_P_OUI */
664 #ifdef CONFIG_IEEE80211R_AP
666 struct oui_deliver_later_data {
668 u8 src_addr[ETH_ALEN];
669 u8 dst_addr[ETH_ALEN];
672 /* followed by data_len octets of data */
675 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
677 struct hostapd_data *hapd = eloop_ctx;
678 struct oui_deliver_later_data *data, *n;
679 struct eth_p_oui_ctx *oui_ctx;
681 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
682 struct oui_deliver_later_data, list) {
683 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
684 if (hapd->wpa_auth && oui_ctx) {
685 eth_p_oui_deliver(oui_ctx, data->src_addr,
687 (const u8 *) (data + 1),
690 dl_list_del(&data->list);
696 struct wpa_auth_oui_iface_iter_data {
697 struct hostapd_data *src_hapd;
704 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
706 struct wpa_auth_oui_iface_iter_data *idata = ctx;
707 struct oui_deliver_later_data *data;
708 struct hostapd_data *hapd;
711 for (j = 0; j < iface->num_bss; j++) {
712 hapd = iface->bss[j];
713 if (hapd == idata->src_hapd)
715 if (!is_multicast_ether_addr(idata->dst_addr) &&
716 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
719 /* defer eth_p_oui_deliver until next eloop step as this is
720 * when it would be triggerd from reading from sock
722 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
723 * that is calling hapd0:recv handler from within
724 * hapd0:send directly.
726 data = os_zalloc(sizeof(*data) + idata->data_len);
730 os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
731 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
732 os_memcpy(data + 1, idata->data, idata->data_len);
733 data->data_len = idata->data_len;
734 data->oui_suffix = idata->oui_suffix;
736 dl_list_add(&hapd->l2_oui_queue, &data->list);
738 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
740 eloop_register_timeout(0, 0,
741 hostapd_oui_deliver_later,
750 #endif /* CONFIG_IEEE80211R_AP */
753 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
754 const u8 *data, size_t data_len)
756 #ifdef CONFIG_ETH_P_OUI
757 struct hostapd_data *hapd = ctx;
758 struct eth_p_oui_ctx *oui_ctx;
760 #ifdef CONFIG_IEEE80211R_AP
761 if (hapd->iface->interfaces &&
762 hapd->iface->interfaces->for_each_interface) {
763 struct wpa_auth_oui_iface_iter_data idata;
766 idata.src_hapd = hapd;
767 idata.dst_addr = dst;
769 idata.data_len = data_len;
770 idata.oui_suffix = oui_suffix;
771 res = hapd->iface->interfaces->for_each_interface(
772 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
777 #endif /* CONFIG_IEEE80211R_AP */
779 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
783 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
784 #else /* CONFIG_ETH_P_OUI */
786 #endif /* CONFIG_ETH_P_OUI */
790 static int hostapd_channel_info(void *ctx, struct wpa_channel_info *ci)
792 struct hostapd_data *hapd = ctx;
794 return hostapd_drv_channel_info(hapd, ci);
798 static int hostapd_wpa_auth_update_vlan(void *ctx, const u8 *addr, int vlan_id)
800 #ifndef CONFIG_NO_VLAN
801 struct hostapd_data *hapd = ctx;
802 struct sta_info *sta;
803 struct vlan_description vlan_desc;
805 sta = ap_get_sta(hapd, addr);
809 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
810 vlan_desc.notempty = 1;
811 vlan_desc.untagged = vlan_id;
812 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
813 wpa_printf(MSG_INFO, "Invalid VLAN ID %d in wpa_psk_file",
818 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0) {
820 "Failed to assign VLAN ID %d from wpa_psk_file to "
821 MACSTR, vlan_id, MAC2STR(sta->addr));
826 "Assigned VLAN ID %d from wpa_psk_file to " MACSTR,
827 vlan_id, MAC2STR(sta->addr));
828 if ((sta->flags & WLAN_STA_ASSOC) &&
829 ap_sta_bind_vlan(hapd, sta) < 0)
831 #endif /* CONFIG_NO_VLAN */
838 static int hostapd_get_sta_tx_params(void *ctx, const u8 *addr,
839 int ap_max_chanwidth, int ap_seg1_idx,
840 int *bandwidth, int *seg1_idx)
842 struct hostapd_data *hapd = ctx;
843 struct sta_info *sta;
845 sta = ap_get_sta(hapd, addr);
847 hostapd_wpa_auth_logger(hapd, addr, LOGGER_INFO,
848 "Failed to get STA info to validate received OCI");
852 return get_tx_parameters(sta, ap_max_chanwidth, ap_seg1_idx, bandwidth,
855 #endif /* CONFIG_OCV */
858 #ifdef CONFIG_IEEE80211R_AP
860 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
861 const u8 *data, size_t data_len)
863 struct hostapd_data *hapd = ctx;
865 struct ieee80211_mgmt *m;
867 struct sta_info *sta;
869 sta = ap_get_sta(hapd, dst);
870 if (sta == NULL || sta->wpa_sm == NULL)
873 m = os_zalloc(sizeof(*m) + data_len);
876 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
877 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
878 WLAN_FC_STYPE_ACTION);
879 os_memcpy(m->da, dst, ETH_ALEN);
880 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
881 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
882 os_memcpy(&m->u, data, data_len);
884 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
890 static struct wpa_state_machine *
891 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
893 struct hostapd_data *hapd = ctx;
894 struct sta_info *sta;
896 wpa_printf(MSG_DEBUG, "Add station entry for " MACSTR
897 " based on WPA authenticator callback",
899 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
902 sta = ap_sta_add(hapd, sta_addr);
905 if (hapd->driver && hapd->driver->add_sta_node)
906 sta->added_unassoc = 1;
909 sta->auth_alg = WLAN_AUTH_FT;
913 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
914 if (sta->wpa_sm == NULL) {
915 ap_free_sta(hapd, sta);
918 sta->auth_alg = WLAN_AUTH_FT;
924 static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
925 struct vlan_description *vlan)
927 struct hostapd_data *hapd = ctx;
928 struct sta_info *sta;
930 sta = ap_get_sta(hapd, sta_addr);
931 if (!sta || !sta->wpa_sm)
934 if (vlan->notempty &&
935 !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
936 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
938 "Invalid VLAN %d%s received from FT",
939 vlan->untagged, vlan->tagged[0] ? "+" : "");
943 if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
945 /* Configure wpa_group for GTK but ignore error due to driver not
946 * knowing this STA. */
947 ap_sta_bind_vlan(hapd, sta);
950 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
951 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
957 static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
958 struct vlan_description *vlan)
960 struct hostapd_data *hapd = ctx;
961 struct sta_info *sta;
963 sta = ap_get_sta(hapd, sta_addr);
968 *vlan = *sta->vlan_desc;
970 os_memset(vlan, 0, sizeof(*vlan));
977 hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
978 const u8 *identity, size_t identity_len)
980 struct hostapd_data *hapd = ctx;
981 struct sta_info *sta;
983 sta = ap_get_sta(hapd, sta_addr);
987 os_free(sta->identity);
988 sta->identity = NULL;
991 os_free(sta->eapol_sm->identity);
992 sta->eapol_sm->identity = NULL;
993 sta->eapol_sm->identity_len = 0;
999 /* sta->identity is NULL terminated */
1000 sta->identity = os_zalloc(identity_len + 1);
1003 os_memcpy(sta->identity, identity, identity_len);
1005 if (sta->eapol_sm) {
1006 sta->eapol_sm->identity = os_zalloc(identity_len);
1007 if (!sta->eapol_sm->identity)
1009 os_memcpy(sta->eapol_sm->identity, identity, identity_len);
1010 sta->eapol_sm->identity_len = identity_len;
1018 hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
1020 struct hostapd_data *hapd = ctx;
1021 struct sta_info *sta;
1025 sta = ap_get_sta(hapd, sta_addr);
1029 *buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
1033 if (!sta->identity) {
1038 identity = sta->identity;
1039 len = os_strlen(identity);
1040 *buf = (u8 *) identity;
1047 hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
1048 const u8 *radius_cui, size_t radius_cui_len)
1050 struct hostapd_data *hapd = ctx;
1051 struct sta_info *sta;
1053 sta = ap_get_sta(hapd, sta_addr);
1057 os_free(sta->radius_cui);
1058 sta->radius_cui = NULL;
1060 if (sta->eapol_sm) {
1061 wpabuf_free(sta->eapol_sm->radius_cui);
1062 sta->eapol_sm->radius_cui = NULL;
1068 /* sta->radius_cui is NULL terminated */
1069 sta->radius_cui = os_zalloc(radius_cui_len + 1);
1070 if (!sta->radius_cui)
1072 os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
1074 if (sta->eapol_sm) {
1075 sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
1077 if (!sta->eapol_sm->radius_cui)
1086 hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1088 struct hostapd_data *hapd = ctx;
1089 struct sta_info *sta;
1094 sta = ap_get_sta(hapd, sta_addr);
1098 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1100 len = wpabuf_len(b);
1101 *buf = wpabuf_head(b);
1105 if (!sta->radius_cui) {
1110 radius_cui = sta->radius_cui;
1111 len = os_strlen(radius_cui);
1112 *buf = (u8 *) radius_cui;
1118 static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1119 int session_timeout)
1121 struct hostapd_data *hapd = ctx;
1122 struct sta_info *sta;
1124 sta = ap_get_sta(hapd, sta_addr);
1128 if (session_timeout) {
1129 os_get_reltime(&sta->session_timeout);
1130 sta->session_timeout.sec += session_timeout;
1131 sta->session_timeout_set = 1;
1132 ap_sta_session_timeout(hapd, sta, session_timeout);
1134 sta->session_timeout_set = 0;
1135 ap_sta_no_session_timeout(hapd, sta);
1140 static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1142 struct hostapd_data *hapd = ctx;
1143 struct sta_info *sta;
1144 struct os_reltime now, remaining;
1146 sta = ap_get_sta(hapd, sta_addr);
1147 if (!sta || !sta->session_timeout_set)
1150 os_get_reltime(&now);
1151 if (os_reltime_before(&sta->session_timeout, &now)) {
1152 /* already expired, return >0 as timeout was set */
1156 os_reltime_sub(&sta->session_timeout, &now, &remaining);
1158 return (remaining.sec > 0) ? remaining.sec : 1;
1162 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1165 struct hostapd_data *hapd = ctx;
1166 struct l2_ethhdr *ethhdr;
1167 if (len < sizeof(*ethhdr))
1169 ethhdr = (struct l2_ethhdr *) buf;
1170 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1171 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
1172 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1173 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
1175 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1176 len - sizeof(*ethhdr));
1180 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1181 const u8 *dst_addr, u8 oui_suffix,
1182 const u8 *buf, size_t len)
1184 struct hostapd_data *hapd = ctx;
1186 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1187 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1188 if (!is_multicast_ether_addr(dst_addr) &&
1189 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
1191 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1196 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1197 u8 *tspec_ie, size_t tspec_ielen)
1199 struct hostapd_data *hapd = ctx;
1200 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1205 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1206 const char *ft_iface)
1208 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1209 FT_PACKET_R0KH_R1KH_PULL,
1210 hostapd_rrb_oui_receive, hapd);
1211 if (!hapd->oui_pull)
1214 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1215 FT_PACKET_R0KH_R1KH_RESP,
1216 hostapd_rrb_oui_receive, hapd);
1217 if (!hapd->oui_resp)
1220 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1221 FT_PACKET_R0KH_R1KH_PUSH,
1222 hostapd_rrb_oui_receive, hapd);
1223 if (!hapd->oui_push)
1226 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1227 FT_PACKET_R0KH_R1KH_SEQ_REQ,
1228 hostapd_rrb_oui_receive, hapd);
1229 if (!hapd->oui_sreq)
1232 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1233 FT_PACKET_R0KH_R1KH_SEQ_RESP,
1234 hostapd_rrb_oui_receive, hapd);
1235 if (!hapd->oui_sresp)
1242 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1244 eth_p_oui_unregister(hapd->oui_pull);
1245 hapd->oui_pull = NULL;
1246 eth_p_oui_unregister(hapd->oui_resp);
1247 hapd->oui_resp = NULL;
1248 eth_p_oui_unregister(hapd->oui_push);
1249 hapd->oui_push = NULL;
1250 eth_p_oui_unregister(hapd->oui_sreq);
1251 hapd->oui_sreq = NULL;
1252 eth_p_oui_unregister(hapd->oui_sresp);
1253 hapd->oui_sresp = NULL;
1255 #endif /* CONFIG_IEEE80211R_AP */
1258 int hostapd_setup_wpa(struct hostapd_data *hapd)
1260 struct wpa_auth_config _conf;
1261 static const struct wpa_auth_callbacks cb = {
1262 .logger = hostapd_wpa_auth_logger,
1263 .disconnect = hostapd_wpa_auth_disconnect,
1264 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1265 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1266 .set_eapol = hostapd_wpa_auth_set_eapol,
1267 .get_eapol = hostapd_wpa_auth_get_eapol,
1268 .get_psk = hostapd_wpa_auth_get_psk,
1269 .get_msk = hostapd_wpa_auth_get_msk,
1270 .set_key = hostapd_wpa_auth_set_key,
1271 .get_seqnum = hostapd_wpa_auth_get_seqnum,
1272 .send_eapol = hostapd_wpa_auth_send_eapol,
1273 .for_each_sta = hostapd_wpa_auth_for_each_sta,
1274 .for_each_auth = hostapd_wpa_auth_for_each_auth,
1275 .send_ether = hostapd_wpa_auth_send_ether,
1276 .send_oui = hostapd_wpa_auth_send_oui,
1277 .channel_info = hostapd_channel_info,
1278 .update_vlan = hostapd_wpa_auth_update_vlan,
1280 .get_sta_tx_params = hostapd_get_sta_tx_params,
1281 #endif /* CONFIG_OCV */
1282 #ifdef CONFIG_IEEE80211R_AP
1283 .send_ft_action = hostapd_wpa_auth_send_ft_action,
1284 .add_sta = hostapd_wpa_auth_add_sta,
1285 .add_tspec = hostapd_wpa_auth_add_tspec,
1286 .set_vlan = hostapd_wpa_auth_set_vlan,
1287 .get_vlan = hostapd_wpa_auth_get_vlan,
1288 .set_identity = hostapd_wpa_auth_set_identity,
1289 .get_identity = hostapd_wpa_auth_get_identity,
1290 .set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1291 .get_radius_cui = hostapd_wpa_auth_get_radius_cui,
1292 .set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1293 .get_session_timeout = hostapd_wpa_auth_get_session_timeout,
1294 #endif /* CONFIG_IEEE80211R_AP */
1299 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
1300 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1301 _conf.tx_status = 1;
1302 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1304 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
1305 if (hapd->wpa_auth == NULL) {
1306 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1310 if (hostapd_set_privacy(hapd, 1)) {
1311 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1312 "for interface %s", hapd->conf->iface);
1316 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1317 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1318 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1319 "the kernel driver.");
1323 if (rsn_preauth_iface_init(hapd)) {
1324 wpa_printf(MSG_ERROR, "Initialization of RSN "
1325 "pre-authentication failed.");
1329 #ifdef CONFIG_IEEE80211R_AP
1330 if (!hostapd_drv_none(hapd) &&
1331 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
1332 const char *ft_iface;
1334 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1336 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
1337 hostapd_rrb_receive, hapd, 1);
1338 if (hapd->l2 == NULL &&
1339 (hapd->driver == NULL ||
1340 hapd->driver->send_ether == NULL)) {
1341 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1346 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1347 wpa_printf(MSG_ERROR,
1348 "Failed to open ETH_P_OUI interface");
1352 #endif /* CONFIG_IEEE80211R_AP */
1359 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1361 struct wpa_auth_config wpa_auth_conf;
1362 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1363 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1367 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1369 ieee80211_tkip_countermeasures_deinit(hapd);
1370 rsn_preauth_iface_deinit(hapd);
1371 if (hapd->wpa_auth) {
1372 wpa_deinit(hapd->wpa_auth);
1373 hapd->wpa_auth = NULL;
1375 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1376 wpa_printf(MSG_DEBUG, "Could not disable "
1377 "PrivacyInvoked for interface %s",
1381 if (hapd->drv_priv &&
1382 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1383 wpa_printf(MSG_DEBUG, "Could not remove generic "
1384 "information element from interface %s",
1388 ieee802_1x_deinit(hapd);
1390 #ifdef CONFIG_IEEE80211R_AP
1391 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1392 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1393 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1394 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1395 l2_packet_deinit(hapd->l2);
1397 hostapd_wpa_unregister_ft_oui(hapd);
1398 #endif /* CONFIG_IEEE80211R_AP */