Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * hostapd / Callback functions for driver wrappers |
| 3 | * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 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. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "utils/includes.h" |
| 16 | |
| 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" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | #include "crypto/random.h" |
| 23 | #include "p2p/p2p.h" |
| 24 | #include "wps/wps.h" |
| 25 | #include "hostapd.h" |
| 26 | #include "ieee802_11.h" |
| 27 | #include "sta_info.h" |
| 28 | #include "accounting.h" |
| 29 | #include "tkip_countermeasures.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 30 | #include "ieee802_1x.h" |
| 31 | #include "wpa_auth.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 32 | #include "wps_hostapd.h" |
| 33 | #include "ap_drv_ops.h" |
| 34 | #include "ap_config.h" |
| 35 | |
| 36 | |
| 37 | int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 38 | const u8 *req_ies, size_t req_ies_len, int reassoc) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 39 | { |
| 40 | struct sta_info *sta; |
| 41 | int new_assoc, res; |
| 42 | struct ieee802_11_elems elems; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 43 | const u8 *ie; |
| 44 | size_t ielen; |
| 45 | u16 reason = WLAN_REASON_UNSPECIFIED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 46 | |
| 47 | if (addr == NULL) { |
| 48 | /* |
| 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 |
| 53 | * event here. |
| 54 | */ |
| 55 | wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with " |
| 56 | "no address"); |
| 57 | return -1; |
| 58 | } |
| 59 | random_add_randomness(addr, ETH_ALEN); |
| 60 | |
| 61 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, |
| 62 | HOSTAPD_LEVEL_INFO, "associated"); |
| 63 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 64 | ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 65 | if (elems.wps_ie) { |
| 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"); |
| 77 | } else { |
| 78 | ie = NULL; |
| 79 | ielen = 0; |
| 80 | wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in " |
| 81 | "(Re)AssocReq"); |
| 82 | } |
| 83 | |
| 84 | sta = ap_get_sta(hapd, addr); |
| 85 | if (sta) { |
| 86 | accounting_sta_stop(hapd, sta); |
| 87 | } else { |
| 88 | sta = ap_sta_add(hapd, addr); |
| 89 | if (sta == NULL) |
| 90 | return -1; |
| 91 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 92 | sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 93 | |
| 94 | #ifdef CONFIG_P2P |
| 95 | if (elems.p2p) { |
| 96 | wpabuf_free(sta->p2p_ie); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 97 | sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 98 | P2P_IE_VENDOR_TYPE); |
| 99 | } |
| 100 | #endif /* CONFIG_P2P */ |
| 101 | |
| 102 | if (hapd->conf->wpa) { |
| 103 | if (ie == NULL || ielen == 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 104 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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; |
| 110 | goto skip_wpa_check; |
| 111 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 112 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | |
| 114 | wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA"); |
| 115 | return -1; |
| 116 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 117 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 118 | if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 && |
| 119 | os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 120 | struct wpabuf *wps; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 121 | sta->flags |= WLAN_STA_WPS; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 122 | wps = ieee802_11_vendor_ie_concat(ie, ielen, |
| 123 | WPS_IE_VENDOR_TYPE); |
| 124 | if (wps) { |
| 125 | if (wps_is_20(wps)) { |
| 126 | wpa_printf(MSG_DEBUG, "WPS: STA " |
| 127 | "supports WPS 2.0"); |
| 128 | sta->flags |= WLAN_STA_WPS2; |
| 129 | } |
| 130 | wpabuf_free(wps); |
| 131 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 132 | goto skip_wpa_check; |
| 133 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 134 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 135 | |
| 136 | if (sta->wpa_sm == NULL) |
| 137 | sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, |
| 138 | sta->addr); |
| 139 | if (sta->wpa_sm == NULL) { |
| 140 | wpa_printf(MSG_ERROR, "Failed to initialize WPA state " |
| 141 | "machine"); |
| 142 | return -1; |
| 143 | } |
| 144 | res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm, |
| 145 | ie, ielen, NULL, 0); |
| 146 | if (res != WPA_IE_OK) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 151 | reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 152 | else if (res == WPA_INVALID_PAIRWISE) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 153 | reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 154 | else if (res == WPA_INVALID_AKMP) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 155 | reason = WLAN_REASON_AKMP_NOT_VALID; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 156 | #ifdef CONFIG_IEEE80211W |
| 157 | else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 158 | reason = WLAN_REASON_INVALID_IE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 159 | else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 160 | reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 161 | #endif /* CONFIG_IEEE80211W */ |
| 162 | else |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 163 | reason = WLAN_REASON_INVALID_IE; |
| 164 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 165 | } |
| 166 | } else if (hapd->conf->wps_state) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 167 | #ifdef CONFIG_WPS |
| 168 | struct wpabuf *wps; |
| 169 | if (req_ies) |
| 170 | wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 171 | WPS_IE_VENDOR_TYPE); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 172 | else |
| 173 | wps = NULL; |
| 174 | #ifdef CONFIG_WPS_STRICT |
| 175 | if (wps && wps_validate_assoc_req(wps) < 0) { |
| 176 | reason = WLAN_REASON_INVALID_IE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 177 | wpabuf_free(wps); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 178 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 179 | } |
| 180 | #endif /* CONFIG_WPS_STRICT */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 181 | if (wps) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 182 | sta->flags |= WLAN_STA_WPS; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 183 | if (wps_is_20(wps)) { |
| 184 | wpa_printf(MSG_DEBUG, "WPS: STA supports " |
| 185 | "WPS 2.0"); |
| 186 | sta->flags |= WLAN_STA_WPS2; |
| 187 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 188 | } else |
| 189 | sta->flags |= WLAN_STA_MAYBE_WPS; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 190 | wpabuf_free(wps); |
| 191 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 192 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 193 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 194 | skip_wpa_check: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 195 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 196 | |
| 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); |
| 200 | |
| 201 | hostapd_new_assoc_sta(hapd, sta, !new_assoc); |
| 202 | |
| 203 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 1); |
| 204 | |
| 205 | #ifdef CONFIG_P2P |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 206 | if (req_ies) { |
| 207 | p2p_group_notif_assoc(hapd->p2p_group, sta->addr, |
| 208 | req_ies, req_ies_len); |
| 209 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 210 | #endif /* CONFIG_P2P */ |
| 211 | |
| 212 | return 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 213 | |
| 214 | fail: |
| 215 | hostapd_drv_sta_disassoc(hapd, sta->addr, reason); |
| 216 | ap_free_sta(hapd, sta); |
| 217 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr) |
| 222 | { |
| 223 | struct sta_info *sta; |
| 224 | |
| 225 | if (addr == NULL) { |
| 226 | /* |
| 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 |
| 231 | * event here. |
| 232 | */ |
| 233 | wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event " |
| 234 | "with no address"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, |
| 239 | HOSTAPD_LEVEL_INFO, "disassociated"); |
| 240 | |
| 241 | sta = ap_get_sta(hapd, addr); |
| 242 | if (sta == NULL) { |
| 243 | wpa_printf(MSG_DEBUG, "Disassociation notification for " |
| 244 | "unknown STA " MACSTR, MAC2STR(addr)); |
| 245 | return; |
| 246 | } |
| 247 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 248 | ap_sta_set_authorized(hapd, sta, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 249 | sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr) |
| 258 | { |
| 259 | struct sta_info *sta = ap_get_sta(hapd, addr); |
| 260 | |
| 261 | if (!sta || !hapd->conf->disassoc_low_ack) |
| 262 | return; |
| 263 | |
| 264 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, |
| 265 | HOSTAPD_LEVEL_INFO, "disconnected due to excessive " |
| 266 | "missing ACKs"); |
| 267 | hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK); |
| 268 | if (sta) |
| 269 | ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK); |
| 270 | } |
| 271 | |
| 272 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 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) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 275 | { |
| 276 | size_t i; |
| 277 | int ret = 0; |
| 278 | |
| 279 | if (sa == NULL || ie == NULL) |
| 280 | return -1; |
| 281 | |
| 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, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 285 | sa, da, bssid, ie, ie_len) > 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 286 | ret = 1; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | #ifdef HOSTAPD |
| 295 | |
| 296 | #ifdef NEED_AP_MLME |
| 297 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 298 | #define HAPD_BROADCAST ((struct hostapd_data *) -1) |
| 299 | |
| 300 | static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface, |
| 301 | const u8 *bssid) |
| 302 | { |
| 303 | size_t i; |
| 304 | |
| 305 | if (bssid == NULL) |
| 306 | return NULL; |
| 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; |
| 310 | |
| 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]; |
| 314 | } |
| 315 | |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 321 | const u8 *bssid, const u8 *addr, |
| 322 | int wds) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 323 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 324 | hapd = get_hapd_bssid(hapd->iface, bssid); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 325 | if (hapd == NULL || hapd == HAPD_BROADCAST) |
| 326 | return; |
| 327 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 328 | ieee802_11_rx_from_unknown(hapd, addr, wds); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt) |
| 333 | { |
| 334 | struct hostapd_iface *iface = hapd->iface; |
| 335 | const struct ieee80211_hdr *hdr; |
| 336 | const u8 *bssid; |
| 337 | struct hostapd_frame_info fi; |
| 338 | |
| 339 | hdr = (const struct ieee80211_hdr *) rx_mgmt->frame; |
| 340 | bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len); |
| 341 | if (bssid == NULL) |
| 342 | return; |
| 343 | |
| 344 | hapd = get_hapd_bssid(iface, bssid); |
| 345 | if (hapd == NULL) { |
| 346 | u16 fc; |
| 347 | fc = le_to_host16(hdr->frame_control); |
| 348 | |
| 349 | /* |
| 350 | * Drop frames to unknown BSSIDs except for Beacon frames which |
| 351 | * could be used to update neighbor information. |
| 352 | */ |
| 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]; |
| 356 | else |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | os_memset(&fi, 0, sizeof(fi)); |
| 361 | fi.datarate = rx_mgmt->datarate; |
| 362 | fi.ssi_signal = rx_mgmt->ssi_signal; |
| 363 | |
| 364 | if (hapd == HAPD_BROADCAST) { |
| 365 | size_t i; |
| 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); |
| 369 | } else |
| 370 | ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi); |
| 371 | |
| 372 | random_add_randomness(&fi, sizeof(fi)); |
| 373 | } |
| 374 | |
| 375 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 376 | static void hostapd_rx_action(struct hostapd_data *hapd, |
| 377 | struct rx_action *rx_action) |
| 378 | { |
| 379 | struct rx_mgmt rx_mgmt; |
| 380 | u8 *buf; |
| 381 | struct ieee80211_hdr *hdr; |
| 382 | |
| 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); |
| 389 | |
| 390 | buf = os_zalloc(24 + 1 + rx_action->len); |
| 391 | if (buf == NULL) |
| 392 | return; |
| 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) { |
| 397 | /* |
| 398 | * Assume frame was protected; it would have been dropped if |
| 399 | * not. |
| 400 | */ |
| 401 | hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP); |
| 402 | } |
| 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)); |
| 409 | rx_mgmt.frame = buf; |
| 410 | rx_mgmt.frame_len = 24 + 1 + rx_action->len; |
| 411 | hostapd_mgmt_rx(hapd, &rx_mgmt); |
| 412 | os_free(buf); |
| 413 | } |
| 414 | |
| 415 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 416 | static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf, |
| 417 | size_t len, u16 stype, int ok) |
| 418 | { |
| 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) |
| 423 | return; |
| 424 | ieee802_11_mgmt_cb(hapd, buf, len, stype, ok); |
| 425 | } |
| 426 | |
| 427 | #endif /* NEED_AP_MLME */ |
| 428 | |
| 429 | |
| 430 | static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr) |
| 431 | { |
| 432 | struct sta_info *sta = ap_get_sta(hapd, addr); |
| 433 | if (sta) |
| 434 | return 0; |
| 435 | |
| 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); |
| 439 | if (sta) { |
| 440 | hostapd_new_assoc_sta(hapd, sta, 0); |
| 441 | } else { |
| 442 | wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR, |
| 443 | MAC2STR(addr)); |
| 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src, |
| 452 | const u8 *data, size_t data_len) |
| 453 | { |
| 454 | struct hostapd_iface *iface = hapd->iface; |
| 455 | size_t j; |
| 456 | |
| 457 | for (j = 0; j < iface->num_bss; j++) { |
| 458 | if (ap_get_sta(iface->bss[j], src)) { |
| 459 | hapd = iface->bss[j]; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | ieee802_1x_receive(hapd, src, data, data_len); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | void wpa_supplicant_event(void *ctx, enum wpa_event_type event, |
| 469 | union wpa_event_data *data) |
| 470 | { |
| 471 | struct hostapd_data *hapd = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 472 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 473 | int level = MSG_DEBUG; |
| 474 | |
| 475 | if (event == EVENT_RX_MGMT && data && data->rx_mgmt.frame && |
| 476 | data->rx_mgmt.frame_len >= 24) { |
| 477 | const struct ieee80211_hdr *hdr; |
| 478 | u16 fc; |
| 479 | hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame; |
| 480 | fc = le_to_host16(hdr->frame_control); |
| 481 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && |
| 482 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) |
| 483 | level = MSG_EXCESSIVE; |
| 484 | } |
| 485 | |
| 486 | wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received", |
| 487 | event_to_string(event), event); |
| 488 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 489 | |
| 490 | switch (event) { |
| 491 | case EVENT_MICHAEL_MIC_FAILURE: |
| 492 | michael_mic_failure(hapd, data->michael_mic_failure.src, 1); |
| 493 | break; |
| 494 | case EVENT_SCAN_RESULTS: |
| 495 | if (hapd->iface->scan_cb) |
| 496 | hapd->iface->scan_cb(hapd->iface); |
| 497 | break; |
| 498 | #ifdef CONFIG_IEEE80211R |
| 499 | case EVENT_FT_RRB_RX: |
| 500 | wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src, |
| 501 | data->ft_rrb_rx.data, data->ft_rrb_rx.data_len); |
| 502 | break; |
| 503 | #endif /* CONFIG_IEEE80211R */ |
| 504 | case EVENT_WPS_BUTTON_PUSHED: |
| 505 | hostapd_wps_button_pushed(hapd, NULL); |
| 506 | break; |
| 507 | #ifdef NEED_AP_MLME |
| 508 | case EVENT_TX_STATUS: |
| 509 | switch (data->tx_status.type) { |
| 510 | case WLAN_FC_TYPE_MGMT: |
| 511 | hostapd_mgmt_tx_cb(hapd, data->tx_status.data, |
| 512 | data->tx_status.data_len, |
| 513 | data->tx_status.stype, |
| 514 | data->tx_status.ack); |
| 515 | break; |
| 516 | case WLAN_FC_TYPE_DATA: |
| 517 | hostapd_tx_status(hapd, data->tx_status.dst, |
| 518 | data->tx_status.data, |
| 519 | data->tx_status.data_len, |
| 520 | data->tx_status.ack); |
| 521 | break; |
| 522 | } |
| 523 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 524 | case EVENT_EAPOL_TX_STATUS: |
| 525 | hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst, |
| 526 | data->eapol_tx_status.data, |
| 527 | data->eapol_tx_status.data_len, |
| 528 | data->eapol_tx_status.ack); |
| 529 | break; |
| 530 | case EVENT_DRIVER_CLIENT_POLL_OK: |
| 531 | hostapd_client_poll_ok(hapd, data->client_poll.addr); |
| 532 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 533 | case EVENT_RX_FROM_UNKNOWN: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 534 | hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid, |
| 535 | data->rx_from_unknown.addr, |
| 536 | data->rx_from_unknown.wds); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 537 | break; |
| 538 | case EVENT_RX_MGMT: |
| 539 | hostapd_mgmt_rx(hapd, &data->rx_mgmt); |
| 540 | break; |
| 541 | #endif /* NEED_AP_MLME */ |
| 542 | case EVENT_RX_PROBE_REQ: |
| 543 | if (data->rx_probe_req.sa == NULL || |
| 544 | data->rx_probe_req.ie == NULL) |
| 545 | break; |
| 546 | hostapd_probe_req_rx(hapd, data->rx_probe_req.sa, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame^] | 547 | data->rx_probe_req.da, |
| 548 | data->rx_probe_req.bssid, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 549 | data->rx_probe_req.ie, |
| 550 | data->rx_probe_req.ie_len); |
| 551 | break; |
| 552 | case EVENT_NEW_STA: |
| 553 | hostapd_event_new_sta(hapd, data->new_sta.addr); |
| 554 | break; |
| 555 | case EVENT_EAPOL_RX: |
| 556 | hostapd_event_eapol_rx(hapd, data->eapol_rx.src, |
| 557 | data->eapol_rx.data, |
| 558 | data->eapol_rx.data_len); |
| 559 | break; |
| 560 | case EVENT_ASSOC: |
| 561 | hostapd_notif_assoc(hapd, data->assoc_info.addr, |
| 562 | data->assoc_info.req_ies, |
| 563 | data->assoc_info.req_ies_len, |
| 564 | data->assoc_info.reassoc); |
| 565 | break; |
| 566 | case EVENT_DISASSOC: |
| 567 | if (data) |
| 568 | hostapd_notif_disassoc(hapd, data->disassoc_info.addr); |
| 569 | break; |
| 570 | case EVENT_DEAUTH: |
| 571 | if (data) |
| 572 | hostapd_notif_disassoc(hapd, data->deauth_info.addr); |
| 573 | break; |
| 574 | case EVENT_STATION_LOW_ACK: |
| 575 | if (!data) |
| 576 | break; |
| 577 | hostapd_event_sta_low_ack(hapd, data->low_ack.addr); |
| 578 | break; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 579 | #ifdef NEED_AP_MLME |
| 580 | case EVENT_RX_ACTION: |
| 581 | if (data->rx_action.da == NULL || data->rx_action.sa == NULL || |
| 582 | data->rx_action.bssid == NULL) |
| 583 | break; |
| 584 | hostapd_rx_action(hapd, &data->rx_action); |
| 585 | break; |
| 586 | #endif /* NEED_AP_MLME */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 587 | default: |
| 588 | wpa_printf(MSG_DEBUG, "Unknown event %d", event); |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | #endif /* HOSTAPD */ |