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.
19 #include "ieee802_11.h"
20 #include "radius/radius.h"
22 #include "accounting.h"
23 #include "tkip_countermeasures.h"
24 #include "ieee802_1x.h"
31 struct hostapd_data *hapd;
35 static int prune_associations(struct hostapd_iface *iface, void *ctx)
37 struct prune_data *data = ctx;
38 struct sta_info *osta;
39 struct hostapd_data *ohapd;
42 for (j = 0; j < iface->num_bss; j++) {
43 ohapd = iface->bss[j];
44 if (ohapd == data->hapd)
46 osta = ap_get_sta(ohapd, data->addr);
50 ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
57 * hostapd_prune_associations - Remove extraneous associations
58 * @hapd: Pointer to BSS data for the most recent association
59 * @sta: Pointer to the associated STA data
61 * This function looks through all radios and BSS's for previous
62 * (stale) associations of STA. If any are found they are removed.
64 static void hostapd_prune_associations(struct hostapd_data *hapd,
67 struct prune_data data;
69 data.addr = sta->addr;
70 hostapd_for_each_interface(prune_associations, &data);
75 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
76 * @hapd: Pointer to BSS data
77 * @sta: Pointer to the associated STA data
78 * @reassoc: 1 to indicate this was a re-association; 0 = first association
80 * This function will be called whenever a station associates with the AP. It
81 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
82 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
83 * authentication and association) internally.
85 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
88 if (hapd->tkip_countermeasures) {
89 hostapd_sta_deauth(hapd, sta->addr,
90 WLAN_REASON_MICHAEL_MIC_FAILURE);
94 hostapd_prune_associations(hapd, sta);
96 /* IEEE 802.11F (IAPP) */
97 if (hapd->conf->ieee802_11f)
98 iapp_new_station(hapd->iapp, sta);
100 /* Start accounting here, if IEEE 802.1X and WPA are not used.
101 * IEEE 802.1X/WPA code will start accounting after the station has
102 * been authorized. */
103 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
104 accounting_sta_start(hapd, sta);
106 hostapd_wmm_sta_config(hapd, sta);
108 /* Start IEEE 802.1X authentication process for new stations */
109 ieee802_1x_new_station(hapd, sta);
111 if (sta->auth_alg != WLAN_AUTH_FT &&
112 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
113 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
115 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
119 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
120 const u8 *buf, size_t len, int ack)
122 struct sta_info *sta;
123 struct hostapd_iface *iface = hapd->iface;
125 sta = ap_get_sta(hapd, addr);
126 if (sta == NULL && iface->num_bss > 1) {
128 for (j = 0; j < iface->num_bss; j++) {
129 hapd = iface->bss[j];
130 sta = ap_get_sta(hapd, addr);
137 if (sta->flags & WLAN_STA_PENDING_POLL) {
138 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
139 "activity poll", MAC2STR(sta->addr),
140 ack ? "ACKed" : "did not ACK");
142 sta->flags &= ~WLAN_STA_PENDING_POLL;
145 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
149 static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
154 * PS-Poll frames are 16 bytes. All other frames are
155 * 24 bytes or longer.
160 fc = le_to_host16(hdr->frame_control);
161 type = WLAN_FC_GET_TYPE(fc);
162 stype = WLAN_FC_GET_STYPE(fc);
165 case WLAN_FC_TYPE_DATA:
168 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
176 case WLAN_FC_TYPE_CTRL:
177 if (stype != WLAN_FC_STYPE_PSPOLL)
180 case WLAN_FC_TYPE_MGMT:
188 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
190 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
197 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
198 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
199 return HAPD_BROADCAST;
201 for (i = 0; i < iface->num_bss; i++) {
202 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
203 return iface->bss[i];
210 void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
211 const struct ieee80211_hdr *hdr, size_t len)
213 struct sta_info *sta;
216 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
217 if (hapd == NULL || hapd == HAPD_BROADCAST)
221 sta = ap_get_sta(hapd, addr);
222 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
223 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
224 "STA " MACSTR, MAC2STR(addr));
225 if (sta && (sta->flags & WLAN_STA_AUTH))
226 hostapd_sta_disassoc(
228 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
232 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
237 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
238 const u8 *ie, size_t ielen)
240 struct sta_info *sta;
243 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
244 HOSTAPD_LEVEL_INFO, "associated");
246 sta = ap_get_sta(hapd, addr);
248 accounting_sta_stop(hapd, sta);
250 sta = ap_sta_add(hapd, addr);
254 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
256 if (hapd->conf->wpa) {
257 if (ie == NULL || ielen == 0) {
258 if (hapd->conf->wps_state) {
259 wpa_printf(MSG_DEBUG, "STA did not include "
260 "WPA/RSN IE in (Re)Association "
261 "Request - possible WPS use");
262 sta->flags |= WLAN_STA_MAYBE_WPS;
266 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
269 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
270 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
271 sta->flags |= WLAN_STA_WPS;
275 if (sta->wpa_sm == NULL)
276 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
278 if (sta->wpa_sm == NULL) {
279 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
283 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
285 if (res != WPA_IE_OK) {
286 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
287 "rejected? (res %u)", res);
288 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
291 } else if (hapd->conf->wps_state) {
292 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
293 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
294 sta->flags |= WLAN_STA_WPS;
296 sta->flags |= WLAN_STA_MAYBE_WPS;
300 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
301 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
302 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
304 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
306 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
312 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
314 struct sta_info *sta;
316 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
317 HOSTAPD_LEVEL_INFO, "disassociated");
319 sta = ap_get_sta(hapd, addr);
321 wpa_printf(MSG_DEBUG, "Disassociation notification for "
322 "unknown STA " MACSTR, MAC2STR(addr));
326 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
327 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
328 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
329 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
330 ap_free_sta(hapd, sta);
334 void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
335 const u8 *buf, size_t len)
337 ieee802_1x_receive(hapd, sa, buf, len);
342 void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
343 u16 stype, struct hostapd_frame_info *fi)
345 struct hostapd_iface *iface = hapd->iface;
346 struct ieee80211_hdr *hdr;
349 hdr = (struct ieee80211_hdr *) buf;
350 bssid = get_hdr_bssid(hdr, len);
354 hapd = get_hapd_bssid(iface, bssid);
357 fc = le_to_host16(hdr->frame_control);
360 * Drop frames to unknown BSSIDs except for Beacon frames which
361 * could be used to update neighbor information.
363 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
364 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
365 hapd = iface->bss[0];
370 if (hapd == HAPD_BROADCAST) {
372 for (i = 0; i < iface->num_bss; i++)
373 ieee802_11_mgmt(iface->bss[i], buf, len, stype, fi);
375 ieee802_11_mgmt(hapd, buf, len, stype, fi);
379 void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
382 struct ieee80211_hdr *hdr;
383 hdr = (struct ieee80211_hdr *) buf;
384 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
385 if (hapd == NULL || hapd == HAPD_BROADCAST)
387 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
389 #endif /* NEED_MLME */
392 void hostapd_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr)
394 michael_mic_failure(hapd, addr, 1);
398 struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
401 struct hostapd_iface *iface = hapd->iface;
404 for (j = 0; j < iface->num_bss; j++) {
405 hapd = iface->bss[j];
406 if (ap_get_sta(hapd, addr))
415 void wpa_supplicant_event(void *ctx, wpa_event_type event,
416 union wpa_event_data *data)
418 struct hostapd_data *hapd = ctx;
421 case EVENT_MICHAEL_MIC_FAILURE:
422 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
424 case EVENT_SCAN_RESULTS:
425 if (hapd->iface->scan_cb)
426 hapd->iface->scan_cb(hapd->iface);
429 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
433 #endif /* CONFIG_AP */