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