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 "pmksa_cache_auth.h"
32 #include "wpa_auth_glue.h"
35 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
36 struct hostapd_config *iconf,
37 struct wpa_auth_config *wconf)
39 os_memset(wconf, 0, sizeof(*wconf));
40 wconf->wpa = conf->wpa;
41 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
42 wconf->wpa_pairwise = conf->wpa_pairwise;
43 wconf->wpa_group = conf->wpa_group;
44 wconf->wpa_group_rekey = conf->wpa_group_rekey;
45 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
46 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
47 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
48 wconf->wpa_group_update_count = conf->wpa_group_update_count;
49 wconf->wpa_disable_eapol_key_retries =
50 conf->wpa_disable_eapol_key_retries;
51 wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
52 wconf->rsn_pairwise = conf->rsn_pairwise;
53 wconf->rsn_preauth = conf->rsn_preauth;
54 wconf->eapol_version = conf->eapol_version;
55 wconf->wmm_enabled = conf->wmm_enabled;
56 wconf->wmm_uapsd = conf->wmm_uapsd;
57 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
58 wconf->okc = conf->okc;
59 #ifdef CONFIG_IEEE80211W
60 wconf->ieee80211w = conf->ieee80211w;
61 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
62 wconf->sae_require_mfp = conf->sae_require_mfp;
63 #endif /* CONFIG_IEEE80211W */
64 #ifdef CONFIG_IEEE80211R_AP
65 wconf->ssid_len = conf->ssid.ssid_len;
66 if (wconf->ssid_len > SSID_MAX_LEN)
67 wconf->ssid_len = SSID_MAX_LEN;
68 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
69 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
70 MOBILITY_DOMAIN_ID_LEN);
71 if (conf->nas_identifier &&
72 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
73 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
74 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
75 wconf->r0_key_holder_len);
77 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
78 wconf->r0_key_lifetime = conf->r0_key_lifetime;
79 wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
80 wconf->reassociation_deadline = conf->reassociation_deadline;
81 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
82 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
83 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
84 wconf->rkh_pull_retries = conf->rkh_pull_retries;
85 wconf->r0kh_list = &conf->r0kh_list;
86 wconf->r1kh_list = &conf->r1kh_list;
87 wconf->pmk_r1_push = conf->pmk_r1_push;
88 wconf->ft_over_ds = conf->ft_over_ds;
89 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
90 #endif /* CONFIG_IEEE80211R_AP */
92 wconf->disable_gtk = conf->disable_dgaf;
94 wconf->disable_gtk = 1;
95 wconf->wpa = WPA_PROTO_OSEN;
96 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
97 wconf->wpa_pairwise = 0;
98 wconf->wpa_group = WPA_CIPHER_CCMP;
99 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
100 wconf->rsn_preauth = 0;
101 wconf->disable_pmksa_caching = 1;
102 #ifdef CONFIG_IEEE80211W
103 wconf->ieee80211w = 1;
104 #endif /* CONFIG_IEEE80211W */
106 #endif /* CONFIG_HS20 */
107 #ifdef CONFIG_TESTING_OPTIONS
108 wconf->corrupt_gtk_rekey_mic_probability =
109 iconf->corrupt_gtk_rekey_mic_probability;
110 if (conf->own_ie_override &&
111 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
112 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
113 os_memcpy(wconf->own_ie_override,
114 wpabuf_head(conf->own_ie_override),
115 wconf->own_ie_override_len);
117 #endif /* CONFIG_TESTING_OPTIONS */
119 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
120 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
121 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
122 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
123 #endif /* CONFIG_P2P */
125 wconf->fils_cache_id_set = conf->fils_cache_id_set;
126 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
128 #endif /* CONFIG_FILS */
132 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
133 logger_level level, const char *txt)
135 #ifndef CONFIG_NO_HOSTAPD_LOGGER
136 struct hostapd_data *hapd = ctx;
141 hlevel = HOSTAPD_LEVEL_WARNING;
144 hlevel = HOSTAPD_LEVEL_INFO;
148 hlevel = HOSTAPD_LEVEL_DEBUG;
152 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
153 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
157 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
160 struct hostapd_data *hapd = ctx;
161 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
162 "STA " MACSTR " reason %d",
163 __func__, MAC2STR(addr), reason);
164 ap_sta_disconnect(hapd, NULL, addr, reason);
168 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
170 struct hostapd_data *hapd = ctx;
171 return michael_mic_failure(hapd, addr, 0);
175 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
177 struct hostapd_data *hapd = ctx;
178 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
183 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
184 wpa_eapol_variable var, int value)
186 struct hostapd_data *hapd = ctx;
187 struct sta_info *sta = ap_get_sta(hapd, addr);
191 case WPA_EAPOL_portEnabled:
192 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
194 case WPA_EAPOL_portValid:
195 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
197 case WPA_EAPOL_authorized:
198 ieee802_1x_set_sta_authorized(hapd, sta, value);
200 case WPA_EAPOL_portControl_Auto:
202 sta->eapol_sm->portControl = Auto;
204 case WPA_EAPOL_keyRun:
206 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
208 case WPA_EAPOL_keyAvailable:
210 sta->eapol_sm->eap_if->eapKeyAvailable =
211 value ? TRUE : FALSE;
213 case WPA_EAPOL_keyDone:
215 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
217 case WPA_EAPOL_inc_EapolFramesTx:
219 sta->eapol_sm->dot1xAuthEapolFramesTx++;
225 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
226 wpa_eapol_variable var)
228 struct hostapd_data *hapd = ctx;
229 struct sta_info *sta = ap_get_sta(hapd, addr);
230 if (sta == NULL || sta->eapol_sm == NULL)
233 case WPA_EAPOL_keyRun:
234 return sta->eapol_sm->keyRun;
235 case WPA_EAPOL_keyAvailable:
236 return sta->eapol_sm->eap_if->eapKeyAvailable;
243 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
244 const u8 *p2p_dev_addr,
245 const u8 *prev_psk, size_t *psk_len)
247 struct hostapd_data *hapd = ctx;
248 struct sta_info *sta = ap_get_sta(hapd, addr);
255 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
256 if (!sta->sae || prev_psk)
258 return sta->sae->pmk;
260 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
261 wpa_printf(MSG_DEBUG,
262 "No PSK for STA trying to use SAE with PMKSA caching");
265 #endif /* CONFIG_SAE */
268 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
269 sta && sta->owe_pmk) {
271 *psk_len = sta->owe_pmk_len;
274 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
275 struct rsn_pmksa_cache_entry *sa;
277 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
278 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
280 *psk_len = sa->pmk_len;
284 #endif /* CONFIG_OWE */
286 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
288 * This is about to iterate over all psks, prev_psk gives the last
289 * returned psk which should not be returned again.
290 * logic list (all hostapd_get_psk; all sta->psk)
292 if (sta && sta->psk && !psk) {
293 struct hostapd_sta_wpa_psk_short *pos;
295 for (pos = sta->psk; pos; pos = pos->next) {
296 if (pos->is_passphrase) {
297 pbkdf2_sha1(pos->passphrase,
298 hapd->conf->ssid.ssid,
299 hapd->conf->ssid.ssid_len, 4096,
301 pos->is_passphrase = 0;
303 if (pos->psk == prev_psk) {
304 psk = pos->next ? pos->next->psk : NULL;
313 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
316 struct hostapd_data *hapd = ctx;
319 struct sta_info *sta;
321 sta = ap_get_sta(hapd, addr);
323 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
327 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
329 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
336 os_memcpy(msk, key, keylen);
343 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
344 const u8 *addr, int idx, u8 *key,
347 struct hostapd_data *hapd = ctx;
348 const char *ifname = hapd->conf->iface;
351 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
356 #ifdef CONFIG_TESTING_OPTIONS
357 if (addr && !is_broadcast_ether_addr(addr)) {
358 struct sta_info *sta;
360 sta = ap_get_sta(hapd, addr);
362 sta->last_tk_alg = alg;
363 sta->last_tk_key_idx = idx;
365 os_memcpy(sta->last_tk, key, key_len);
366 sta->last_tk_len = key_len;
368 #ifdef CONFIG_IEEE80211W
369 } else if (alg == WPA_ALG_IGTK ||
370 alg == WPA_ALG_BIP_GMAC_128 ||
371 alg == WPA_ALG_BIP_GMAC_256 ||
372 alg == WPA_ALG_BIP_CMAC_256) {
373 hapd->last_igtk_alg = alg;
374 hapd->last_igtk_key_idx = idx;
376 os_memcpy(hapd->last_igtk, key, key_len);
377 hapd->last_igtk_len = key_len;
378 #endif /* CONFIG_IEEE80211W */
380 hapd->last_gtk_alg = alg;
381 hapd->last_gtk_key_idx = idx;
383 os_memcpy(hapd->last_gtk, key, key_len);
384 hapd->last_gtk_len = key_len;
386 #endif /* CONFIG_TESTING_OPTIONS */
387 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
392 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
395 struct hostapd_data *hapd = ctx;
396 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
400 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
401 const u8 *data, size_t data_len,
404 struct hostapd_data *hapd = ctx;
405 struct sta_info *sta;
408 #ifdef CONFIG_TESTING_OPTIONS
409 if (hapd->ext_eapol_frame_io) {
410 size_t hex_len = 2 * data_len + 1;
411 char *hex = os_malloc(hex_len);
415 wpa_snprintf_hex(hex, hex_len, data, data_len);
416 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
421 #endif /* CONFIG_TESTING_OPTIONS */
423 sta = ap_get_sta(hapd, addr);
425 flags = hostapd_sta_flags_to_drv(sta->flags);
427 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
432 static int hostapd_wpa_auth_for_each_sta(
433 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
436 struct hostapd_data *hapd = ctx;
437 struct sta_info *sta;
439 for (sta = hapd->sta_list; sta; sta = sta->next) {
440 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
447 struct wpa_auth_iface_iter_data {
448 int (*cb)(struct wpa_authenticator *sm, void *ctx);
452 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
454 struct wpa_auth_iface_iter_data *data = ctx;
456 for (i = 0; i < iface->num_bss; i++) {
457 if (iface->bss[i]->wpa_auth &&
458 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
465 static int hostapd_wpa_auth_for_each_auth(
466 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
469 struct hostapd_data *hapd = ctx;
470 struct wpa_auth_iface_iter_data data;
471 if (hapd->iface->interfaces == NULL ||
472 hapd->iface->interfaces->for_each_interface == NULL)
475 data.cb_ctx = cb_ctx;
476 return hapd->iface->interfaces->for_each_interface(
477 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
481 #ifdef CONFIG_IEEE80211R_AP
483 struct wpa_ft_rrb_rx_later_data {
487 /* followed by data_len octets of data */
490 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
492 struct hostapd_data *hapd = eloop_ctx;
493 struct wpa_ft_rrb_rx_later_data *data, *n;
495 dl_list_for_each_safe(data, n, &hapd->l2_queue,
496 struct wpa_ft_rrb_rx_later_data, list) {
497 if (hapd->wpa_auth) {
498 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
499 (const u8 *) (data + 1),
502 dl_list_del(&data->list);
508 struct wpa_auth_ft_iface_iter_data {
509 struct hostapd_data *src_hapd;
516 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
518 struct wpa_auth_ft_iface_iter_data *idata = ctx;
519 struct wpa_ft_rrb_rx_later_data *data;
520 struct hostapd_data *hapd;
523 for (j = 0; j < iface->num_bss; j++) {
524 hapd = iface->bss[j];
525 if (hapd == idata->src_hapd ||
527 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
530 wpa_printf(MSG_DEBUG,
531 "FT: Send RRB data directly to locally managed BSS "
532 MACSTR "@%s -> " MACSTR "@%s",
533 MAC2STR(idata->src_hapd->own_addr),
534 idata->src_hapd->conf->iface,
535 MAC2STR(hapd->own_addr), hapd->conf->iface);
537 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
538 * when it would be triggered when reading from a socket.
540 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
541 * that is calling hapd0:recv handler from within
542 * hapd0:send directly.
544 data = os_zalloc(sizeof(*data) + idata->data_len);
548 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
549 os_memcpy(data + 1, idata->data, idata->data_len);
550 data->data_len = idata->data_len;
552 dl_list_add(&hapd->l2_queue, &data->list);
554 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
556 eloop_register_timeout(0, 0,
557 hostapd_wpa_ft_rrb_rx_later,
566 #endif /* CONFIG_IEEE80211R_AP */
569 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
570 const u8 *data, size_t data_len)
572 struct hostapd_data *hapd = ctx;
573 struct l2_ethhdr *buf;
576 #ifdef CONFIG_TESTING_OPTIONS
577 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
578 size_t hex_len = 2 * data_len + 1;
579 char *hex = os_malloc(hex_len);
583 wpa_snprintf_hex(hex, hex_len, data, data_len);
584 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
589 #endif /* CONFIG_TESTING_OPTIONS */
591 #ifdef CONFIG_IEEE80211R_AP
592 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
593 hapd->iface->interfaces->for_each_interface) {
595 struct wpa_auth_ft_iface_iter_data idata;
596 idata.src_hapd = hapd;
599 idata.data_len = data_len;
600 res = hapd->iface->interfaces->for_each_interface(
601 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
606 #endif /* CONFIG_IEEE80211R_AP */
608 if (hapd->driver && hapd->driver->send_ether)
609 return hapd->driver->send_ether(hapd->drv_priv, dst,
610 hapd->own_addr, proto,
612 if (hapd->l2 == NULL)
615 buf = os_malloc(sizeof(*buf) + data_len);
618 os_memcpy(buf->h_dest, dst, ETH_ALEN);
619 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
620 buf->h_proto = host_to_be16(proto);
621 os_memcpy(buf + 1, data, data_len);
622 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
623 sizeof(*buf) + data_len);
629 #ifdef CONFIG_ETH_P_OUI
630 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
633 switch (oui_suffix) {
634 #ifdef CONFIG_IEEE80211R_AP
635 case FT_PACKET_R0KH_R1KH_PULL:
636 return hapd->oui_pull;
637 case FT_PACKET_R0KH_R1KH_RESP:
638 return hapd->oui_resp;
639 case FT_PACKET_R0KH_R1KH_PUSH:
640 return hapd->oui_push;
641 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
642 return hapd->oui_sreq;
643 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
644 return hapd->oui_sresp;
645 #endif /* CONFIG_IEEE80211R_AP */
650 #endif /* CONFIG_ETH_P_OUI */
653 #ifdef CONFIG_IEEE80211R_AP
655 struct oui_deliver_later_data {
657 u8 src_addr[ETH_ALEN];
658 u8 dst_addr[ETH_ALEN];
661 /* followed by data_len octets of data */
664 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
666 struct hostapd_data *hapd = eloop_ctx;
667 struct oui_deliver_later_data *data, *n;
668 struct eth_p_oui_ctx *oui_ctx;
670 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
671 struct oui_deliver_later_data, list) {
672 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
673 if (hapd->wpa_auth && oui_ctx) {
674 eth_p_oui_deliver(oui_ctx, data->src_addr,
676 (const u8 *) (data + 1),
679 dl_list_del(&data->list);
685 struct wpa_auth_oui_iface_iter_data {
686 struct hostapd_data *src_hapd;
693 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
695 struct wpa_auth_oui_iface_iter_data *idata = ctx;
696 struct oui_deliver_later_data *data;
697 struct hostapd_data *hapd;
700 for (j = 0; j < iface->num_bss; j++) {
701 hapd = iface->bss[j];
702 if (hapd == idata->src_hapd)
704 if (!is_multicast_ether_addr(idata->dst_addr) &&
705 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
708 /* defer eth_p_oui_deliver until next eloop step as this is
709 * when it would be triggerd from reading from sock
711 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
712 * that is calling hapd0:recv handler from within
713 * hapd0:send directly.
715 data = os_zalloc(sizeof(*data) + idata->data_len);
719 os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
720 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
721 os_memcpy(data + 1, idata->data, idata->data_len);
722 data->data_len = idata->data_len;
723 data->oui_suffix = idata->oui_suffix;
725 dl_list_add(&hapd->l2_oui_queue, &data->list);
727 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
729 eloop_register_timeout(0, 0,
730 hostapd_oui_deliver_later,
739 #endif /* CONFIG_IEEE80211R_AP */
742 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
743 const u8 *data, size_t data_len)
745 #ifdef CONFIG_ETH_P_OUI
746 struct hostapd_data *hapd = ctx;
747 struct eth_p_oui_ctx *oui_ctx;
749 #ifdef CONFIG_IEEE80211R_AP
750 if (hapd->iface->interfaces &&
751 hapd->iface->interfaces->for_each_interface) {
752 struct wpa_auth_oui_iface_iter_data idata;
755 idata.src_hapd = hapd;
756 idata.dst_addr = dst;
758 idata.data_len = data_len;
759 idata.oui_suffix = oui_suffix;
760 res = hapd->iface->interfaces->for_each_interface(
761 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
766 #endif /* CONFIG_IEEE80211R_AP */
768 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
772 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
773 #else /* CONFIG_ETH_P_OUI */
775 #endif /* CONFIG_ETH_P_OUI */
779 #ifdef CONFIG_IEEE80211R_AP
781 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
782 const u8 *data, size_t data_len)
784 struct hostapd_data *hapd = ctx;
786 struct ieee80211_mgmt *m;
788 struct sta_info *sta;
790 sta = ap_get_sta(hapd, dst);
791 if (sta == NULL || sta->wpa_sm == NULL)
794 m = os_zalloc(sizeof(*m) + data_len);
797 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
798 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
799 WLAN_FC_STYPE_ACTION);
800 os_memcpy(m->da, dst, ETH_ALEN);
801 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
802 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
803 os_memcpy(&m->u, data, data_len);
805 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
811 static struct wpa_state_machine *
812 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
814 struct hostapd_data *hapd = ctx;
815 struct sta_info *sta;
817 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
820 sta = ap_sta_add(hapd, sta_addr);
824 sta->auth_alg = WLAN_AUTH_FT;
828 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
829 if (sta->wpa_sm == NULL) {
830 ap_free_sta(hapd, sta);
833 sta->auth_alg = WLAN_AUTH_FT;
839 static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
840 struct vlan_description *vlan)
842 struct hostapd_data *hapd = ctx;
843 struct sta_info *sta;
845 sta = ap_get_sta(hapd, sta_addr);
846 if (!sta || !sta->wpa_sm)
849 if (vlan->notempty &&
850 !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
851 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
853 "Invalid VLAN %d%s received from FT",
854 vlan->untagged, vlan->tagged[0] ? "+" : "");
858 if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
860 /* Configure wpa_group for GTK but ignore error due to driver not
861 * knowing this STA. */
862 ap_sta_bind_vlan(hapd, sta);
865 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
866 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
872 static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
873 struct vlan_description *vlan)
875 struct hostapd_data *hapd = ctx;
876 struct sta_info *sta;
878 sta = ap_get_sta(hapd, sta_addr);
883 *vlan = *sta->vlan_desc;
885 os_memset(vlan, 0, sizeof(*vlan));
892 hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
893 const u8 *identity, size_t identity_len)
895 struct hostapd_data *hapd = ctx;
896 struct sta_info *sta;
898 sta = ap_get_sta(hapd, sta_addr);
902 os_free(sta->identity);
903 sta->identity = NULL;
906 os_free(sta->eapol_sm->identity);
907 sta->eapol_sm->identity = NULL;
908 sta->eapol_sm->identity_len = 0;
914 /* sta->identity is NULL terminated */
915 sta->identity = os_zalloc(identity_len + 1);
918 os_memcpy(sta->identity, identity, identity_len);
921 sta->eapol_sm->identity = os_zalloc(identity_len);
922 if (!sta->eapol_sm->identity)
924 os_memcpy(sta->eapol_sm->identity, identity, identity_len);
925 sta->eapol_sm->identity_len = identity_len;
933 hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
935 struct hostapd_data *hapd = ctx;
936 struct sta_info *sta;
940 sta = ap_get_sta(hapd, sta_addr);
944 *buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
948 if (!sta->identity) {
953 identity = sta->identity;
954 len = os_strlen(identity);
955 *buf = (u8 *) identity;
962 hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
963 const u8 *radius_cui, size_t radius_cui_len)
965 struct hostapd_data *hapd = ctx;
966 struct sta_info *sta;
968 sta = ap_get_sta(hapd, sta_addr);
972 os_free(sta->radius_cui);
973 sta->radius_cui = NULL;
976 wpabuf_free(sta->eapol_sm->radius_cui);
977 sta->eapol_sm->radius_cui = NULL;
983 /* sta->radius_cui is NULL terminated */
984 sta->radius_cui = os_zalloc(radius_cui_len + 1);
985 if (!sta->radius_cui)
987 os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
990 sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
992 if (!sta->eapol_sm->radius_cui)
1001 hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1003 struct hostapd_data *hapd = ctx;
1004 struct sta_info *sta;
1009 sta = ap_get_sta(hapd, sta_addr);
1013 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1015 len = wpabuf_len(b);
1016 *buf = wpabuf_head(b);
1020 if (!sta->radius_cui) {
1025 radius_cui = sta->radius_cui;
1026 len = os_strlen(radius_cui);
1027 *buf = (u8 *) radius_cui;
1033 static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1034 int session_timeout)
1036 struct hostapd_data *hapd = ctx;
1037 struct sta_info *sta;
1039 sta = ap_get_sta(hapd, sta_addr);
1043 if (session_timeout) {
1044 os_get_reltime(&sta->session_timeout);
1045 sta->session_timeout.sec += session_timeout;
1046 sta->session_timeout_set = 1;
1047 ap_sta_session_timeout(hapd, sta, session_timeout);
1049 sta->session_timeout_set = 0;
1050 ap_sta_no_session_timeout(hapd, sta);
1055 static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1057 struct hostapd_data *hapd = ctx;
1058 struct sta_info *sta;
1059 struct os_reltime now, remaining;
1061 sta = ap_get_sta(hapd, sta_addr);
1062 if (!sta || !sta->session_timeout_set)
1065 os_get_reltime(&now);
1066 if (os_reltime_before(&sta->session_timeout, &now)) {
1067 /* already expired, return >0 as timeout was set */
1071 os_reltime_sub(&sta->session_timeout, &now, &remaining);
1073 return (remaining.sec > 0) ? remaining.sec : 1;
1077 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1080 struct hostapd_data *hapd = ctx;
1081 struct l2_ethhdr *ethhdr;
1082 if (len < sizeof(*ethhdr))
1084 ethhdr = (struct l2_ethhdr *) buf;
1085 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1086 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
1087 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1088 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
1090 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1091 len - sizeof(*ethhdr));
1095 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1096 const u8 *dst_addr, u8 oui_suffix,
1097 const u8 *buf, size_t len)
1099 struct hostapd_data *hapd = ctx;
1101 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1102 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1103 if (!is_multicast_ether_addr(dst_addr) &&
1104 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
1106 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1111 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1112 u8 *tspec_ie, size_t tspec_ielen)
1114 struct hostapd_data *hapd = ctx;
1115 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1120 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1121 const char *ft_iface)
1123 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1124 FT_PACKET_R0KH_R1KH_PULL,
1125 hostapd_rrb_oui_receive, hapd);
1126 if (!hapd->oui_pull)
1129 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1130 FT_PACKET_R0KH_R1KH_RESP,
1131 hostapd_rrb_oui_receive, hapd);
1132 if (!hapd->oui_resp)
1135 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1136 FT_PACKET_R0KH_R1KH_PUSH,
1137 hostapd_rrb_oui_receive, hapd);
1138 if (!hapd->oui_push)
1141 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1142 FT_PACKET_R0KH_R1KH_SEQ_REQ,
1143 hostapd_rrb_oui_receive, hapd);
1144 if (!hapd->oui_sreq)
1147 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1148 FT_PACKET_R0KH_R1KH_SEQ_RESP,
1149 hostapd_rrb_oui_receive, hapd);
1150 if (!hapd->oui_sresp)
1157 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1159 eth_p_oui_unregister(hapd->oui_pull);
1160 hapd->oui_pull = NULL;
1161 eth_p_oui_unregister(hapd->oui_resp);
1162 hapd->oui_resp = NULL;
1163 eth_p_oui_unregister(hapd->oui_push);
1164 hapd->oui_push = NULL;
1165 eth_p_oui_unregister(hapd->oui_sreq);
1166 hapd->oui_sreq = NULL;
1167 eth_p_oui_unregister(hapd->oui_sresp);
1168 hapd->oui_sresp = NULL;
1170 #endif /* CONFIG_IEEE80211R_AP */
1173 int hostapd_setup_wpa(struct hostapd_data *hapd)
1175 struct wpa_auth_config _conf;
1176 static const struct wpa_auth_callbacks cb = {
1177 .logger = hostapd_wpa_auth_logger,
1178 .disconnect = hostapd_wpa_auth_disconnect,
1179 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1180 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1181 .set_eapol = hostapd_wpa_auth_set_eapol,
1182 .get_eapol = hostapd_wpa_auth_get_eapol,
1183 .get_psk = hostapd_wpa_auth_get_psk,
1184 .get_msk = hostapd_wpa_auth_get_msk,
1185 .set_key = hostapd_wpa_auth_set_key,
1186 .get_seqnum = hostapd_wpa_auth_get_seqnum,
1187 .send_eapol = hostapd_wpa_auth_send_eapol,
1188 .for_each_sta = hostapd_wpa_auth_for_each_sta,
1189 .for_each_auth = hostapd_wpa_auth_for_each_auth,
1190 .send_ether = hostapd_wpa_auth_send_ether,
1191 .send_oui = hostapd_wpa_auth_send_oui,
1192 #ifdef CONFIG_IEEE80211R_AP
1193 .send_ft_action = hostapd_wpa_auth_send_ft_action,
1194 .add_sta = hostapd_wpa_auth_add_sta,
1195 .add_tspec = hostapd_wpa_auth_add_tspec,
1196 .set_vlan = hostapd_wpa_auth_set_vlan,
1197 .get_vlan = hostapd_wpa_auth_get_vlan,
1198 .set_identity = hostapd_wpa_auth_set_identity,
1199 .get_identity = hostapd_wpa_auth_get_identity,
1200 .set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1201 .get_radius_cui = hostapd_wpa_auth_get_radius_cui,
1202 .set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1203 .get_session_timeout = hostapd_wpa_auth_get_session_timeout,
1204 #endif /* CONFIG_IEEE80211R_AP */
1209 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
1210 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1211 _conf.tx_status = 1;
1212 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1214 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
1215 if (hapd->wpa_auth == NULL) {
1216 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1220 if (hostapd_set_privacy(hapd, 1)) {
1221 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1222 "for interface %s", hapd->conf->iface);
1226 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1227 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1228 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1229 "the kernel driver.");
1233 if (rsn_preauth_iface_init(hapd)) {
1234 wpa_printf(MSG_ERROR, "Initialization of RSN "
1235 "pre-authentication failed.");
1239 #ifdef CONFIG_IEEE80211R_AP
1240 if (!hostapd_drv_none(hapd) &&
1241 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
1242 const char *ft_iface;
1244 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1246 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
1247 hostapd_rrb_receive, hapd, 1);
1248 if (hapd->l2 == NULL &&
1249 (hapd->driver == NULL ||
1250 hapd->driver->send_ether == NULL)) {
1251 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1256 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1257 wpa_printf(MSG_ERROR,
1258 "Failed to open ETH_P_OUI interface");
1262 #endif /* CONFIG_IEEE80211R_AP */
1269 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1271 struct wpa_auth_config wpa_auth_conf;
1272 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1273 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1277 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1279 ieee80211_tkip_countermeasures_deinit(hapd);
1280 rsn_preauth_iface_deinit(hapd);
1281 if (hapd->wpa_auth) {
1282 wpa_deinit(hapd->wpa_auth);
1283 hapd->wpa_auth = NULL;
1285 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1286 wpa_printf(MSG_DEBUG, "Could not disable "
1287 "PrivacyInvoked for interface %s",
1291 if (hapd->drv_priv &&
1292 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1293 wpa_printf(MSG_DEBUG, "Could not remove generic "
1294 "information element from interface %s",
1298 ieee802_1x_deinit(hapd);
1300 #ifdef CONFIG_IEEE80211R_AP
1301 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1302 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1303 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1304 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1305 l2_packet_deinit(hapd->l2);
1307 hostapd_wpa_unregister_ft_oui(hapd);
1308 #endif /* CONFIG_IEEE80211R_AP */