2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "eapol_auth/eapol_auth_sm_i.h"
16 #include "eap_server/eap.h"
17 #include "l2_packet/l2_packet.h"
19 #include "ieee802_1x.h"
20 #include "preauth_auth.h"
22 #include "tkip_countermeasures.h"
23 #include "ap_drv_ops.h"
24 #include "ap_config.h"
26 #include "wpa_auth_glue.h"
29 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
30 struct hostapd_config *iconf,
31 struct wpa_auth_config *wconf)
33 os_memset(wconf, 0, sizeof(*wconf));
34 wconf->wpa = conf->wpa;
35 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
36 wconf->wpa_pairwise = conf->wpa_pairwise;
37 wconf->wpa_group = conf->wpa_group;
38 wconf->wpa_group_rekey = conf->wpa_group_rekey;
39 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
40 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
41 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
42 wconf->rsn_pairwise = conf->rsn_pairwise;
43 wconf->rsn_preauth = conf->rsn_preauth;
44 wconf->eapol_version = conf->eapol_version;
45 wconf->peerkey = conf->peerkey;
46 wconf->wmm_enabled = conf->wmm_enabled;
47 wconf->wmm_uapsd = conf->wmm_uapsd;
48 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
49 wconf->okc = conf->okc;
50 #ifdef CONFIG_IEEE80211W
51 wconf->ieee80211w = conf->ieee80211w;
52 #endif /* CONFIG_IEEE80211W */
53 #ifdef CONFIG_IEEE80211R
54 wconf->ssid_len = conf->ssid.ssid_len;
55 if (wconf->ssid_len > SSID_LEN)
56 wconf->ssid_len = SSID_LEN;
57 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
58 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
59 MOBILITY_DOMAIN_ID_LEN);
60 if (conf->nas_identifier &&
61 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
62 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
63 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
64 wconf->r0_key_holder_len);
66 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
67 wconf->r0_key_lifetime = conf->r0_key_lifetime;
68 wconf->reassociation_deadline = conf->reassociation_deadline;
69 wconf->r0kh_list = conf->r0kh_list;
70 wconf->r1kh_list = conf->r1kh_list;
71 wconf->pmk_r1_push = conf->pmk_r1_push;
72 wconf->ft_over_ds = conf->ft_over_ds;
73 #endif /* CONFIG_IEEE80211R */
75 wconf->disable_gtk = conf->disable_dgaf;
76 #endif /* CONFIG_HS20 */
77 #ifdef CONFIG_TESTING_OPTIONS
78 wconf->corrupt_gtk_rekey_mic_probability =
79 iconf->corrupt_gtk_rekey_mic_probability;
80 #endif /* CONFIG_TESTING_OPTIONS */
82 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
83 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
84 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
85 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
86 #endif /* CONFIG_P2P */
90 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
91 logger_level level, const char *txt)
93 #ifndef CONFIG_NO_HOSTAPD_LOGGER
94 struct hostapd_data *hapd = ctx;
99 hlevel = HOSTAPD_LEVEL_WARNING;
102 hlevel = HOSTAPD_LEVEL_INFO;
106 hlevel = HOSTAPD_LEVEL_DEBUG;
110 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
111 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
115 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
118 struct hostapd_data *hapd = ctx;
119 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
120 "STA " MACSTR " reason %d",
121 __func__, MAC2STR(addr), reason);
122 ap_sta_disconnect(hapd, NULL, addr, reason);
126 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
128 struct hostapd_data *hapd = ctx;
129 return michael_mic_failure(hapd, addr, 0);
133 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
134 wpa_eapol_variable var, int value)
136 struct hostapd_data *hapd = ctx;
137 struct sta_info *sta = ap_get_sta(hapd, addr);
141 case WPA_EAPOL_portEnabled:
142 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
144 case WPA_EAPOL_portValid:
145 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
147 case WPA_EAPOL_authorized:
148 ieee802_1x_set_sta_authorized(hapd, sta, value);
150 case WPA_EAPOL_portControl_Auto:
152 sta->eapol_sm->portControl = Auto;
154 case WPA_EAPOL_keyRun:
156 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
158 case WPA_EAPOL_keyAvailable:
160 sta->eapol_sm->eap_if->eapKeyAvailable =
161 value ? TRUE : FALSE;
163 case WPA_EAPOL_keyDone:
165 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
167 case WPA_EAPOL_inc_EapolFramesTx:
169 sta->eapol_sm->dot1xAuthEapolFramesTx++;
175 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
176 wpa_eapol_variable var)
178 struct hostapd_data *hapd = ctx;
179 struct sta_info *sta = ap_get_sta(hapd, addr);
180 if (sta == NULL || sta->eapol_sm == NULL)
183 case WPA_EAPOL_keyRun:
184 return sta->eapol_sm->keyRun;
185 case WPA_EAPOL_keyAvailable:
186 return sta->eapol_sm->eap_if->eapKeyAvailable;
193 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
194 const u8 *p2p_dev_addr,
197 struct hostapd_data *hapd = ctx;
198 struct sta_info *sta = ap_get_sta(hapd, addr);
202 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
203 if (!sta->sae || prev_psk)
205 return sta->sae->pmk;
207 #endif /* CONFIG_SAE */
209 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
211 * This is about to iterate over all psks, prev_psk gives the last
212 * returned psk which should not be returned again.
213 * logic list (all hostapd_get_psk; all sta->psk)
215 if (sta && sta->psk && !psk) {
216 struct hostapd_sta_wpa_psk_short *pos;
218 for (pos = sta->psk; pos; pos = pos->next) {
219 if (pos->psk == prev_psk) {
220 psk = pos->next ? pos->next->psk : NULL;
229 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
232 struct hostapd_data *hapd = ctx;
235 struct sta_info *sta;
237 sta = ap_get_sta(hapd, addr);
241 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
247 os_memcpy(msk, key, keylen);
254 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
255 const u8 *addr, int idx, u8 *key,
258 struct hostapd_data *hapd = ctx;
259 const char *ifname = hapd->conf->iface;
262 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
267 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
272 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
275 struct hostapd_data *hapd = ctx;
276 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
280 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
281 const u8 *data, size_t data_len,
284 struct hostapd_data *hapd = ctx;
285 struct sta_info *sta;
288 sta = ap_get_sta(hapd, addr);
290 flags = hostapd_sta_flags_to_drv(sta->flags);
292 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
297 static int hostapd_wpa_auth_for_each_sta(
298 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
301 struct hostapd_data *hapd = ctx;
302 struct sta_info *sta;
304 for (sta = hapd->sta_list; sta; sta = sta->next) {
305 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
312 struct wpa_auth_iface_iter_data {
313 int (*cb)(struct wpa_authenticator *sm, void *ctx);
317 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
319 struct wpa_auth_iface_iter_data *data = ctx;
321 for (i = 0; i < iface->num_bss; i++) {
322 if (iface->bss[i]->wpa_auth &&
323 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
330 static int hostapd_wpa_auth_for_each_auth(
331 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
334 struct hostapd_data *hapd = ctx;
335 struct wpa_auth_iface_iter_data data;
336 if (hapd->iface->interfaces == NULL ||
337 hapd->iface->interfaces->for_each_interface == NULL)
340 data.cb_ctx = cb_ctx;
341 return hapd->iface->interfaces->for_each_interface(
342 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
346 #ifdef CONFIG_IEEE80211R
348 struct wpa_auth_ft_iface_iter_data {
349 struct hostapd_data *src_hapd;
356 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
358 struct wpa_auth_ft_iface_iter_data *idata = ctx;
359 struct hostapd_data *hapd;
362 for (j = 0; j < iface->num_bss; j++) {
363 hapd = iface->bss[j];
364 if (hapd == idata->src_hapd)
366 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
367 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
368 "locally managed BSS " MACSTR "@%s -> "
370 MAC2STR(idata->src_hapd->own_addr),
371 idata->src_hapd->conf->iface,
372 MAC2STR(hapd->own_addr), hapd->conf->iface);
373 wpa_ft_rrb_rx(hapd->wpa_auth,
374 idata->src_hapd->own_addr,
375 idata->data, idata->data_len);
383 #endif /* CONFIG_IEEE80211R */
386 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
387 const u8 *data, size_t data_len)
389 struct hostapd_data *hapd = ctx;
390 struct l2_ethhdr *buf;
393 #ifdef CONFIG_IEEE80211R
394 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
395 hapd->iface->interfaces->for_each_interface) {
397 struct wpa_auth_ft_iface_iter_data idata;
398 idata.src_hapd = hapd;
401 idata.data_len = data_len;
402 res = hapd->iface->interfaces->for_each_interface(
403 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
408 #endif /* CONFIG_IEEE80211R */
410 if (hapd->driver && hapd->driver->send_ether)
411 return hapd->driver->send_ether(hapd->drv_priv, dst,
412 hapd->own_addr, proto,
414 if (hapd->l2 == NULL)
417 buf = os_malloc(sizeof(*buf) + data_len);
420 os_memcpy(buf->h_dest, dst, ETH_ALEN);
421 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
422 buf->h_proto = host_to_be16(proto);
423 os_memcpy(buf + 1, data, data_len);
424 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
425 sizeof(*buf) + data_len);
431 #ifdef CONFIG_IEEE80211R
433 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
434 const u8 *data, size_t data_len)
436 struct hostapd_data *hapd = ctx;
438 struct ieee80211_mgmt *m;
440 struct sta_info *sta;
442 sta = ap_get_sta(hapd, dst);
443 if (sta == NULL || sta->wpa_sm == NULL)
446 m = os_zalloc(sizeof(*m) + data_len);
449 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
450 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
451 WLAN_FC_STYPE_ACTION);
452 os_memcpy(m->da, dst, ETH_ALEN);
453 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
454 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
455 os_memcpy(&m->u, data, data_len);
457 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
463 static struct wpa_state_machine *
464 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
466 struct hostapd_data *hapd = ctx;
467 struct sta_info *sta;
469 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
472 sta = ap_sta_add(hapd, sta_addr);
476 sta->auth_alg = WLAN_AUTH_FT;
480 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
481 if (sta->wpa_sm == NULL) {
482 ap_free_sta(hapd, sta);
485 sta->auth_alg = WLAN_AUTH_FT;
491 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
494 struct hostapd_data *hapd = ctx;
495 struct l2_ethhdr *ethhdr;
496 if (len < sizeof(*ethhdr))
498 ethhdr = (struct l2_ethhdr *) buf;
499 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
500 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
501 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
502 len - sizeof(*ethhdr));
506 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
507 u8 *tspec_ie, size_t tspec_ielen)
509 struct hostapd_data *hapd = ctx;
510 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
513 #endif /* CONFIG_IEEE80211R */
516 int hostapd_setup_wpa(struct hostapd_data *hapd)
518 struct wpa_auth_config _conf;
519 struct wpa_auth_callbacks cb;
523 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
524 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
526 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
528 os_memset(&cb, 0, sizeof(cb));
530 cb.logger = hostapd_wpa_auth_logger;
531 cb.disconnect = hostapd_wpa_auth_disconnect;
532 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
533 cb.set_eapol = hostapd_wpa_auth_set_eapol;
534 cb.get_eapol = hostapd_wpa_auth_get_eapol;
535 cb.get_psk = hostapd_wpa_auth_get_psk;
536 cb.get_msk = hostapd_wpa_auth_get_msk;
537 cb.set_key = hostapd_wpa_auth_set_key;
538 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
539 cb.send_eapol = hostapd_wpa_auth_send_eapol;
540 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
541 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
542 cb.send_ether = hostapd_wpa_auth_send_ether;
543 #ifdef CONFIG_IEEE80211R
544 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
545 cb.add_sta = hostapd_wpa_auth_add_sta;
546 cb.add_tspec = hostapd_wpa_auth_add_tspec;
547 #endif /* CONFIG_IEEE80211R */
548 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
549 if (hapd->wpa_auth == NULL) {
550 wpa_printf(MSG_ERROR, "WPA initialization failed.");
554 if (hostapd_set_privacy(hapd, 1)) {
555 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
556 "for interface %s", hapd->conf->iface);
560 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
561 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
562 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
563 "the kernel driver.");
567 if (rsn_preauth_iface_init(hapd)) {
568 wpa_printf(MSG_ERROR, "Initialization of RSN "
569 "pre-authentication failed.");
573 #ifdef CONFIG_IEEE80211R
574 if (!hostapd_drv_none(hapd)) {
575 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
577 hapd->conf->iface, NULL, ETH_P_RRB,
578 hostapd_rrb_receive, hapd, 1);
579 if (hapd->l2 == NULL &&
580 (hapd->driver == NULL ||
581 hapd->driver->send_ether == NULL)) {
582 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
587 #endif /* CONFIG_IEEE80211R */
594 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
596 struct wpa_auth_config wpa_auth_conf;
597 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
598 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
602 void hostapd_deinit_wpa(struct hostapd_data *hapd)
604 ieee80211_tkip_countermeasures_deinit(hapd);
605 rsn_preauth_iface_deinit(hapd);
606 if (hapd->wpa_auth) {
607 wpa_deinit(hapd->wpa_auth);
608 hapd->wpa_auth = NULL;
610 if (hostapd_set_privacy(hapd, 0)) {
611 wpa_printf(MSG_DEBUG, "Could not disable "
612 "PrivacyInvoked for interface %s",
616 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
617 wpa_printf(MSG_DEBUG, "Could not remove generic "
618 "information element from interface %s",
622 ieee802_1x_deinit(hapd);
624 #ifdef CONFIG_IEEE80211R
625 l2_packet_deinit(hapd->l2);
627 #endif /* CONFIG_IEEE80211R */