2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
21 #include "eap_server/eap.h"
22 #include "l2_packet/l2_packet.h"
23 #include "drivers/driver.h"
25 #include "ieee802_1x.h"
26 #include "preauth_auth.h"
28 #include "tkip_countermeasures.h"
29 #include "ap_drv_ops.h"
30 #include "ap_config.h"
34 #ifdef CONFIG_IEEE80211R
35 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
37 #endif /* CONFIG_IEEE80211R */
40 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
41 struct wpa_auth_config *wconf)
43 wconf->wpa = conf->wpa;
44 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
45 wconf->wpa_pairwise = conf->wpa_pairwise;
46 wconf->wpa_group = conf->wpa_group;
47 wconf->wpa_group_rekey = conf->wpa_group_rekey;
48 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
49 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
50 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
51 wconf->rsn_pairwise = conf->rsn_pairwise;
52 wconf->rsn_preauth = conf->rsn_preauth;
53 wconf->eapol_version = conf->eapol_version;
54 wconf->peerkey = conf->peerkey;
55 wconf->wmm_enabled = conf->wmm_enabled;
56 wconf->wmm_uapsd = conf->wmm_uapsd;
57 wconf->okc = conf->okc;
58 #ifdef CONFIG_IEEE80211W
59 wconf->ieee80211w = conf->ieee80211w;
60 #endif /* CONFIG_IEEE80211W */
61 #ifdef CONFIG_IEEE80211R
62 wconf->ssid_len = conf->ssid.ssid_len;
63 if (wconf->ssid_len > SSID_LEN)
64 wconf->ssid_len = SSID_LEN;
65 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
66 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
67 MOBILITY_DOMAIN_ID_LEN);
68 if (conf->nas_identifier &&
69 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
70 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
71 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
72 wconf->r0_key_holder_len);
74 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
75 wconf->r0_key_lifetime = conf->r0_key_lifetime;
76 wconf->reassociation_deadline = conf->reassociation_deadline;
77 wconf->r0kh_list = conf->r0kh_list;
78 wconf->r1kh_list = conf->r1kh_list;
79 wconf->pmk_r1_push = conf->pmk_r1_push;
80 #endif /* CONFIG_IEEE80211R */
84 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
85 logger_level level, const char *txt)
87 #ifndef CONFIG_NO_HOSTAPD_LOGGER
88 struct hostapd_data *hapd = ctx;
93 hlevel = HOSTAPD_LEVEL_WARNING;
96 hlevel = HOSTAPD_LEVEL_INFO;
100 hlevel = HOSTAPD_LEVEL_DEBUG;
104 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
105 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
109 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
112 struct hostapd_data *hapd = ctx;
113 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
114 "STA " MACSTR " reason %d",
115 __func__, MAC2STR(addr), reason);
116 ap_sta_disconnect(hapd, NULL, addr, reason);
120 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
122 struct hostapd_data *hapd = ctx;
123 michael_mic_failure(hapd, addr, 0);
127 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
128 wpa_eapol_variable var, int value)
130 struct hostapd_data *hapd = ctx;
131 struct sta_info *sta = ap_get_sta(hapd, addr);
135 case WPA_EAPOL_portEnabled:
136 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
138 case WPA_EAPOL_portValid:
139 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
141 case WPA_EAPOL_authorized:
142 ieee802_1x_set_sta_authorized(hapd, sta, value);
144 case WPA_EAPOL_portControl_Auto:
146 sta->eapol_sm->portControl = Auto;
148 case WPA_EAPOL_keyRun:
150 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
152 case WPA_EAPOL_keyAvailable:
154 sta->eapol_sm->eap_if->eapKeyAvailable =
155 value ? TRUE : FALSE;
157 case WPA_EAPOL_keyDone:
159 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
161 case WPA_EAPOL_inc_EapolFramesTx:
163 sta->eapol_sm->dot1xAuthEapolFramesTx++;
169 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
170 wpa_eapol_variable var)
172 struct hostapd_data *hapd = ctx;
173 struct sta_info *sta = ap_get_sta(hapd, addr);
174 if (sta == NULL || sta->eapol_sm == NULL)
177 case WPA_EAPOL_keyRun:
178 return sta->eapol_sm->keyRun;
179 case WPA_EAPOL_keyAvailable:
180 return sta->eapol_sm->eap_if->eapKeyAvailable;
187 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
190 struct hostapd_data *hapd = ctx;
191 return hostapd_get_psk(hapd->conf, addr, prev_psk);
195 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
198 struct hostapd_data *hapd = ctx;
201 struct sta_info *sta;
203 sta = ap_get_sta(hapd, addr);
207 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
213 os_memcpy(msk, key, keylen);
220 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
221 const u8 *addr, int idx, u8 *key,
224 struct hostapd_data *hapd = ctx;
225 const char *ifname = hapd->conf->iface;
228 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
233 return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
238 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
241 struct hostapd_data *hapd = ctx;
242 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
246 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
247 const u8 *data, size_t data_len,
250 struct hostapd_data *hapd = ctx;
251 return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
255 static int hostapd_wpa_auth_for_each_sta(
256 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
259 struct hostapd_data *hapd = ctx;
260 struct sta_info *sta;
262 for (sta = hapd->sta_list; sta; sta = sta->next) {
263 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
270 struct wpa_auth_iface_iter_data {
271 int (*cb)(struct wpa_authenticator *sm, void *ctx);
275 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
277 struct wpa_auth_iface_iter_data *data = ctx;
279 for (i = 0; i < iface->num_bss; i++) {
280 if (iface->bss[i]->wpa_auth &&
281 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
288 static int hostapd_wpa_auth_for_each_auth(
289 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
292 struct hostapd_data *hapd = ctx;
293 struct wpa_auth_iface_iter_data data;
294 if (hapd->iface->for_each_interface == NULL)
297 data.cb_ctx = cb_ctx;
298 return hapd->iface->for_each_interface(hapd->iface->interfaces,
299 wpa_auth_iface_iter, &data);
303 #ifdef CONFIG_IEEE80211R
305 struct wpa_auth_ft_iface_iter_data {
306 struct hostapd_data *src_hapd;
313 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
315 struct wpa_auth_ft_iface_iter_data *idata = ctx;
316 struct hostapd_data *hapd;
319 for (j = 0; j < iface->num_bss; j++) {
320 hapd = iface->bss[j];
321 if (hapd == idata->src_hapd)
323 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
324 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
325 "locally managed BSS " MACSTR "@%s -> "
327 MAC2STR(idata->src_hapd->own_addr),
328 idata->src_hapd->conf->iface,
329 MAC2STR(hapd->own_addr), hapd->conf->iface);
330 hostapd_rrb_receive(hapd, idata->src_hapd->own_addr,
331 idata->data, idata->data_len);
339 #endif /* CONFIG_IEEE80211R */
342 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
343 const u8 *data, size_t data_len)
345 struct hostapd_data *hapd = ctx;
347 #ifdef CONFIG_IEEE80211R
348 if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
350 struct wpa_auth_ft_iface_iter_data idata;
351 idata.src_hapd = hapd;
354 idata.data_len = data_len;
355 res = hapd->iface->for_each_interface(hapd->iface->interfaces,
356 hostapd_wpa_auth_ft_iter,
361 #endif /* CONFIG_IEEE80211R */
363 if (hapd->driver && hapd->driver->send_ether)
364 return hapd->driver->send_ether(hapd->drv_priv, dst,
365 hapd->own_addr, proto,
367 if (hapd->l2 == NULL)
369 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
373 #ifdef CONFIG_IEEE80211R
375 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
376 const u8 *data, size_t data_len)
378 struct hostapd_data *hapd = ctx;
380 struct ieee80211_mgmt *m;
382 struct sta_info *sta;
384 sta = ap_get_sta(hapd, dst);
385 if (sta == NULL || sta->wpa_sm == NULL)
388 m = os_zalloc(sizeof(*m) + data_len);
391 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
392 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
393 WLAN_FC_STYPE_ACTION);
394 os_memcpy(m->da, dst, ETH_ALEN);
395 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
396 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
397 os_memcpy(&m->u, data, data_len);
399 res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
405 static struct wpa_state_machine *
406 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
408 struct hostapd_data *hapd = ctx;
409 struct sta_info *sta;
411 sta = ap_sta_add(hapd, sta_addr);
415 sta->auth_alg = WLAN_AUTH_FT;
419 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
420 if (sta->wpa_sm == NULL) {
421 ap_free_sta(hapd, sta);
424 sta->auth_alg = WLAN_AUTH_FT;
430 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
433 struct hostapd_data *hapd = ctx;
434 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
437 #endif /* CONFIG_IEEE80211R */
440 int hostapd_setup_wpa(struct hostapd_data *hapd)
442 struct wpa_auth_config _conf;
443 struct wpa_auth_callbacks cb;
447 hostapd_wpa_auth_conf(hapd->conf, &_conf);
448 os_memset(&cb, 0, sizeof(cb));
450 cb.logger = hostapd_wpa_auth_logger;
451 cb.disconnect = hostapd_wpa_auth_disconnect;
452 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
453 cb.set_eapol = hostapd_wpa_auth_set_eapol;
454 cb.get_eapol = hostapd_wpa_auth_get_eapol;
455 cb.get_psk = hostapd_wpa_auth_get_psk;
456 cb.get_msk = hostapd_wpa_auth_get_msk;
457 cb.set_key = hostapd_wpa_auth_set_key;
458 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
459 cb.send_eapol = hostapd_wpa_auth_send_eapol;
460 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
461 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
462 cb.send_ether = hostapd_wpa_auth_send_ether;
463 #ifdef CONFIG_IEEE80211R
464 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
465 cb.add_sta = hostapd_wpa_auth_add_sta;
466 #endif /* CONFIG_IEEE80211R */
467 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
468 if (hapd->wpa_auth == NULL) {
469 wpa_printf(MSG_ERROR, "WPA initialization failed.");
473 if (hostapd_set_privacy(hapd, 1)) {
474 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
475 "for interface %s", hapd->conf->iface);
479 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
480 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
481 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
482 "the kernel driver.");
486 if (rsn_preauth_iface_init(hapd)) {
487 wpa_printf(MSG_ERROR, "Initialization of RSN "
488 "pre-authentication failed.");
492 #ifdef CONFIG_IEEE80211R
493 if (!hostapd_drv_none(hapd)) {
494 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
496 hapd->conf->iface, NULL, ETH_P_RRB,
497 hostapd_rrb_receive, hapd, 0);
498 if (hapd->l2 == NULL &&
499 (hapd->driver == NULL ||
500 hapd->driver->send_ether == NULL)) {
501 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
506 #endif /* CONFIG_IEEE80211R */
513 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
515 struct wpa_auth_config wpa_auth_conf;
516 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
517 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
521 void hostapd_deinit_wpa(struct hostapd_data *hapd)
523 rsn_preauth_iface_deinit(hapd);
524 if (hapd->wpa_auth) {
525 wpa_deinit(hapd->wpa_auth);
526 hapd->wpa_auth = NULL;
528 if (hostapd_set_privacy(hapd, 0)) {
529 wpa_printf(MSG_DEBUG, "Could not disable "
530 "PrivacyInvoked for interface %s",
534 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
535 wpa_printf(MSG_DEBUG, "Could not remove generic "
536 "information element from interface %s",
540 ieee802_1x_deinit(hapd);
542 #ifdef CONFIG_IEEE80211R
543 l2_packet_deinit(hapd->l2);
544 #endif /* CONFIG_IEEE80211R */