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 "common/wpa_ctrl.h"
23 #include "crypto/random.h"
27 #include "ieee802_11.h"
29 #include "accounting.h"
30 #include "tkip_countermeasures.h"
32 #include "ieee802_1x.h"
35 #include "wps_hostapd.h"
36 #include "ap_drv_ops.h"
37 #include "ap_config.h"
40 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
41 const u8 *req_ies, size_t req_ies_len, int reassoc)
45 struct ieee802_11_elems elems;
48 u16 reason = WLAN_REASON_UNSPECIFIED;
52 * This could potentially happen with unexpected event from the
53 * driver wrapper. This was seen at least in one case where the
54 * driver ended up being set to station mode while hostapd was
55 * running, so better make sure we stop processing such an
58 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
62 random_add_randomness(addr, ETH_ALEN);
64 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
65 HOSTAPD_LEVEL_INFO, "associated");
67 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
69 ie = elems.wps_ie - 2;
70 ielen = elems.wps_ie_len + 2;
71 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
72 } else if (elems.rsn_ie) {
73 ie = elems.rsn_ie - 2;
74 ielen = elems.rsn_ie_len + 2;
75 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
76 } else if (elems.wpa_ie) {
77 ie = elems.wpa_ie - 2;
78 ielen = elems.wpa_ie_len + 2;
79 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
83 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
87 sta = ap_get_sta(hapd, addr);
89 accounting_sta_stop(hapd, sta);
91 sta = ap_sta_add(hapd, addr);
95 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
99 wpabuf_free(sta->p2p_ie);
100 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
103 #endif /* CONFIG_P2P */
105 if (hapd->conf->wpa) {
106 if (ie == NULL || ielen == 0) {
108 if (hapd->conf->wps_state) {
109 wpa_printf(MSG_DEBUG, "STA did not include "
110 "WPA/RSN IE in (Re)Association "
111 "Request - possible WPS use");
112 sta->flags |= WLAN_STA_MAYBE_WPS;
115 #endif /* CONFIG_WPS */
117 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
121 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
122 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
124 sta->flags |= WLAN_STA_WPS;
125 wps = ieee802_11_vendor_ie_concat(ie, ielen,
128 if (wps_is_20(wps)) {
129 wpa_printf(MSG_DEBUG, "WPS: STA "
131 sta->flags |= WLAN_STA_WPS2;
137 #endif /* CONFIG_WPS */
139 if (sta->wpa_sm == NULL)
140 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
142 if (sta->wpa_sm == NULL) {
143 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
147 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
149 if (res != WPA_IE_OK) {
150 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
151 "rejected? (res %u)", res);
152 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
153 if (res == WPA_INVALID_GROUP)
154 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
155 else if (res == WPA_INVALID_PAIRWISE)
156 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
157 else if (res == WPA_INVALID_AKMP)
158 reason = WLAN_REASON_AKMP_NOT_VALID;
159 #ifdef CONFIG_IEEE80211W
160 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
161 reason = WLAN_REASON_INVALID_IE;
162 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
163 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
164 #endif /* CONFIG_IEEE80211W */
166 reason = WLAN_REASON_INVALID_IE;
169 } else if (hapd->conf->wps_state) {
173 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
177 #ifdef CONFIG_WPS_STRICT
178 if (wps && wps_validate_assoc_req(wps) < 0) {
179 reason = WLAN_REASON_INVALID_IE;
183 #endif /* CONFIG_WPS_STRICT */
185 sta->flags |= WLAN_STA_WPS;
186 if (wps_is_20(wps)) {
187 wpa_printf(MSG_DEBUG, "WPS: STA supports "
189 sta->flags |= WLAN_STA_WPS2;
192 sta->flags |= WLAN_STA_MAYBE_WPS;
194 #endif /* CONFIG_WPS */
198 #endif /* CONFIG_WPS */
200 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
201 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
202 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
204 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
206 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
209 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
210 req_ies, req_ies_len);
211 #endif /* CONFIG_P2P */
216 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
217 ap_free_sta(hapd, sta);
222 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
224 struct sta_info *sta;
228 * This could potentially happen with unexpected event from the
229 * driver wrapper. This was seen at least in one case where the
230 * driver ended up reporting a station mode event while hostapd
231 * was running, so better make sure we stop processing such an
234 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
239 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
240 HOSTAPD_LEVEL_INFO, "disassociated");
242 sta = ap_get_sta(hapd, addr);
244 wpa_printf(MSG_DEBUG, "Disassociation notification for "
245 "unknown STA " MACSTR, MAC2STR(addr));
249 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
250 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
252 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
253 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
254 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
255 ap_free_sta(hapd, sta);
259 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
261 struct sta_info *sta = ap_get_sta(hapd, addr);
263 if (!sta || !hapd->conf->disassoc_low_ack)
266 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
267 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
269 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
271 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
275 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
276 const u8 *bssid, const u8 *ie, size_t ie_len)
281 if (sa == NULL || ie == NULL)
284 random_add_randomness(sa, ETH_ALEN);
285 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
286 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
287 sa, da, bssid, ie, ie_len) > 0) {
300 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
302 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
309 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
310 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
311 return HAPD_BROADCAST;
313 for (i = 0; i < iface->num_bss; i++) {
314 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
315 return iface->bss[i];
322 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
323 const u8 *bssid, const u8 *addr,
326 hapd = get_hapd_bssid(hapd->iface, bssid);
327 if (hapd == NULL || hapd == HAPD_BROADCAST)
330 ieee802_11_rx_from_unknown(hapd, addr, wds);
334 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
336 struct hostapd_iface *iface = hapd->iface;
337 const struct ieee80211_hdr *hdr;
339 struct hostapd_frame_info fi;
341 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
342 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
346 hapd = get_hapd_bssid(iface, bssid);
349 fc = le_to_host16(hdr->frame_control);
352 * Drop frames to unknown BSSIDs except for Beacon frames which
353 * could be used to update neighbor information.
355 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
356 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
357 hapd = iface->bss[0];
362 os_memset(&fi, 0, sizeof(fi));
363 fi.datarate = rx_mgmt->datarate;
364 fi.ssi_signal = rx_mgmt->ssi_signal;
366 if (hapd == HAPD_BROADCAST) {
368 for (i = 0; i < iface->num_bss; i++)
369 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
370 rx_mgmt->frame_len, &fi);
372 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
374 random_add_randomness(&fi, sizeof(fi));
378 static void hostapd_rx_action(struct hostapd_data *hapd,
379 struct rx_action *rx_action)
381 struct rx_mgmt rx_mgmt;
383 struct ieee80211_hdr *hdr;
385 wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
386 " BSSID=" MACSTR " category=%u",
387 MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
388 MAC2STR(rx_action->bssid), rx_action->category);
389 wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
390 rx_action->data, rx_action->len);
392 buf = os_zalloc(24 + 1 + rx_action->len);
395 hdr = (struct ieee80211_hdr *) buf;
396 hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
397 WLAN_FC_STYPE_ACTION);
398 if (rx_action->category == WLAN_ACTION_SA_QUERY) {
400 * Assume frame was protected; it would have been dropped if
403 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
405 os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
406 os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
407 os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
408 buf[24] = rx_action->category;
409 os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
410 os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
412 rx_mgmt.frame_len = 24 + 1 + rx_action->len;
413 hostapd_mgmt_rx(hapd, &rx_mgmt);
418 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
419 size_t len, u16 stype, int ok)
421 struct ieee80211_hdr *hdr;
422 hdr = (struct ieee80211_hdr *) buf;
423 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
424 if (hapd == NULL || hapd == HAPD_BROADCAST)
426 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
429 #endif /* NEED_AP_MLME */
432 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
434 struct sta_info *sta = ap_get_sta(hapd, addr);
438 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
439 " - adding a new STA", MAC2STR(addr));
440 sta = ap_sta_add(hapd, addr);
442 hostapd_new_assoc_sta(hapd, sta, 0);
444 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
453 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
454 const u8 *data, size_t data_len)
456 struct hostapd_iface *iface = hapd->iface;
459 for (j = 0; j < iface->num_bss; j++) {
460 if (ap_get_sta(iface->bss[j], src)) {
461 hapd = iface->bss[j];
466 ieee802_1x_receive(hapd, src, data, data_len);
470 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
471 union wpa_event_data *data)
473 struct hostapd_data *hapd = ctx;
476 case EVENT_MICHAEL_MIC_FAILURE:
477 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
479 case EVENT_SCAN_RESULTS:
480 if (hapd->iface->scan_cb)
481 hapd->iface->scan_cb(hapd->iface);
483 #ifdef CONFIG_IEEE80211R
484 case EVENT_FT_RRB_RX:
485 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
486 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
488 #endif /* CONFIG_IEEE80211R */
489 case EVENT_WPS_BUTTON_PUSHED:
490 hostapd_wps_button_pushed(hapd, NULL);
493 case EVENT_TX_STATUS:
494 switch (data->tx_status.type) {
495 case WLAN_FC_TYPE_MGMT:
496 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
497 data->tx_status.data_len,
498 data->tx_status.stype,
499 data->tx_status.ack);
501 case WLAN_FC_TYPE_DATA:
502 hostapd_tx_status(hapd, data->tx_status.dst,
503 data->tx_status.data,
504 data->tx_status.data_len,
505 data->tx_status.ack);
509 case EVENT_DRIVER_CLIENT_POLL_OK:
510 hostapd_client_poll_ok(hapd, data->client_poll.addr);
512 case EVENT_RX_FROM_UNKNOWN:
513 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
514 data->rx_from_unknown.addr,
515 data->rx_from_unknown.wds);
518 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
520 #endif /* NEED_AP_MLME */
521 case EVENT_RX_PROBE_REQ:
522 if (data->rx_probe_req.sa == NULL ||
523 data->rx_probe_req.ie == NULL)
525 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
526 data->rx_probe_req.da,
527 data->rx_probe_req.bssid,
528 data->rx_probe_req.ie,
529 data->rx_probe_req.ie_len);
532 hostapd_event_new_sta(hapd, data->new_sta.addr);
535 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
537 data->eapol_rx.data_len);
540 hostapd_notif_assoc(hapd, data->assoc_info.addr,
541 data->assoc_info.req_ies,
542 data->assoc_info.req_ies_len,
543 data->assoc_info.reassoc);
547 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
551 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
553 case EVENT_STATION_LOW_ACK:
556 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
559 case EVENT_RX_ACTION:
560 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
561 data->rx_action.bssid == NULL)
563 hostapd_rx_action(hapd, &data->rx_action);
565 #endif /* NEED_AP_MLME */
567 wpa_printf(MSG_DEBUG, "Unknown event %d", event);