]>
Commit | Line | Data |
---|---|---|
b5b969e9 JM |
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 | ||
6226e38d | 15 | #include "utils/includes.h" |
b5b969e9 | 16 | |
6226e38d | 17 | #include "utils/common.h" |
b5b969e9 | 18 | #include "radius/radius.h" |
6e6e8c31 | 19 | #include "drivers/driver.h" |
81f4f619 | 20 | #include "common/ieee802_11_defs.h" |
c41a1095 | 21 | #include "common/ieee802_11_common.h" |
9a3cb18d | 22 | #include "common/wpa_ctrl.h" |
54f489be | 23 | #include "wps/wps.h" |
6226e38d JM |
24 | #include "hostapd.h" |
25 | #include "ieee802_11.h" | |
26 | #include "sta_info.h" | |
27 | #include "accounting.h" | |
28 | #include "tkip_countermeasures.h" | |
29 | #include "iapp.h" | |
30 | #include "ieee802_1x.h" | |
31 | #include "wpa_auth.h" | |
32 | #include "wmm.h" | |
33 | #include "wps_hostapd.h" | |
8b06c1ed | 34 | #include "ap_config.h" |
b5b969e9 JM |
35 | |
36 | ||
b5b969e9 JM |
37 | int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr, |
38 | const u8 *ie, size_t ielen) | |
39 | { | |
40 | struct sta_info *sta; | |
41 | int new_assoc, res; | |
c41a1095 | 42 | struct ieee802_11_elems elems; |
b5b969e9 | 43 | |
68532a9c JM |
44 | if (addr == NULL) { |
45 | /* | |
46 | * This could potentially happen with unexpected event from the | |
47 | * driver wrapper. This was seen at least in one case where the | |
48 | * driver ended up being set to station mode while hostapd was | |
49 | * running, so better make sure we stop processing such an | |
50 | * event here. | |
51 | */ | |
52 | wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with " | |
53 | "no address"); | |
54 | return -1; | |
55 | } | |
56 | ||
b5b969e9 JM |
57 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, |
58 | HOSTAPD_LEVEL_INFO, "associated"); | |
59 | ||
c41a1095 JM |
60 | ieee802_11_parse_elems(ie, ielen, &elems, 0); |
61 | if (elems.wps_ie) { | |
62 | ie = elems.wps_ie - 2; | |
63 | ielen = elems.wps_ie_len + 2; | |
64 | wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq"); | |
65 | } else if (elems.rsn_ie) { | |
66 | ie = elems.rsn_ie - 2; | |
67 | ielen = elems.rsn_ie_len + 2; | |
68 | wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq"); | |
69 | } else if (elems.wpa_ie) { | |
70 | ie = elems.wpa_ie - 2; | |
71 | ielen = elems.wpa_ie_len + 2; | |
72 | wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq"); | |
73 | } else { | |
74 | ie = NULL; | |
75 | ielen = 0; | |
76 | wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in " | |
77 | "(Re)AssocReq"); | |
78 | } | |
79 | ||
b5b969e9 JM |
80 | sta = ap_get_sta(hapd, addr); |
81 | if (sta) { | |
82 | accounting_sta_stop(hapd, sta); | |
83 | } else { | |
84 | sta = ap_sta_add(hapd, addr); | |
85 | if (sta == NULL) | |
86 | return -1; | |
87 | } | |
88 | sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS); | |
89 | ||
90 | if (hapd->conf->wpa) { | |
91 | if (ie == NULL || ielen == 0) { | |
92 | if (hapd->conf->wps_state) { | |
93 | wpa_printf(MSG_DEBUG, "STA did not include " | |
94 | "WPA/RSN IE in (Re)Association " | |
95 | "Request - possible WPS use"); | |
96 | sta->flags |= WLAN_STA_MAYBE_WPS; | |
97 | goto skip_wpa_check; | |
98 | } | |
99 | ||
100 | wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA"); | |
101 | return -1; | |
102 | } | |
103 | if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 && | |
104 | os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) { | |
105 | sta->flags |= WLAN_STA_WPS; | |
106 | goto skip_wpa_check; | |
107 | } | |
108 | ||
109 | if (sta->wpa_sm == NULL) | |
110 | sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, | |
111 | sta->addr); | |
112 | if (sta->wpa_sm == NULL) { | |
113 | wpa_printf(MSG_ERROR, "Failed to initialize WPA state " | |
114 | "machine"); | |
115 | return -1; | |
116 | } | |
117 | res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm, | |
118 | ie, ielen, NULL, 0); | |
119 | if (res != WPA_IE_OK) { | |
355d36a7 | 120 | int resp; |
b5b969e9 JM |
121 | wpa_printf(MSG_DEBUG, "WPA/RSN information element " |
122 | "rejected? (res %u)", res); | |
123 | wpa_hexdump(MSG_DEBUG, "IE", ie, ielen); | |
355d36a7 AT |
124 | if (res == WPA_INVALID_GROUP) |
125 | resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID; | |
126 | else if (res == WPA_INVALID_PAIRWISE) | |
127 | resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID; | |
128 | else if (res == WPA_INVALID_AKMP) | |
129 | resp = WLAN_REASON_AKMP_NOT_VALID; | |
130 | #ifdef CONFIG_IEEE80211W | |
131 | else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) | |
132 | resp = WLAN_REASON_INVALID_IE; | |
133 | else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) | |
134 | resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID; | |
135 | #endif /* CONFIG_IEEE80211W */ | |
136 | else | |
137 | resp = WLAN_REASON_INVALID_IE; | |
bdee6fce | 138 | hapd->drv.sta_disassoc(hapd, sta->addr, resp); |
355d36a7 | 139 | ap_free_sta(hapd, sta); |
b5b969e9 JM |
140 | return -1; |
141 | } | |
a9aca28b | 142 | } else if (hapd->conf->wps_state) { |
54f489be JM |
143 | #ifdef CONFIG_WPS_STRICT |
144 | struct wpabuf *wps; | |
145 | wps = ieee802_11_vendor_ie_concat(ie, ielen, | |
146 | WPS_IE_VENDOR_TYPE); | |
147 | if (wps && wps_validate_assoc_req(wps) < 0) { | |
148 | hapd->drv.sta_disassoc(hapd, sta->addr, | |
149 | WLAN_REASON_INVALID_IE); | |
150 | ap_free_sta(hapd, sta); | |
151 | wpabuf_free(wps); | |
152 | return -1; | |
153 | } | |
154 | wpabuf_free(wps); | |
155 | #endif /* CONFIG_WPS_STRICT */ | |
a9aca28b JM |
156 | if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 && |
157 | os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) { | |
158 | sta->flags |= WLAN_STA_WPS; | |
159 | } else | |
160 | sta->flags |= WLAN_STA_MAYBE_WPS; | |
b5b969e9 JM |
161 | } |
162 | skip_wpa_check: | |
163 | ||
164 | new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0; | |
165 | sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC; | |
166 | wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC); | |
167 | ||
168 | hostapd_new_assoc_sta(hapd, sta, !new_assoc); | |
169 | ||
170 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 1); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | ||
176 | void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr) | |
177 | { | |
178 | struct sta_info *sta; | |
179 | ||
180 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, | |
181 | HOSTAPD_LEVEL_INFO, "disassociated"); | |
182 | ||
183 | sta = ap_get_sta(hapd, addr); | |
184 | if (sta == NULL) { | |
185 | wpa_printf(MSG_DEBUG, "Disassociation notification for " | |
186 | "unknown STA " MACSTR, MAC2STR(addr)); | |
187 | return; | |
188 | } | |
189 | ||
190 | sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC); | |
9a3cb18d JM |
191 | wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR, |
192 | MAC2STR(sta->addr)); | |
b5b969e9 JM |
193 | wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC); |
194 | sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST; | |
195 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 0); | |
196 | ap_free_sta(hapd, sta); | |
197 | } | |
198 | ||
199 | ||
f8b1f695 JM |
200 | #ifdef HOSTAPD |
201 | ||
fe6bdb77 | 202 | #ifdef NEED_AP_MLME |
f8b1f695 JM |
203 | |
204 | static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len) | |
205 | { | |
206 | u16 fc, type, stype; | |
207 | ||
208 | /* | |
209 | * PS-Poll frames are 16 bytes. All other frames are | |
210 | * 24 bytes or longer. | |
211 | */ | |
212 | if (len < 16) | |
213 | return NULL; | |
214 | ||
215 | fc = le_to_host16(hdr->frame_control); | |
216 | type = WLAN_FC_GET_TYPE(fc); | |
217 | stype = WLAN_FC_GET_STYPE(fc); | |
218 | ||
219 | switch (type) { | |
220 | case WLAN_FC_TYPE_DATA: | |
221 | if (len < 24) | |
222 | return NULL; | |
223 | switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) { | |
fbbfcbac | 224 | case WLAN_FC_FROMDS | WLAN_FC_TODS: |
f8b1f695 JM |
225 | case WLAN_FC_TODS: |
226 | return hdr->addr1; | |
227 | case WLAN_FC_FROMDS: | |
228 | return hdr->addr2; | |
229 | default: | |
230 | return NULL; | |
231 | } | |
232 | case WLAN_FC_TYPE_CTRL: | |
233 | if (stype != WLAN_FC_STYPE_PSPOLL) | |
234 | return NULL; | |
235 | return hdr->addr1; | |
236 | case WLAN_FC_TYPE_MGMT: | |
237 | return hdr->addr3; | |
238 | default: | |
239 | return NULL; | |
240 | } | |
241 | } | |
242 | ||
243 | ||
244 | #define HAPD_BROADCAST ((struct hostapd_data *) -1) | |
245 | ||
246 | static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface, | |
247 | const u8 *bssid) | |
248 | { | |
249 | size_t i; | |
250 | ||
251 | if (bssid == NULL) | |
252 | return NULL; | |
253 | if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff && | |
254 | bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff) | |
255 | return HAPD_BROADCAST; | |
256 | ||
257 | for (i = 0; i < iface->num_bss; i++) { | |
258 | if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) | |
259 | return iface->bss[i]; | |
260 | } | |
261 | ||
262 | return NULL; | |
263 | } | |
264 | ||
265 | ||
266 | static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, | |
0d9fc3d8 | 267 | const u8 *frame, size_t len) |
f8b1f695 | 268 | { |
0d9fc3d8 | 269 | const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame; |
fbbfcbac | 270 | u16 fc = le_to_host16(hdr->frame_control); |
f8b1f695 JM |
271 | hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len)); |
272 | if (hapd == NULL || hapd == HAPD_BROADCAST) | |
273 | return; | |
274 | ||
fbbfcbac FF |
275 | ieee802_11_rx_from_unknown(hapd, hdr->addr2, |
276 | (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == | |
277 | (WLAN_FC_TODS | WLAN_FC_FROMDS)); | |
f8b1f695 JM |
278 | } |
279 | ||
280 | ||
2a8b7416 | 281 | static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt) |
b5b969e9 | 282 | { |
4b9841d3 | 283 | struct hostapd_iface *iface = hapd->iface; |
b57e086c | 284 | const struct ieee80211_hdr *hdr; |
4b9841d3 | 285 | const u8 *bssid; |
2a8b7416 | 286 | struct hostapd_frame_info fi; |
4b9841d3 | 287 | |
2a8b7416 JM |
288 | hdr = (const struct ieee80211_hdr *) rx_mgmt->frame; |
289 | bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len); | |
4b9841d3 JM |
290 | if (bssid == NULL) |
291 | return; | |
292 | ||
293 | hapd = get_hapd_bssid(iface, bssid); | |
294 | if (hapd == NULL) { | |
295 | u16 fc; | |
296 | fc = le_to_host16(hdr->frame_control); | |
297 | ||
298 | /* | |
299 | * Drop frames to unknown BSSIDs except for Beacon frames which | |
300 | * could be used to update neighbor information. | |
301 | */ | |
302 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && | |
303 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) | |
304 | hapd = iface->bss[0]; | |
305 | else | |
306 | return; | |
307 | } | |
308 | ||
2a8b7416 JM |
309 | os_memset(&fi, 0, sizeof(fi)); |
310 | fi.datarate = rx_mgmt->datarate; | |
311 | fi.ssi_signal = rx_mgmt->ssi_signal; | |
312 | ||
4b9841d3 JM |
313 | if (hapd == HAPD_BROADCAST) { |
314 | size_t i; | |
315 | for (i = 0; i < iface->num_bss; i++) | |
2a8b7416 JM |
316 | ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame, |
317 | rx_mgmt->frame_len, &fi); | |
4b9841d3 | 318 | } else |
2a8b7416 | 319 | ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi); |
b5b969e9 JM |
320 | } |
321 | ||
322 | ||
f8b1f695 JM |
323 | static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf, |
324 | size_t len, u16 stype, int ok) | |
b5b969e9 | 325 | { |
4b9841d3 JM |
326 | struct ieee80211_hdr *hdr; |
327 | hdr = (struct ieee80211_hdr *) buf; | |
328 | hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len)); | |
329 | if (hapd == NULL || hapd == HAPD_BROADCAST) | |
330 | return; | |
b5b969e9 JM |
331 | ieee802_11_mgmt_cb(hapd, buf, len, stype, ok); |
332 | } | |
f82ef4d8 | 333 | |
f8b1f695 | 334 | #endif /* NEED_AP_MLME */ |
ad1e68e6 JM |
335 | |
336 | ||
a0e0d3bb JM |
337 | static int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, |
338 | const u8 *ie, size_t ie_len) | |
339 | { | |
340 | size_t i; | |
341 | int ret = 0; | |
342 | ||
343 | for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) { | |
344 | if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx, | |
345 | sa, ie, ie_len) > 0) { | |
346 | ret = 1; | |
347 | break; | |
348 | } | |
349 | } | |
350 | return ret; | |
351 | } | |
352 | ||
353 | ||
a8e0505b JM |
354 | static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr) |
355 | { | |
356 | struct sta_info *sta = ap_get_sta(hapd, addr); | |
357 | if (sta) | |
358 | return 0; | |
359 | ||
360 | wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR | |
361 | " - adding a new STA", MAC2STR(addr)); | |
362 | sta = ap_sta_add(hapd, addr); | |
363 | if (sta) { | |
364 | hostapd_new_assoc_sta(hapd, sta, 0); | |
365 | } else { | |
366 | wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR, | |
367 | MAC2STR(addr)); | |
368 | return -1; | |
369 | } | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | ||
375 | static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src, | |
376 | const u8 *data, size_t data_len) | |
377 | { | |
378 | struct hostapd_iface *iface = hapd->iface; | |
379 | size_t j; | |
380 | ||
381 | for (j = 0; j < iface->num_bss; j++) { | |
382 | if (ap_get_sta(iface->bss[j], src)) { | |
383 | hapd = iface->bss[j]; | |
384 | break; | |
385 | } | |
386 | } | |
387 | ||
388 | ieee802_1x_receive(hapd, src, data, data_len); | |
389 | } | |
390 | ||
391 | ||
9646a8ab | 392 | void wpa_supplicant_event(void *ctx, enum wpa_event_type event, |
ad1e68e6 JM |
393 | union wpa_event_data *data) |
394 | { | |
395 | struct hostapd_data *hapd = ctx; | |
396 | ||
397 | switch (event) { | |
398 | case EVENT_MICHAEL_MIC_FAILURE: | |
399 | michael_mic_failure(hapd, data->michael_mic_failure.src, 1); | |
400 | break; | |
401 | case EVENT_SCAN_RESULTS: | |
402 | if (hapd->iface->scan_cb) | |
403 | hapd->iface->scan_cb(hapd->iface); | |
404 | break; | |
08fd8c15 | 405 | #ifdef CONFIG_IEEE80211R |
f2dab64e | 406 | case EVENT_FT_RRB_RX: |
08fd8c15 JM |
407 | wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src, |
408 | data->ft_rrb_rx.data, data->ft_rrb_rx.data_len); | |
409 | break; | |
410 | #endif /* CONFIG_IEEE80211R */ | |
fcf0f87d JM |
411 | case EVENT_WPS_BUTTON_PUSHED: |
412 | hostapd_wps_button_pushed(hapd); | |
413 | break; | |
f8b1f695 JM |
414 | #ifdef NEED_AP_MLME |
415 | case EVENT_TX_STATUS: | |
416 | switch (data->tx_status.type) { | |
417 | case WLAN_FC_TYPE_MGMT: | |
418 | hostapd_mgmt_tx_cb(hapd, data->tx_status.data, | |
419 | data->tx_status.data_len, | |
420 | data->tx_status.stype, | |
421 | data->tx_status.ack); | |
422 | break; | |
423 | case WLAN_FC_TYPE_DATA: | |
424 | hostapd_tx_status(hapd, data->tx_status.dst, | |
425 | data->tx_status.data, | |
426 | data->tx_status.data_len, | |
427 | data->tx_status.ack); | |
428 | break; | |
429 | } | |
430 | break; | |
431 | case EVENT_RX_FROM_UNKNOWN: | |
0d9fc3d8 | 432 | hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.frame, |
f8b1f695 JM |
433 | data->rx_from_unknown.len); |
434 | break; | |
435 | case EVENT_RX_MGMT: | |
2a8b7416 | 436 | hostapd_mgmt_rx(hapd, &data->rx_mgmt); |
f8b1f695 JM |
437 | break; |
438 | #endif /* NEED_AP_MLME */ | |
a0e0d3bb JM |
439 | case EVENT_RX_PROBE_REQ: |
440 | hostapd_probe_req_rx(hapd, data->rx_probe_req.sa, | |
441 | data->rx_probe_req.ie, | |
442 | data->rx_probe_req.ie_len); | |
443 | break; | |
a70a5d6d | 444 | case EVENT_NEW_STA: |
a8e0505b JM |
445 | hostapd_event_new_sta(hapd, data->new_sta.addr); |
446 | break; | |
447 | case EVENT_EAPOL_RX: | |
448 | hostapd_event_eapol_rx(hapd, data->eapol_rx.src, | |
449 | data->eapol_rx.data, | |
450 | data->eapol_rx.data_len); | |
451 | break; | |
1d041bec JM |
452 | case EVENT_ASSOC: |
453 | hostapd_notif_assoc(hapd, data->assoc_info.addr, | |
454 | data->assoc_info.req_ies, | |
455 | data->assoc_info.req_ies_len); | |
456 | break; | |
457 | case EVENT_DISASSOC: | |
458 | if (data) | |
459 | hostapd_notif_disassoc(hapd, data->disassoc_info.addr); | |
460 | break; | |
461 | case EVENT_DEAUTH: | |
462 | if (data) | |
463 | hostapd_notif_disassoc(hapd, data->deauth_info.addr); | |
464 | break; | |
ad1e68e6 JM |
465 | default: |
466 | wpa_printf(MSG_DEBUG, "Unknown event %d", event); | |
467 | break; | |
468 | } | |
469 | } | |
f8b1f695 JM |
470 | |
471 | #endif /* HOSTAPD */ |