2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "common/wpa_ctrl.h"
15 #include "eapol_auth/eapol_auth_sm.h"
16 #include "eapol_auth/eapol_auth_sm_i.h"
17 #include "eap_server/eap.h"
18 #include "l2_packet/l2_packet.h"
20 #include "ieee802_1x.h"
21 #include "preauth_auth.h"
23 #include "tkip_countermeasures.h"
24 #include "ap_drv_ops.h"
25 #include "ap_config.h"
27 #include "wpa_auth_glue.h"
30 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
31 struct hostapd_config *iconf,
32 struct wpa_auth_config *wconf)
34 os_memset(wconf, 0, sizeof(*wconf));
35 wconf->wpa = conf->wpa;
36 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
37 wconf->wpa_pairwise = conf->wpa_pairwise;
38 wconf->wpa_group = conf->wpa_group;
39 wconf->wpa_group_rekey = conf->wpa_group_rekey;
40 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
41 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
42 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
43 wconf->rsn_pairwise = conf->rsn_pairwise;
44 wconf->rsn_preauth = conf->rsn_preauth;
45 wconf->eapol_version = conf->eapol_version;
46 wconf->peerkey = conf->peerkey;
47 wconf->wmm_enabled = conf->wmm_enabled;
48 wconf->wmm_uapsd = conf->wmm_uapsd;
49 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
50 wconf->okc = conf->okc;
51 #ifdef CONFIG_IEEE80211W
52 wconf->ieee80211w = conf->ieee80211w;
53 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
54 #endif /* CONFIG_IEEE80211W */
55 #ifdef CONFIG_IEEE80211R
56 wconf->ssid_len = conf->ssid.ssid_len;
57 if (wconf->ssid_len > SSID_MAX_LEN)
58 wconf->ssid_len = SSID_MAX_LEN;
59 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
60 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
61 MOBILITY_DOMAIN_ID_LEN);
62 if (conf->nas_identifier &&
63 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
64 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
65 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
66 wconf->r0_key_holder_len);
68 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
69 wconf->r0_key_lifetime = conf->r0_key_lifetime;
70 wconf->reassociation_deadline = conf->reassociation_deadline;
71 wconf->r0kh_list = conf->r0kh_list;
72 wconf->r1kh_list = conf->r1kh_list;
73 wconf->pmk_r1_push = conf->pmk_r1_push;
74 wconf->ft_over_ds = conf->ft_over_ds;
75 #endif /* CONFIG_IEEE80211R */
77 wconf->disable_gtk = conf->disable_dgaf;
79 wconf->disable_gtk = 1;
80 wconf->wpa = WPA_PROTO_OSEN;
81 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
82 wconf->wpa_pairwise = 0;
83 wconf->wpa_group = WPA_CIPHER_CCMP;
84 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
85 wconf->rsn_preauth = 0;
86 wconf->disable_pmksa_caching = 1;
87 #ifdef CONFIG_IEEE80211W
88 wconf->ieee80211w = 1;
89 #endif /* CONFIG_IEEE80211W */
91 #endif /* CONFIG_HS20 */
92 #ifdef CONFIG_TESTING_OPTIONS
93 wconf->corrupt_gtk_rekey_mic_probability =
94 iconf->corrupt_gtk_rekey_mic_probability;
95 if (conf->own_ie_override &&
96 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
97 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
98 os_memcpy(wconf->own_ie_override,
99 wpabuf_head(conf->own_ie_override),
100 wconf->own_ie_override_len);
102 #endif /* CONFIG_TESTING_OPTIONS */
104 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
105 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
106 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
107 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
108 #endif /* CONFIG_P2P */
112 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
113 logger_level level, const char *txt)
115 #ifndef CONFIG_NO_HOSTAPD_LOGGER
116 struct hostapd_data *hapd = ctx;
121 hlevel = HOSTAPD_LEVEL_WARNING;
124 hlevel = HOSTAPD_LEVEL_INFO;
128 hlevel = HOSTAPD_LEVEL_DEBUG;
132 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
133 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
137 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
140 struct hostapd_data *hapd = ctx;
141 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
142 "STA " MACSTR " reason %d",
143 __func__, MAC2STR(addr), reason);
144 ap_sta_disconnect(hapd, NULL, addr, reason);
148 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
150 struct hostapd_data *hapd = ctx;
151 return michael_mic_failure(hapd, addr, 0);
155 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
157 struct hostapd_data *hapd = ctx;
158 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
163 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
164 wpa_eapol_variable var, int value)
166 struct hostapd_data *hapd = ctx;
167 struct sta_info *sta = ap_get_sta(hapd, addr);
171 case WPA_EAPOL_portEnabled:
172 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
174 case WPA_EAPOL_portValid:
175 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
177 case WPA_EAPOL_authorized:
178 ieee802_1x_set_sta_authorized(hapd, sta, value);
180 case WPA_EAPOL_portControl_Auto:
182 sta->eapol_sm->portControl = Auto;
184 case WPA_EAPOL_keyRun:
186 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
188 case WPA_EAPOL_keyAvailable:
190 sta->eapol_sm->eap_if->eapKeyAvailable =
191 value ? TRUE : FALSE;
193 case WPA_EAPOL_keyDone:
195 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
197 case WPA_EAPOL_inc_EapolFramesTx:
199 sta->eapol_sm->dot1xAuthEapolFramesTx++;
205 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
206 wpa_eapol_variable var)
208 struct hostapd_data *hapd = ctx;
209 struct sta_info *sta = ap_get_sta(hapd, addr);
210 if (sta == NULL || sta->eapol_sm == NULL)
213 case WPA_EAPOL_keyRun:
214 return sta->eapol_sm->keyRun;
215 case WPA_EAPOL_keyAvailable:
216 return sta->eapol_sm->eap_if->eapKeyAvailable;
223 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
224 const u8 *p2p_dev_addr,
227 struct hostapd_data *hapd = ctx;
228 struct sta_info *sta = ap_get_sta(hapd, addr);
232 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
233 if (!sta->sae || prev_psk)
235 return sta->sae->pmk;
237 #endif /* CONFIG_SAE */
239 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
241 * This is about to iterate over all psks, prev_psk gives the last
242 * returned psk which should not be returned again.
243 * logic list (all hostapd_get_psk; all sta->psk)
245 if (sta && sta->psk && !psk) {
246 struct hostapd_sta_wpa_psk_short *pos;
248 for (pos = sta->psk; pos; pos = pos->next) {
249 if (pos->psk == prev_psk) {
250 psk = pos->next ? pos->next->psk : NULL;
259 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
262 struct hostapd_data *hapd = ctx;
265 struct sta_info *sta;
267 sta = ap_get_sta(hapd, addr);
269 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
273 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
275 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
282 os_memcpy(msk, key, keylen);
289 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
290 const u8 *addr, int idx, u8 *key,
293 struct hostapd_data *hapd = ctx;
294 const char *ifname = hapd->conf->iface;
297 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
302 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
307 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
310 struct hostapd_data *hapd = ctx;
311 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
315 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
316 const u8 *data, size_t data_len,
319 struct hostapd_data *hapd = ctx;
320 struct sta_info *sta;
323 #ifdef CONFIG_TESTING_OPTIONS
324 if (hapd->ext_eapol_frame_io) {
325 size_t hex_len = 2 * data_len + 1;
326 char *hex = os_malloc(hex_len);
330 wpa_snprintf_hex(hex, hex_len, data, data_len);
331 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
336 #endif /* CONFIG_TESTING_OPTIONS */
338 sta = ap_get_sta(hapd, addr);
340 flags = hostapd_sta_flags_to_drv(sta->flags);
342 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
347 static int hostapd_wpa_auth_for_each_sta(
348 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
351 struct hostapd_data *hapd = ctx;
352 struct sta_info *sta;
354 for (sta = hapd->sta_list; sta; sta = sta->next) {
355 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
362 struct wpa_auth_iface_iter_data {
363 int (*cb)(struct wpa_authenticator *sm, void *ctx);
367 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
369 struct wpa_auth_iface_iter_data *data = ctx;
371 for (i = 0; i < iface->num_bss; i++) {
372 if (iface->bss[i]->wpa_auth &&
373 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
380 static int hostapd_wpa_auth_for_each_auth(
381 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
384 struct hostapd_data *hapd = ctx;
385 struct wpa_auth_iface_iter_data data;
386 if (hapd->iface->interfaces == NULL ||
387 hapd->iface->interfaces->for_each_interface == NULL)
390 data.cb_ctx = cb_ctx;
391 return hapd->iface->interfaces->for_each_interface(
392 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
396 #ifdef CONFIG_IEEE80211R
398 struct wpa_auth_ft_iface_iter_data {
399 struct hostapd_data *src_hapd;
406 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
408 struct wpa_auth_ft_iface_iter_data *idata = ctx;
409 struct hostapd_data *hapd;
412 for (j = 0; j < iface->num_bss; j++) {
413 hapd = iface->bss[j];
414 if (hapd == idata->src_hapd)
416 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
417 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
418 "locally managed BSS " MACSTR "@%s -> "
420 MAC2STR(idata->src_hapd->own_addr),
421 idata->src_hapd->conf->iface,
422 MAC2STR(hapd->own_addr), hapd->conf->iface);
423 wpa_ft_rrb_rx(hapd->wpa_auth,
424 idata->src_hapd->own_addr,
425 idata->data, idata->data_len);
433 #endif /* CONFIG_IEEE80211R */
436 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
437 const u8 *data, size_t data_len)
439 struct hostapd_data *hapd = ctx;
440 struct l2_ethhdr *buf;
443 #ifdef CONFIG_TESTING_OPTIONS
444 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
445 size_t hex_len = 2 * data_len + 1;
446 char *hex = os_malloc(hex_len);
450 wpa_snprintf_hex(hex, hex_len, data, data_len);
451 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
456 #endif /* CONFIG_TESTING_OPTIONS */
458 #ifdef CONFIG_IEEE80211R
459 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
460 hapd->iface->interfaces->for_each_interface) {
462 struct wpa_auth_ft_iface_iter_data idata;
463 idata.src_hapd = hapd;
466 idata.data_len = data_len;
467 res = hapd->iface->interfaces->for_each_interface(
468 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
473 #endif /* CONFIG_IEEE80211R */
475 if (hapd->driver && hapd->driver->send_ether)
476 return hapd->driver->send_ether(hapd->drv_priv, dst,
477 hapd->own_addr, proto,
479 if (hapd->l2 == NULL)
482 buf = os_malloc(sizeof(*buf) + data_len);
485 os_memcpy(buf->h_dest, dst, ETH_ALEN);
486 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
487 buf->h_proto = host_to_be16(proto);
488 os_memcpy(buf + 1, data, data_len);
489 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
490 sizeof(*buf) + data_len);
496 #ifdef CONFIG_IEEE80211R
498 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
499 const u8 *data, size_t data_len)
501 struct hostapd_data *hapd = ctx;
503 struct ieee80211_mgmt *m;
505 struct sta_info *sta;
507 sta = ap_get_sta(hapd, dst);
508 if (sta == NULL || sta->wpa_sm == NULL)
511 m = os_zalloc(sizeof(*m) + data_len);
514 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
515 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
516 WLAN_FC_STYPE_ACTION);
517 os_memcpy(m->da, dst, ETH_ALEN);
518 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
519 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
520 os_memcpy(&m->u, data, data_len);
522 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
528 static struct wpa_state_machine *
529 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
531 struct hostapd_data *hapd = ctx;
532 struct sta_info *sta;
534 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
537 sta = ap_sta_add(hapd, sta_addr);
541 sta->auth_alg = WLAN_AUTH_FT;
545 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
546 if (sta->wpa_sm == NULL) {
547 ap_free_sta(hapd, sta);
550 sta->auth_alg = WLAN_AUTH_FT;
556 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
559 struct hostapd_data *hapd = ctx;
560 struct l2_ethhdr *ethhdr;
561 if (len < sizeof(*ethhdr))
563 ethhdr = (struct l2_ethhdr *) buf;
564 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
565 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
566 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
567 len - sizeof(*ethhdr));
571 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
572 u8 *tspec_ie, size_t tspec_ielen)
574 struct hostapd_data *hapd = ctx;
575 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
578 #endif /* CONFIG_IEEE80211R */
581 int hostapd_setup_wpa(struct hostapd_data *hapd)
583 struct wpa_auth_config _conf;
584 struct wpa_auth_callbacks cb;
588 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
589 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
591 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
593 os_memset(&cb, 0, sizeof(cb));
595 cb.logger = hostapd_wpa_auth_logger;
596 cb.disconnect = hostapd_wpa_auth_disconnect;
597 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
598 cb.psk_failure_report = hostapd_wpa_auth_psk_failure_report;
599 cb.set_eapol = hostapd_wpa_auth_set_eapol;
600 cb.get_eapol = hostapd_wpa_auth_get_eapol;
601 cb.get_psk = hostapd_wpa_auth_get_psk;
602 cb.get_msk = hostapd_wpa_auth_get_msk;
603 cb.set_key = hostapd_wpa_auth_set_key;
604 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
605 cb.send_eapol = hostapd_wpa_auth_send_eapol;
606 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
607 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
608 cb.send_ether = hostapd_wpa_auth_send_ether;
609 #ifdef CONFIG_IEEE80211R
610 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
611 cb.add_sta = hostapd_wpa_auth_add_sta;
612 cb.add_tspec = hostapd_wpa_auth_add_tspec;
613 #endif /* CONFIG_IEEE80211R */
614 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
615 if (hapd->wpa_auth == NULL) {
616 wpa_printf(MSG_ERROR, "WPA initialization failed.");
620 if (hostapd_set_privacy(hapd, 1)) {
621 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
622 "for interface %s", hapd->conf->iface);
626 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
627 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
628 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
629 "the kernel driver.");
633 if (rsn_preauth_iface_init(hapd)) {
634 wpa_printf(MSG_ERROR, "Initialization of RSN "
635 "pre-authentication failed.");
639 #ifdef CONFIG_IEEE80211R
640 if (!hostapd_drv_none(hapd) && hapd->conf->ft_over_ds &&
641 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
642 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
644 hapd->conf->iface, NULL, ETH_P_RRB,
645 hostapd_rrb_receive, hapd, 1);
646 if (hapd->l2 == NULL &&
647 (hapd->driver == NULL ||
648 hapd->driver->send_ether == NULL)) {
649 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
654 #endif /* CONFIG_IEEE80211R */
661 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
663 struct wpa_auth_config wpa_auth_conf;
664 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
665 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
669 void hostapd_deinit_wpa(struct hostapd_data *hapd)
671 ieee80211_tkip_countermeasures_deinit(hapd);
672 rsn_preauth_iface_deinit(hapd);
673 if (hapd->wpa_auth) {
674 wpa_deinit(hapd->wpa_auth);
675 hapd->wpa_auth = NULL;
677 if (hostapd_set_privacy(hapd, 0)) {
678 wpa_printf(MSG_DEBUG, "Could not disable "
679 "PrivacyInvoked for interface %s",
683 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
684 wpa_printf(MSG_DEBUG, "Could not remove generic "
685 "information element from interface %s",
689 ieee802_1x_deinit(hapd);
691 #ifdef CONFIG_IEEE80211R
692 l2_packet_deinit(hapd->l2);
694 #endif /* CONFIG_IEEE80211R */