2 * hostapd / Callback functions for driver wrappers
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 "radius/radius.h"
19 #include "drivers/driver.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
22 #include "crypto/random.h"
26 #include "ieee802_11.h"
28 #include "accounting.h"
29 #include "tkip_countermeasures.h"
30 #include "ieee802_1x.h"
32 #include "wps_hostapd.h"
33 #include "ap_drv_ops.h"
34 #include "ap_config.h"
37 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
38 const u8 *req_ies, size_t req_ies_len, int reassoc)
42 struct ieee802_11_elems elems;
45 u16 reason = WLAN_REASON_UNSPECIFIED;
49 * This could potentially happen with unexpected event from the
50 * driver wrapper. This was seen at least in one case where the
51 * driver ended up being set to station mode while hostapd was
52 * running, so better make sure we stop processing such an
55 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
59 random_add_randomness(addr, ETH_ALEN);
61 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
62 HOSTAPD_LEVEL_INFO, "associated");
64 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
66 ie = elems.wps_ie - 2;
67 ielen = elems.wps_ie_len + 2;
68 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
69 } else if (elems.rsn_ie) {
70 ie = elems.rsn_ie - 2;
71 ielen = elems.rsn_ie_len + 2;
72 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
73 } else if (elems.wpa_ie) {
74 ie = elems.wpa_ie - 2;
75 ielen = elems.wpa_ie_len + 2;
76 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
80 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
84 sta = ap_get_sta(hapd, addr);
86 accounting_sta_stop(hapd, sta);
88 sta = ap_sta_add(hapd, addr);
92 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
96 wpabuf_free(sta->p2p_ie);
97 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
100 #endif /* CONFIG_P2P */
102 if (hapd->conf->wpa) {
103 if (ie == NULL || ielen == 0) {
105 if (hapd->conf->wps_state) {
106 wpa_printf(MSG_DEBUG, "STA did not include "
107 "WPA/RSN IE in (Re)Association "
108 "Request - possible WPS use");
109 sta->flags |= WLAN_STA_MAYBE_WPS;
112 #endif /* CONFIG_WPS */
114 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
118 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
119 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
121 sta->flags |= WLAN_STA_WPS;
122 wps = ieee802_11_vendor_ie_concat(ie, ielen,
125 if (wps_is_20(wps)) {
126 wpa_printf(MSG_DEBUG, "WPS: STA "
128 sta->flags |= WLAN_STA_WPS2;
134 #endif /* CONFIG_WPS */
136 if (sta->wpa_sm == NULL)
137 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
139 if (sta->wpa_sm == NULL) {
140 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
144 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
146 if (res != WPA_IE_OK) {
147 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
148 "rejected? (res %u)", res);
149 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
150 if (res == WPA_INVALID_GROUP)
151 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
152 else if (res == WPA_INVALID_PAIRWISE)
153 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
154 else if (res == WPA_INVALID_AKMP)
155 reason = WLAN_REASON_AKMP_NOT_VALID;
156 #ifdef CONFIG_IEEE80211W
157 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
158 reason = WLAN_REASON_INVALID_IE;
159 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
160 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
161 #endif /* CONFIG_IEEE80211W */
163 reason = WLAN_REASON_INVALID_IE;
166 } else if (hapd->conf->wps_state) {
170 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
174 #ifdef CONFIG_WPS_STRICT
175 if (wps && wps_validate_assoc_req(wps) < 0) {
176 reason = WLAN_REASON_INVALID_IE;
180 #endif /* CONFIG_WPS_STRICT */
182 sta->flags |= WLAN_STA_WPS;
183 if (wps_is_20(wps)) {
184 wpa_printf(MSG_DEBUG, "WPS: STA supports "
186 sta->flags |= WLAN_STA_WPS2;
189 sta->flags |= WLAN_STA_MAYBE_WPS;
191 #endif /* CONFIG_WPS */
195 #endif /* CONFIG_WPS */
197 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
198 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
199 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
201 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
203 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
207 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
208 req_ies, req_ies_len);
210 #endif /* CONFIG_P2P */
215 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
216 ap_free_sta(hapd, sta);
221 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
223 struct sta_info *sta;
227 * This could potentially happen with unexpected event from the
228 * driver wrapper. This was seen at least in one case where the
229 * driver ended up reporting a station mode event while hostapd
230 * was running, so better make sure we stop processing such an
233 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
238 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
239 HOSTAPD_LEVEL_INFO, "disassociated");
241 sta = ap_get_sta(hapd, addr);
243 wpa_printf(MSG_DEBUG, "Disassociation notification for "
244 "unknown STA " MACSTR, MAC2STR(addr));
248 ap_sta_set_authorized(hapd, sta, 0);
249 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
250 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
251 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
252 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
253 ap_free_sta(hapd, sta);
257 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
259 struct sta_info *sta = ap_get_sta(hapd, addr);
261 if (!sta || !hapd->conf->disassoc_low_ack)
264 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
265 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
267 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
269 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
273 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
274 const u8 *bssid, const u8 *ie, size_t ie_len)
279 if (sa == NULL || ie == NULL)
282 random_add_randomness(sa, ETH_ALEN);
283 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
284 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
285 sa, da, bssid, ie, ie_len) > 0) {
298 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
300 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
307 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
308 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
309 return HAPD_BROADCAST;
311 for (i = 0; i < iface->num_bss; i++) {
312 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
313 return iface->bss[i];
320 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
321 const u8 *bssid, const u8 *addr,
324 hapd = get_hapd_bssid(hapd->iface, bssid);
325 if (hapd == NULL || hapd == HAPD_BROADCAST)
328 ieee802_11_rx_from_unknown(hapd, addr, wds);
332 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
334 struct hostapd_iface *iface = hapd->iface;
335 const struct ieee80211_hdr *hdr;
337 struct hostapd_frame_info fi;
339 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
340 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
344 hapd = get_hapd_bssid(iface, bssid);
347 fc = le_to_host16(hdr->frame_control);
350 * Drop frames to unknown BSSIDs except for Beacon frames which
351 * could be used to update neighbor information.
353 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
354 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
355 hapd = iface->bss[0];
360 os_memset(&fi, 0, sizeof(fi));
361 fi.datarate = rx_mgmt->datarate;
362 fi.ssi_signal = rx_mgmt->ssi_signal;
364 if (hapd == HAPD_BROADCAST) {
366 for (i = 0; i < iface->num_bss; i++)
367 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
368 rx_mgmt->frame_len, &fi);
370 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
372 random_add_randomness(&fi, sizeof(fi));
376 static void hostapd_rx_action(struct hostapd_data *hapd,
377 struct rx_action *rx_action)
379 struct rx_mgmt rx_mgmt;
381 struct ieee80211_hdr *hdr;
383 wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
384 " BSSID=" MACSTR " category=%u",
385 MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
386 MAC2STR(rx_action->bssid), rx_action->category);
387 wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
388 rx_action->data, rx_action->len);
390 buf = os_zalloc(24 + 1 + rx_action->len);
393 hdr = (struct ieee80211_hdr *) buf;
394 hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
395 WLAN_FC_STYPE_ACTION);
396 if (rx_action->category == WLAN_ACTION_SA_QUERY) {
398 * Assume frame was protected; it would have been dropped if
401 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
403 os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
404 os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
405 os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
406 buf[24] = rx_action->category;
407 os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
408 os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
410 rx_mgmt.frame_len = 24 + 1 + rx_action->len;
411 hostapd_mgmt_rx(hapd, &rx_mgmt);
416 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
417 size_t len, u16 stype, int ok)
419 struct ieee80211_hdr *hdr;
420 hdr = (struct ieee80211_hdr *) buf;
421 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
422 if (hapd == NULL || hapd == HAPD_BROADCAST)
424 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
427 #endif /* NEED_AP_MLME */
430 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
432 struct sta_info *sta = ap_get_sta(hapd, addr);
436 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
437 " - adding a new STA", MAC2STR(addr));
438 sta = ap_sta_add(hapd, addr);
440 hostapd_new_assoc_sta(hapd, sta, 0);
442 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
451 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
452 const u8 *data, size_t data_len)
454 struct hostapd_iface *iface = hapd->iface;
457 for (j = 0; j < iface->num_bss; j++) {
458 if (ap_get_sta(iface->bss[j], src)) {
459 hapd = iface->bss[j];
464 ieee802_1x_receive(hapd, src, data, data_len);
468 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
469 union wpa_event_data *data)
471 struct hostapd_data *hapd = ctx;
474 case EVENT_MICHAEL_MIC_FAILURE:
475 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
477 case EVENT_SCAN_RESULTS:
478 if (hapd->iface->scan_cb)
479 hapd->iface->scan_cb(hapd->iface);
481 #ifdef CONFIG_IEEE80211R
482 case EVENT_FT_RRB_RX:
483 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
484 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
486 #endif /* CONFIG_IEEE80211R */
487 case EVENT_WPS_BUTTON_PUSHED:
488 hostapd_wps_button_pushed(hapd, NULL);
491 case EVENT_TX_STATUS:
492 switch (data->tx_status.type) {
493 case WLAN_FC_TYPE_MGMT:
494 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
495 data->tx_status.data_len,
496 data->tx_status.stype,
497 data->tx_status.ack);
499 case WLAN_FC_TYPE_DATA:
500 hostapd_tx_status(hapd, data->tx_status.dst,
501 data->tx_status.data,
502 data->tx_status.data_len,
503 data->tx_status.ack);
507 case EVENT_DRIVER_CLIENT_POLL_OK:
508 hostapd_client_poll_ok(hapd, data->client_poll.addr);
510 case EVENT_RX_FROM_UNKNOWN:
511 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
512 data->rx_from_unknown.addr,
513 data->rx_from_unknown.wds);
516 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
518 #endif /* NEED_AP_MLME */
519 case EVENT_RX_PROBE_REQ:
520 if (data->rx_probe_req.sa == NULL ||
521 data->rx_probe_req.ie == NULL)
523 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
524 data->rx_probe_req.da,
525 data->rx_probe_req.bssid,
526 data->rx_probe_req.ie,
527 data->rx_probe_req.ie_len);
530 hostapd_event_new_sta(hapd, data->new_sta.addr);
533 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
535 data->eapol_rx.data_len);
538 hostapd_notif_assoc(hapd, data->assoc_info.addr,
539 data->assoc_info.req_ies,
540 data->assoc_info.req_ies_len,
541 data->assoc_info.reassoc);
545 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
549 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
551 case EVENT_STATION_LOW_ACK:
554 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
557 case EVENT_RX_ACTION:
558 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
559 data->rx_action.bssid == NULL)
561 hostapd_rx_action(hapd, &data->rx_action);
563 #endif /* NEED_AP_MLME */
565 wpa_printf(MSG_DEBUG, "Unknown event %d", event);