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"
18 #include "drivers/driver.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 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 */
80 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
81 logger_level level, const char *txt)
83 #ifndef CONFIG_NO_HOSTAPD_LOGGER
84 struct hostapd_data *hapd = ctx;
89 hlevel = HOSTAPD_LEVEL_WARNING;
92 hlevel = HOSTAPD_LEVEL_INFO;
96 hlevel = HOSTAPD_LEVEL_DEBUG;
100 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
101 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
105 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
108 struct hostapd_data *hapd = ctx;
109 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
110 "STA " MACSTR " reason %d",
111 __func__, MAC2STR(addr), reason);
112 ap_sta_disconnect(hapd, NULL, addr, reason);
116 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
118 struct hostapd_data *hapd = ctx;
119 return michael_mic_failure(hapd, addr, 0);
123 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
124 wpa_eapol_variable var, int value)
126 struct hostapd_data *hapd = ctx;
127 struct sta_info *sta = ap_get_sta(hapd, addr);
131 case WPA_EAPOL_portEnabled:
132 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
134 case WPA_EAPOL_portValid:
135 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
137 case WPA_EAPOL_authorized:
138 ieee802_1x_set_sta_authorized(hapd, sta, value);
140 case WPA_EAPOL_portControl_Auto:
142 sta->eapol_sm->portControl = Auto;
144 case WPA_EAPOL_keyRun:
146 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
148 case WPA_EAPOL_keyAvailable:
150 sta->eapol_sm->eap_if->eapKeyAvailable =
151 value ? TRUE : FALSE;
153 case WPA_EAPOL_keyDone:
155 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
157 case WPA_EAPOL_inc_EapolFramesTx:
159 sta->eapol_sm->dot1xAuthEapolFramesTx++;
165 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
166 wpa_eapol_variable var)
168 struct hostapd_data *hapd = ctx;
169 struct sta_info *sta = ap_get_sta(hapd, addr);
170 if (sta == NULL || sta->eapol_sm == NULL)
173 case WPA_EAPOL_keyRun:
174 return sta->eapol_sm->keyRun;
175 case WPA_EAPOL_keyAvailable:
176 return sta->eapol_sm->eap_if->eapKeyAvailable;
183 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
186 struct hostapd_data *hapd = ctx;
187 struct sta_info *sta = ap_get_sta(hapd, addr);
191 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
192 if (!sta->sae || prev_psk)
194 return sta->sae->pmk;
196 #endif /* CONFIG_SAE */
198 psk = hostapd_get_psk(hapd->conf, addr, prev_psk);
200 * This is about to iterate over all psks, prev_psk gives the last
201 * returned psk which should not be returned again.
202 * logic list (all hostapd_get_psk; all sta->psk)
204 if (sta && sta->psk && !psk) {
205 struct hostapd_sta_wpa_psk_short *pos;
207 for (pos = sta->psk; pos; pos = pos->next) {
208 if (pos->psk == prev_psk) {
209 psk = pos->next ? pos->next->psk : NULL;
218 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
221 struct hostapd_data *hapd = ctx;
224 struct sta_info *sta;
226 sta = ap_get_sta(hapd, addr);
230 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
236 os_memcpy(msk, key, keylen);
243 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
244 const u8 *addr, int idx, u8 *key,
247 struct hostapd_data *hapd = ctx;
248 const char *ifname = hapd->conf->iface;
251 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
256 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
261 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
264 struct hostapd_data *hapd = ctx;
265 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
269 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
270 const u8 *data, size_t data_len,
273 struct hostapd_data *hapd = ctx;
274 struct sta_info *sta;
277 sta = ap_get_sta(hapd, addr);
279 flags = hostapd_sta_flags_to_drv(sta->flags);
281 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
286 static int hostapd_wpa_auth_for_each_sta(
287 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
290 struct hostapd_data *hapd = ctx;
291 struct sta_info *sta;
293 for (sta = hapd->sta_list; sta; sta = sta->next) {
294 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
301 struct wpa_auth_iface_iter_data {
302 int (*cb)(struct wpa_authenticator *sm, void *ctx);
306 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
308 struct wpa_auth_iface_iter_data *data = ctx;
310 for (i = 0; i < iface->num_bss; i++) {
311 if (iface->bss[i]->wpa_auth &&
312 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
319 static int hostapd_wpa_auth_for_each_auth(
320 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
323 struct hostapd_data *hapd = ctx;
324 struct wpa_auth_iface_iter_data data;
325 if (hapd->iface->interfaces == NULL ||
326 hapd->iface->interfaces->for_each_interface == NULL)
329 data.cb_ctx = cb_ctx;
330 return hapd->iface->interfaces->for_each_interface(
331 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
335 #ifdef CONFIG_IEEE80211R
337 struct wpa_auth_ft_iface_iter_data {
338 struct hostapd_data *src_hapd;
345 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
347 struct wpa_auth_ft_iface_iter_data *idata = ctx;
348 struct hostapd_data *hapd;
351 for (j = 0; j < iface->num_bss; j++) {
352 hapd = iface->bss[j];
353 if (hapd == idata->src_hapd)
355 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
356 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
357 "locally managed BSS " MACSTR "@%s -> "
359 MAC2STR(idata->src_hapd->own_addr),
360 idata->src_hapd->conf->iface,
361 MAC2STR(hapd->own_addr), hapd->conf->iface);
362 wpa_ft_rrb_rx(hapd->wpa_auth,
363 idata->src_hapd->own_addr,
364 idata->data, idata->data_len);
372 #endif /* CONFIG_IEEE80211R */
375 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
376 const u8 *data, size_t data_len)
378 struct hostapd_data *hapd = ctx;
379 struct l2_ethhdr *buf;
382 #ifdef CONFIG_IEEE80211R
383 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
384 hapd->iface->interfaces->for_each_interface) {
386 struct wpa_auth_ft_iface_iter_data idata;
387 idata.src_hapd = hapd;
390 idata.data_len = data_len;
391 res = hapd->iface->interfaces->for_each_interface(
392 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
397 #endif /* CONFIG_IEEE80211R */
399 if (hapd->driver && hapd->driver->send_ether)
400 return hapd->driver->send_ether(hapd->drv_priv, dst,
401 hapd->own_addr, proto,
403 if (hapd->l2 == NULL)
406 buf = os_malloc(sizeof(*buf) + data_len);
409 os_memcpy(buf->h_dest, dst, ETH_ALEN);
410 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
411 buf->h_proto = host_to_be16(proto);
412 os_memcpy(buf + 1, data, data_len);
413 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
414 sizeof(*buf) + data_len);
420 #ifdef CONFIG_IEEE80211R
422 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
423 const u8 *data, size_t data_len)
425 struct hostapd_data *hapd = ctx;
427 struct ieee80211_mgmt *m;
429 struct sta_info *sta;
431 sta = ap_get_sta(hapd, dst);
432 if (sta == NULL || sta->wpa_sm == NULL)
435 m = os_zalloc(sizeof(*m) + data_len);
438 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
439 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
440 WLAN_FC_STYPE_ACTION);
441 os_memcpy(m->da, dst, ETH_ALEN);
442 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
443 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
444 os_memcpy(&m->u, data, data_len);
446 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
452 static struct wpa_state_machine *
453 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
455 struct hostapd_data *hapd = ctx;
456 struct sta_info *sta;
458 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
461 sta = ap_sta_add(hapd, sta_addr);
465 sta->auth_alg = WLAN_AUTH_FT;
469 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
470 if (sta->wpa_sm == NULL) {
471 ap_free_sta(hapd, sta);
474 sta->auth_alg = WLAN_AUTH_FT;
480 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
483 struct hostapd_data *hapd = ctx;
484 struct l2_ethhdr *ethhdr;
485 if (len < sizeof(*ethhdr))
487 ethhdr = (struct l2_ethhdr *) buf;
488 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
489 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
490 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
491 len - sizeof(*ethhdr));
495 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
496 u8 *tspec_ie, size_t tspec_ielen)
498 struct hostapd_data *hapd = ctx;
499 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
502 #endif /* CONFIG_IEEE80211R */
505 int hostapd_setup_wpa(struct hostapd_data *hapd)
507 struct wpa_auth_config _conf;
508 struct wpa_auth_callbacks cb;
512 hostapd_wpa_auth_conf(hapd->conf, &_conf);
513 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
515 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
517 os_memset(&cb, 0, sizeof(cb));
519 cb.logger = hostapd_wpa_auth_logger;
520 cb.disconnect = hostapd_wpa_auth_disconnect;
521 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
522 cb.set_eapol = hostapd_wpa_auth_set_eapol;
523 cb.get_eapol = hostapd_wpa_auth_get_eapol;
524 cb.get_psk = hostapd_wpa_auth_get_psk;
525 cb.get_msk = hostapd_wpa_auth_get_msk;
526 cb.set_key = hostapd_wpa_auth_set_key;
527 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
528 cb.send_eapol = hostapd_wpa_auth_send_eapol;
529 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
530 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
531 cb.send_ether = hostapd_wpa_auth_send_ether;
532 #ifdef CONFIG_IEEE80211R
533 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
534 cb.add_sta = hostapd_wpa_auth_add_sta;
535 cb.add_tspec = hostapd_wpa_auth_add_tspec;
536 #endif /* CONFIG_IEEE80211R */
537 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
538 if (hapd->wpa_auth == NULL) {
539 wpa_printf(MSG_ERROR, "WPA initialization failed.");
543 if (hostapd_set_privacy(hapd, 1)) {
544 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
545 "for interface %s", hapd->conf->iface);
549 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
550 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
551 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
552 "the kernel driver.");
556 if (rsn_preauth_iface_init(hapd)) {
557 wpa_printf(MSG_ERROR, "Initialization of RSN "
558 "pre-authentication failed.");
562 #ifdef CONFIG_IEEE80211R
563 if (!hostapd_drv_none(hapd)) {
564 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
566 hapd->conf->iface, NULL, ETH_P_RRB,
567 hostapd_rrb_receive, hapd, 1);
568 if (hapd->l2 == NULL &&
569 (hapd->driver == NULL ||
570 hapd->driver->send_ether == NULL)) {
571 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
576 #endif /* CONFIG_IEEE80211R */
583 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
585 struct wpa_auth_config wpa_auth_conf;
586 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
587 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
591 void hostapd_deinit_wpa(struct hostapd_data *hapd)
593 ieee80211_tkip_countermeasures_deinit(hapd);
594 rsn_preauth_iface_deinit(hapd);
595 if (hapd->wpa_auth) {
596 wpa_deinit(hapd->wpa_auth);
597 hapd->wpa_auth = NULL;
599 if (hostapd_set_privacy(hapd, 0)) {
600 wpa_printf(MSG_DEBUG, "Could not disable "
601 "PrivacyInvoked for interface %s",
605 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
606 wpa_printf(MSG_DEBUG, "Could not remove generic "
607 "information element from interface %s",
611 ieee802_1x_deinit(hapd);
613 #ifdef CONFIG_IEEE80211R
614 l2_packet_deinit(hapd->l2);
615 #endif /* CONFIG_IEEE80211R */