2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2011, 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 "eapol_auth/eapol_auth_sm.h"
14 #include "eapol_auth/eapol_auth_sm_i.h"
15 #include "eap_server/eap.h"
16 #include "l2_packet/l2_packet.h"
17 #include "drivers/driver.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 wpa_auth_config *wconf)
32 os_memset(wconf, 0, sizeof(*wconf));
33 wconf->wpa = conf->wpa;
34 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
35 wconf->wpa_pairwise = conf->wpa_pairwise;
36 wconf->wpa_group = conf->wpa_group;
37 wconf->wpa_group_rekey = conf->wpa_group_rekey;
38 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
39 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
40 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
41 wconf->rsn_pairwise = conf->rsn_pairwise;
42 wconf->rsn_preauth = conf->rsn_preauth;
43 wconf->eapol_version = conf->eapol_version;
44 wconf->peerkey = conf->peerkey;
45 wconf->wmm_enabled = conf->wmm_enabled;
46 wconf->wmm_uapsd = conf->wmm_uapsd;
47 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
48 wconf->okc = conf->okc;
49 #ifdef CONFIG_IEEE80211W
50 wconf->ieee80211w = conf->ieee80211w;
51 #endif /* CONFIG_IEEE80211W */
52 #ifdef CONFIG_IEEE80211R
53 wconf->ssid_len = conf->ssid.ssid_len;
54 if (wconf->ssid_len > SSID_LEN)
55 wconf->ssid_len = SSID_LEN;
56 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
57 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
58 MOBILITY_DOMAIN_ID_LEN);
59 if (conf->nas_identifier &&
60 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
61 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
62 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
63 wconf->r0_key_holder_len);
65 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
66 wconf->r0_key_lifetime = conf->r0_key_lifetime;
67 wconf->reassociation_deadline = conf->reassociation_deadline;
68 wconf->r0kh_list = conf->r0kh_list;
69 wconf->r1kh_list = conf->r1kh_list;
70 wconf->pmk_r1_push = conf->pmk_r1_push;
71 wconf->ft_over_ds = conf->ft_over_ds;
72 #endif /* CONFIG_IEEE80211R */
76 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
77 logger_level level, const char *txt)
79 #ifndef CONFIG_NO_HOSTAPD_LOGGER
80 struct hostapd_data *hapd = ctx;
85 hlevel = HOSTAPD_LEVEL_WARNING;
88 hlevel = HOSTAPD_LEVEL_INFO;
92 hlevel = HOSTAPD_LEVEL_DEBUG;
96 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
97 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
101 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
104 struct hostapd_data *hapd = ctx;
105 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
106 "STA " MACSTR " reason %d",
107 __func__, MAC2STR(addr), reason);
108 ap_sta_disconnect(hapd, NULL, addr, reason);
112 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
114 struct hostapd_data *hapd = ctx;
115 michael_mic_failure(hapd, addr, 0);
119 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
120 wpa_eapol_variable var, int value)
122 struct hostapd_data *hapd = ctx;
123 struct sta_info *sta = ap_get_sta(hapd, addr);
127 case WPA_EAPOL_portEnabled:
128 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
130 case WPA_EAPOL_portValid:
131 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
133 case WPA_EAPOL_authorized:
134 ieee802_1x_set_sta_authorized(hapd, sta, value);
136 case WPA_EAPOL_portControl_Auto:
138 sta->eapol_sm->portControl = Auto;
140 case WPA_EAPOL_keyRun:
142 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
144 case WPA_EAPOL_keyAvailable:
146 sta->eapol_sm->eap_if->eapKeyAvailable =
147 value ? TRUE : FALSE;
149 case WPA_EAPOL_keyDone:
151 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
153 case WPA_EAPOL_inc_EapolFramesTx:
155 sta->eapol_sm->dot1xAuthEapolFramesTx++;
161 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
162 wpa_eapol_variable var)
164 struct hostapd_data *hapd = ctx;
165 struct sta_info *sta = ap_get_sta(hapd, addr);
166 if (sta == NULL || sta->eapol_sm == NULL)
169 case WPA_EAPOL_keyRun:
170 return sta->eapol_sm->keyRun;
171 case WPA_EAPOL_keyAvailable:
172 return sta->eapol_sm->eap_if->eapKeyAvailable;
179 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
182 struct hostapd_data *hapd = ctx;
183 struct sta_info *sta = ap_get_sta(hapd, addr);
186 return hostapd_get_psk(hapd->conf, addr, prev_psk);
190 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
193 struct hostapd_data *hapd = ctx;
196 struct sta_info *sta;
198 sta = ap_get_sta(hapd, addr);
202 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
208 os_memcpy(msk, key, keylen);
215 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
216 const u8 *addr, int idx, u8 *key,
219 struct hostapd_data *hapd = ctx;
220 const char *ifname = hapd->conf->iface;
223 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
228 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
233 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
236 struct hostapd_data *hapd = ctx;
237 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
241 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
242 const u8 *data, size_t data_len,
245 struct hostapd_data *hapd = ctx;
246 struct sta_info *sta;
249 sta = ap_get_sta(hapd, addr);
251 flags = hostapd_sta_flags_to_drv(sta->flags);
253 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
258 static int hostapd_wpa_auth_for_each_sta(
259 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
262 struct hostapd_data *hapd = ctx;
263 struct sta_info *sta;
265 for (sta = hapd->sta_list; sta; sta = sta->next) {
266 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
273 struct wpa_auth_iface_iter_data {
274 int (*cb)(struct wpa_authenticator *sm, void *ctx);
278 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
280 struct wpa_auth_iface_iter_data *data = ctx;
282 for (i = 0; i < iface->num_bss; i++) {
283 if (iface->bss[i]->wpa_auth &&
284 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
291 static int hostapd_wpa_auth_for_each_auth(
292 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
295 struct hostapd_data *hapd = ctx;
296 struct wpa_auth_iface_iter_data data;
297 if (hapd->iface->for_each_interface == NULL)
300 data.cb_ctx = cb_ctx;
301 return hapd->iface->for_each_interface(hapd->iface->interfaces,
302 wpa_auth_iface_iter, &data);
306 #ifdef CONFIG_IEEE80211R
308 struct wpa_auth_ft_iface_iter_data {
309 struct hostapd_data *src_hapd;
316 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
318 struct wpa_auth_ft_iface_iter_data *idata = ctx;
319 struct hostapd_data *hapd;
322 for (j = 0; j < iface->num_bss; j++) {
323 hapd = iface->bss[j];
324 if (hapd == idata->src_hapd)
326 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
327 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
328 "locally managed BSS " MACSTR "@%s -> "
330 MAC2STR(idata->src_hapd->own_addr),
331 idata->src_hapd->conf->iface,
332 MAC2STR(hapd->own_addr), hapd->conf->iface);
333 wpa_ft_rrb_rx(hapd->wpa_auth,
334 idata->src_hapd->own_addr,
335 idata->data, idata->data_len);
343 #endif /* CONFIG_IEEE80211R */
346 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
347 const u8 *data, size_t data_len)
349 struct hostapd_data *hapd = ctx;
350 struct l2_ethhdr *buf;
353 #ifdef CONFIG_IEEE80211R
354 if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
356 struct wpa_auth_ft_iface_iter_data idata;
357 idata.src_hapd = hapd;
360 idata.data_len = data_len;
361 res = hapd->iface->for_each_interface(hapd->iface->interfaces,
362 hostapd_wpa_auth_ft_iter,
367 #endif /* CONFIG_IEEE80211R */
369 if (hapd->driver && hapd->driver->send_ether)
370 return hapd->driver->send_ether(hapd->drv_priv, dst,
371 hapd->own_addr, proto,
373 if (hapd->l2 == NULL)
376 buf = os_malloc(sizeof(*buf) + data_len);
379 os_memcpy(buf->h_dest, dst, ETH_ALEN);
380 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
381 buf->h_proto = host_to_be16(proto);
382 os_memcpy(buf + 1, data, data_len);
383 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
384 sizeof(*buf) + data_len);
390 #ifdef CONFIG_IEEE80211R
392 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
393 const u8 *data, size_t data_len)
395 struct hostapd_data *hapd = ctx;
397 struct ieee80211_mgmt *m;
399 struct sta_info *sta;
401 sta = ap_get_sta(hapd, dst);
402 if (sta == NULL || sta->wpa_sm == NULL)
405 m = os_zalloc(sizeof(*m) + data_len);
408 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
409 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
410 WLAN_FC_STYPE_ACTION);
411 os_memcpy(m->da, dst, ETH_ALEN);
412 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
413 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
414 os_memcpy(&m->u, data, data_len);
416 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
422 static struct wpa_state_machine *
423 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
425 struct hostapd_data *hapd = ctx;
426 struct sta_info *sta;
428 sta = ap_sta_add(hapd, sta_addr);
432 sta->auth_alg = WLAN_AUTH_FT;
436 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
437 if (sta->wpa_sm == NULL) {
438 ap_free_sta(hapd, sta);
441 sta->auth_alg = WLAN_AUTH_FT;
447 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
450 struct hostapd_data *hapd = ctx;
451 struct l2_ethhdr *ethhdr;
452 if (len < sizeof(*ethhdr))
454 ethhdr = (struct l2_ethhdr *) buf;
455 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
456 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
457 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
458 len - sizeof(*ethhdr));
461 #endif /* CONFIG_IEEE80211R */
464 int hostapd_setup_wpa(struct hostapd_data *hapd)
466 struct wpa_auth_config _conf;
467 struct wpa_auth_callbacks cb;
471 hostapd_wpa_auth_conf(hapd->conf, &_conf);
472 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
474 os_memset(&cb, 0, sizeof(cb));
476 cb.logger = hostapd_wpa_auth_logger;
477 cb.disconnect = hostapd_wpa_auth_disconnect;
478 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
479 cb.set_eapol = hostapd_wpa_auth_set_eapol;
480 cb.get_eapol = hostapd_wpa_auth_get_eapol;
481 cb.get_psk = hostapd_wpa_auth_get_psk;
482 cb.get_msk = hostapd_wpa_auth_get_msk;
483 cb.set_key = hostapd_wpa_auth_set_key;
484 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
485 cb.send_eapol = hostapd_wpa_auth_send_eapol;
486 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
487 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
488 cb.send_ether = hostapd_wpa_auth_send_ether;
489 #ifdef CONFIG_IEEE80211R
490 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
491 cb.add_sta = hostapd_wpa_auth_add_sta;
492 #endif /* CONFIG_IEEE80211R */
493 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
494 if (hapd->wpa_auth == NULL) {
495 wpa_printf(MSG_ERROR, "WPA initialization failed.");
499 if (hostapd_set_privacy(hapd, 1)) {
500 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
501 "for interface %s", hapd->conf->iface);
505 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
506 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
507 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
508 "the kernel driver.");
512 if (rsn_preauth_iface_init(hapd)) {
513 wpa_printf(MSG_ERROR, "Initialization of RSN "
514 "pre-authentication failed.");
518 #ifdef CONFIG_IEEE80211R
519 if (!hostapd_drv_none(hapd)) {
520 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
522 hapd->conf->iface, NULL, ETH_P_RRB,
523 hostapd_rrb_receive, hapd, 1);
524 if (hapd->l2 == NULL &&
525 (hapd->driver == NULL ||
526 hapd->driver->send_ether == NULL)) {
527 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
532 #endif /* CONFIG_IEEE80211R */
539 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
541 struct wpa_auth_config wpa_auth_conf;
542 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
543 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
547 void hostapd_deinit_wpa(struct hostapd_data *hapd)
549 ieee80211_tkip_countermeasures_deinit(hapd);
550 rsn_preauth_iface_deinit(hapd);
551 if (hapd->wpa_auth) {
552 wpa_deinit(hapd->wpa_auth);
553 hapd->wpa_auth = NULL;
555 if (hostapd_set_privacy(hapd, 0)) {
556 wpa_printf(MSG_DEBUG, "Could not disable "
557 "PrivacyInvoked for interface %s",
561 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
562 wpa_printf(MSG_DEBUG, "Could not remove generic "
563 "information element from interface %s",
567 ieee802_1x_deinit(hapd);
569 #ifdef CONFIG_IEEE80211R
570 l2_packet_deinit(hapd->l2);
571 #endif /* CONFIG_IEEE80211R */