]>
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" |
6226e38d JM |
20 | #include "hostapd.h" |
21 | #include "ieee802_11.h" | |
22 | #include "sta_info.h" | |
23 | #include "accounting.h" | |
24 | #include "tkip_countermeasures.h" | |
25 | #include "iapp.h" | |
26 | #include "ieee802_1x.h" | |
27 | #include "wpa_auth.h" | |
28 | #include "wmm.h" | |
29 | #include "wps_hostapd.h" | |
8b06c1ed | 30 | #include "ap_config.h" |
b5b969e9 JM |
31 | |
32 | ||
05310066 JM |
33 | int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr) |
34 | { | |
35 | struct sta_info *sta = ap_get_sta(hapd, addr); | |
36 | if (sta) | |
37 | return 0; | |
38 | ||
39 | wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR | |
40 | " - adding a new STA", MAC2STR(addr)); | |
41 | sta = ap_sta_add(hapd, addr); | |
42 | if (sta) { | |
43 | hostapd_new_assoc_sta(hapd, sta, 0); | |
44 | } else { | |
45 | wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR, | |
46 | MAC2STR(addr)); | |
47 | return -1; | |
48 | } | |
49 | ||
50 | return 0; | |
51 | } | |
52 | ||
53 | ||
b5b969e9 JM |
54 | int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr, |
55 | const u8 *ie, size_t ielen) | |
56 | { | |
57 | struct sta_info *sta; | |
58 | int new_assoc, res; | |
59 | ||
60 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, | |
61 | HOSTAPD_LEVEL_INFO, "associated"); | |
62 | ||
63 | sta = ap_get_sta(hapd, addr); | |
64 | if (sta) { | |
65 | accounting_sta_stop(hapd, sta); | |
66 | } else { | |
67 | sta = ap_sta_add(hapd, addr); | |
68 | if (sta == NULL) | |
69 | return -1; | |
70 | } | |
71 | sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS); | |
72 | ||
73 | if (hapd->conf->wpa) { | |
74 | if (ie == NULL || ielen == 0) { | |
75 | if (hapd->conf->wps_state) { | |
76 | wpa_printf(MSG_DEBUG, "STA did not include " | |
77 | "WPA/RSN IE in (Re)Association " | |
78 | "Request - possible WPS use"); | |
79 | sta->flags |= WLAN_STA_MAYBE_WPS; | |
80 | goto skip_wpa_check; | |
81 | } | |
82 | ||
83 | wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA"); | |
84 | return -1; | |
85 | } | |
86 | if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 && | |
87 | os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) { | |
88 | sta->flags |= WLAN_STA_WPS; | |
89 | goto skip_wpa_check; | |
90 | } | |
91 | ||
92 | if (sta->wpa_sm == NULL) | |
93 | sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, | |
94 | sta->addr); | |
95 | if (sta->wpa_sm == NULL) { | |
96 | wpa_printf(MSG_ERROR, "Failed to initialize WPA state " | |
97 | "machine"); | |
98 | return -1; | |
99 | } | |
100 | res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm, | |
101 | ie, ielen, NULL, 0); | |
102 | if (res != WPA_IE_OK) { | |
355d36a7 | 103 | int resp; |
b5b969e9 JM |
104 | wpa_printf(MSG_DEBUG, "WPA/RSN information element " |
105 | "rejected? (res %u)", res); | |
106 | wpa_hexdump(MSG_DEBUG, "IE", ie, ielen); | |
355d36a7 AT |
107 | if (res == WPA_INVALID_GROUP) |
108 | resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID; | |
109 | else if (res == WPA_INVALID_PAIRWISE) | |
110 | resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID; | |
111 | else if (res == WPA_INVALID_AKMP) | |
112 | resp = WLAN_REASON_AKMP_NOT_VALID; | |
113 | #ifdef CONFIG_IEEE80211W | |
114 | else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) | |
115 | resp = WLAN_REASON_INVALID_IE; | |
116 | else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) | |
117 | resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID; | |
118 | #endif /* CONFIG_IEEE80211W */ | |
119 | else | |
120 | resp = WLAN_REASON_INVALID_IE; | |
bdee6fce | 121 | hapd->drv.sta_disassoc(hapd, sta->addr, resp); |
355d36a7 | 122 | ap_free_sta(hapd, sta); |
b5b969e9 JM |
123 | return -1; |
124 | } | |
a9aca28b JM |
125 | } else if (hapd->conf->wps_state) { |
126 | if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 && | |
127 | os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) { | |
128 | sta->flags |= WLAN_STA_WPS; | |
129 | } else | |
130 | sta->flags |= WLAN_STA_MAYBE_WPS; | |
b5b969e9 JM |
131 | } |
132 | skip_wpa_check: | |
133 | ||
134 | new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0; | |
135 | sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC; | |
136 | wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC); | |
137 | ||
138 | hostapd_new_assoc_sta(hapd, sta, !new_assoc); | |
139 | ||
140 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 1); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | ||
146 | void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr) | |
147 | { | |
148 | struct sta_info *sta; | |
149 | ||
150 | hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, | |
151 | HOSTAPD_LEVEL_INFO, "disassociated"); | |
152 | ||
153 | sta = ap_get_sta(hapd, addr); | |
154 | if (sta == NULL) { | |
155 | wpa_printf(MSG_DEBUG, "Disassociation notification for " | |
156 | "unknown STA " MACSTR, MAC2STR(addr)); | |
157 | return; | |
158 | } | |
159 | ||
160 | sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC); | |
161 | wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC); | |
162 | sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST; | |
163 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 0); | |
164 | ap_free_sta(hapd, sta); | |
165 | } | |
166 | ||
167 | ||
168 | void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa, | |
169 | const u8 *buf, size_t len) | |
170 | { | |
171 | ieee802_1x_receive(hapd, sa, buf, len); | |
172 | } | |
173 | ||
174 | ||
f8b1f695 JM |
175 | struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd, |
176 | const u8 *addr) | |
177 | { | |
178 | struct hostapd_iface *iface = hapd->iface; | |
179 | size_t j; | |
180 | ||
181 | for (j = 0; j < iface->num_bss; j++) { | |
182 | hapd = iface->bss[j]; | |
183 | if (ap_get_sta(hapd, addr)) | |
184 | return hapd; | |
185 | } | |
186 | ||
187 | return NULL; | |
188 | } | |
189 | ||
190 | ||
191 | #ifdef HOSTAPD | |
192 | ||
fe6bdb77 | 193 | #ifdef NEED_AP_MLME |
f8b1f695 JM |
194 | |
195 | static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len) | |
196 | { | |
197 | u16 fc, type, stype; | |
198 | ||
199 | /* | |
200 | * PS-Poll frames are 16 bytes. All other frames are | |
201 | * 24 bytes or longer. | |
202 | */ | |
203 | if (len < 16) | |
204 | return NULL; | |
205 | ||
206 | fc = le_to_host16(hdr->frame_control); | |
207 | type = WLAN_FC_GET_TYPE(fc); | |
208 | stype = WLAN_FC_GET_STYPE(fc); | |
209 | ||
210 | switch (type) { | |
211 | case WLAN_FC_TYPE_DATA: | |
212 | if (len < 24) | |
213 | return NULL; | |
214 | switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) { | |
fbbfcbac | 215 | case WLAN_FC_FROMDS | WLAN_FC_TODS: |
f8b1f695 JM |
216 | case WLAN_FC_TODS: |
217 | return hdr->addr1; | |
218 | case WLAN_FC_FROMDS: | |
219 | return hdr->addr2; | |
220 | default: | |
221 | return NULL; | |
222 | } | |
223 | case WLAN_FC_TYPE_CTRL: | |
224 | if (stype != WLAN_FC_STYPE_PSPOLL) | |
225 | return NULL; | |
226 | return hdr->addr1; | |
227 | case WLAN_FC_TYPE_MGMT: | |
228 | return hdr->addr3; | |
229 | default: | |
230 | return NULL; | |
231 | } | |
232 | } | |
233 | ||
234 | ||
235 | #define HAPD_BROADCAST ((struct hostapd_data *) -1) | |
236 | ||
237 | static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface, | |
238 | const u8 *bssid) | |
239 | { | |
240 | size_t i; | |
241 | ||
242 | if (bssid == NULL) | |
243 | return NULL; | |
244 | if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff && | |
245 | bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff) | |
246 | return HAPD_BROADCAST; | |
247 | ||
248 | for (i = 0; i < iface->num_bss; i++) { | |
249 | if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) | |
250 | return iface->bss[i]; | |
251 | } | |
252 | ||
253 | return NULL; | |
254 | } | |
255 | ||
256 | ||
257 | static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, | |
258 | const struct ieee80211_hdr *hdr, | |
259 | size_t len) | |
260 | { | |
fbbfcbac | 261 | u16 fc = le_to_host16(hdr->frame_control); |
f8b1f695 JM |
262 | hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len)); |
263 | if (hapd == NULL || hapd == HAPD_BROADCAST) | |
264 | return; | |
265 | ||
fbbfcbac FF |
266 | ieee802_11_rx_from_unknown(hapd, hdr->addr2, |
267 | (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == | |
268 | (WLAN_FC_TODS | WLAN_FC_FROMDS)); | |
f8b1f695 JM |
269 | } |
270 | ||
271 | ||
b57e086c | 272 | static void hostapd_mgmt_rx(struct hostapd_data *hapd, const u8 *buf, |
f8b1f695 | 273 | size_t len, struct hostapd_frame_info *fi) |
b5b969e9 | 274 | { |
4b9841d3 | 275 | struct hostapd_iface *iface = hapd->iface; |
b57e086c | 276 | const struct ieee80211_hdr *hdr; |
4b9841d3 JM |
277 | const u8 *bssid; |
278 | ||
b57e086c | 279 | hdr = (const struct ieee80211_hdr *) buf; |
4b9841d3 JM |
280 | bssid = get_hdr_bssid(hdr, len); |
281 | if (bssid == NULL) | |
282 | return; | |
283 | ||
284 | hapd = get_hapd_bssid(iface, bssid); | |
285 | if (hapd == NULL) { | |
286 | u16 fc; | |
287 | fc = le_to_host16(hdr->frame_control); | |
288 | ||
289 | /* | |
290 | * Drop frames to unknown BSSIDs except for Beacon frames which | |
291 | * could be used to update neighbor information. | |
292 | */ | |
293 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && | |
294 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) | |
295 | hapd = iface->bss[0]; | |
296 | else | |
297 | return; | |
298 | } | |
299 | ||
300 | if (hapd == HAPD_BROADCAST) { | |
301 | size_t i; | |
302 | for (i = 0; i < iface->num_bss; i++) | |
f8b1f695 | 303 | ieee802_11_mgmt(iface->bss[i], buf, len, fi); |
4b9841d3 | 304 | } else |
f8b1f695 | 305 | ieee802_11_mgmt(hapd, buf, len, fi); |
b5b969e9 JM |
306 | } |
307 | ||
308 | ||
f8b1f695 JM |
309 | static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf, |
310 | size_t len, u16 stype, int ok) | |
b5b969e9 | 311 | { |
4b9841d3 JM |
312 | struct ieee80211_hdr *hdr; |
313 | hdr = (struct ieee80211_hdr *) buf; | |
314 | hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len)); | |
315 | if (hapd == NULL || hapd == HAPD_BROADCAST) | |
316 | return; | |
b5b969e9 JM |
317 | ieee802_11_mgmt_cb(hapd, buf, len, stype, ok); |
318 | } | |
f82ef4d8 | 319 | |
f8b1f695 | 320 | #endif /* NEED_AP_MLME */ |
ad1e68e6 JM |
321 | |
322 | ||
ad1e68e6 JM |
323 | void wpa_supplicant_event(void *ctx, wpa_event_type event, |
324 | union wpa_event_data *data) | |
325 | { | |
326 | struct hostapd_data *hapd = ctx; | |
327 | ||
328 | switch (event) { | |
329 | case EVENT_MICHAEL_MIC_FAILURE: | |
330 | michael_mic_failure(hapd, data->michael_mic_failure.src, 1); | |
331 | break; | |
332 | case EVENT_SCAN_RESULTS: | |
333 | if (hapd->iface->scan_cb) | |
334 | hapd->iface->scan_cb(hapd->iface); | |
335 | break; | |
08fd8c15 | 336 | #ifdef CONFIG_IEEE80211R |
f2dab64e | 337 | case EVENT_FT_RRB_RX: |
08fd8c15 JM |
338 | wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src, |
339 | data->ft_rrb_rx.data, data->ft_rrb_rx.data_len); | |
340 | break; | |
341 | #endif /* CONFIG_IEEE80211R */ | |
fcf0f87d JM |
342 | case EVENT_WPS_BUTTON_PUSHED: |
343 | hostapd_wps_button_pushed(hapd); | |
344 | break; | |
f8b1f695 JM |
345 | #ifdef NEED_AP_MLME |
346 | case EVENT_TX_STATUS: | |
347 | switch (data->tx_status.type) { | |
348 | case WLAN_FC_TYPE_MGMT: | |
349 | hostapd_mgmt_tx_cb(hapd, data->tx_status.data, | |
350 | data->tx_status.data_len, | |
351 | data->tx_status.stype, | |
352 | data->tx_status.ack); | |
353 | break; | |
354 | case WLAN_FC_TYPE_DATA: | |
355 | hostapd_tx_status(hapd, data->tx_status.dst, | |
356 | data->tx_status.data, | |
357 | data->tx_status.data_len, | |
358 | data->tx_status.ack); | |
359 | break; | |
360 | } | |
361 | break; | |
362 | case EVENT_RX_FROM_UNKNOWN: | |
363 | hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.hdr, | |
364 | data->rx_from_unknown.len); | |
365 | break; | |
366 | case EVENT_RX_MGMT: | |
367 | hostapd_mgmt_rx(hapd, data->rx_mgmt.frame, | |
368 | data->rx_mgmt.frame_len, data->rx_mgmt.fi); | |
369 | break; | |
370 | #endif /* NEED_AP_MLME */ | |
ad1e68e6 JM |
371 | default: |
372 | wpa_printf(MSG_DEBUG, "Unknown event %d", event); | |
373 | break; | |
374 | } | |
375 | } | |
f8b1f695 JM |
376 | |
377 | #endif /* HOSTAPD */ | |
3fed6f25 JM |
378 | |
379 | ||
380 | void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, | |
381 | const u8 *ie, size_t ie_len) | |
382 | { | |
fa16028d JM |
383 | size_t i; |
384 | ||
385 | for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) | |
386 | hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx, | |
387 | sa, ie, ie_len); | |
3fed6f25 | 388 | } |